Three Stability Constraints for Reinforcement Learning Post-Training of Large Language Models
This article examines why large-scale reinforcement learning post-training for large language models can become unstable and presents three practical stability constraints. It reviews the GRPO objective, then covers an unbiased KL estimator with importance weighting, sequence-level drift filtering, and inference-training consistency for expert routing and sampling masks. It also provides implementation notes and a deployment checklist for monitoring and tuning these controls.
1. Background: Why Large-Scale Reinforcement Learning Is Prone to Instability
After pretraining, large language models usually go through supervised fine-tuning and reinforcement learning. The reinforcement learning stage lets the model continuously adjust its output distribution based on reward signals, which can significantly improve performance on reasoning, code, and math tasks. However, as training scales up, stability problems become more visible, mainly in three areas:
- Numerical issues: Some tokens have extremely low probabilities, and estimators that rely on probability ratios can produce excessively large gradients.
- Distribution shift: Sampled data comes from an old policy. After multiple parameter updates, the gap between old samples and the current policy grows.
- Environment mismatch: The inference engine and the training forward pass differ in details such as expert routing and sampling truncation, causing the optimization objective to diverge from the actual generation process.
These three problems share a common cause: formulas that are correct in theory fail under finite precision, asynchronous sampling, and engineering implementation constraints. As a result, common engineering practice is to trade off theoretical exactness for numerical stability and prioritize preventing training collapse.
2. Basic Framework: GRPO Objective Review
GRPO (Group Relative Policy Optimization) is a commonly used policy optimization algorithm. For the same question, it samples a group of responses and estimates the advantage function from the relative rewards within the group, avoiding the overhead of training a separate value network. Its objective roughly consists of three parts:
- Policy term: Multiply the advantage by the importance ratio π_θ / π_old, and use clipping to limit the magnitude of each update.
- KL regularization term: Constrain the current policy π_θ from deviating too far from the reference policy π_ref, preventing the model from drifting too far in reward optimization.
- Data term: Determine which samples participate in gradient computation.
The three corrections discussed in this article act on the KL regularization term, data filtering, and inference-training consistency, respectively.
3. Correction 1: Unbiased KL Estimation and Gradient Stability
3.1 Source of the Problem
The KL regularization term is usually written as DKL(πθ ‖ πref). In actual training, KL must be estimated from sampled tokens. A common estimator has the form r − log r − 1, where r = π_ref / πθ. Its advantages are that it is always nonnegative and simple to compute. However, after differentiating with respect to θ, the gradient coefficient is (1 − r)∇log π_θ. When a token has a very low probability under the current policy while the reference policy still assigns it a high probability, r becomes large and the gradient grows accordingly.
3.2 Unbiased Correction
Samples are drawn from πold. Directly averaging over them gives an expectation under π_old, not the KL under the current policy πθ. After introducing the importance weight πθ / π_old, as long as π_old has positive probability wherever πθ has probability mass, we have:
E{π_old}[ (πθ / πold) · f ] = E{π_θ}[ f ]
Therefore, the corrected estimator equals the KL under the current policy in expectation, i.e., it is “unbiased.” This step solves the problem of mismatched estimation targets. The corresponding formula is:
DKL ≈ (πθ / πold) · ( r − log r − 1 ), where r = π_ref / πθ
3.3 Implementation Notes
The importance weight itself can also be large. In engineering practice, an upper bound is often placed on the weight, trading a small amount of bias for controllable variance. The specific value should be tuned based on validation data. Example code:
# Log probabilities per token, aligned with response tokenslog_ratio_ref = logp_ref - logp_theta # log(π_ref / π_θ)r = exp(log_ratio_ref)kl_raw = r - log_ratio_ref - 1 # Nonnegative; grows quickly when r is largew = exp(logp_theta - logp_old) # π_θ / π_oldw = min(w, w_max) # Engineering upper bound on weightkl_token = w * kl_rawkl_loss = masked_mean(kl_token, response_mask)
4. Correction 2: Sequence-Level Drift Filtering
4.1 Why Filtering Is Needed
PPO-style clipping is performed per token. However, a single response may contain many tokens that simultaneously deviate from the old policy, and per-token clipping cannot reflect the distribution difference of the entire sequence. If a sequence drifts significantly overall and its advantage is negative, continuing to use it to lower its probability may push the model in an unreliable direction.
4.2 Method
A common design is to compute the sequence-level mean log ratio. If the sequence drift exceeds a threshold, the entire sequence is masked from gradient computation. The filter can be further restricted to sequences with negative advantage to reduce erroneous penalties. The filtering criterion is whether the sample is still representative, independent of reward magnitude.
for seq in batch:# Mean log ratio across tokens in the sequencemean_log_ratio = mean(logp_theta[seq] - logp_old[seq])drift = abs(mean_log_ratio)if drift > drift_threshold:seq.loss_mask = 0 # Entire sequence does not contribute to gradientelse:seq.loss_mask = 1
4.3 Threshold and Monitoring
There is no universal threshold value; it needs to be adjusted together with the masking ratio. If the masking ratio is too high, the training data is severely out of sync with the current policy, and one should consider reducing the number of update steps per round or lowering the learning rate. If the ratio is too low, the filtering may have no practical effect.
5. Correction 3: Consistency Constraints Between Inference and Training
5.1 Source of the Problem
In theory, reinforcement learning requires gradients to act on the computation process used during actual sampling. However, in large-scale systems, inference and training often differ in several details:
- In Mixture-of-Experts (MoE) models, which experts each token is routed to may differ due to numerical precision or batch organization.
- If the Top-P or Top-K truncation used during sampling is not synchronized in the training forward pass, the candidate set differs, and the normalized probabilities change accordingly.
These differences make the optimization objective inconsistent with the actual generation distribution. They can manifest as rising training reward but unstable online generation quality.
5.2 Two Consistency Constraints
Routing consistency: During inference, record the selected expert indices for each layer and each token, and force the training forward pass to use the same experts.
Sampling mask consistency: During inference, record the candidate set after truncation. When computing log probabilities during training, apply the same mask and renormalize.
# Inference stage: record routing results and sampling candidate setrouting_record[token_id, layer] = selected_expertskeep_mask[token_id] = top_p_mask(probs, p)# Training stage: reuse records so gradients follow the actual sampling pathlogits_train = forward_train(x, forced_experts=routing_record)masked_logits = where(keep_mask, logits_train, -inf)logp_theta = log_softmax(masked_logits)[action]
5.3 Cost
Recording and reuse increase storage and communication overhead, and may also constrain expert load balancing. Engineering teams need to evaluate whether the benefits cover these costs.
6. Implementation Checklist
- Monitor the distribution of KL estimates, focusing on extreme values and the frequency of NaN.
- Record quantiles of importance weights and set a reasonable upper bound based on them.
- Track the sequence masking ratio and tune it jointly with the learning rate and the number of iterations per round.
- Compare token probability differences between inference and training to locate sources of inconsistency.
- Fix random seeds and run small-scale reproductions to distinguish numerical issues from algorithmic issues.
7. Summary
The core of stability corrections is to keep the optimization objective consistent with the actual data and actual computation process, and to keep numerical values controllable. The three corrections address estimation bias, data mismatch, and environment mismatch. In engineering practice, each item should be validated with monitoring metrics rather than enabling all of them at once.
