Skip to content

fix(loss): do not count fully masked rows in the per-token loss normalizer - #2562

Open
morluto wants to merge 1 commit into
THUDM:mainfrom
morluto:fix/per-token-loss-masked-rows
Open

morluto wants to merge 1 commit into
THUDM:mainfrom
morluto:fix/per-token-loss-masked-rows

Conversation

@morluto

@morluto morluto commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Fixes part 1 of #2560.

Problem. With --calculate-per-token-loss, loss_function computes num_tokens as sum(clamp_min(loss_mask.sum(), 1)). A row whose loss mask is all zero, such as a sample with remove_sample=True, adds one token. So the loss is divided by a total that is too large. The documented formula is sum(sum(sample_i)) / sum(len(sample_i)).

Change.

  • loss.py: num_tokens = sum(loss_mask.sum() ...). Megatron's finalize_model_grads already clamps the all-reduced total.
  • train_metric_utils.reduce_train_step_metrics: divide by max(values[0], 1), so a step where every row is masked does not raise ZeroDivisionError in the metric report.

Tests.

  • test_per_token_loss_normalizer_ignores_fully_masked_rows (in the ast style of tests/test_policy_loss.py): fails on main with assert 3 == 2.
  • test_per_token_loss_report_handles_step_without_loss_tokens: guards the zero-token step after the change.
  • tests/test_policy_loss.py and tests/test_metric_report.py: 40 passed, CPU.
  • ruff, black and isort from the pre-commit config pass on the changed files.
  • Not run: test_metric_report_dist.py and test_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.

…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.
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-25T17:20:59.488594Z
We couldn’t complete this review. Comment @codex review to try again.
e70779a PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant