Repository navigation
feat(loss): add equal-group token normalization - #4947
Conversation
249b2c1 to
5b821f4
Compare
mehraakash
left a comment
There was a problem hiding this comment.
Review of the incremental layer over #4946 (this PR is stacked on it; diff reviewed = pr-4946-head..7d470da6). No correctness issues found in the group-token-mean path. The comments below are test-coverage and documentation asks, plus a request for before/after curves against global_token_mean.
u-brixton
left a comment
There was a problem hiding this comment.
Thank you for the review!
Iterated on review comments, attached curves for the small comparison run, GPU parity test now also covers prompt groups with uneven response lengths
Apply the reviewer's suggested code from the NVIDIA-NeMo#4947 review: - full docstring for group_token_mean_weights; - explain why GroupTokenMeanReduction inherits metric_normalizer=TOKENS; - Literal types for the validate_setup trainer/algorithm parameters; - state in ppo_math_1B.yaml that group_token_mean is GRPO-only; - note why validate_loss_options also runs at config time; - assert in the advantage-stage test that mask_sample reached the row; - CPU tests for the explicit-denominator requirement, malformed row weights, the loss/KL metric pop, group_token_mean_weights input checks, validate_setup rejections, and reducer column registration in the controller train fields and the partition warmup. Signed-off-by: Renat Zayashnikov <[email protected]> Co-authored-by: Akash Mehra <[email protected]>
7d470da to
435d24a
Compare
|
/ok to test 435d24a |
Apply the reviewer's suggested code from the NVIDIA-NeMo#4947 review: - full docstring for group_token_mean_weights; - explain why GroupTokenMeanReduction inherits metric_normalizer=TOKENS; - Literal types for the validate_setup trainer/algorithm parameters; - state in ppo_math_1B.yaml that group_token_mean is GRPO-only; - note why validate_loss_options also runs at config time; - assert in the advantage-stage test that mask_sample reached the row; - CPU tests for the explicit-denominator requirement, malformed row weights, the loss/KL metric pop, group_token_mean_weights input checks, validate_setup rejections, and reducer column registration in the controller train fields and the partition warmup. Signed-off-by: Renat Zayashnikov <[email protected]> Co-authored-by: Akash Mehra <[email protected]>
435d24a to
196395b
Compare
|
/ok to test 196395b |
Apply the reviewer's suggested code from the NVIDIA-NeMo#4947 review: - full docstring for group_token_mean_weights; - explain why GroupTokenMeanReduction inherits metric_normalizer=TOKENS; - Literal types for the validate_setup trainer/algorithm parameters; - state in ppo_math_1B.yaml that group_token_mean is GRPO-only; - note why validate_loss_options also runs at config time; - assert in the advantage-stage test that mask_sample reached the row; - CPU tests for the explicit-denominator requirement, malformed row weights, the loss/KL metric pop, group_token_mean_weights input checks, validate_setup rejections, and reducer column registration in the controller train fields and the partition warmup. Signed-off-by: Renat Zayashnikov <[email protected]> Co-authored-by: Akash Mehra <[email protected]>
196395b to
4849a5a
Compare
|
/ok to test 4849a5a |
1 similar comment
|
/ok to test 4849a5a |
Apply the reviewer's suggested code from the NVIDIA-NeMo#4947 review: - full docstring for group_token_mean_weights; - explain why GroupTokenMeanReduction inherits metric_normalizer=TOKENS; - Literal types for the validate_setup trainer/algorithm parameters; - state in ppo_math_1B.yaml that group_token_mean is GRPO-only; - note why validate_loss_options also runs at config time; - assert in the advantage-stage test that mask_sample reached the row; - CPU tests for the explicit-denominator requirement, malformed row weights, the loss/KL metric pop, group_token_mean_weights input checks, validate_setup rejections, and reducer column registration in the controller train fields and the partition warmup. Signed-off-by: Renat Zayashnikov <[email protected]> Co-authored-by: Akash Mehra <[email protected]>
4849a5a to
dee9aaf
Compare
|
/ok to test dee9aaf |
|
/ok to test 1c4f8ba |
Apply the reviewer's suggested code from the NVIDIA-NeMo#4947 review: - full docstring for group_token_mean_weights; - explain why GroupTokenMeanReduction inherits metric_normalizer=TOKENS; - Literal types for the validate_setup trainer/algorithm parameters; - state in ppo_math_1B.yaml that group_token_mean is GRPO-only; - note why validate_loss_options also runs at config time; - assert in the advantage-stage test that mask_sample reached the row; - CPU tests for the explicit-denominator requirement, malformed row weights, the loss/KL metric pop, group_token_mean_weights input checks, validate_setup rejections, and reducer column registration in the controller train fields and the partition warmup. Signed-off-by: Renat Zayashnikov <[email protected]> Co-authored-by: Akash Mehra <[email protected]>
dee9aaf to
1c4f8ba
Compare
Apply the reviewer's suggested code from the #4947 review: - full docstring for group_token_mean_weights; - explain why GroupTokenMeanReduction inherits metric_normalizer=TOKENS; - Literal types for the validate_setup trainer/algorithm parameters; - state in ppo_math_1B.yaml that group_token_mean is GRPO-only; - note why validate_loss_options also runs at config time; - assert in the advantage-stage test that mask_sample reached the row; - CPU tests for the explicit-denominator requirement, malformed row weights, the loss/KL metric pop, group_token_mean_weights input checks, validate_setup rejections, and reducer column registration in the controller train fields and the partition warmup. Signed-off-by: Renat Zayashnikov <[email protected]> Co-authored-by: Akash Mehra <[email protected]>
92b0ebf to
0acc8ab
Compare
|
/ok to test 0acc8ab |
Apply the reviewer's suggested code from the #4947 review: - full docstring for group_token_mean_weights; - explain why GroupTokenMeanReduction inherits metric_normalizer=TOKENS; - Literal types for the validate_setup trainer/algorithm parameters; - state in ppo_math_1B.yaml that group_token_mean is GRPO-only; - note why validate_loss_options also runs at config time; - assert in the advantage-stage test that mask_sample reached the row; - CPU tests for the explicit-denominator requirement, malformed row weights, the loss/KL metric pop, group_token_mean_weights input checks, validate_setup rejections, and reducer column registration in the controller train fields and the partition warmup. Signed-off-by: Renat Zayashnikov <[email protected]> Co-authored-by: Akash Mehra <[email protected]>
0acc8ab to
61f6ec7
Compare
|
/ok to test 61f6ec7 |
mehraakash
left a comment
There was a problem hiding this comment.
Re-checked all earlier threads at 61f6ec7: every fix is in, CI is green. Approving. Please rebase for the conflict in tests/unit/single_controller/test_resiliency_config.py before merging.
Signed-off-by: Renat Zayashnikov <[email protected]>
Signed-off-by: Renat Zayashnikov <[email protected]>
Apply the reviewer's suggested code from the NVIDIA-NeMo#4947 review: - full docstring for group_token_mean_weights; - explain why GroupTokenMeanReduction inherits metric_normalizer=TOKENS; - Literal types for the validate_setup trainer/algorithm parameters; - state in ppo_math_1B.yaml that group_token_mean is GRPO-only; - note why validate_loss_options also runs at config time; - assert in the advantage-stage test that mask_sample reached the row; - CPU tests for the explicit-denominator requirement, malformed row weights, the loss/KL metric pop, group_token_mean_weights input checks, validate_setup rejections, and reducer column registration in the controller train fields and the partition warmup. Signed-off-by: Renat Zayashnikov <[email protected]> Co-authored-by: Akash Mehra <[email protected]>
The helper's only production caller is GroupTokenMeanReduction.prepare_batch, and it depends on nothing in loss/utils.py. Defining it next to the reduction removes the call-time import that existed only to break the utils -> interfaces import cycle. Signed-off-by: Renat Zayashnikov <[email protected]>
…tion AdvantageStageConfig gained a required objective_reduction field with group_token_mean; the logical-advantage tests added on main after this branch was first written construct the config directly and need it. Signed-off-by: Renat Zayashnikov <[email protected]>
Add docstrings to validate_setup, validate_loss_options and prepare_batch; the validate_setup wording follows the reviewer's suggestion. Signed-off-by: Renat Zayashnikov <[email protected]>
Extend the reduction-consistency test with group_token_mean: every term, including the score-centering correction, must equal the sum of per-token values weighted by 1/T_g of their prompt group divided by the number of groups with positive token mass. The denominator comes from the reduction's own local_counts, as the split worker would all-reduce it. Also check that OPD-full is rejected under group_token_mean. Signed-off-by: Renat Zayashnikov <[email protected]>
Compare a streaming group_token_mean run (DP2, two train_microbatch chunks per step) with a sync train() oracle: global_token_mean on a token_mask scaled per row by its 1/T_g group weight. The global token mass is then the group count and the objective is exactly the group token mean; with equal group masses it is plain global_token_mean. Parametrize over response lengths. "equal" uses fixed-length rows without packing. "ragged" uses unequal group masses, a sample_mask=0 row and sequence packing on the split run, and checks against CPU-recomputed step-0 losses that plain global_token_mean is far outside the parity tolerance. Groups interleave rows so each spans both chunks. A CPU test checks the oracle: on group-scaled masks global_token_mean matches group_token_mean in loss and gradient. Signed-off-by: Renat Zayashnikov <[email protected]>
Keep validate_loss_options in both places: ClippedPGLossFn.__init__ covers every construction path, and the SingleController config validation fails before the Ray cluster starts. Say so at the loss-side call too. Signed-off-by: Renat Zayashnikov <[email protected]>
GroupTokenMeanReduction overrode every GlobalTokenMeanReduction method and only inherited its ClassVars. Derive it from ObjectiveReduction, write the ClassVars out (TOKEN_LEVEL legacy loss_type, not token normalized, explicit denominator, row-weight extra field) and inline the weighted masked_mean. With loss/KL advertised as LOSS_DIVISOR, the group's logged loss and KL are divided by the global group count, so the comment about the inherited metric normalizer is obsolete. The table test now checks the LOSS_DIVISOR entries and that rescaling streamed sums by them matches the synchronous metrics. Signed-off-by: Renat Zayashnikov <[email protected]>
Signed-off-by: Renat Zayashnikov <[email protected]>
Signed-off-by: Renat Zayashnikov <[email protected]>
61f6ec7 to
818d7bf
Compare
|
/ok to test 818d7bf |
What does this PR do?
Add equal prompt-group token averaging to SingleController Megatron GRPO: take the token mean within each prompt group, then give each nonempty group equal weight.
This PR builds on #4946, now merged into
main. The Files changed tab shows the group-normalization layer.global_token_meansequence_meangroup_token_meanThe group reducer prepares one FP32 inverse group-token weight per response after masks are finalized, while groups are complete. Setup and training use its declared data-plane field; packing preserves the weights. The shared streaming worker recovers the group denominator across chunks and DP ranks without group-specific worker code.
Valid zero-advantage tokens remain in the denominator; importance-weight rejection does not renormalize it. Logged loss and KL divide by the group count, the same divisor as the gradient; token diagnostics and detached MTP retain their token denominator. Group averaging is SingleController GRPO only and rejects unsupported trainers and backends (including PPO), OPD, sequence-level importance ratios, positive-example NLL, in-loss sequence filtering, and active attached MTP/router auxiliary losses whose gradients cannot use the group denominator.
Group-weight preparation follows Afonso Marques's approach in MFN of calculating weights before packing, adapted to NeMo-RL's explicit masks and streaming updates.
Usage
On a SingleController GRPO recipe with Megatron:
Validation
Validation covers the combined #4946 + #4947 stack:
group_token_mean(DP2, each step split into twotrain_microbatchchunks) against an exact synchronous oracle:global_token_meanon masks scaled by each group's inverse token mass. Cases: equal-length groups, and ragged groups with a dropped row and sequence packing; the ragged case also checks that plainglobal_token_meanfalls outside the tolerance.group_token_mean, including dropped and fractional sample masks; each group guard has a test.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.sequence_mean); curves and table are in this review thread.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.
Before your PR is "Ready for review"