verl icon indicating copy to clipboard operation
verl copied to clipboard

`log_prob` and `old_log_prob` differ in on-policy setting when rollout and actor micro batch per gpu differ

Open kqhxnm-zzz opened this issue 6 months ago • 6 comments

When following the official recommendation to set rollout.log_prob_micro_batch_size_per_gpu = 2 * actor.ppo_micro_batch_size_per_gpu, log_prob and old_log_prob are different even in an on-policy setting:

actor.ppo_epochs = 1 and train_batch_size = actor.ppo_mini_batch_size.

In this setting, the training should be on-policy, but pg_clipfrac in the logs is non-zero, indicating that the logits are different.

A simple workaround is to set:

rollout.log_prob_micro_batch_size_per_gpu = actor.ppo_micro_batch_size_per_gpu

Another possible workaround for the on-policy case is to directly set:

old_log_prob = log_prob

This was done in #3119, but seems to have been removed in newer versions. Also, this workaround is not valid for off-policy training.

Is this caused by kernels that are not batch-invariant? Is there a way to fix this without significantly affecting performance?

kqhxnm-zzz avatar May 08 '26 10:05 kqhxnm-zzz

This is an important observation about logprobs consistency.

Root cause analysis: The micro batch size difference causes non-deterministic tensor ordering in the gradient accumulation. When differs from , the and may be computed on different data orderings even in an on-policy setting.

Your workaround () is the correct immediate fix.

For a proper fix, the issue would need to be addressed in the data pipeline. The root cause is that the log_prob computation batch ordering doesn't match the training batch ordering when micro batch sizes differ.

Note: This is related to the "Keep Sampling Mask" issue from DeepSeek-V3.2 (issue #6240). Using for both rollout and training helps maintain consistency, but the micro batch size issue is independent.

Would you like to open a bug report or PR for this? The fix would likely involve ensuring the data ordering is deterministic across the rollout → log_prob → training pipeline.

Kirrito-k423 avatar May 14 '26 18:05 Kirrito-k423

I had similar observations when experimenting with Qwen3.5 / Qwen3-VL models.

  • It happened to me only on MoE model Qwen3.5-35B-A3B. No such mismatch on Qwen3-VL-4B/8B dense models. Haven't tested on Qwen3.5 dense models yet.
  • I took similar workaround aforementioned, i.e., setting the two micro-batch-sizes the same, then the log_prob_old and log_prob gap gone on strict on-policy setup.

My env: Image and code from commit 2eab9364. Image on arm64 (GB200). Image modified based on original vllm dockerfile, changed transformers to 5.7.0 and vllm to 0.19.1.

jianwenzhzm avatar Jun 21 '26 02:06 jianwenzhzm

Hi @kqhxnm-zzz, I traced this through current main and the historical fix in #3119.

My understanding is that #3119 did not make the two log-prob computations micro-batch invariant. In the strict on-policy case, it bypassed the discrepancy by replacing the stored old_log_probs with the actor forward's log_prob.detach(). That shortcut disappeared when the legacy dp_actor.py path was removed in #6067. Current main recomputes and stores old_log_probs before update_actor, then compares them with a second actor forward in ppo_loss.

The reports here indicate that the mismatch is reproducible, but I do not think the current evidence establishes data ordering as the root cause yet. The first divergence may come from ordering or masks, padding conversion, fused kernels, numerical batch dependence, or a MoE-specific path.

I would like to take a diagnostic-first pass:

  1. keep weights, inputs, token order, and seeds fixed, varying only the two micro-batch sizes;
  2. compare per-token log-prob errors while checking UID/order, masks, and positions before both forwards;
  3. isolate remove-padding, fused-kernel, and dense-vs-MoE paths;
  4. add a focused regression test and fix the first boundary where parity is lost.

I would avoid restoring the #3119 shortcut as a general fix because it masks the discrepancy and does not cover off-policy or asynchronous training.

@vermouth1992, does this scope look reasonable, and is anyone already working on it? If not, I would be happy to take it.

kaining-never-stop avatar Jul 31 '26 06:07 kaining-never-stop

Hi @kqhxnm-zzz, @jianwenzhzm, @Kirrito-k423, and @vermouth1992, I completed the controlled checks I described above on the main snapshot at aebd1f8a27d5606226f2b85682cacaf2fdf7eaa7.

The real-model run used Qwen3-30B-A3B (48 MoE layers, 128 experts, top-8), 8x H20, PyTorch 2.8.0+cu128, Transformers 4.57.1, FSDP2 BF16, static batching, use_remove_padding=true, and fused kernels disabled. Inputs, token order, masks, positions, weights, and seeds were fixed.

  • Forward-only control: MBS=1 versus MBS=2 was exactly equal over 4,096 response tokens.
  • Training-boundary control: no-grad eval and grad-enabled train forward, with MBS=1/2 and same-shape repeats, were exactly equal in all nine comparisons.
  • Every comparison had max_abs_diff=0, ratio [1, 1], and PPO clip fraction 0.
  • Ordered and set-wise router top-k mismatches were both 0 over 48 layers and 786,432 layer-token events per comparison.
  • No backward or optimizer step was executed, and sampled parameters were unchanged.

I therefore cannot justify a production change from this configuration alone. Could you share the exact veRL commit, model, backend, dtype, relevant actor/rollout config, remove-padding/dynamic-batch/fused-kernel settings, sequence lengths, and Transformers/vLLM versions from a reproducing run? A minimal config or a small per-token log-prob dump would help identify which condition should be isolated next.

kaining-never-stop avatar Jul 31 '26 12:07 kaining-never-stop

@kqhxnm-zzz you asked whether this is caused by kernels that are not batch-invariant. I could not run verl's production path, but I could isolate the question on a single GPU, and the answer there is yes, with a specific shape.

What was measured. Same checkpoint, no optimizer step, the same token ids scored twice by the same trainer forward, differing only in batch shape. exp(logp_b8 - logp_b1) should be 1 for every token. Percent of tokens for which it leaves [0.9, 1.1]:

model bf16 b1 vs b8 fp16 b1 vs b8 fp32 b1 vs b8 bf16 b1 rerun bf16 b1 vs b32
pythia-160m (NeoX 0.16B) 60.03% 22.02% 0.00% 0.00% 59.70%
pythia-410m (NeoX 0.41B) 36.70% 2.49% 0.00% 0.00% 36.65%
Qwen2.5-0.5B 9.87% 0.07% 0.00% 0.00% 10.75%
Granite-3.1-1B-A400M (MoE) 13.69% 0.97% 0.00% 0.00% 13.85%
Agents-A1-4B 1.78% 0.00% 0.00% 0.00% 1.64%
Qwen2.5-1.5B 9.37% 0.00% 0.00% 0.00% 8.87%
Qwen2.5-3B 6.87% 0.00% 0.00% 0.00% 7.17%
Qwen2.5-7B 5.60% 0.00% 0.00% 0.00% 5.71%

Repeating the same batch is bit-exact in every row, so this is a deterministic function of the batch shape, not run-to-run noise. fp32 is 0.00% on every row.

What it is and is not.

model A padded ragged b1v8 B equal-length no pad b1v8 C same seq x8 vs alone D two groupings of batch 8
pythia-410m (NeoX 0.41B) 36.87% 36.23% 18.75% 0.00%
Qwen2.5-0.5B 9.74% 10.06% 6.25% 0.00%
Granite-3.1-1B-A400M (MoE) 14.09% 14.21% 20.31% 12.60%
Agents-A1-4B 1.78% 3.25% 6.25% 0.00%
  • Not padding: B (equal length, no padding) matches A.
  • Batch size alone is sufficient: C scores eight copies of one sequence together against that sequence alone.
  • Membership only matters for the MoE (D): 0.00% on every dense model, 12.60% on Granite MoE. That is consistent with @jianwenzhzm seeing it on Qwen3.5-35B-A3B and not on dense Qwen3-VL: expert routing is a second, batch-composition-dependent channel that dense models lack.
  • It survives eager, sdpa and flex_attention (three seeds each, Wilson CIs in the repo), and it is bf16 specifically: fp16 cuts it by an order of magnitude with the same 16 bits.

Does it reach the objective, or does clipping absorb it? With group-normalised advantages and eps=0.2, the fraction of clipped tokens barely moves, but which tokens are clipped does:

model clipped at b1 clipped at b8 clip status flips n
pythia-410m (NeoX 0.41B) 19.97% 20.07% 10.11% 4096
Qwen2.5-0.5B 21.41% 21.48% 1.68% 4096
Granite-3.1-1B-A400M (MoE) 3.15% 2.86% 2.78% 4096

So it is not absorbed; it silently reassigns which tokens carry gradient.

Why @kaining-never-stop measured exactly zero, as far as I can tell. That run was FSDP2 + static batching + use_remove_padding (flash-attn varlen) with MBS 1 vs 2 on H20. Two of those are things this harness cannot reproduce: flash_attn will not build here (no nvcc), and every number above is SM89. I did check the nearest thing I could - sequence packing with an explicit block-diagonal mask - and it does not remove the effect (32.2% vs 31.8% on pythia-410m), and switching only the mask representation moves 21.4% of tokens by itself. So the varlen kernel is the open variable, and the single most useful next measurement is the one I cannot make: the same [1,L] vs [8,L] comparison under use_remove_padding on the hardware that reported zero. If it is zero there, remove_padding is the fix and the docs should say so; if it is not, MBS 1 vs 2 was simply too small a change to expose it.

Boundaries, stated plainly. Single GPU generation (RTX 4090); largest model Qwen2.5-7B; trainer is plain HF forward, not verl's actor; advantages are synthetic, and the clip-flip rate depends on their distribution (2.5% to 8.6% on pythia-410m across three distributions).

Scripts, JSON and every table: https://github.com/GuoCheng24/batch-logprob-gap. Each script takes a model id and runs on one 24GB card; pythia-410m shows the largest effect and runs in minutes.

GuoCheng24 avatar Sep 17 '26 06:09 GuoCheng24

Follow-up on the measurement I posted above, with the rest of the harness now public (https://github.com/GuoCheng24/batch-logprob-gap, every table regenerated from the committed JSON).

Where the bf16 batch-shape noise enters is architecture-dependent. Tracking the batch-1 vs batch-8 divergence of the hidden states layer by layer: GPT-NeoX (pythia-410m) builds almost all of it in the last quarter of the stack, so an fp32 lm_head leaves the out-of-band rate at 32.2% (from 34.3%); Qwen2/Qwen3 inject it in the first five layers and carry it, so an fp32 head removes two thirds to four fifths there (Qwen2.5-1.5B: 8.04% → 1.51%). Upcasting the eight layers with the largest divergence increment beats a fixed "last 8" at equal parameter budget (pythia 0.41% vs 1.12%).

But the cheap fix is fp16, not selective fp32. Scoring the same tokens in fp16, with no fp32 anywhere and at 0.93–0.99× the bf16 time, gives 0.00% out-of-band on Qwen2.5-1.5B, Qwen3-1.7B and DeepSeek-R1-Distill-Qwen-1.5B, 0.02% on Qwen2.5-0.5B, 0.47% on pythia-410m; on Qwen2.5-3B and 7B fp16 produced no non-finite log-prob over 8,192 tokens (max |hidden| 12,712 against 65,504). That is the same prescription as sail-sg's arXiv:2510.26788 for the whole training loop, applied to the scoring pass only.

Does it reach the reward? Six ways of computing old_log_prob in TRL's GRPO (default bf16 chunking; an fp32 clone; bf16 with chunk = micro-batch; bf16 + divergence-guided fp32 layers + head; bf16 + fp32 head; an fp16 clone), Qwen2.5-1.5B-Instruct on GSM8K, 150 steps, two seeds, same prompt order and vLLM sampling per seed: the last-30-step reward stays within −0.013 to +0.008 of the default on every seed, the clip fraction is 0.0000–0.0005 throughout (one optimisation step per generation keeps the ratio inside the clip band), and the 150-step mean within +0.001 to +0.011. So at 1.5B with default hyper-parameters the noise is real and in the ratio, but removing it — fully or partly — does not move the reward. The bound is about a point with no consistent sign.

Two limits stand: this harness still cannot build flash-attn varlen, so the use_remove_padding path that reported max_abs_diff = 0 remains untested here; and 150 steps at 1.5B says nothing about long-horizon collapse, which arXiv:2602.01826 attributes to optimisation dynamics rather than precision alone.

GuoCheng24 avatar Sep 18 '26 03:09 GuoCheng24