<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Masked Importance Sampling | Yingru Li</title><link>https://richardli.xyz/tag/masked-importance-sampling/</link><atom:link href="https://richardli.xyz/tag/masked-importance-sampling/index.xml" rel="self" type="application/rss+xml"/><description>Masked Importance Sampling</description><generator>Hugo Blox Builder (https://hugoblox.com)</generator><language>en-us</language><lastBuildDate>Sat, 20 Dec 2025 00:00:00 +0000</lastBuildDate><image><url>https://richardli.xyz/media/sharing.png</url><title>Masked Importance Sampling</title><link>https://richardli.xyz/tag/masked-importance-sampling/</link></image><item><title>Trust Region Masking for Long-Horizon LLM Reinforcement Learning</title><link>https://richardli.xyz/post/trust-region-masking/</link><pubDate>Sat, 20 Dec 2025 00:00:00 +0000</pubDate><guid>https://richardli.xyz/post/trust-region-masking/</guid><description>&lt;p>&lt;strong>Authors:&lt;/strong> Yingru Li, Jiacai Liu, Jiawei Xu, Yuxuan Tong, Ziniu Li, Baoxiang Wang&lt;/p>
&lt;p>📄 &lt;strong>arXiv:&lt;/strong> &lt;a href="https://arxiv.org/abs/2512.23075" target="_blank" rel="noopener">https://arxiv.org/abs/2512.23075&lt;/a>&lt;/p>
&lt;p>📊 &lt;strong>Slides:&lt;/strong> &lt;a href="https://richardli.xyz/trust_region_masking_slides_yingru.pdf">trust_region_masking_slides_yingru.pdf&lt;/a>&lt;/p>
&lt;p>🔧 &lt;strong>PR #4544:&lt;/strong> &lt;a href="https://github.com/volcengine/verl/pull/4544" target="_blank" rel="noopener">feat: trust region sequence masking&lt;/a>&lt;/p>
&lt;hr>
&lt;h2 id="abstract">Abstract&lt;/h2>
&lt;p>Policy gradient methods for large language models optimize a surrogate objective computed from samples of a rollout policy $\pi_{\mathrm{roll}}$. When $\pi_{\mathrm{roll}} \neq \pi_{\theta}$, there is approximation error between the surrogate and the true objective. Prior work has shown that this off-policy mismatch is unavoidable in modern LLM-RL due to implementation divergence, mixture-of-experts routing discontinuities, and distributed training staleness.&lt;/p>
&lt;p>Classical trust region bounds on the resulting error scale as $O(T^2)$ with sequence length $T$, rendering them vacuous for long-horizon tasks.&lt;/p>
&lt;hr>
&lt;h2 id="the-long-horizon-problem">The Long-Horizon Problem&lt;/h2>
&lt;p>Given that $\pi_{\mathrm{roll}} \neq \pi_{\theta}$ is unavoidable, approximation error becomes a central concern. Classical error bounds scale as $O(T^2)$ with sequence length $T$. For modern LLMs generating responses of $T=4096$ tokens, these bounds become &lt;strong>vacuous&lt;/strong>: even with small per-token divergence ($D_{\mathrm{KL}}^{\mathrm{tok,max}} = 10^{-4}$), the classical bound yields an error of $\approx 1677$, far exceeding any plausible improvement.&lt;/p>
&lt;hr>
&lt;h2 id="off-policy-mismatch-sources">Off-Policy Mismatch Sources&lt;/h2>
&lt;p>Three factors contribute to the unavoidable mismatch between $\pi_{\mathrm{roll}}$ and $\pi_{\theta}$:&lt;/p>
&lt;ol>
&lt;li>
&lt;p>&lt;strong>Implementation divergence&lt;/strong>: Different numerical implementations for inference (vLLM, SGLang) versus training (Megatron-LM, PyTorch FSDP) produce different logits from identical weights.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>MoE routing discontinuities&lt;/strong>: In mixture-of-experts models, small numerical differences can trigger different expert selections, causing discrete jumps in token probabilities.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Distributed staleness&lt;/strong>: Asynchronous training pipelines create lag between rollout generation and gradient computation, so training occurs with updated weights $\pi_{\theta}$ while rollouts were generated with stale weights $\pi_{\mathrm{roll}}$.&lt;/p>
&lt;/li>
&lt;/ol>
&lt;hr>
&lt;h2 id="our-contributions">Our Contributions&lt;/h2>
&lt;p>We derive two tighter bounds:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Bound Type&lt;/th>
&lt;th>Complexity&lt;/th>
&lt;th>Improvement&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Classical (TRPO)&lt;/td>
&lt;td>$O(T^2)$&lt;/td>
&lt;td>Baseline&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;strong>Pinsker-Marginal&lt;/strong>&lt;/td>
&lt;td>$O(T^{3/2})$&lt;/td>
&lt;td>Tighter marginal analysis&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;strong>Mixed&lt;/strong>&lt;/td>
&lt;td>$O(T)$&lt;/td>
&lt;td>Linear scaling&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>Crucially, both bounds depend on $D_{\mathrm{KL}}^{\mathrm{tok,max}}$ — the &lt;strong>maximum token-level KL divergence&lt;/strong> across all positions in a sequence. This is inherently a &lt;em>sequence-level&lt;/em> quantity: it requires examining the entire trajectory to compute, and therefore cannot be controlled by token-independent methods like PPO clipping.&lt;/p>
&lt;hr>
&lt;h2 id="why-token-level-methods-fail">Why Token-Level Methods Fail&lt;/h2>
&lt;h3 id="ppo-clipping">PPO Clipping&lt;/h3>
&lt;p>PPO clipping attempts to control divergence by clipping the importance ratio. However, this suffers from &lt;strong>gradient leakage&lt;/strong> — clipping only affects the gradient magnitude, not the fundamental approximation error.&lt;/p>
&lt;h3 id="token-masking">Token Masking&lt;/h3>
&lt;p>Token-level masking excludes individual tokens that violate the trust region. However, this creates a &lt;strong>theoretical problem&lt;/strong>: the masked gradient is no longer an unbiased estimator of the policy gradient.&lt;/p>
&lt;h3 id="the-fundamental-dilemma">The Fundamental Dilemma&lt;/h3>
&lt;p>The &lt;strong>root cause&lt;/strong> is that $D_{\mathrm{KL}}^{\mathrm{tok,max}}$ is a sequence-level quantity that cannot be decomposed into independent token-level constraints. &lt;strong>The only solution&lt;/strong> is to operate at the sequence level.&lt;/p>
&lt;hr>
&lt;h2 id="solution-trust-region-masking-trm">Solution: Trust Region Masking (TRM)&lt;/h2>
&lt;p>We propose &lt;strong>Trust Region Masking (TRM)&lt;/strong>, which excludes entire sequences from gradient computation if any token violates the trust region.&lt;/p>
&lt;p>TRM is the principled form of masked importance sampling (MIS), first introduced in &lt;a href="https://richardli.xyz/rl-collapse">When Speed Kills Stability&lt;/a>: a sequence-level trust-region mask combined with per-token importance sampling.&lt;/p>
&lt;h3 id="why-sequence-masking-works">Why Sequence Masking Works&lt;/h3>
&lt;p>By masking at the sequence level, we ensure that:&lt;/p>
&lt;ol>
&lt;li>The remaining samples come from policies that are provably close&lt;/li>
&lt;li>The gradient estimator remains unbiased over the unmasked samples&lt;/li>
&lt;li>The trust region bound applies to all included sequences&lt;/li>
&lt;/ol>
&lt;h3 id="masking-criterion">Masking Criterion&lt;/h3>
&lt;p>A sequence is masked if:
$$\max_{t} D_{\mathrm{KL}}(\pi_{\mathrm{roll}}(\cdot|c_t) | \pi_{\theta}(\cdot|c_t)) &amp;gt; \epsilon$$&lt;/p>
&lt;p>where $\epsilon$ is the trust region threshold.&lt;/p>
&lt;h3 id="exact-computation">Exact Computation&lt;/h3>
&lt;p>The rigorous guarantee requires &lt;strong>exact KL computation with stored logits&lt;/strong> from the rollout policy.&lt;/p>
&lt;h3 id="sample-based-approximation">Sample-Based Approximation&lt;/h3>
&lt;p>In practice, storing full logits may be expensive. The paper proposes sample-based approximations using importance ratios $\rho_t = \frac{\pi_{\theta}(y_t|c_t)}{\pi_{\mathrm{roll}}(y_t|c_t)}$:&lt;/p>
&lt;h4 id="the-k_3-estimator-for-average-based-filtering">The $k_3$ Estimator (for average-based filtering)&lt;/h4>
&lt;p>$$k_3(\rho) = \rho - 1 - \log \rho$$&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>$\rho$&lt;/th>
&lt;th>$k_1 = -\log \rho$&lt;/th>
&lt;th>$k_3 = \rho - 1 - \log \rho$&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>0.5&lt;/td>
&lt;td>0.69&lt;/td>
&lt;td>0.19&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>1.0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>2.0&lt;/td>
&lt;td>−0.69&lt;/td>
&lt;td>0.31&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>10&lt;/td>
&lt;td>−2.30&lt;/td>
&lt;td>6.70&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>100&lt;/td>
&lt;td>−4.61&lt;/td>
&lt;td>94.4&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>&lt;strong>Properties of $k_3$:&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Non-negative&lt;/strong>: $\rho - 1 - \log \rho \geq 0$ for all $\rho &amp;gt; 0$&lt;/li>
&lt;li>&lt;strong>Unbiased&lt;/strong>:
$\mathbb{E}_{y \sim \pi_{\mathrm{roll}}}[k_3(\rho)] = D_{\mathrm{KL}}$&lt;/li>
&lt;li>Ideal for average-based filtering since $(1/T)\sum_t k_3(\rho_t)$ converges to true average KL&lt;/li>
&lt;/ul>
&lt;h4 id="the-log-rho-estimator-for-max-based-filtering">The $|\log \rho|$ Estimator (for max-based filtering)&lt;/h4>
&lt;p>For the max criterion, we need a &lt;strong>symmetric&lt;/strong> detector since both $\rho \gg 1$ and $\rho \ll 1$ indicate large divergence:
$$|\log(100)| = |\log(0.01)| = 4.6$$&lt;/p>
&lt;p>In contrast, $k_3$ is asymmetric: $k_3(100) = 94.4$ but $k_3(0.01) = 3.6$ (26× difference).&lt;/p>
&lt;h4 id="caveat">Caveat&lt;/h4>
&lt;p>Neither sample-based method provides a rigorous &lt;em>bound&lt;/em> on $D_{\mathrm{KL}}^{\mathrm{tok,max}}$ — both are approximate detectors based on single samples per context.&lt;/p>
&lt;h3 id="key-result">Key Result&lt;/h3>
&lt;p>TRM provides the &lt;strong>first non-vacuous monotonic improvement guarantees&lt;/strong> for long-horizon LLM-RL.&lt;/p>
&lt;hr>
&lt;h2 id="paper-structure">Paper Structure&lt;/h2>
&lt;ol>
&lt;li>&lt;strong>Introduction&lt;/strong>: Off-Policy Mismatch, Long-Horizon Problem, Contributions&lt;/li>
&lt;li>&lt;strong>Background&lt;/strong>: Autoregressive Language Generation, Optimization Problem, Surrogate Objective, Divergence Measures&lt;/li>
&lt;li>&lt;strong>Theoretical Analysis&lt;/strong>: Performance Difference Identity, Key Lemmas, Classical TRPO Bound, New Bounds&lt;/li>
&lt;li>&lt;strong>Why Token-Level Methods Fail&lt;/strong>: PPO Clipping, Token Masking, Fundamental Dilemma&lt;/li>
&lt;li>&lt;strong>Solution&lt;/strong>: Trust Region Masking&lt;/li>
&lt;li>&lt;strong>Discussion&lt;/strong>: Limitations, Practical Considerations, Future Work&lt;/li>
&lt;li>&lt;strong>Conclusion&lt;/strong>&lt;/li>
&lt;/ol>
&lt;hr>
&lt;h2 id="citation">Citation&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bibtex" data-lang="bibtex">&lt;span class="line">&lt;span class="cl">&lt;span class="nc">@article&lt;/span>&lt;span class="p">{&lt;/span>&lt;span class="nl">li2025trustregion&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">title&lt;/span> &lt;span class="p">=&lt;/span> &lt;span class="s">{Trust Region Masking for Long-Horizon LLM Reinforcement Learning}&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">author&lt;/span> &lt;span class="p">=&lt;/span> &lt;span class="s">{Li, Yingru and Liu, Jiacai and Xu, Jiawei and Tong, Yuxuan and Li, Ziniu and Wang, Baoxiang}&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">journal&lt;/span> &lt;span class="p">=&lt;/span> &lt;span class="s">{arXiv preprint arXiv:2512.23075}&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">year&lt;/span> &lt;span class="p">=&lt;/span> &lt;span class="s">{2025}&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">url&lt;/span> &lt;span class="p">=&lt;/span> &lt;span class="s">{https://arxiv.org/abs/2512.23075}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description></item><item><title>Part 3: Trust Region Optimization via Sequence Masking</title><link>https://richardli.xyz/post/rl-collapse-part3/</link><pubDate>Tue, 04 Nov 2025 00:00:00 +0000</pubDate><guid>https://richardli.xyz/post/rl-collapse-part3/</guid><description>&lt;p>&lt;strong>Authors:&lt;/strong> &lt;a href="http://richardli.xyz" target="_blank" rel="noopener">Yingru Li&lt;/a>, &lt;a href="https://www.jiacailiu.cn/" target="_blank" rel="noopener">Jiacai Liu&lt;/a>&lt;/p>
&lt;div class="alert alert-note">
&lt;div>
&lt;strong>Original Blog:&lt;/strong> &lt;a href="https://richardli.xyz/rl-collapse" target="_blank" rel="noopener">When Speed Kills Stability: Demystifying RL Collapse from the Training-Inference Mismatch&lt;/a>
&lt;/div>
&lt;/div>
&lt;div class="alert alert-note">
&lt;div>
&lt;p>&lt;strong>Series Context&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://richardli.xyz/rl-collapse-1" target="_blank" rel="noopener">&lt;strong>Part 1&lt;/strong>&lt;/a>: We established the SGA (Stochastic Gradient Ascent) framework and identified two failure modes of off-policy mismatch: Bias (measured by $D_{TV}$) and Variance (measured by $\chi^2$-divergence).&lt;/li>
&lt;li>&lt;a href="https://richardli.xyz/rl-collapse-2" target="_blank" rel="noopener">&lt;strong>Part 2&lt;/strong>&lt;/a>: We analyzed gradient estimators and showed that Token-level IS (PPO/GRPO) has $O(T^2 \Delta_{\max})$ bias, while Sequence-Level Truncated IS (Seq-TIS) achieves a controllable bias-variance trade-off via clipping: $\rho(y) \to \min(\rho(y), C)$.&lt;/li>
&lt;/ul>
&lt;/div>
&lt;/div>
&lt;div class="alert alert-note">
&lt;div>
&lt;p>&lt;strong>TL;DR&lt;/strong>&lt;/p>
&lt;p>In a standard statistical setting, Part 2 solved the problem with Seq-TIS.&lt;/p>
&lt;p>However, when training Agents or Reasoning Models (Chain-of-Thought), two practical phenomena violate the assumptions underlying Seq-TIS:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Out-of-Distribution (OOD) High-Weight Samples:&lt;/strong> Extremely high importance weights ($\rho \gg C$) often correspond to samples outside the behavior policy&amp;rsquo;s support—numerical errors or distribution shift artifacts. Clipping these samples still includes them in the gradient update. &lt;strong>Solution: Enforce a Hard Trust Region via Rejection/Masking (Seq-MIS).&lt;/strong>&lt;/li>
&lt;li>&lt;strong>Length-Dependent Rejection Bias:&lt;/strong> The importance ratio $\rho(y) = \prod_t \rho_t$ grows exponentially with sequence length $T$, causing systematic rejection of long sequences regardless of per-step quality. &lt;strong>Solution: Geometric Sequence Masking (Geo-Mask), which enforces a Per-Token Trust Region using a length-normalized KL divergence criterion.&lt;/strong>&lt;/li>
&lt;/ol>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Pathology&lt;/th>
&lt;th>What Breaks&lt;/th>
&lt;th>Why Standard Methods Fail&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>OOD High-Weight Samples&lt;/td>
&lt;td>Samples with $\rho \gg C$ lie outside trust region&lt;/td>
&lt;td>Clipping retains them with weight $C$—still corrupts gradients&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Length-Dependent Bias&lt;/td>
&lt;td>$\rho(y) = \prod_t \rho_t$ grows exponentially with $T$&lt;/td>
&lt;td>Fixed threshold $C$ systematically rejects long sequences&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Theory-Practice Gap&lt;/td>
&lt;td>TRPO requires trust region $\propto 1/T^2$&lt;/td>
&lt;td>PPO/GRPO use fixed clipping $\epsilon$ regardless of $T$&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;/div>
&lt;/div>
&lt;hr>
&lt;h2 id="citation">Citation&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bibtex" data-lang="bibtex">&lt;span class="line">&lt;span class="cl">&lt;span class="nc">@online&lt;/span>&lt;span class="p">{&lt;/span>&lt;span class="nl">liu-li-2025-rl-collapse&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">title&lt;/span> &lt;span class="p">=&lt;/span> &lt;span class="s">{When Speed Kills Stability: Demystifying {RL} Collapse from the Training-Inference Mismatch}&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">author&lt;/span> &lt;span class="p">=&lt;/span> &lt;span class="s">{Liu, Jiacai and Li, Yingru and Fu, Yuqian and Wang, Jiawei and Liu, Qian and Shen, Yu}&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">year&lt;/span> &lt;span class="p">=&lt;/span> &lt;span class="s">{2025}&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">month&lt;/span> &lt;span class="p">=&lt;/span> &lt;span class="nv">sep&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">url&lt;/span> &lt;span class="p">=&lt;/span> &lt;span class="s">{https://richardli.xyz/rl-collapse}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;hr>
&lt;h2 id="recap-the-trust-region-framework">Recap: The Trust Region Framework&lt;/h2>
&lt;p>In Part 1, we established the theoretical foundation for trust region optimization. Here we recap the key results that motivate the methods in this part.&lt;/p>
&lt;h3 id="the-surrogate-objective-and-its-limitations">The Surrogate Objective and Its Limitations&lt;/h3>
&lt;p>Consider an autoregressive language model generating a sequence $y = (y_0, y_1, \ldots, y_{T-1})$ given prompt $x$. When optimizing a policy $\pi$ using samples from a behavior policy $\mu$, we cannot directly optimize the true objective $J(\pi)$. Instead, we optimize a &lt;strong>surrogate objective&lt;/strong>:&lt;/p>
$$
L_\mu(\pi) = J(\mu) + \sum_{t=0}^{T-1} \mathbb{E}_{(x, y_{\lt t}) \sim d_{\mu,t}} \mathbb{E}_{y_t \sim \pi(\cdot|x, y_{\lt t})} [A_\mu(x, y_{\le t})]
$$
&lt;p>where $d_{\mu,t}$ is the context distribution at step $t$ under $\mu$, and $A_\mu(x, y_{\le t})$ is the advantage of generating token $y_t$ in context $(x, y_{\lt t})$.&lt;/p>
&lt;p>The surrogate is a first-order approximation (&lt;a href="https://dl.acm.org/doi/10.5555/645531.656005" target="_blank" rel="noopener">Kakade &amp;amp; Langford, 2002&lt;/a>; &lt;a href="https://arxiv.org/abs/1502.05477" target="_blank" rel="noopener">Schulman et al., 2015&lt;/a>) that satisfies:&lt;/p>
&lt;ul>
&lt;li>$L_\mu(\mu) = J(\mu)$ (equal values at $\pi = \mu$)&lt;/li>
&lt;li>
$\nabla L_\mu(\pi)\big|_{\pi=\mu} = \nabla J(\pi)\big|_{\pi=\mu}$ (equal gradients at $\pi = \mu$)&lt;/li>
&lt;/ul>
&lt;p>However, the approximation degrades as $\pi$ moves away from $\mu$.&lt;/p>
&lt;p>&lt;strong>The Surrogate Gradient (Token-IS):&lt;/strong> Taking the gradient of $L_\mu(\pi)$:&lt;/p>
$$
\nabla L_\mu(\pi) = \sum_{t=0}^{T-1} \mathbb{E}_{(x, y_{\lt t}) \sim d_{\mu,t}} \mathbb{E}_{y_t \sim \pi(\cdot|x, y_{\lt t})} [A_\mu(x, y_{\le t}) \nabla \log \pi(y_t|x, y_{\lt t})]
$$
&lt;p>To estimate this from samples $y \sim \mu$, we apply importance sampling at each token:&lt;/p>
$$
\nabla L_\mu(\pi) = \mathbb{E}_{y \sim \mu} \left[ \sum_{t=0}^{T-1} \rho_t \cdot A_t \nabla \log \pi(y_t|x, y_{\lt t}) \right]
$$
&lt;p>where $\rho_t = \frac{\pi(y_t|x, y_{\lt t})}{\mu(y_t|x, y_{\lt t})}$ and $A_t = A_\mu(x, y_{\le t})$. This is the &lt;strong>Token-IS gradient&lt;/strong>—the foundation for PPO (&lt;a href="https://arxiv.org/abs/1707.06347" target="_blank" rel="noopener">Schulman et al., 2017&lt;/a>) and GRPO (&lt;a href="https://arxiv.org/abs/2402.03300" target="_blank" rel="noopener">Shao et al., 2024&lt;/a>).&lt;/p>
&lt;h3 id="the-trpo-lower-bound">The TRPO Lower Bound&lt;/h3>
&lt;p>The &lt;strong>Performance Difference Lemma&lt;/strong> (&lt;a href="https://dl.acm.org/doi/10.5555/645531.656005" target="_blank" rel="noopener">Kakade &amp;amp; Langford, 2002&lt;/a>) quantifies the gap between the surrogate and true objectives:&lt;/p>
$$
J(\pi) - J(\mu) = \sum_{t=0}^{T-1} \mathbb{E}_{(x, y_{\lt t}) \sim d_{\pi,t}} \mathbb{E}_{y_t \sim \pi(\cdot|x, y_{\lt t})} [A_\mu(x, y_{\le t})]
$$
&lt;p>The key difference from the surrogate is that the true improvement uses the context distribution $d_{\pi,t}$ (under the new policy), while the surrogate uses $d_{\mu,t}$ (under the old policy).&lt;/p>
&lt;p>Using the &lt;strong>Simulation Lemma&lt;/strong> (&lt;a href="https://dl.acm.org/doi/10.5555/645531.757765" target="_blank" rel="noopener">Kearns &amp;amp; Singh, 2002&lt;/a>), which bounds how context distributions diverge over the sequence:&lt;/p>
$$
D_{TV}(d_{\pi,t} \| d_{\mu,t}) \leq t \cdot D_{TV}^{\max}(\pi, \mu)
$$
&lt;p>we derive the &lt;strong>TRPO lower bound&lt;/strong> (&lt;a href="https://arxiv.org/abs/1502.05477" target="_blank" rel="noopener">Schulman et al., 2015&lt;/a>):&lt;/p>
$$
\boxed{J(\pi) \geq L_\mu(\pi) - C \cdot T^2 \cdot D_{TV}^{\max}(\pi, \mu)}
$$
&lt;p>where:&lt;/p>
&lt;ul>
&lt;li>$C$ is a constant depending on the maximum advantage $\max_{x, y_{\le t}}|A_\mu(x, y_{\le t})|$&lt;/li>
&lt;li>$T$ is the sequence length&lt;/li>
&lt;li>$D_{TV}^{\max} = \max_{x, y_{\lt t}} D_{TV}(\pi(\cdot|x, y_{\lt t}) | \mu(\cdot|x, y_{\lt t}))$ is the maximum per-token TV distance&lt;/li>
&lt;/ul>
&lt;h3 id="the-trust-region-requirement">The Trust Region Requirement&lt;/h3>
&lt;p>For surrogate optimization to guarantee improvement in the true objective, the policy must stay within a &lt;strong>trust region&lt;/strong>:&lt;/p>
$$
D_{TV}^{\max}(\pi, \mu) \leq \delta
$$
&lt;p>The critical insight is that &lt;strong>the valid trust region size must shrink with sequence length&lt;/strong>:&lt;/p>
$$
\delta \propto \frac{1}{T^2}
$$
&lt;p>This $T^2$ dependence arises because context distribution errors accumulate linearly over $T$ tokens, and the total error (summed over all tokens) scales quadratically.&lt;/p>
&lt;h3 id="soft-vs-hard-trust-regions">Soft vs. Hard Trust Regions&lt;/h3>
&lt;p>The TRPO framework suggests two approaches to enforce trust regions:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Type&lt;/th>
&lt;th>Mechanism&lt;/th>
&lt;th>Implementation&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>&lt;strong>Soft (Clipping)&lt;/strong>&lt;/td>
&lt;td>Down-weight samples outside the region&lt;/td>
&lt;td>$\min(\rho, C)$ — sample included with bounded weight&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;strong>Hard (Rejection)&lt;/strong>&lt;/td>
&lt;td>Exclude samples outside the region&lt;/td>
&lt;td>$\mathbb{I}(\rho \leq C)$ — sample excluded entirely&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>&lt;strong>Soft trust regions&lt;/strong> use clipped importance sampling: $\min(\rho_t, 1+\epsilon)$. This is computationally efficient but retains potentially problematic samples.&lt;/p>
&lt;p>&lt;strong>This part develops hard trust region methods&lt;/strong>—Seq-MIS and Geo-Mask—that completely exclude samples outside the trusted region. We show when and why hard rejection outperforms soft clipping.&lt;/p>
&lt;hr>
&lt;h2 id="1-ood-high-weight-samples-why-rejection-outperforms-clipping">1. OOD High-Weight Samples: Why Rejection Outperforms Clipping&lt;/h2>
&lt;h3 id="11-the-problem-clipping-retains-problematic-samples">1.1 The Problem: Clipping Retains Problematic Samples&lt;/h3>
&lt;p>In Part 2, we derived the Seq-TIS estimator:&lt;/p>
$$
\hat{g}_{\text{seq-tis}}(y) = \min(\rho(y), C) \cdot f(y)
$$
&lt;p>where $f(y) = R(y) \cdot \nabla \log \pi(y)$ is the score function (reward-weighted gradient), and $\rho(y) = \pi(y)/\mu(y)$ is the sequence-level importance ratio.&lt;/p>
&lt;p>The implicit assumption was that all samples $y \sim \mu$ are valid learning signals—samples with high weights $\rho(y)$ simply require variance control via clipping.&lt;/p>
&lt;p>However, this assumption fails in practice. Consider a sample with $\rho(y) = 10,000$. This means:&lt;/p>
$$
\frac{\pi(y)}{\mu(y)} = 10,000 \implies \mu(y) = \frac{\pi(y)}{10,000}
$$
&lt;p>Such extreme ratios typically arise when $\mu(y)$ is near the numerical precision floor (e.g., $\mu(y) \approx 10^{-9}$). These are &lt;strong>Out-of-Distribution (OOD)&lt;/strong> samples—samples that lie outside the trust region where importance sampling is valid. They occur due to:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Numerical precision artifacts&lt;/strong> in probability computation&lt;/li>
&lt;li>&lt;strong>Distribution shift&lt;/strong> between $\pi$ and $\mu$ beyond the valid IS regime&lt;/li>
&lt;/ol>
&lt;p>&lt;strong>The Clipping Problem:&lt;/strong> When we apply Seq-TIS, we compute $\min(10000, C) \cdot f(y) = C \cdot f(y)$. The sample is still included in the gradient update with weight $C$. If $f(y)$ is malformed (due to the OOD nature of $y$), we introduce a systematic error into every gradient step.&lt;/p>
&lt;p>&lt;strong>Why This Violates the Trust Region:&lt;/strong> Recall from the TRPO lower bound that the surrogate objective $L_\mu(\pi)$ is only valid when $D_{TV}^{\max}(\pi, \mu) \leq \delta$. Samples with extreme importance ratios $\rho(y) \gg C$ indicate that $\pi$ and $\mu$ have diverged far beyond this trust region for that particular sample. Clipping the weight does not fix the underlying problem—the sample itself lies outside the region where the first-order approximation holds.&lt;/p>
&lt;h3 id="12-the-solution-hard-trust-region-via-rejection-seq-mis">1.2 The Solution: Hard Trust Region via Rejection (Seq-MIS)&lt;/h3>
&lt;p>Instead of soft clipping, we enforce a &lt;strong>Hard Trust Region&lt;/strong>: samples outside the trusted region are rejected entirely.&lt;/p>
&lt;div class="alert alert-note">
&lt;div>
&lt;p>&lt;strong>Definition: Sequence-Level Masked IS (Seq-MIS)&lt;/strong>&lt;/p>
&lt;p>$$
\hat{g}_{\text{seq-mis}}(y) = \mathbb{I}(\rho(y) \le C) \cdot \rho(y) \cdot f(y)
$$&lt;/p>
&lt;p>where $\mathbb{I}(\cdot)$ is the indicator function.&lt;/p>
&lt;/div>
&lt;/div>
&lt;p>&lt;strong>Mathematical Interpretation:&lt;/strong> The trust region is defined as:&lt;/p>
$$
\mathcal{T}_C = \{y : \rho(y) \le C\} = \left\{y : \frac{\pi(y)}{\mu(y)} \le C\right\}
$$
&lt;p>Only samples within this region contribute to the gradient. Samples with $\rho(y) \gt C$ are treated as unreliable and excluded.&lt;/p>
&lt;p>&lt;strong>Connection to TRPO:&lt;/strong> This implements the trust region concept from TRPO theory (&lt;a href="https://richardli.xyz/rl-collapse-1" target="_blank" rel="noopener">Part 1&lt;/a>), where the trust region constraint $D_{TV}(\pi | \mu) \le \delta$ must be enforced to guarantee improvement. Seq-MIS enforces this constraint via &lt;strong>rejection&lt;/strong> (hard trust region) rather than &lt;strong>penalization&lt;/strong> (soft trust region), ensuring that gradient updates only use samples from the valid trust region.&lt;/p>
&lt;p>The principled version, a sequence mask with per-token IS, is &lt;a href="https://richardli.xyz/post/trust-region-masking/">Trust Region Masking&lt;/a>.&lt;/p>
&lt;h3 id="13-bias-variance-analysis-of-seq-mis">1.3 Bias-Variance Analysis of Seq-MIS&lt;/h3>
&lt;p>&lt;strong>Bias:&lt;/strong> The Seq-MIS estimator is biased. By importance sampling identity:&lt;/p>
$$
\mathbb{E}_\mu[\hat{g}_{\text{seq-mis}}] = \mathbb{E}_\mu[\mathbb{I}(\rho \le C) \cdot \rho \cdot f] = \sum_y \mu(y) \cdot \mathbb{I}(\rho(y) \le C) \cdot \frac{\pi(y)}{\mu(y)} \cdot f(y)
$$
$$
= \sum_y \pi(y) \cdot \mathbb{I}(\rho(y) \le C) \cdot f(y) = \mathbb{E}_\pi[f \cdot \mathbb{I}(\rho \le C)]
$$
&lt;p>The true gradient is $g = \mathbb{E}_\pi[f]$. Therefore:&lt;/p>
$$
\text{Bias} = \mathbb{E}_\mu[\hat{g}_{\text{seq-mis}}] - g = \mathbb{E}_\pi[f \cdot \mathbb{I}(\rho \le C)] - \mathbb{E}_\pi[f] = -\mathbb{E}_\pi[f \cdot \mathbb{I}(\rho > C)]
$$
&lt;p>The bias is the negative of the contribution from rejected samples (weighted by $\pi$).&lt;/p>
&lt;p>&lt;strong>Variance:&lt;/strong> Since $\mathbb{I}(\rho \le C) \cdot \rho \le C$ when the indicator is 1, and 0 otherwise:&lt;/p>
$$
\mathbf{Var}(\hat{g}_{\text{seq-mis}}) \le \mathbb{E}_\mu[\|\hat{g}_{\text{seq-mis}}\|^2] \le C^2 \cdot \mathbb{E}_\mu[\|f\|^2] = O(T^2 C^2)
$$
&lt;h3 id="14-when-to-use-each-estimator">1.4 When to Use Each Estimator&lt;/h3>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>&lt;strong>Estimator&lt;/strong>&lt;/th>
&lt;th>&lt;strong>Mechanism&lt;/strong>&lt;/th>
&lt;th>&lt;strong>Use Case&lt;/strong>&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>&lt;strong>Seq-TIS&lt;/strong>&lt;/td>
&lt;td>$\min(\rho, C) \cdot f$&lt;/td>
&lt;td>Moderate mismatch; maximize sample efficiency&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;strong>Seq-MIS&lt;/strong>&lt;/td>
&lt;td>$\mathbb{I}(\rho \le C) \cdot \rho \cdot f$&lt;/td>
&lt;td>Large mismatch; OOD samples likely; prioritize robustness&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>The choice depends on the reliability of high-weight samples. When the behavior policy $\mu$ is well-calibrated and mismatch is controlled, Seq-TIS extracts more information. When OOD samples are prevalent, Seq-MIS provides a Hard Trust Region that prevents gradient corruption.&lt;/p>
&lt;h3 id="15-practical-consequences-for-llm-rl-training">1.5 Practical Consequences for LLM-RL Training&lt;/h3>
&lt;p>The distinction between soft and hard trust regions has concrete effects on training stability:&lt;/p>
&lt;p>&lt;strong>When using Seq-TIS (soft clipping):&lt;/strong>&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Sample efficiency is higher&lt;/strong> — all samples contribute to gradient updates&lt;/li>
&lt;li>&lt;strong>Risk of gradient corruption&lt;/strong> — OOD samples with $\rho \gg C$ still influence updates with weight $C$&lt;/li>
&lt;li>&lt;strong>Systematic bias accumulation&lt;/strong> — if many samples are OOD, clipping introduces consistent directional bias&lt;/li>
&lt;/ol>
&lt;p>&lt;strong>When using Seq-MIS (hard rejection):&lt;/strong>&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Training is more robust&lt;/strong> — OOD samples are completely excluded&lt;/li>
&lt;li>&lt;strong>Lower sample efficiency&lt;/strong> — rejected samples provide no learning signal&lt;/li>
&lt;li>&lt;strong>Cleaner gradient estimates&lt;/strong> — only samples within the trust region contribute&lt;/li>
&lt;/ol>
&lt;p>In practice, monitoring the &lt;strong>rejection rate&lt;/strong> (fraction of samples with $\rho &amp;gt; C$) provides insight into policy-behavior divergence and can guide the choice between estimators.&lt;/p>
&lt;hr>
&lt;h2 id="2-length-dependent-rejection-bias-the-failure-of-sequence-level-is-for-long-horizons">2. Length-Dependent Rejection Bias: The Failure of Sequence-Level IS for Long Horizons&lt;/h2>
&lt;h3 id="21-the-problem-exponential-growth-of-the-importance-ratio">2.1 The Problem: Exponential Growth of the Importance Ratio&lt;/h3>
&lt;p>For autoregressive generation, the sequence-level importance ratio is a product of per-token ratios:&lt;/p>
$$
\rho(y) = \prod_{t=0}^{T-1} \rho_t, \quad \text{where} \quad \rho_t = \frac{\pi(y_t|x, y_{\lt t})}{\mu(y_t|x, y_{\lt t})}
$$
&lt;p>Even when the per-token mismatch is small, this product grows (or shrinks) exponentially with sequence length $T$ &lt;strong>almost everywhere&lt;/strong> along the trajectory.&lt;/p>
&lt;p>&lt;strong>Formal Analysis:&lt;/strong> Let $\bar{\rho} = \mathbb{E}[\rho_t]$ denote the expected per-token ratio. If we assume (for simplicity) that the $\rho_t$ are independent with $\bar{\rho} \gt 1$, then:&lt;/p>
$$
\mathbb{E}[\rho(y)] = \prod_{t=0}^{T-1} \mathbb{E}[\rho_t] = \bar{\rho}^T
$$
&lt;p>Similarly, for the log-ratio:&lt;/p>
$$
\log \rho(y) = \sum_{t=0}^{T-1} \log \rho_t
$$
&lt;p>If each $\log \rho_t$ has mean $\delta \gt 0$ (i.e., $\pi$ assigns slightly higher probability than $\mu$ on average), then:&lt;/p>
$$
\mathbb{E}[\log \rho(y)] = T \cdot \delta \implies \rho(y) \approx e^{T\delta}
$$
&lt;p>&lt;strong>Numerical Example:&lt;/strong> Consider $\bar{\rho} = 1.001$ (0.1% per-token drift):&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Sequence Length $T$&lt;/th>
&lt;th>$\rho(y) \approx 1.001^T$&lt;/th>
&lt;th>Within Trust Region ($C=5$)?&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>10&lt;/td>
&lt;td>1.01&lt;/td>
&lt;td>Accepted&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>1,000&lt;/td>
&lt;td>2.72&lt;/td>
&lt;td>Accepted&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>2,000&lt;/td>
&lt;td>7.39&lt;/td>
&lt;td>Rejected&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>5,000&lt;/td>
&lt;td>148.4&lt;/td>
&lt;td>Rejected&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;h3 id="22-the-consequence-systematic-length-bias">2.2 The Consequence: Systematic Length Bias&lt;/h3>
&lt;p>This exponential scaling creates a &lt;strong>length-dependent acceptance probability&lt;/strong>. For any fixed threshold $C$, there exists a critical length $T^*$ beyond which almost all samples are rejected:&lt;/p>
$$
T^* = \frac{\log C}{\log \bar{\rho}}
$$
&lt;p>For $C = 5$ and $\bar{\rho} = 1.001$: $T^* = \frac{\log 5}{\log 1.001} \approx 1609$ tokens.&lt;/p>
&lt;p>&lt;strong>Challenge 1: Structural Length Bias.&lt;/strong> Chain-of-Thought (CoT) models and agents often generate sequences of 2,000-10,000+ tokens. With standard Seq-TIS or Seq-MIS:&lt;/p>
&lt;ol>
&lt;li>Short responses (&amp;lt; 1000 tokens) are almost always accepted&lt;/li>
&lt;li>Long reasoning chains (&amp;gt; 2000 tokens) are almost always rejected or heavily clipped&lt;/li>
&lt;li>The model receives &lt;strong>systematically biased feedback&lt;/strong> favoring short outputs&lt;/li>
&lt;/ol>
&lt;p>This is not a variance problem—it is a &lt;strong>structural bias&lt;/strong> against long-horizon reasoning, independent of the quality of individual reasoning steps.&lt;/p>
&lt;p>&lt;strong>Challenge 2: The Sequence-Level IS Dilemma.&lt;/strong> We face a fundamental trade-off:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Use a large threshold&lt;/strong> ($C \gg 1$): Accepts long sequences, but introduces high variance and allows OOD samples&lt;/li>
&lt;li>&lt;strong>Use a small threshold&lt;/strong> ($C \approx 1$): Controls variance and rejects OOD samples, but systematically biases against long sequences&lt;/li>
&lt;/ul>
&lt;p>There is no value of $C$ that simultaneously:&lt;/p>
&lt;ol>
&lt;li>Accepts high-quality long reasoning chains (requires $C$ large enough for $\rho(y) \le C$ when $T$ is large)&lt;/li>
&lt;li>Rejects low-quality or OOD samples (requires $C$ small enough to filter outliers)&lt;/li>
&lt;li>Provides a length-invariant acceptance criterion (requires $C$ to adapt with $T$, contradicting it being a fixed constant)&lt;/li>
&lt;/ol>
&lt;h3 id="23-the-gap-between-theory-and-practice">2.3 The Gap Between Theory and Practice&lt;/h3>
&lt;p>Here&amp;rsquo;s a critical insight: &lt;strong>Standard PPO/GRPO implementations do not properly account for the horizon dependence of the trust region&lt;/strong>.&lt;/p>
&lt;p>The TRPO lower bound shows that for the surrogate objective to remain valid, the trust region must satisfy:&lt;/p>
$$
D_{TV}^{\max}(\pi, \mu) \leq \delta, \quad \text{where} \quad \delta \propto \frac{1}{T^2}
$$
&lt;p>However, PPO and GRPO use a &lt;strong>constant clipping factor&lt;/strong> (e.g., $\epsilon = 0.2$ for token-level clipping, or $C = 5$ for sequence-level clipping) regardless of sequence length. This creates a fundamental mismatch:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Aspect&lt;/th>
&lt;th>TRPO Theory&lt;/th>
&lt;th>PPO/GRPO Practice&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Trust region size&lt;/td>
&lt;td>Shrinks as $O(1/T^2)$&lt;/td>
&lt;td>Fixed constant $\epsilon$ or $C$&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Sequence-level threshold&lt;/td>
&lt;td>Should be $C \approx O(1/T^2)$&lt;/td>
&lt;td>Typically $C \in [2, 10]$ regardless of $T$&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Long sequences ($T \gg 100$)&lt;/td>
&lt;td>Requires very tight trust region&lt;/td>
&lt;td>Uses same threshold as short sequences&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>&lt;strong>Why This Matters for Long-Horizon Tasks:&lt;/strong> When $T = 2000$, the theory suggests we should use $C \approx O(10^{-7})$, but in practice we use $C = 5$. This means:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>For short sequences:&lt;/strong> The fixed threshold is too conservative (rejects valid samples)&lt;/li>
&lt;li>&lt;strong>For long sequences:&lt;/strong> The fixed threshold is too permissive (accepts out-of-trust-region samples)&lt;/li>
&lt;/ol>
&lt;p>The sequence-level estimators (Seq-TIS, Seq-MIS) suffer from this mismatch because $\rho(y) = \prod_t \rho_t$ is an &lt;strong>extensive quantity&lt;/strong> that naturally grows with $T$, while the trust region constraint requires an &lt;strong>intensive&lt;/strong> (length-normalized) measure.&lt;/p>
&lt;p>This motivates the need for a length-invariant trust region mechanism—which is exactly what Geometric Sequence Masking provides.&lt;/p>
&lt;hr>
&lt;h2 id="3-geometric-sequence-masking-a-per-token-trust-region">3. Geometric Sequence Masking: A Per-Token Trust Region&lt;/h2>
&lt;h3 id="31-from-extensive-to-intensive-metrics">3.1 From Extensive to Intensive Metrics&lt;/h3>
&lt;p>The fundamental problem with sequence-level IS is that $\rho(y) = \prod_t \rho_t$ is an &lt;strong>extensive quantity&lt;/strong>—it scales with sequence length. We need an &lt;strong>intensive quantity&lt;/strong> that measures the &lt;em>average&lt;/em> per-token divergence, independent of length.&lt;/p>
&lt;div class="alert alert-note">
&lt;div>
&lt;p>&lt;strong>Definition: Geometric Mean of the Importance Ratio&lt;/strong>&lt;/p>
&lt;p>$$
\rho_{\text{geo}}(y) = \left( \prod_{t=0}^{T-1} \rho_t \right)^{1/T} = \rho(y)^{1/T}
$$&lt;/p>
&lt;/div>
&lt;/div>
&lt;p>This is the geometric mean of the per-token ratios. It is length-invariant: if every $\rho_t = r$, then $\rho_{\text{geo}} = r$ regardless of $T$.&lt;/p>
&lt;h3 id="32-mathematical-foundation-connection-to-per-token-kl-divergence">3.2 Mathematical Foundation: Connection to Per-Token KL Divergence&lt;/h3>
&lt;p>The geometric mean has a natural interpretation in terms of KL divergence. Taking the logarithm:&lt;/p>
$$
\log \rho_{\text{geo}}(y) = \frac{1}{T} \sum_{t=0}^{T-1} \log \frac{\pi(y_t|x, y_{\lt t})}{\mu(y_t|x, y_{\lt t})}
$$
&lt;p>This is the &lt;strong>sample average of the per-token log-ratios&lt;/strong> along trajectory $y$.&lt;/p>
&lt;p>&lt;strong>Connection to KL Divergence:&lt;/strong> Recall that at each step $t$, given context $(x, y_{\lt t})$:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Forward KL:&lt;/strong>
$D_{KL}(\pi \| \mu) = \mathbb{E}_{y_t \sim \pi}\left[\log \frac{\pi(y_t|x, y_{\lt t})}{\mu(y_t|x, y_{\lt t})}\right]$&lt;/li>
&lt;li>&lt;strong>Reverse KL:&lt;/strong>
$D_{KL}(\mu \| \pi) = \mathbb{E}_{y_t \sim \mu}\left[\log \frac{\mu(y_t|x, y_{\lt t})}{\pi(y_t|x, y_{\lt t})}\right] = -\mathbb{E}_{y_t \sim \mu}\left[\log \frac{\pi(y_t|x, y_{\lt t})}{\mu(y_t|x, y_{\lt t})}\right]$&lt;/li>
&lt;/ul>
&lt;p>Since samples are drawn from $\mu$ (the behavior policy), each term $\log \rho_t = \log \frac{\pi(y_t|x, y_{\lt t})}{\mu(y_t|x, y_{\lt t})}$ is a single-sample estimate of the &lt;strong>negative reverse KL&lt;/strong>:&lt;/p>
$$
\mathbb{E}_{y_t \sim \mu}\left[\log \frac{\pi(y_t|x, y_{\lt t})}{\mu(y_t|x, y_{\lt t})}\right] = -D_{KL}(\mu(\cdot|x, y_{\lt t}) \| \pi(\cdot|x, y_{\lt t}))
$$
&lt;p>Therefore, $\log \rho_{\text{geo}}(y)$ can be interpreted as a trajectory-averaged sample of the negative reverse KL:&lt;/p>
$$
\mathbb{E}_{y \sim \mu}\left[\log \rho_{\text{geo}}(y)\right] = -\frac{1}{T} \sum_{t=0}^{T-1} \mathbb{E}_{(x, y_{\lt t}) \sim d_\mu}\left[D_{KL}(\mu(\cdot|x, y_{\lt t}) \| \pi(\cdot|x, y_{\lt t}))\right]
$$
&lt;p>&lt;strong>Connection to k1 Estimation of KL Divergence:&lt;/strong> Schulman&amp;rsquo;s framework (&lt;a href="http://joschu.net/blog/kl-approx.html" target="_blank" rel="noopener">Schulman, 2016&lt;/a>) defines three estimators for $D_{KL}(\mu | \pi)$ based on ratio $r = \mu/\pi$:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>k1:&lt;/strong> $-\log r = \log(\pi/\mu) = \log \rho$ (unbiased, high variance)&lt;/li>
&lt;li>&lt;strong>k2:&lt;/strong> $(\log r)^2 / 2 = (\log \rho)^2 / 2$ (low variance, biased)&lt;/li>
&lt;li>&lt;strong>k3:&lt;/strong> $(r - 1) - \log r = (1/\rho - 1) + \log \rho$ (unbiased, low variance)&lt;/li>
&lt;/ul>
&lt;p>In our notation with $\rho = \pi/\mu$, we have $r = 1/\rho$, so the k1 estimator equals $\log \rho$.&lt;/p>
&lt;p>Given a single trajectory sample $y \sim \mu$:&lt;/p>
$$
\log \rho_{\text{geo}}(y) = \frac{1}{T} \sum_{t=0}^{T-1} \log \rho_t = \frac{1}{T} \sum_{t=0}^{T-1} \text{(k1 estimator at step } t\text{)}
$$
&lt;p>Therefore, &lt;strong>$\log \rho_{\text{geo}}$ is the average k1 estimate&lt;/strong> along the trajectory. Since
$\mathbb{E}_{\mu}[\log \rho] = -D_{KL}(\mu \| \pi)$, we have:&lt;/p>
$$
\mathbb{E}_{y \sim \mu}\left[\log \rho_{\text{geo}}(y)\right] = -\frac{1}{T} \sum_{t=0}^{T-1} \mathbb{E}_{(x, y_{\lt t}) \sim d_\mu}\left[D_{KL}(\mu(\cdot|x, y_{\lt t}) \| \pi(\cdot|x, y_{\lt t}))\right] = -\bar{D}_{KL}
$$
&lt;p>where $\bar{D}_{KL}$ is the average per-token reverse KL along the trajectory distribution.&lt;/p>
&lt;p>&lt;strong>Why k1-based filtering is natural:&lt;/strong> Filtering by $\log \rho_{\text{geo}} &amp;gt; 0$ means $\pi$ assigns higher probability than $\mu$ on average, corresponding to negative reverse KL (or positive forward KL). Alternative trust region metrics could use k2 or k3, but k1-based filtering via $\log \rho_{\text{geo}}$ directly corresponds to the log of the geometric mean and provides an unbiased estimator of the KL divergence.&lt;/p>
&lt;p>This connects our geometric ratio directly to the trust region constraint: filtering by $|\log \rho_{\text{geo}}| \leq \epsilon$ enforces that the &lt;strong>average per-token KL&lt;/strong> (estimated via k1) remains bounded.&lt;/p>
&lt;p>&lt;strong>Interpretation:&lt;/strong> $\log \rho_{\text{geo}}(y)$ measures the average per-step log-likelihood ratio along the specific trajectory $y$. Unlike the sequence-level ratio, this quantity:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Does not grow with $T$&lt;/strong> (it&amp;rsquo;s an average, not a sum)&lt;/li>
&lt;li>&lt;strong>Can be positive or negative&lt;/strong> ($\log \rho_{\text{geo}} \gt 0$ when $\pi$ assigns higher probability, $\lt 0$ when $\mu$ assigns higher probability)&lt;/li>
&lt;li>&lt;strong>Detects both directions of drift&lt;/strong> ($\rho_{\text{geo}} \ll 1$: policy forgetting; $\rho_{\text{geo}} \gg 1$: policy collapse)&lt;/li>
&lt;li>&lt;strong>Is the average k1 estimate&lt;/strong> (where k1 = $\log \rho$ in our notation with $\rho = \pi/\mu$)&lt;/li>
&lt;/ol>
&lt;div class="alert alert-note">
&lt;div>
&lt;p>&lt;strong>Connection to TRPO Theory (Part 1):&lt;/strong> In &lt;a href="https://richardli.xyz/rl-collapse-1" target="_blank" rel="noopener">Part 1&lt;/a>, we showed that TRPO requires the trust region size to shrink with horizon: $\delta \propto 1/T^2$. This ensures the surrogate objective remains a valid approximation.&lt;/p>
&lt;p>However, enforcing a sequence-level constraint that shrinks as $O(1/T^2)$ is impractical—it would require different thresholds for every sequence length and make long sequences nearly impossible to accept.&lt;/p>
&lt;p>&lt;strong>Geo-Mask achieves length-invariance via per-token KL control.&lt;/strong> By constraining $|\log \rho_{\text{geo}}| \le \epsilon$, we enforce:&lt;/p>
&lt;p>$$
\left| \frac{1}{T} \sum_{t=0}^{T-1} \log \frac{\pi(y_t|x, y_{\lt t})}{\mu(y_t|x, y_{\lt t})} \right| \le \epsilon
$$&lt;/p>
&lt;p>This bounds the &lt;strong>average per-token KL divergence&lt;/strong> (via the k1 estimator) along the trajectory. The key insight is that this constraint is &lt;strong>independent of sequence length $T$&lt;/strong>:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Sequence-level trust region:&lt;/strong> Requires $\rho(y) = \prod_t \rho_t \le C$, where $C$ must shrink as $O(1/T^2)$ to satisfy TRPO&lt;/li>
&lt;li>&lt;strong>Per-token trust region (Geo-Mask):&lt;/strong> Requires $\rho_{\text{geo}}(y) = (\prod_t \rho_t)^{1/T} \le C$, where $C$ can be fixed (e.g., $C=2$) for all $T$&lt;/li>
&lt;/ul>
&lt;p>Since $\log \rho_{\text{geo}}(y) = \frac{1}{T}\sum_t \log \rho_t$ is the average k1 estimate (where k1 = $\log \rho$), the constraint $|\log \rho_{\text{geo}}| \le \epsilon$ approximately enforces that the average per-token KL divergence remains bounded. This is precisely the intensive (per-token) version of the trust region constraint, automatically adapting to sequence length by measuring the &lt;em>average&lt;/em> rather than the &lt;em>total&lt;/em> divergence.&lt;/p>
&lt;p>Thus, Geo-Mask is a &lt;strong>practical implementation of the TRPO hard trust region&lt;/strong> in the LLM context, with the crucial property that the acceptance criterion is &lt;strong>length-invariant&lt;/strong>.&lt;/p>
&lt;/div>
&lt;/div>
&lt;h3 id="33-the-two-sided-hard-trust-region-geo-mask">3.3 The Two-Sided Hard Trust Region (Geo-Mask)&lt;/h3>
&lt;p>With the geometric ratio, we can define a &lt;strong>Per-Token Trust Region&lt;/strong> that is independent of sequence length:&lt;/p>
&lt;div class="alert alert-note">
&lt;div>
&lt;p>&lt;strong>Definition: Geometric Sequence Masking (Geo-Mask)&lt;/strong>&lt;/p>
&lt;p>$$
\hat{g}_{\text{geo-mask}}(y) = \mathbb{I}\left( C_{\text{low}} \le \rho_{\text{geo}}(y) \le C_{\text{high}} \right) \cdot f(y)
$$&lt;/p>
&lt;p>Equivalently, in log-space:&lt;/p>
&lt;p>$$
\hat{g}_{\text{geo-mask}}(y) = \mathbb{I}\left( \log C_{\text{low}} \le \frac{1}{T}\sum_{t=0}^{T-1} \log \rho_t \le \log C_{\text{high}} \right) \cdot f(y)
$$&lt;/p>
&lt;/div>
&lt;/div>
&lt;p>&lt;strong>Note on IS Weighting:&lt;/strong> Unlike Seq-MIS which uses $\rho \cdot f$, Geo-Mask uses just $f(y)$ without importance weighting. This is because Geo-Mask is designed as a &lt;strong>pure filtering operation&lt;/strong>—it determines which samples are within the trust region. Within the trust region where $\rho_{\text{geo}} \approx 1$, we have $\pi \approx \mu$ on average, so IS correction is less critical. For full IS correction, combine with Token-TIS (see Section 3.5).&lt;/p>
&lt;p>&lt;strong>Why Two-Sided?&lt;/strong> The trust region enforces constraints in both directions:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Condition&lt;/th>
&lt;th>Meaning&lt;/th>
&lt;th>Detection&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>$\rho_{\text{geo}} \lt C_{\text{low}}$&lt;/td>
&lt;td>$\pi$ assigns much lower probability than $\mu$ on average&lt;/td>
&lt;td>Policy has drifted away from high-likelihood regions of $\mu$&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>$\rho_{\text{geo}} \gt C_{\text{high}}$&lt;/td>
&lt;td>$\pi$ assigns much higher probability than $\mu$ on average&lt;/td>
&lt;td>Policy may be collapsing/overfitting to specific patterns&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>&lt;strong>Typical Values:&lt;/strong> $C_{\text{low}} = 0.5$, $C_{\text{high}} = 2.0$ (or equivalently, $|\log \rho_{\text{geo}}| \le \log 2 \approx 0.69$).&lt;/p>
&lt;h3 id="34-trust-region-interpretation-of-geo-mask">3.4 Trust Region Interpretation of Geo-Mask&lt;/h3>
&lt;p>&lt;strong>Connection to TRPO:&lt;/strong> The masking operation in Geo-Mask directly enforces the trust region constraint from TRPO theory. Recall from the TRPO lower bound:&lt;/p>
$$
J(\pi) \geq L_\mu(\pi) - C \cdot T^2 \cdot D_{TV}^{\max}(\pi, \mu)
$$
&lt;p>The surrogate objective $L_\mu(\pi)$ is only a valid approximation to $J(\pi)$ within the trust region. Samples outside this region violate the approximation assumptions—gradient updates from such samples do not guarantee improvement in the true objective. Geo-Mask excludes these samples entirely, ensuring that &lt;strong>all gradient contributions come from the valid trust region&lt;/strong>.&lt;/p>
&lt;p>&lt;strong>Variance Control:&lt;/strong> Since there is no importance weight multiplication, the variance is naturally bounded:&lt;/p>
$$
\mathbf{Var}(\hat{g}_{\text{geo-mask}}) \le \mathbb{E}_\mu[\|f\|^2] = O(T^2)
$$
&lt;p>&lt;strong>Length Invariance:&lt;/strong> The acceptance criterion $C_{\text{low}} \le \rho_{\text{geo}} \le C_{\text{high}}$ is length-independent. A 100-token sequence and a 10,000-token sequence are judged by the same per-token divergence threshold.&lt;/p>
&lt;h3 id="35-combining-geo-mask-with-token-tis">3.5 Combining Geo-Mask with Token-TIS&lt;/h3>
&lt;p>The TRPO surrogate objective is naturally a sum over tokens, so its gradient is essentially a &lt;strong>Token-level IS&lt;/strong> gradient. This motivates combining Geo-Mask with Token-TIS:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Geo-Mask filter:&lt;/strong> Mask samples with extreme per-token divergence (length-invariant safety)&lt;/li>
&lt;li>&lt;strong>Token-TIS weighting:&lt;/strong> Apply per-token clipped importance sampling for accepted samples&lt;/li>
&lt;/ol>
&lt;div class="alert alert-note">
&lt;div>
&lt;p>&lt;strong>Definition: Geo-Mask-Token-TIS&lt;/strong>&lt;/p>
&lt;p>$$
\hat{g}_{\text{geo-mask-token-tis}}(y) = \mathbb{I}\left( C_{\text{low}} \le \rho_{\text{geo}}(y) \le C_{\text{high}} \right) \cdot \sum_{t=0}^{T-1} \min(\rho_t, C) \cdot A_t \nabla \log \pi(y_t|x, y_{\lt t})
$$&lt;/p>
&lt;/div>
&lt;/div>
&lt;p>This estimator:&lt;/p>
&lt;ol>
&lt;li>First checks if the sample is within the per-token trust region (Geo-Mask)&lt;/li>
&lt;li>Then applies per-token clipped importance weighting for accepted samples (Token-TIS)&lt;/li>
&lt;/ol>
&lt;p>&lt;strong>When to use which component:&lt;/strong>&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Component&lt;/th>
&lt;th>Purpose&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Geo-Mask ($\rho_{\text{geo}}$ filter)&lt;/td>
&lt;td>Ensures length-invariant acceptance; detects per-token drift&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Token-TIS ($\min(\rho_t, C)$ weight)&lt;/td>
&lt;td>Aligns with TRPO surrogate gradient structure&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;hr>
&lt;h2 id="4-summary-hierarchy-of-estimators-and-selection-guidelines">4. Summary: Hierarchy of Estimators and Selection Guidelines&lt;/h2>
&lt;p>We have developed a hierarchy of estimators, each addressing specific failure modes:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>&lt;strong>Estimator&lt;/strong>&lt;/th>
&lt;th>&lt;strong>Formula&lt;/strong>&lt;/th>
&lt;th>&lt;strong>Trust Region Type&lt;/strong>&lt;/th>
&lt;th>&lt;strong>Primary Use Case&lt;/strong>&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>&lt;strong>Token-IS (PPO)&lt;/strong>&lt;/td>
&lt;td>$\sum_t \min(\rho_t, C) A_t \nabla \log \pi_t$&lt;/td>
&lt;td>Per-token, soft&lt;/td>
&lt;td>Stable but has $O(T^2\Delta_{\max})$ bias&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;strong>Seq-TIS&lt;/strong>&lt;/td>
&lt;td>$\min(\rho, C) \cdot f$&lt;/td>
&lt;td>Sequence-level, soft&lt;/td>
&lt;td>Optimal bias-variance when all samples are valid&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;strong>Seq-MIS&lt;/strong>&lt;/td>
&lt;td>$\mathbb{I}(\rho \le C) \cdot \rho \cdot f$&lt;/td>
&lt;td>Sequence-level, hard&lt;/td>
&lt;td>OOD sample filtering; large mismatch scenarios&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;strong>Geo-Mask&lt;/strong>&lt;/td>
&lt;td>$\mathbb{I}(C_{\text{low}} \le \rho_{\text{geo}} \le C_{\text{high}}) \cdot f$&lt;/td>
&lt;td>Per-token, hard&lt;/td>
&lt;td>Long-horizon tasks; length-invariant masking&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;strong>Geo-Mask-Token-TIS&lt;/strong>&lt;/td>
&lt;td>Geo-Mask filter × Token-TIS weight&lt;/td>
&lt;td>Hybrid&lt;/td>
&lt;td>Long-horizon + TRPO-aligned gradient&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>For long-horizon reasoning tasks, &lt;strong>Geometric Sequence Masking (Geo-Mask)&lt;/strong> provides a principled, length-invariant Hard Trust Region that prevents the systematic length bias inherent in standard importance sampling estimators.&lt;/p>
&lt;hr>
&lt;h2 id="references">References&lt;/h2>
&lt;p>&lt;strong>Trust Region Methods:&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>Kakade, S. &amp;amp; Langford, J. (2002). &amp;ldquo;Approximately Optimal Approximate Reinforcement Learning.&amp;rdquo; &lt;em>ICML&lt;/em>. &lt;a href="https://dl.acm.org/doi/10.5555/645531.656005" target="_blank" rel="noopener">https://dl.acm.org/doi/10.5555/645531.656005&lt;/a>&lt;/li>
&lt;li>Kearns, M. &amp;amp; Singh, S. (2002). &amp;ldquo;Near-Optimal Reinforcement Learning in Polynomial Time.&amp;rdquo; &lt;em>Machine Learning&lt;/em>.&lt;/li>
&lt;li>Schulman, J., Levine, S., Moritz, P., Jordan, M. I., &amp;amp; Abbeel, P. (2015). &amp;ldquo;Trust Region Policy Optimization.&amp;rdquo; &lt;em>ICML&lt;/em>. &lt;a href="https://arxiv.org/abs/1502.05477" target="_blank" rel="noopener">https://arxiv.org/abs/1502.05477&lt;/a>&lt;/li>
&lt;li>Schulman, J., Wolski, F., Dhariwal, P., Radford, A., &amp;amp; Klimov, O. (2017). &amp;ldquo;Proximal Policy Optimization Algorithms.&amp;rdquo; arXiv:1707.06347. &lt;a href="https://arxiv.org/abs/1707.06347" target="_blank" rel="noopener">https://arxiv.org/abs/1707.06347&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>KL Divergence Estimation:&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>Schulman, J. (2016). &amp;ldquo;Approximating KL Divergence.&amp;rdquo; Blog post. &lt;a href="http://joschu.net/blog/kl-approx.html" target="_blank" rel="noopener">http://joschu.net/blog/kl-approx.html&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>LLM Reinforcement Learning:&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>Shao, Z., et al. (2024). &amp;ldquo;DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models.&amp;rdquo; arXiv:2402.03300. &lt;a href="https://arxiv.org/abs/2402.03300" target="_blank" rel="noopener">https://arxiv.org/abs/2402.03300&lt;/a>&lt;/li>
&lt;/ul></description></item><item><title>When Speed Kills Stability: Demystifying RL Collapse from the Training-Inference Mismatch</title><link>https://richardli.xyz/post/rl-collapse-training-inference/</link><pubDate>Wed, 17 Sep 2025 00:00:00 +0000</pubDate><guid>https://richardli.xyz/post/rl-collapse-training-inference/</guid><description>&lt;p>&lt;strong>Co-First Authors: Jiacai Liu and Yingru Li&lt;/strong>&lt;/p>
&lt;p>&lt;strong>Corresponding Authors: Yingru Li and Yu Shen&lt;/strong>&lt;/p>
&lt;h2 id="tldr">TL;DR&lt;/h2>
&lt;p>The relentless push for faster inference has created a dangerous &amp;ldquo;training-inference mismatch&amp;rdquo; that can silently kill reinforcement learning with LLMs. Our investigation reveals a vicious cycle that is particularly acute in modern reasoning and agentic RL:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>OOD Contexts Drive Low-Probability Sampling:&lt;/strong> Agentic workflows expose models to external inputs and dynamic environments, forcing frequent generation of low-probability tokens that are essential for novel reasoning, tool calls, and adaptive responses.&lt;/li>
&lt;li>&lt;strong>Low-Probability Tokens Amplify Training Collapse:&lt;/strong> These tokens become the weakest link—the training-inference mismatch is most severe for them, causing catastrophically large gradients that lead to silent degradation and sudden training failure.&lt;/li>
&lt;li>&lt;strong>Hardware Variability Complicates the Problem:&lt;/strong> Different GPU architectures exacerbate the mismatch unpredictably, meaning the same agentic training setup can succeed on one machine and catastrophically fail on another.&lt;/li>
&lt;li>&lt;strong>Sequence-Level IS is the Principled Solution:&lt;/strong> Sequence-level Importance Sampling emerges as the theoretically grounded fix, restoring training stability across different hardware and complex tasks.&lt;/li>
&lt;/ul>
&lt;p>&lt;a href="https://yingru.notion.site/When-Speed-Kills-Stability-Demystifying-RL-Collapse-from-the-Training-Inference-Mismatch-271211a558b7808d8b12d403fd15edda" target="_blank" rel="noopener">Read the full article on Notion →&lt;/a>&lt;/p></description></item></channel></rss>