Sampler Replay: Train the Sampler Not the Trainer in Reinforcement Learning with Verifiable Rewards

Abstract

We reframe RLVR under train-inference mismatch (TIM). Current approaches view TIM as a distribution mismatch in which the sampler's rollouts used to estimate the trainer's gradient are off-policy. In short, they train the trainer using an imperfect sampler. We argue that the sampler is the final product and should be trained. We instead view TIM as an optimization problem in which the trainer inaccurately estimates the sampler's gradient. We propose Sampler Replay, an algorithm that replays the sampler's hidden states and logits in the trainer's forward pass to better approximate the sampler's backward pass, and down-weights tokens whose hidden states still disagree with the sampler's. In short, we train the sampler using an imperfect trainer. We empirically show that Sampler Replay stabilizes RLVR across diverse settings with severe TIM, often outperforming Score Centering. Our frame explains several existing methods that improved RLVR. In particular, we prove that Score Centering mathematically equals replaying sampler's logits in the trainer.

Sampler Replay Two networks side by side, the sampler μ on the left and the trainer π on the right, each going from the input x through Layer 1, more layers, Layer L, the LM head and softmax to log p. Dashed purple connections carry the sampler's recorded values to the trainer after Layer 1 and after Layer L (Hidden States Replay, h_π ← h_π + sg(h_μ − h_π)) and at the logits (Logits Replay, which is Score Centering, ℓ_π ← ℓ_π + sg(ℓ_μ − ℓ_π)). Sampler μ Trainer π x x Sampler Replay Layer 1 h1μ​ ⋮ Layer L hLμ​ LM head logits ℓμ​ softmax log pμ Layer 1 h1π​ ← h1π​ + sg(h1μ​ − h1π​) Hidden States Replay h1π​ ⋮ ⋮ Layer L hLπ​ ← hLπ​ + sg(hLμ​ − hLπ​) Hidden States Replay hLπ​ LM head ℓπ​ ← ℓπ​ + sg(ℓμ​ − ℓπ​) Logits Replay (Score Centering) logits ℓπ​ softmax log pπ recorded recorded recorded
Figure 1. Left: Sampler Replay. Right: Qwen3-4B-Instruct-2507 on Sokoban with a W8A8 + INT4 KV sampler (top, ATW $\tau=0.1$) and Qwen3-4B-Base on DAPO with a W4A8 + INT4 KV sampler (bottom, ATW $\tau=0.3$).

§1Sampler Replay Algorithm

Sampler Replay makes the trainer's forward pass follow the sampler's. During rollout we record the sampler's hidden states and logits; during training we replay them. For a trainer operation $y=\mathcal F_\pi(x)$ whose output the sampler recorded as $y_\mu$, replay uses $y=\mathcal F_\pi(x)+\sg(y_\mu-\mathcal F_\pi(x))$, a straight-through estimator: the forward pass carries the sampler's value, so mismatches cannot accumulate from layer to layer, while the gradient still flows through the trainer's own computation. In Mixture-of-Experts models we also replay the sampler's expert routing [1]. We write $\pi_\theta^{\mathrm{replay}}$ for the trainer with these values replayed. Replaying only the logits gives the same gradient as full-vocabulary Score Centering (§1.1).

1.1 · Score Centering is Mathematically Equivalent to Replaying Sampler Logits

Theorem 1 Score Centering is Replaying Sampler Logits

Let $p=\operatorname{softmax}(z_{\mathrm{trainer}})$ and $q=\operatorname{softmax}(z_{\mathrm{sampler}})$ be the trainer's and the sampler's next-token distributions at a fixed prefix. For each token $v$ in vocabulary $\mathcal V$, let $p_v$ and $q_v$ denote the probabilities that the trainer and sampler assign to $v$, respectively. Let $y$ be the sampled token with advantage $A$. Compare full-vocabulary Score Centering,

$$ \mathcal{L}_{\mathrm{SC}}=-A\left(\log p_y-\sum_v q_v\log p_v\right), $$

with policy-gradient loss when replaying sampler logits,

$$ \mathcal{L}_{\mathrm{replay}}=-A\log\operatorname{softmax}(\tilde z)_y,\;\; \tilde z=z_{\mathrm{trainer}}+\operatorname{stopgrad}\left(z_{\mathrm{sampler}}-z_{\mathrm{trainer}}\right). $$

The two losses have the same gradient:

$$ \nabla_{z_{\mathrm{trainer}}}\mathcal{L}_{\mathrm{SC}}=\nabla_{z_{\mathrm{trainer}}}\mathcal{L}_{\mathrm{replay}}=-A(e_y-\class{hl}{q}), $$

where $e_y$ denotes $y$'s one-hot vector.

Proof

For any logit vector $z$ and token $v$, differentiating the log-softmax gives

$$ \frac{\partial}{\partial z_u}\log[\operatorname{softmax}(z)]_v =\frac{\partial}{\partial z_u}\left(z_v-\log\sum_{w\in\mathcal V}e^{z_w}\right) =\mathbb{1}_{\{u=v\}}-[\operatorname{softmax}(z)]_u. $$

In vector form, this is $\nabla_z\log[\operatorname{softmax}(z)]_v=e_v-\operatorname{softmax}(z)$, where $e_v$ is the one-hot vector for token $v$.

Applying this identity, we differentiate the Score Centering loss

$$ \nabla_{z_{\mathrm{trainer}}}\mathcal L_{\mathrm{SC}}=-A\left[(e_y-p)-\sum_{v\in\mathcal V}q_v(e_v-p)\right]. $$

We expand the sum as

$$ \begin{aligned} \sum_{v\in\mathcal V}q_v(e_v-p) &=\sum_{v\in\mathcal V}q_v e_v-\sum_{v\in\mathcal V}q_v p\\ &=\underbrace{\sum_{v\in\mathcal V}q_v e_v}_{q}-\underbrace{\left(\sum_{v\in\mathcal V}q_v\right)}_{1}p\\ &=q-p. \end{aligned} $$

The first sum equals $q$ because $q_v e_v$ contributes $q_v$ to coordinate $v$ and zero to every other coordinate. The scalar sum equals one because $q$ is a probability distribution.

Substituting gives

$$ \nabla_{z_{\mathrm{trainer}}}\mathcal L_{\mathrm{SC}}=-A\bigl[(e_y-\class{math-dim}{p})-(q-\class{math-dim}{p})\bigr]=-A(e_y-\class{hl}{q}). $$

For sampler replay, applying the chain rule through $\tilde z$ gives

$$ \begin{aligned} \nabla_{z_{\mathrm{trainer}}}\mathcal L_{\mathrm{replay}} &=\left(\frac{\partial\tilde z}{\partial z_{\mathrm{trainer}}}\right)^{\!\top}\nabla_{\tilde z}\mathcal L_{\mathrm{replay}} \\ &=-A\left(\frac{\partial\tilde z}{\partial z_{\mathrm{trainer}}}\right)^{\!\top}\bigl(e_y-\operatorname{softmax}(\tilde z)\bigr), \end{aligned} $$

where the second line uses the same log-softmax derivative. In the backward pass, the $z_{\mathrm{trainer}}$ term in $\tilde z$ contributes the identity and the stop-gradient term contributes zero, so $\partial\tilde z/\partial z_{\mathrm{trainer}}=I$. In the forward pass, $\tilde z=z_{\mathrm{sampler}}$, so $\operatorname{softmax}(\tilde z)=q$. Substituting both into the chain rule gives

$$ \nabla_{z_{\mathrm{trainer}}}\mathcal L_{\mathrm{replay}}=-A(e_y-\class{hl}{q})=\nabla_{z_{\mathrm{trainer}}}\mathcal L_{\mathrm{SC}}. $$

1.2 · Activation Trust Weighting (ATW)

Sampler Replay attempts to reproduce the sampler's forward pass in the trainer, but they can still differ significantly for some tokens. We use the agreement between the trainer's and the sampler's hidden state activations $h$ as a proxy for the reliability of the trainer's gradient estimate. Our intuition is that a large mismatch at even one layer can affect the gradients propagated through that layer to others. We define a measure of mismatch for some token $t$ as the largest cosine distance between hidden states across all layers, $d_t = \max_{i} \bigl(1-\cos(h^{\mu}_{i,t}, h^{\pi}_{i,t})\bigr)$. We therefore down-weight each token's gradient contribution linearly with its mismatch. We call this Activation Trust Weighting.

$$ \widehat{g}_\mu =\mathbb{E}_{x\sim D,\,y\sim\mu}\left[\sum_{t=1}^{|y|} A_t \underbrace{\max\left(0,1-\frac{d_t}{\tau}\right)}_{\text{Trust Weighting}} \underbrace{\vphantom{\max\left(0,1-\frac{d_t}{\tau}\right)}\nabla_\theta \log\pi_\theta^{\mathrm{replay}}(y_t\mid x,y_{\lt t})}_{\text{Sampler replay}}\right]. $$

§2Experiments

We empirically compare Sampler Replay, with and without ATW, to Score Centering [2], with and without Truncated Importance Sampling [3], across setups with high TIM.

2.1 · Qwen3-0.6B on Countdown and Qwen3-0.6B-Base on GSM8K

We train Qwen3-0.6B on Countdown [4] with only the inference quantized to INT8. As shown in Figure 2 (left) Sampler Replay consistently achieves higher training reward than SC. Next, we create a controlled, artificial TIM through weight perturbation: SC stalls at 0.65 and training becomes incredibly noisy, while Sampler Replay is minimally affected (middle). With bf16 weights but a quantized INT4 KV cache on Qwen3-0.6B-Base on GSM8K, SC and SC+TIS initially learn but quickly collapse, while Sampler Replay training is stable (right).

Figure 2. Qwen3-0.6B on Countdown with an INT8 sampler and with sampler weight noise, and Qwen3-0.6B-Base on GSM8K with an INT4 KV cache.

2.2 · Qwen3-4B-Instruct on Sokoban

We train Qwen3-4B-Instruct-2507 on Sokoban puzzles, pushing 3 boxes onto their targets in a maze, with a W8A8+KV-4 sampler. As shown in Figure 3 Replay+ATW and Replay both achieve higher reward than both SC and SC+TIS. At step 600, the end of each curve, Replay+ATW scores $55.9\%$ on validation while SC scores $45.6\%$.

Figure 3. Qwen3-4B-Instruct-2507 on Sokoban with a W8A8 + INT4 KV sampler against wall-clock hours on 4 H100s, up to step 600 (training steps plus validation passes); ATW $\tau=0.1$. Dashed grey: TIS with an unquantized BF16 sampler at step 600.

2.3 · Qwen3-4B-Base on DAPO

We train Qwen3-4B-Base on DAPO-Math-17K [5] and evaluate on AIME 2024 and 2025 [6], with W4A8 or W8A8 and a 4-bit KV cache. As shown in Figure 4, Sampler Replay+ATW achieves the best performance across both. Sampler Replay on its own outperforms SC in higher TIM W4A8 setup, but underperforms in the more stable W8A8. Interestingly, SC+TIS learns slower than SC and underperforms in both setups.

Figure 4. Qwen3-4B-Base on DAPO with a W4A8 + INT4 KV sampler (top) and a W8A8 + INT4 KV sampler (bottom); ATW $\tau=0.3$.

2.4 · Staleness

We follow the minibatch synchronous RL setup of [2][7], where the sampler generates 64 minibatches of data at a time and the trainer then does 64 updates, which make the data 0-63 steps stale. On Countdown, both Sampler Replay+ATW and SC+TIS succeed, but neither Sampler Replay nor SC works without ATW or TIS (Figure 5, left). On GSM8K, only Sampler Replay+ATW can successfully improve through RL (right).

Figure 5. Qwen3-0.6B on Countdown and Qwen3.5-0.8B-Base on GSM8K, the sampler syncing its weights every 64 steps; ATW $\tau=10^{-4}$ and $10^{-3}$.

2.5 · KL Regularization Toward the Sampler

Following Thinking Machines Lab [8], we add a KL penalty toward the sampler, another way to keep the trainer close to the sampler, estimating $\mathrm{KL}(\mu\|\pi_\theta)$ with $k_3$. On Countdown, it modestly improves SC with the INT8 sampler, but SC + KL collapses under weight perturbation and REINFORCE + KL collapses in both settings. On GSM8K, $\beta=10^{-4}$ improves over SC, though it remains below Sampler Replay (Figure 6).

Figure 6. KL regularization toward the sampler in the settings of Figure 2, with $\beta=0.01$ on Countdown and $\beta=10^{-4}$ on GSM8K.

§3Streaming Hidden-State Replay

Hidden State Replay records the sampler's hidden states entering every layer, and a single rollout batch can produce hundreds of GiB of them. We store them in node-local RAM or on local disk, so they don't use GPU memory. Like the KV cache, replay states accumulate as the sampler generates tokens, and once recorded they can leave the GPU: we preallocate two buffers in GPU memory, and while the sampler fills one, the other drains asynchronously to local storage; the buffers then switch roles. The trainer fetches the states in microbatch order, transferring microbatch $i+1$'s states to the GPU while it runs the forward and backward passes on microbatch $i$. Overlapping transfers with computation keeps the wall-clock cost of replay small.

Streaming hidden-state replay One RL step. Top, GPU memory; bottom, node-local RAM or disk. Sampler (vLLM, rollout): during generation two preallocated GPU buffers alternate, one fills with hidden states while the other drains to node-local storage, appended token by token into one array per trajectory. Trainer (PyTorch FSDP, update): microbatch i+1's states load trajectory by trajectory while microbatch i runs its forward and backward pass. The last drain and the first load are hatched: no computation overlaps them. GPUMEMORY NODE-LOCALRAM OR DISK Two preallocated buffers: one fills while the other drains Sampler: rollout vLLM generation buffer A buffer B fill drain fill drain fill drain fill drain appended token by token Read-ahead: microbatch i+1 loads while i trains Trainer: update PyTorch FSDP microbatch 1forward + backward microbatch 2forward + backward microbatch 3forward + backward … load 1 load 2 load 3 read trajectory by trajectory Hidden states live off the GPU: one array per trajectory trajectory 1 trajectory 2 trajectory 3 trajectory 4 trajectory 5 trajectory 6 … layers tokens microbatch 1 microbatch 2 microbatch 3

References

  1. Wenhan Ma, Hailin Zhang, Liang Zhao, Yifan Song, Yudong Wang, Zhifang Sui and Fuli Luo. Stabilizing MoE Reinforcement Learning by Aligning Training and Inference Routers. International Conference on Machine Learning (ICML), 2026.
  2. Martin Marek and Max Ryabinin. Score Centering Stabilizes Off-policy Reinforcement Learning. arXiv preprint arXiv:2609.20807, 2026.
  3. Feng Yao, Liyuan Liu, Dinghuai Zhang, Chengyu Dong, Jingbo Shang and Jianfeng Gao. Your Efficient RL Framework Secretly Brings You Off-Policy RL Training. Feng Yao's Notion, 2025.
  4. Kanishk Gandhi, Denise Lee, Gabriel Grand, Muxin Liu, Winson Cheng, Archit Sharma and Noah D. Goodman. Stream of Search (SoS): Learning to Search in Language. COLM, 2024.
  5. Qiying Yu, Zheng Zhang, Ruofei Zhu, Yufeng Yuan, Xiaochen Zuo, Yu Yue, Weinan Dai, Tiantian Fan et al. DAPO: An Open-Source LLM Reinforcement Learning System at Scale. arXiv:2503.14476 [cs], 2025.
  6. Jasper Dekoninck, Nikola Jovanović, Tim Gehrunger, Kári Rögnvalddson, Ivo Petrov, Chenhao Sun and Martin Vechev. Beyond Benchmarks: MathArena as an Evaluation Platform for Mathematics with LLMs. \_eprint: 2605.00674, 2026.
  7. Shu Liu, Qi Fang and Shubham Agarwal. Drift and Distance: When Is Score Centering Enough in Async RL?. Blog post, 2026.
  8. Thinking Machines Lab. StableReinforce (alpha). Tinker Documentation, 2026.

Citation

If you found this post useful and would like to cite it:

Plain text

Aghajohari, Milad, Kamran Chitsaz, Sarath Chandar, Alessandro Sordoni, Michael Noukhovitch, and Aaron Courville. “Sampler Replay: Train The Sampler Not The Trainer in Reinforcement Learning with Verifiable Rewards.” arXiv:2611.41862, 2026.

BibTeX

@misc{aghajohari2026samplerreplay,
      title={Sampler Replay: Train The Sampler Not The Trainer in Reinforcement Learning with Verifiable Rewards},
      author={Milad Aghajohari and Kamran Chitsaz and Sarath Chandar and Alessandro Sordoni and Michael Noukhovitch and Aaron Courville},
      year={2026},
      eprint={2611.41862},
      archivePrefix={arXiv},
      primaryClass={cs.LG},
      url={https://arxiv.org/abs/2611.41862},
}