Skip to content

Fix DSA indexer loss not averaged across micro-batches#4070

Open
kaimo455 wants to merge 2 commits intoNVIDIA:mainfrom
kaimo455:fix/dsa-loss-mbs-averaging
Open

Fix DSA indexer loss not averaged across micro-batches#4070
kaimo455 wants to merge 2 commits intoNVIDIA:mainfrom
kaimo455:fix/dsa-loss-mbs-averaging

Conversation

@kaimo455
Copy link
Copy Markdown

@kaimo455 kaimo455 commented Mar 31, 2026

What does this PR do ?

Fixes DSA indexer loss scaling so it is consistent across micro-batches under gradient accumulation.

Background

DSA injects the indexer loss into the main loss backward pass via DSAIndexerLossAutoScaler. When Megatron is not calculating per-token loss, the main loss is reduced by 1 / num_microbatches, and other auxiliary losses (e.g., MTP) are scaled similarly.

Previously, the DSA indexer loss scale was not normalized by num_microbatches, so gradients changed as num_microbatches changed.

Change

Set DSAIndexerLossAutoScaler's loss scale to loss_scale / num_microbatches when calculate_per_token_loss is false, matching the existing scaling pattern used by MTP.

Validation

  • DSA run with gradient accumulation: confirmed grad-norm stays comparable between num_microbatches=1 and num_microbatches>1.
  • No behavior change expected when calculate_per_token_loss is true.

cc @mcore-oncall (vetting needed so CI can run)

Contribution process

Pre-checks

Code review

Feel free to message or comment the [@mcore-oncall](https://github.com/orgs/NVIDIA/teams/mcore-oncall) to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!

All PRs start as draft. If you open a non-draft PR, it will be automatically converted to draft.

Step 1: Mark PR as "Ready for Review"

  1. When your PR is ready, click Ready for Review.
  2. An oncall reviewer is auto-assigned and expert reviewers are notified based on your changes.
    • Some PRs may jump straight to step 2. This is determined by .github/CODEOWNERS.

⚠️ Only mark as ready once merge-conflicts are resolved and the CI is passing.
Final Review might get declined if these requirements are not fulfilled.

Step 2: Final Review

For PRs that change megatron/core, once all expert reviewers have approved, the Final Review label is applied automatically and final reviewers are assigned.

For PRs outside megatron/core, this step is skipped.

Step 3: Approved

Once all required reviewers have approved, the Approved label is applied automatically.

Merge

Any member of [mcore-engineers](https://github.com/orgs/NVIDIA/teams/mcore-engineers) will be able to merge your PR.

For MRs into `dev` branch The proposed review process for `dev` branch is under active discussion.

MRs are mergable after one approval by either eharper@nvidia.com or zijiey@nvidia.com.

@kaimo455 kaimo455 requested review from a team as code owners March 31, 2026 04:38
@copy-pr-bot
Copy link
Copy Markdown

copy-pr-bot bot commented Mar 31, 2026

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@svcnvidia-nemo-ci svcnvidia-nemo-ci marked this pull request as draft March 31, 2026 04:39
@github-actions
Copy link
Copy Markdown
Contributor

This PR has been automatically converted to draft because all PRs must start as drafts.

When you are ready for review, click Ready for Review to begin the review process. This will:

  1. Add the oncall reviewer (optional reviewer)
  2. Add required review teams based on your changes

See the contribution guide for more details.

@kaimo455 kaimo455 marked this pull request as ready for review March 31, 2026 07:18
@svcnvidia-nemo-ci svcnvidia-nemo-ci requested a review from a team March 31, 2026 07:18
@kaimo455
Copy link
Copy Markdown
Author

@mcore-oncall PR #4070 is ready for review. Could you help with vetting so CI can complete on NVIDIA runners? Thanks!

@janEbert
Copy link
Copy Markdown
Contributor

/ok to test 47764ce

@svcnvidia-nemo-ci svcnvidia-nemo-ci added this to the Core 0.16 milestone Mar 31, 2026
@kaimo455 kaimo455 marked this pull request as draft April 2, 2026 07:38
@kaimo455 kaimo455 marked this pull request as ready for review April 2, 2026 07:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants