Mila's Sampler Replay Fixes AI Training Drift Under Aggressive Quantization
A new RLVR method copies the sampler's hidden states and logits into the trainer, stabilizing training under heavy quantization and staleness.
Takeaways
- Sampler Replay splices the sampler's hidden states and logits into the trainer's forward pass via straight-through estimation.
- The paper proves full-vocabulary Score Centering is mathematically equivalent to replaying sampler logits.
- Replay plus Activation Trust Weighting beats SC and SC+TIS across Countdown, GSM8K, DAPO, Sokoban, and a 30B MoE.
- On Sokoban with W8A8+INT4 KV at step 600, Replay+ATW hits 55.9% validation vs 45.6% for SC.
- TIS can conflict with logit replay because importance ratios collapse to 1 on replayed tokens.
- Code is open source at chandar-lab/Sampler-Replay, built on verl, vLLM, and FSDP.
Reinforcement learning with verifiable rewards (RLVR), where an automatic checker scores model outputs, commonly splits one training policy across two software stacks. A fast inference engine generates rollouts with quantized weights and a key-value (KV) cache; a higher-precision training engine recomputes those sequences and produces gradients. Their token probabilities and internal activations can diverge, so the gradient optimizes a forward pass that did not generate the data. A Mila paper proposes Sampler Replay, which makes the trainer’s forward pass reproduce the sampler’s computation while preserving the trainer’s backward pass.
The serving model sets the target
Standard RLVR pipelines denote the trainer policy as π and the sampler policy as μ. Truncated Importance Sampling (TIS) treats sampler trajectories as off-policy data and scales each token gradient by a clipped probability ratio, π(a|s) / μ(a|s). Sampler Replay treats the served sampler as the target behavior and uses the trainer as a gradient carrier for an inference stack that has no backward graph.
Users interact with the inference stack, including its quantized weights, cache format, sampling masks, and mixture-of-experts routing. Reward gains confined to the higher-precision trainer may disappear when those serving choices change the model’s forward computation.
Borrowing the sampler’s forward pass
- Record the rollout: The sampler stores each layer’s hidden states and the final logits while generating a response.
- Replay the activations: The trainer computes each layer, then injects the sampler’s recorded value through a stop-gradient operation.
- Backpropagate through the trainer: Automatic differentiation follows the trainer’s parameters and local derivatives, even though the forward values match the sampler.
Each injected layer output uses the following expression:
h = h_π + sg(h_μ - h_π)
The stop-gradient operator sg returns its input during the forward pass and has zero derivative. The expression therefore evaluates to the sampler activation h_μ, while its gradient follows the trainer activation h_π. Applying it at every layer reproduces the sampler’s forward trajectory and retains the trainer’s Jacobians for optimization.
Logit replay also explains an existing RLVR stabilization method. The authors prove that full-vocabulary Score Centering and sampler-logit replay produce the same gradient with respect to the trainer’s logits. Hidden-state replay extends that equivalence deeper into the network.
Keep Sampling Mask fits the same pattern by reusing the sampler’s top-p mask. Rollout Router Replay reuses expert selections in a mixture-of-experts model, where a router sends each token to a subset of parameter blocks.
Importance ratios collapse
Replaying sampler logits makes the trainer’s forward probabilities equal to the sampler’s probabilities. The importance ratio π / μ then evaluates to 1, removing the signal TIS uses to select or down-weight tokens. The paper reports slower training for Score Centering combined with TIS than for Score Centering alone in several configurations.
Activation Trust Weighting (ATW) replaces that probability-based filter with an activation comparison. For each token, it finds the largest cosine distance between sampler and trainer hidden states across all layers. A linear weight, max(0, 1 - d_max / τ), reduces the token’s gradient as the mismatch grows and sets it to zero beyond threshold τ.
ATW and importance sampling identify different tokens. On Sokoban rollouts generated with a W4A8 sampler and an INT4 KV cache, their Spearman correlation is about -0.05. Among the tokens ATW assigns zero weight, 80.5% receive an importance weight of at least 1.
Quantization, noise, and stale rollouts
| Model | Evaluation | Relevant feature |
|---|---|---|
| Qwen3-0.6B | Countdown and GSM8K | Small-model training and sampler staleness |
| Qwen3-4B-Base | DAPO math | Aggressive weight quantization |
| Qwen3-4B-Instruct-2507 | Sokoban | Quantized weights and KV caches |
| Qwen3-30B-A3B-Base | INTELLECT-2 math | Mixture-of-experts router replay |
W4A8 uses 4-bit weights and 8-bit activations, while W8A8 uses 8-bit weights and activations. An INT4 KV cache stores attention keys and values at 4-bit precision. The experiments also inject weight noise and test data staleness by running 64 trainer updates between sampler synchronizations.
Results under stress
| Setup | Reported result |
|---|---|
| Sokoban, W8A8 with INT4 KV | At step 600, Replay+ATW reaches a 55.9% validation score, compared with 45.6% for Score Centering. Score Centering with TIS underperforms Score Centering alone. |
| DAPO, W4A8 | Replay+ATW is the most stable and highest-performing tested configuration. |
| GSM8K, 64-update staleness | Replay+ATW trains successfully; Score Centering and unweighted Sampler Replay fail. |
Ablations isolate the gains
- Logit replay: Training tracks Score Centering almost exactly, matching the paper’s equivalence theorem.
- Hidden-state replay: Replaying intermediate activations improves training independently, and combining hidden states with logits produces a larger gain.
- Gradient direction: In a perturbed-weight analysis, mean layer-0 cosine similarity with the reference gradient over the final 20 steps is 0.76 for full Sampler Replay, 0.49 for logit replay, 0.43 for Score Centering, and 0.37 for Score Centering with TIS.
- Gradient scale: Full replay keeps the gradient-norm ratio near 1, compared with 0.65 for Score Centering with TIS.
Replay shifts the cost to memory
Full hidden-state replay can generate about 370 GiB of activation data for one Sokoban batch containing 256 responses of 8,192 tokens each. Keeping that volume in GPU memory is impractical.
Sampler Replay streams activations from the GPU to node-local RAM or disk through double-buffered transfers adapted from vLLM’s PagedAttention memory management. Transfers overlap the trainer’s forward and backward passes to limit idle time.
An unquantized language-model head permits replaying its input, reducing per-token storage on Qwen3-4B from 297 KiB of logits to 5 KiB. Custom CUDA kernels for the INT4 KV cache, developed with Claude, deliver roughly twice the output throughput of BF16 attention on H100 and A100 GPUs. The authors report end-to-end wall-clock cost comparable to Score Centering.
Untested edges
- ATW theory: The weighting rule remains heuristic. Under stale sampling, its down-weighting can resemble a lower effective learning rate, which may confound the staleness results.
- Asynchronous rollouts: The method has not been evaluated when one rollout spans multiple sampler updates. Such systems must determine whether to replay recorded states or recompute them.
- Distributed topology: The current implementation assumes one GPU per sampler replica on a single node. Tensor-parallel samplers and multi-node replay remain future work.
Code and deployment fit
The GitHub code builds on verl, with vLLM for sampling and FSDP for training. The project site provides the paper and supporting material.
Teams using aggressive inference quantization or long sampler-sync intervals can evaluate Replay+ATW when Score Centering or TIS becomes unstable. Existing Score Centering systems already implement the logit-only case in gradient terms; hidden-state and router replay extend that design at the cost of activation transport.