Skip to content

[Determinism] Support deterministic fused MoE auxiliary loss - #7697

Open
zhongbozhu wants to merge 6 commits into
NVIDIA:mainfrom
zhongbozhu:fix/deterministic-moe-aux-loss
Open

zhongbozhu wants to merge 6 commits into
NVIDIA:mainfrom
zhongbozhu:fix/deterministic-moe-aux-loss

Conversation

@zhongbozhu

@zhongbozhu zhongbozhu commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Detect TE's deterministic aux-loss API and propagate explicit or PyTorch global determinism through all three router loss paths. Allow supported fused execution in deterministic mode while preserving legacy TE compatibility and existing flag precedence.

Validate capability and dispatch compatibility, unfused-reference equivalence, and bitwise loss and gradient replay. Focused GB200 tests: 40 passed, with the atomic negative control observing nondeterminism.

TE PR should be merged first: NVIDIA/TransformerEngine#3587

Fixes #7221

  • I, the PR author, have personally reviewed every line of this PR.

What does this PR do?

⚠️ For major changes (either in lines of code or in its impact), please make sure to first share a design doc with the team. If you're unsure what's the best way to do so, contact @NVIDIA/mcore-oncall.

Issue tracking

For PRs from open-source community contributors:

  • New features: a linked issue is required. Please open a feature request and reference it here before submitting the PR.
  • Small updates (bug fixes, minor improvements): a linked issue is recommended and will accelerate the PR review process.

Linked issue:

Contribution process

Pre-checks

  • I have added relevant unit tests
  • I have added relevant functional tests
  • If this PR adds or changes a GPU kernel (Triton, jit_fuser/torch.compile, CUDA extension, TE or external-library dispatch, or a scatter/index accumulation), I have added or updated its bit-exact determinism test and registered it in tests/unit_tests/determinism/kernels/manifest.py (guide)
  • I have added proper typing to my code Typing guidelines
  • I have added relevant documentation
  • I have run the autoformatter.sh on my PR

Code review

Feel free to message or comment @NVIDIA/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 will be able to merge your PR.

@copy-pr-bot

copy-pr-bot Bot commented Sep 29, 2026

Copy link
Copy Markdown

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.

@zhongbozhu
zhongbozhu marked this pull request as ready for review October 1, 2026 22:39
@zhongbozhu
zhongbozhu requested review from a team as code owners October 1, 2026 22:39
@zhongbozhu

Copy link
Copy Markdown
Contributor Author

/ok to test b02ba07

if fused:
if not HAVE_TE or fused_moe_aux_loss is None:
raise ValueError("fused_moe_aux_loss is not available. Please install TE >= 2.7.0.")
deterministic = deterministic or torch.are_deterministic_algorithms_enabled()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit.

Should we just trust deterministic and remove this line?

The False-vs-global-True case should be a conflict.

  • config.deterministic_mode=True together with the torch global enabled is consistent (in deterministic setup), so nothing to raise.
  • deterministic_mode=False with the torch global True currently runs deterministic silently because of the or. If you'd rather trust the config, validate once at config setup (raise on that mismatch) instead of re-checking inside every loss call. As a nit, either is acceptable.

@ZhiyuLi-Nvidia

Copy link
Copy Markdown
Contributor

Hi, @zhongbozhu, I'd expect you may remove the flag moe_router_aux_loss_fusion. Is that still the plan with an incoming PR?

@zhongbozhu
zhongbozhu force-pushed the fix/deterministic-moe-aux-loss branch from b02ba07 to be26a58 Compare October 20, 2026 05:11
Detect TE's deterministic aux-loss API and propagate explicit or PyTorch global determinism through all three router loss paths. Allow supported fused execution in deterministic mode while preserving legacy TE compatibility and existing flag precedence.

Validate capability and dispatch compatibility, unfused-reference equivalence, and bitwise loss and gradient replay. Focused GB200 tests: 40 passed, with the atomic negative control observing nondeterminism.

Signed-off-by: zhongboz <zhongboz@nvidia.com>
Signed-off-by: zhongboz <zhongboz@nvidia.com>
Signed-off-by: zhongboz <zhongboz@nvidia.com>
@zhongbozhu
zhongbozhu force-pushed the fix/deterministic-moe-aux-loss branch from be26a58 to 15a5285 Compare October 28, 2026 07:04
@zhongbozhu

Copy link
Copy Markdown
Contributor Author

/ok to test 15a5285

@balasaajay

Copy link
Copy Markdown
Contributor

/ok to test 4dc9d6b

Signed-off-by: Ajay Balasa <abalasa@nvidia.com>
@balasaajay

Copy link
Copy Markdown
Contributor

/ok to test 44034a3

Complete the sequence aux-loss router stub configuration and keep dense TopK replay coverage independent of the optional deterministic fused aux-loss API.

Signed-off-by: zhongboz <zhongboz@nvidia.com>
@zhongbozhu

Copy link
Copy Markdown
Contributor Author

/ok to test ba117d6

This branch was successfully deployed

1 active and 1 inactive deployments
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.

[Determinism] Add deterministic TE fused loss support

6 participants