Repository navigation
Conversation
|
Measured this in a training run rather than only in the unit test. The inflation factor comes out at exactly the number of microbatches, which is the quantity the bug predicts. MeasurementGRPO, Qwen3-0.6B, gsm8k,
That is The loss figures corroborate it independently: What this means for the configured weightThe practical consequence is that Merge orderingThis PR and #3792 should land together, or this one first. On current main the NLL term never executes, so this bug is latent. #3792 activates the term, and without this fix it comes back at 32x its configured weight. A wrong contribution to the gradient is harder to detect than no contribution, so activating the term without this fix would be a regression relative to today's silent no-op. One limitation, stated explicitlyI could not demonstrate the microbatch dependence end to end. The natural experiment is to hold The unit test in this PR covers the invariance claim precisely, by comparing a single call against the same computation split into two microbatches and summed, which is the pattern the trainers use. |
…n count ClippedPGLossFn normalized the positive-example NLL term by correct_mask.sum(), the microbatch's own count of correct-sample tokens, while every other term in the same loss is normalized by global_valid_toks. Trainers call the loss once per microbatch, sum the results and rescale by the DP/CP world size, so a local denominator makes each microbatch contribute a full mean instead of its share of one. The effective weight of the term became mu times the number of microbatches times dp_size times cp_size, so changing train_micro_batch_size or the world size silently changed the objective. Normalize by global_valid_toks instead, which makes the term invariant to the microbatch split and to the world size. mu is now defined relative to all valid tokens rather than to correct-sample tokens alone, which is a constant rescaling of the weight; the docs and the config comment say so. positive_nll_loss also moves from MetricNormalizer.NONE to TOKENS, since it is now normalized the same way as the loss term it reports. Signed-off-by: khazic <[email protected]>
d4ff4e7 to
b6bc930
Compare
Problem
ClippedPGLossFnimplements VAPO's positive-example NLL term (arXiv:2504.05118, Eq. 10). Every other term in the same loss function is normalized byglobal_valid_toks, a count all-reduced across the DP/CP world before the loss call, so that summing the per-microbatch losses on each rank reproduces the correct global mean. The NLL term instead normalized bycorrect_mask.sum(), the current microbatch's own count of correct-sample tokens:Trainers call the loss function once per microbatch and sum the results (
dtensor_policy_worker.pyalso multiplies bydp_size * cp_sizeto undo FSDP's gradient averaging). A term normalized by a local count contributes a full local mean on every microbatch instead of its share of one global mean, so:train_micro_batch_sizeis a hardware knob);dp_size * cp_size, since that factor is applied uniformly to the whole loss including this now-inflated term.Changing
train_micro_batch_size,dp_size, orcp_sizetherefore silently changes whatmuinL = L_PPO + mu * L_NLL(correct)actually means, with no error or warning.Fix
Normalize by
global_valid_toks, the same denominatoractor_lossandkluse.muis now defined relative to all valid tokens in the batch rather than to correct-sample tokens alone; that is a constant rescaling of the weight (correct-token count / total valid-token count, roughly the training accuracy), not a change to what the term computes, and it is now invariant to how the batch is split into microbatches or across DP/CP.positive_nll_lossmoves fromMetricNormalizer.NONEtoTOKENSinmetric_normalizations, since it is now normalized the same way as the loss term it reports, and the metric-normalization table indocs/guides/grpo.mdand the config comment inClippedPGLossConfigare updated to match.Evidence
tests/unit/algorithms/test_loss_functions.py::test_clipped_pg_positive_nll_is_microbatch_invariantbuilds one batch of 4 samples (3 correct, 1 incorrect) and compares the loss and the input gradient computed in a single call against the same computation split into two microbatches of 2, summed. This is exactly the microbatch patterndtensor_policy_worker.pyandmegatron_policy_worker.pyuse.On the current
main, withpositive_example_nll_weightdisabled the two computations agree, and with it enabled they diverge:mu=0.0passing on unpatched main andmu=0.5failing isolates the bug to the VAPO term specifically: everything else in the loss is already microbatch-invariant.With the fix applied, both parametrizations pass:
The full
test_loss_functions.pysuite (79 tests, covering GRPO/PPO clipping, GSPO, CISPO, dual-clip, TIS variants, DPO, distillation, and the metric-normalization advertisement) passes on the fix branch, so the change does not regress any other loss path:Scope
This only touches the VAPO NLL term's normalization and its metric classification. The rest of
ClippedPGLossFn, the PPO/GRPO/GSPO/CISPO ratio computation, KL penalty, and truncated importance sampling, is unchanged.