Skip to content

Do not count MoE tokens during evaluation - #4148

Merged
tianyu-l merged 1 commit into
pytorch:mainfrom
taking-lying-flat:fix/moe-eval-token-counts
Aug 15, 2026
Merged

tianyu-l merged 1 commit into
pytorch:mainfrom
taking-lying-flat:fix/moe-eval-token-counts

Conversation

@taking-lying-flat

Copy link
Copy Markdown
Contributor

MoE.forward() currently updates tokens_per_expert_E in eval mode. Validation runs after the optimizer hook has consumed and cleared the training counts, so validation routing counts can leak into the next step's expert_bias_E update. torch.no_grad() does not prevent this buffer mutation.

Only accumulate the counter while the module is in training mode. Activation-checkpoint recomputation still runs in training mode, so its existing behavior is unchanged.

Added a CPU regression test that first records training counts, then verifies an eval forward leaves them unchanged.

Tests:

  • python -m pytest -q tests/unit_tests/test_moe.py tests/unit_tests/test_optimizer_param_groups.py tests/unit_tests/test_inference_moe.py tests/unit_tests/test_gpt_oss_moe.py (28 passed)
  • File-scoped pre-commit hooks for the changed files
  • Pyrefly on torchtitan/models/common/moe.py (0 errors)

@meta-cla

meta-cla Bot commented Aug 14, 2026

Copy link
Copy Markdown

Hi @taking-lying-flat!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@pytorch-bot

pytorch-bot Bot commented Aug 14, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@taking-lying-flat
taking-lying-flat marked this pull request as ready for review August 14, 2026 11:45
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 14, 2026
@meta-cla

meta-cla Bot commented Aug 14, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@tianyu-l
tianyu-l requested a review from acisseJZhong August 15, 2026 00:28

@tianyu-l tianyu-l left a comment

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.

Sounds legit. Let's stop bleeding and worry about inference metrics later. @felipemello1

@acisseJZhong

Copy link
Copy Markdown
Contributor

lgtm! we are not logging tokens_per_expert_E during training and inference at all right?

@tianyu-l
tianyu-l merged commit c4dff13 into pytorch:main Aug 15, 2026
29 checks passed
@taking-lying-flat
taking-lying-flat deleted the fix/moe-eval-token-counts branch August 15, 2026 23:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants