Repository navigation
refactor(loss): unify normalization and streaming reduction - #4946
Conversation
|
/ok to test 5624775 |
5624775 to
c47eeb2
Compare
mehraakash
left a comment
There was a problem hiding this comment.
Overall: this is a parity-preserving refactor and nothing here blocks merge. #4947 is stacked on it.
Three asks for the PR description rather than the code:
- Say that the router-auxiliary guard is a behaviour change: a config that previously ran now fails hard at setup.
- Say that the shipped recipes keep using the
token_level_lossalias on purpose, so readers do not open 24 migration PRs. - Say that the synchronous (non-streaming) trainers do not carry the third count, so
sequence_meanparity there rests on the single-chunk path and is not tested.
Inline comments: 5 fixes (one real bug in the warning category, the rest tests and design), 6 nits.
u-brixton
left a comment
There was a problem hiding this comment.
Thank you for the review!
Addressed review comments and rebased on latest main.
Logged loss and KL now also use the same divisor as gradient, GPU parity tests cover both global_token_mean and sequence_mean with 2 chunks per step. Also added a startup check that rejects resuming with a different normalization.
c47eeb2 to
668d521
Compare
Apply the reviewer's suggested code from the NVIDIA-NeMo#4946 review: - emit FutureWarning (shown by default) instead of DeprecationWarning for the token_level_loss alias, and match it in the config test; - forbid any warning when the alias is unset in that test; - StreamingObjectiveLoss: docstring, __getattr__ guard for the loss_fn probe during deepcopy/unpickle, and positional-argument forwarding; - document the ObjectiveReduction ClassVars and the global_objective_denominator argument; - pin the router auxiliary-loss guard to the Megatron-LM source it mirrors; - CPU tests for denominator forwarding through StreamingObjectiveLoss, get_objective_reduction precedence and local_counts without token_mask. Signed-off-by: Renat Zayashnikov <[email protected]> Co-authored-by: Akash Mehra <[email protected]>
|
/ok to test 668d521 |
668d521 to
f969bbb
Compare
|
/ok to test f969bbb |
f969bbb to
2349a9b
Compare
|
/ok to test 2349a9b |
Apply the reviewer's suggested code from the #4946 review: - emit FutureWarning (shown by default) instead of DeprecationWarning for the token_level_loss alias, and match it in the config test; - forbid any warning when the alias is unset in that test; - StreamingObjectiveLoss: docstring, __getattr__ guard for the loss_fn probe during deepcopy/unpickle, and positional-argument forwarding; - document the ObjectiveReduction ClassVars and the global_objective_denominator argument; - pin the router auxiliary-loss guard to the Megatron-LM source it mirrors; - CPU tests for denominator forwarding through StreamingObjectiveLoss, get_objective_reduction precedence and local_counts without token_mask. Signed-off-by: Renat Zayashnikov <[email protected]> Co-authored-by: Akash Mehra <[email protected]>
|
/ok to test 2349a9b |
2349a9b to
88af029
Compare
|
/ok to test 88af029 |
Apply the reviewer's suggested code from the #4946 review: - emit FutureWarning (shown by default) instead of DeprecationWarning for the token_level_loss alias, and match it in the config test; - forbid any warning when the alias is unset in that test; - StreamingObjectiveLoss: docstring, __getattr__ guard for the loss_fn probe during deepcopy/unpickle, and positional-argument forwarding; - document the ObjectiveReduction ClassVars and the global_objective_denominator argument; - pin the router auxiliary-loss guard to the Megatron-LM source it mirrors; - CPU tests for denominator forwarding through StreamingObjectiveLoss, get_objective_reduction precedence and local_counts without token_mask. Signed-off-by: Renat Zayashnikov <[email protected]> Co-authored-by: Akash Mehra <[email protected]>
Apply the reviewer's suggested code from the NVIDIA-NeMo#4946 review: - emit FutureWarning (shown by default) instead of DeprecationWarning for the token_level_loss alias, and match it in the config test; - forbid any warning when the alias is unset in that test; - StreamingObjectiveLoss: docstring, __getattr__ guard for the loss_fn probe during deepcopy/unpickle, and positional-argument forwarding; - document the ObjectiveReduction ClassVars and the global_objective_denominator argument; - pin the router auxiliary-loss guard to the Megatron-LM source it mirrors; - CPU tests for denominator forwarding through StreamingObjectiveLoss, get_objective_reduction precedence and local_counts without token_mask. Signed-off-by: Renat Zayashnikov <[email protected]> Co-authored-by: Akash Mehra <[email protected]>
88af029 to
8b6820f
Compare
|
/ok to test 8b6820f |
Apply the reviewer's suggested code from the #4946 review: - emit FutureWarning (shown by default) instead of DeprecationWarning for the token_level_loss alias, and match it in the config test; - forbid any warning when the alias is unset in that test; - StreamingObjectiveLoss: docstring, __getattr__ guard for the loss_fn probe during deepcopy/unpickle, and positional-argument forwarding; - document the ObjectiveReduction ClassVars and the global_objective_denominator argument; - pin the router auxiliary-loss guard to the Megatron-LM source it mirrors; - CPU tests for denominator forwarding through StreamingObjectiveLoss, get_objective_reduction precedence and local_counts without token_mask. Signed-off-by: Renat Zayashnikov <[email protected]> Co-authored-by: Akash Mehra <[email protected]>
|
/ok to test 9e0c176 |
9e0c176 to
e1bd8f9
Compare
|
/ok to test e1bd8f9 |
e1bd8f9 to
6972df5
Compare
|
/ok to test 6972df5 |
Signed-off-by: Renat Zayashnikov <[email protected]>
Apply the reviewer's suggested code from the NVIDIA-NeMo#4946 review: - emit FutureWarning (shown by default) instead of DeprecationWarning for the token_level_loss alias, and match it in the config test; - forbid any warning when the alias is unset in that test; - StreamingObjectiveLoss: docstring, __getattr__ guard for the loss_fn probe during deepcopy/unpickle, and positional-argument forwarding; - document the ObjectiveReduction ClassVars and the global_objective_denominator argument; - pin the router auxiliary-loss guard to the Megatron-LM source it mirrors; - CPU tests for denominator forwarding through StreamingObjectiveLoss, get_objective_reduction precedence and local_counts without token_mask. Signed-off-by: Renat Zayashnikov <[email protected]> Co-authored-by: Akash Mehra <[email protected]>
…reduction The OPD-full helper and the score-centering correction picked their denominator from loss_type, bypassing the configured reduction. Both now call objective_reduction.reduce with the same divisor and per-response weights as the actor objective, and _opd_full_call receives the streaming global_objective_denominator. The explicit-denominator metric pop now runs after the opd_full metric table is built so it applies to both branches. global_token_mean and sequence_mean results are unchanged. Signed-off-by: Renat Zayashnikov <[email protected]>
Build a two-group batch with unequal response lengths and check that ClippedPGLossFn's value and gradients equal a hand-written oracle for each normalization with no optional term, the reference KL penalty, score centering and opd_full (with and without KL). The same cases pin the global_token_mean and sequence_mean results to the pre-refactor loss_type-branched formula. New loss terms should be added to this file. Signed-off-by: Renat Zayashnikov <[email protected]>
Make global_valid_seqs/global_valid_toks keyword-only in StreamingObjectiveLoss.__call__, so a positional caller gets a TypeError instead of a KeyError. Every caller already passes them by keyword. Pin the __getattr__ guard: copy.deepcopy and pickle probe attributes before __init__ runs, which used to recurse through the loss_fn lookup. Also run the streaming-vs-full equivalence test with an explicit-denominator reduction, so it fails if the wrapper does not forward the denominator. Signed-off-by: Renat Zayashnikov <[email protected]>
…steps Parametrize test_split_train_matches_sync_train over global_token_mean and sequence_mean, and dispatch each split step as two half-batch train_microbatch calls so cross-chunk accumulation of the objective and its denominator is exercised. GPU test; not run locally. Signed-off-by: Renat Zayashnikov <[email protected]>
Cover every normalization/token_level_loss pair in the conflict check, including agreeing pairs (the strict one-selector rule is kept during the deprecation window), and assert the error tells users to set token_level_loss=null. Signed-off-by: Renat Zayashnikov <[email protected]>
Describe the arguments of the shared reduction entry point and state that every gradient-normalized loss term must go through it. Signed-off-by: Renat Zayashnikov <[email protected]>
validate_forest_objective rejected configs with 'not loss.token_level_loss', which is now true for the default (token_level_loss unset), so every Forest config failed at startup after rebasing on main. Compare the resolved normalization instead and name the selector the user actually set. Signed-off-by: Renat Zayashnikov <[email protected]>
Express the GSPO golden case with normalization=sequence_mean instead of the deprecated token_level_loss=False. The pinned golden source predates the normalization field, so the generator maps it back to the legacy alias there; loss, gradients and metrics are unchanged and the committed golden JSON is not regenerated. Signed-off-by: Renat Zayashnikov <[email protected]>
Add MetricNormalizer.LOSS_DIVISOR and advertise it for ClippedPGLossFn's loss and kl_penalty entries (main and opd_full tables), so the logged objective is divided by exactly the divisor the gradient used instead of a separately maintained label. This removes the metric_normalizer ClassVar from the objective reductions and the requires_explicit_denominator pop of the loss/KL entries. The Megatron split-step finish resolves LOSS_DIVISOR through the step's objective reduction (metric_normalizer_scales); survivor rescaling applies the gradient's factor. Logged values are unchanged for global_token_mean and sequence_mean. Signed-off-by: Renat Zayashnikov <[email protected]>
Rows in a restored data-plane snapshot were prepared for the loss normalization they were written under, so resuming the same checkpoint_dir must keep it. Record the resolved loss_fn normalization in the data-plane checkpoint metadata and reject a restore whose current normalization differs. Initializing from a checkpoint's weights in a new checkpoint_dir restores no rows and is unaffected. Checkpoints written before the field existed are accepted only for the two original modes, global_token_mean and sequence_mean. Signed-off-by: Renat Zayashnikov <[email protected]>
…guide Signed-off-by: Renat Zayashnikov <[email protected]>
Signed-off-by: Renat Zayashnikov <[email protected]>
Signed-off-by: Renat Zayashnikov <[email protected]>
6972df5 to
d01c03a
Compare
|
/ok to test d01c03a |
What does this PR do?
Give global-token and response-mean policy losses one explicit normalization selector and named reducers, so additional averaging modes can reuse the streaming worker.
loss_fn.normalizationselectsglobal_token_meanorsequence_mean. The legacytoken_level_lossflag still works when the new selector is unset but is deprecated (FutureWarning); supplying both non-null selectors is an error. Shared base configs leave both selectors unset; recipes that set the legacy flag explicitly keep working and will be migrated in a follow-up PR.Each reducer owns the reduction of every loss term (actor, KL, score centering, OPD-full) and its denominator counting; its
loss_divisor()is the single denominator for the gradient and for the logged loss and KL. Streaming workers accumulate counts acrosstrain_microbatchchunks and DP ranks and normalize gradients once at update completion, while token diagnostics and detached MTP retain their token denominator. Synchronoustrain()paths keep their token or sequence counts.On the SingleController Megatron streaming path, active router auxiliary and z-losses are rejected under sequence averaging because their mixed gradients cannot receive a separate token denominator. Such runs previously trained with a mismatched denominator; they now fail at split-step setup. Draft-model and detached-MTP corrections under
sequence_meanare covered by mock tests only.Resuming in the same
checkpoint_dirunder a different normalization fails at startup, because buffered rollouts were prepared for the original one. Starting a new run from a checkpoint's weights is unaffected.Group-token averaging is supplied by the dependent PR #4947.
The reducer interface follows Teodor Poncu's MFN loss-averaging design, adapted to NeMo-RL's training interfaces.
Usage
Validation
Validation covers the combined #4946 + #4947 stack:
global_token_meanandsequence_mean, with each step split into twotrain_microbatchchunks: loss, gradient norms and metrics matched over two optimizer steps. Router auxiliary-loss guard tests passed.group_token_meantraining: four optimizer steps with Qwen3-0.6B, Megatron DP2, and sequence packing. Every step had nonzero finite gradients and mixed rewards; two steps accumulated across multiple streaming chunks.main.FAST=0, upstream skips retained, and assertions and thresholds unchanged.Full-matrix outcome: 46 of 52 lanes pass. The six remaining failures reproduce identically on the upstream base in the same environment:
The GRPO NIXL lane needed CUDA transports enabled in UCX. The SGLang fault-tolerance chaos test is intermittent on both base and PR (Ray GCS loss after the injected kill) and passed on rerun.
The upstream actor-support and reference-KL mask separation is preserved.
Before your PR is "Ready for review"