Repository navigation
Conversation
…lizer loss_function clamped each row's loss_mask.sum() to 1, so a row with an all-zero loss mask (for example remove_sample=True) added one token to num_tokens and scaled down the per-token loss. Sum the masks directly. Megatron already clamps the all-reduced total, and the metric reducer now guards a step without loss tokens.
Codex Review Summary: ❌ FailedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
This was referenced Nov 27, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes part 1 of #2560.
Problem. With
--calculate-per-token-loss,loss_functioncomputesnum_tokensassum(clamp_min(loss_mask.sum(), 1)). A row whose loss mask is all zero, such as a sample withremove_sample=True, adds one token. So the loss is divided by a total that is too large. The documented formula issum(sum(sample_i)) / sum(len(sample_i)).Change.
loss.py:num_tokens = sum(loss_mask.sum() ...). Megatron'sfinalize_model_gradsalready clamps the all-reduced total.train_metric_utils.reduce_train_step_metrics: divide bymax(values[0], 1), so a step where every row is masked does not raiseZeroDivisionErrorin the metric report.Tests.
test_per_token_loss_normalizer_ignores_fully_masked_rows(in theaststyle oftests/test_policy_loss.py): fails on main withassert 3 == 2.test_per_token_loss_report_handles_step_without_loss_tokens: guards the zero-token step after the change.tests/test_policy_loss.pyandtests/test_metric_report.py: 40 passed, CPU.ruff,blackandisortfrom the pre-commit config pass on the changed files.test_metric_report_dist.pyandtest_loss_cp_invariance.py(they hung in my CPU environment), and no GPU training.AI assistance: Claude Code wrote this change and its test, and ran the checks above, under my direction. I reviewed it and will answer the review.