# Training-Inference Mismatch:LLM 强化学习训推不一致的根因与修正方法 > 深入分析 LLM 强化学习中的 Training-Inference Mismatch(TIM)问题:拆解训推不一致的误差来源、对策略优化的影响及 TIS/MIS 等修正方案。 - **URL**: https://maosong.website/blog/train-inference-mismatch/ - **Authors**: Mao Song - **Published**: 2026-06-25 10:36:50+08:00 --- > 现代 LLM RL 框架通常将高吞吐推理引擎(vLLM / SGLang)与高精度训练引擎(FSDP / Megatron)解耦,导致同一组权重 $\theta$ 在两端给出不同概率分布($\pi_\theta \neq \mu_\theta$),引发 **Training-Inference Mismatch (TIM)**: - **危害机制**:TIM 将名义上的 on-policy 训练悄悄变为有偏的 off-policy 更新;在 **MoE 模型**中,离散的 Top-K 路由差异会进一步放大这一误差,导致低概率 token 处 $\mathrm{KL}_{\mathrm{TIM}}$ 飙升、梯度范数爆炸与奖励崩塌。 - **修正路径**:一方面在 **Infra 层**对齐精度与算子(FP32 lm_head/softmax、FP16 训练、Batch-Invariant Kernels、MoE Router Replay R2/R3);另一方面在 **算法层**通过截断或掩码重要性采样(TIS、Seq-MIS、Rollout Masking、IcePop、KPop、IPO)抑制高方差离群样本。 ## Introduction 现代的 RL 框架比如 verl [@verl] 会使用不同的框架来完成 inference engine 和 training engine, 比如说使用 megatron [@Megatron] 或者 FSDP [@FSDP] 进行训练,使用 vLLM [@vllm] 或者 SGLang [@SGLang] 进行推理。 尽管使用的模型一致,但由于 training engine 和 inference engine 的优化目标不同:前者要求数值稳定性和精度, 后者强调最大化 throughput. 这种优化目标的不一致使得 inference engine 和 training engine 的实现和最终输出不完全一致,最终体现为随着 RL 训练进行,gradient norm 会突然爆炸,reward 突然下降,模型训练崩溃。 典型现象如下图所示: *[Interactive visualization: tim-training-collapse.html]* 我们将这个问题称为 training-inference mismatch (TIM) 问题。 本文我们将深入探讨 TIM 的原因,解决方法以及相关的前沿研究。 ## Problem Definition 我们首先从优化目标的角度来定义 TIM. ### RL Objective and Policy Gradient RL 的目标函数为 $ \mathcal{J}(\theta) = \mathbb{E}_{x\sim\mathcal{D},y\sim\pi_{\theta}(\cdot\mid x)}[R(x,y)] $ 其中 $\pi_{\theta}$ 是优化的 policy, $R(x,y)$ 是 reward function. See [RL4LLM](https://maosong.website/blog/RL4LLM/) for details. 对应的梯度为 $ \nabla_{\theta}\mathcal{J}(\theta) =\mathbb{E}_{x\sim\mathcal{D},y\sim\pi_{\theta}(\cdot\mid x)}[R(x,y)\nabla_{\theta}\log\pi_{\theta}(\theta)] $ 其计算过程为,先从 $\pi_{\theta}$ 中采样得到轨迹 $\{(x,y, R(x,y))\}$, 然后更新 $\pi_{\theta}$ ### Training and Inference Policies 对于现代 RL 训练框架如 verl [@verl] 来说,由于 training engine 和 inference engine 的不同,我们的采样的输出 $y$ 实际上来源不再是 $\pi_{\theta}$, 而是 inference engine 如 vllm 或 SGLang 对 policy $\pi_{\theta}$ 的实现,我们将这个策略记为 $\mu_{\theta}$. 此时,我们使用的梯度实际上是 $ \nabla_{\theta}\mathcal{J}_{\text{biased}}(\theta) =\mathbb{E}_{x\sim\mathcal{D},y\sim\mu_{\theta}(\cdot\mid x)}[R(x,y)\nabla_{\theta}\log\pi_{\theta}(\theta)] $ 所以,TIM 其实指的就是 $ \nabla_{\theta}\mathcal{J}(\theta) \neq \nabla_{\theta}\mathcal{J}_{\text{biased}}(\theta) $ 其具体原因在于 $ \pi_{\theta}(\cdot\mid x)\neq \mu_{\theta}(\cdot\mid x) $ 也就是说,inference engine 和 training engine 对于同一输入的输出概率不同。 ## Causes and Theoretical Analysis 这一节我们分析如何定量分析 TIM 以及可能导致 TIM 的原因 ### Kernel Consistency and Determinism ### Discrepancy and Policy Staleness 我们使用 MiniRL [@MiniRL] 提出的框架来分析 TIM. 注意到我们可以将 importance sampling (IS) 重写为 $ \frac{\pi_{\theta}(y\mid x)}{\mu_{\theta_{\mathrm{old}}}(y\mid x) } = \frac{\pi_{\theta_{\mathrm{old}}}(y_t\mid x,y_{0$ 是一个超参数。 对于 PPO [@PPO] 算法,其目标函数为 $ \mathcal{J}_{PPO}(\theta) = \mathbb{E}_{x\sim\mathcal{D}, y\sim\mu_{\theta_{\mathrm{old}}}(y\mid x)}\left[\min\left(\frac{\pi_{\theta}(y\mid x)}{\pi_{\theta_{\mathrm{old}}}(y\mid x) }\hat{A}_t,\mathrm{clip}\left(\frac{\pi_{\theta}(y\mid x)}{\pi_{\theta_{\mathrm{old}}}(y\mid x) },1-\epsilon,1+\epsilon\right)\hat{A}_t\right)\right] $ 使用 TIS 之后,梯度为 $ \htmlId{tis_ppo_gradient}{\begin{equation} \nabla_{\theta}\mathcal{J}_{PPO}(\theta) = \mathbb{E}_{x\sim\mathcal{D}, y\sim\mu_{\theta_{\mathrm{old}}}(y\mid x)}\left[\min\left(\frac{\pi_{\theta}(y\mid x)}{\mu_{\theta_{\mathrm{old}}}(y\mid x) }, C\right)\nabla_{\theta}\min\left(\frac{\pi_{\theta}(y\mid x)}{\pi_{\theta_{\mathrm{old}}}(y\mid x) }\hat{A}_t,\mathrm{clip}\left(\frac{\pi_{\theta}(y\mid x)}{\pi_{\theta_{\mathrm{old}}}(y\mid x) },1-\epsilon,1+\epsilon\right)\hat{A}_t\right)\right] \end{equation}} $ #### Masked Importance Sampling ##### Sequence-Level MIS [@liu-li-2025-rl-collapse] 提出了 sequence level 的 MIS $ \mathcal{J}_{seq-mis}(\theta) = \mathbb{E}_{x\sim\mathcal{D}, y\sim\mu_{\theta_{\mathrm{old}}}(y\mid x)}\left[\rho\cdot\mathbf{1}\left(\rho \leq C\right)\cdot\min\left(\frac{\pi_{\theta}(y\mid x)}{\pi_{\theta_{\mathrm{old}}}(y\mid x) }\hat{A}_t,\mathrm{clip}\left(\frac{\pi_{\theta}(y\mid x)}{\pi_{\theta_{\mathrm{old}}}(y\mid x) },1-\epsilon,1+\epsilon\right)\hat{A}_t\right)\right] $ 其中 $ \rho = \frac{\pi_{\theta}(y\mid x)}{\mu_{\theta_{\mathrm{old}}}(y\mid x)} $ ##### DeepSeek-V3.2: Rollout Masking DeepSeek-V3.2 [@DeepSeek-V3.2] 提出了针对 rollout 的的 binary mask $ \mathcal{J}_{GRPO}(\theta) = \mathbb{E}_{x\sim\mathcal{D}, y_i\sim\mu_{\theta_{\mathrm{old}}}(y\mid x),i=1,\dots,G}\left[\frac{1}{G}\sum_{i=1}^G\frac{1}{|y_i|}\sum_{t=1}^{|y_i|}\mathcal{M}_{i,t}\cdot\min\left(r_{i,t}(\theta)\hat{A}_{i,t},\mathrm{clip}\left(r_{i,t}(\theta),1-\epsilon,1+\epsilon\right)\hat{A}_t\right)\right] $ 其中 $ r_{i,t}(\theta) = \frac{\pi_{\theta}(y_{i,t}\mid y_{i,\delta\\ 1, &\text{ otherwise} \end{cases} $ ##### IcePop: Ratio-Based Masking IcePop [@IcePop2025] 提出了基于 mask 的 importance sampling 方法,其目标函数如下所示 $ \mathcal{J}_{IcePop}(\theta) = \mathbb{E}_{x\sim\mathcal{D}, y\sim\mu_{\theta_{\mathrm{old}}}(y\mid x)}\left[\mathcal{M}\left(\frac{\pi_{\theta}(y\mid x)}{\mu_{\theta_{\mathrm{old}}}(y\mid x)},\alpha,\beta\right)\cdot\min\left(\frac{\pi_{\theta}(y\mid x)}{\pi_{\theta_{\mathrm{old}}}(y\mid x) }\hat{A}_t,\mathrm{clip}\left(\frac{\pi_{\theta}(y\mid x)}{\pi_{\theta_{\mathrm{old}}}(y\mid x) },1-\epsilon,1+\epsilon\right)\hat{A}_t\right)\right] $ 其中 $ \mathcal{M}\left(x,\alpha,\beta\right) = \begin{cases} x, &\text{ if } k\in [\alpha,\beta]\\ 0, &\text{ otherwise} \end{cases} $ ##### KPop: Binary KL-Based Masking 但是,随着训练进行, IcePop mask 的 token 数越来越少,导致 TIM 现象仍然存在,为了解决这个问题,KPop [@KPop] 进行了优化,最终目标函数一致,但是 mask 区域变成了 $ \mathcal{M}\left(y_t;\phi\right) = \mathbf{1}\left(\mathrm{KL}_B(\pi_{\theta}(y_t\mid y_{ \epsilon $ 通过实验作者发现,IPO 比 IcePop 更加稳定。 ## Engineering Practices - KAT coder v1 pro - GEPO - GSPO [@GSPO] - top-p sampling candidate sets [@mai_thinking_1] ### Framework Implementations - Torchtitan: vllm, deterministic kernels, customized backward - slime: chunked prefill, CUDA graph, Radix Cache, non-greedy sampling ### Methods Used in Models | Method | Models | Description | | ------------ | ----------------------------- | ------------------------ | | FP32 lm head | MiniMax-M1 | | | R3 | MAI-thinking-1, MiMo-V2-Flash | replay routing (default) | | IcePop, Kpop | Ling-2.6 | TIS | | TIS | Ring-Zero | | | MIS | Nemotron-3-Super | | Step 5 Preview [@Step-5-Preview] 采用了如下优化: 1. infra 上,使用了 bitwise alignment, 以 $<10\%$ overhead 的代价在 staleness 为 0 时实现 0 log prob difference. 2. 算法上,使用了 R3, top-p candidate set 以及 zero-center projection.