Skip to content

MoE routing analysis and metrics capture - #5220

Merged
mathemakitten merged 30 commits into
NVIDIA:mainfrom
mathemakitten:helenn-moe-metrics-basic
Jul 7, 2026
Merged

mathemakitten merged 30 commits into
NVIDIA:mainfrom
mathemakitten:helenn-moe-metrics-basic

Conversation

@mathemakitten

Copy link
Copy Markdown
Contributor
  • I, the PR author, have personally reviewed every line of this PR.

What does this PR do ?

  • Adds a RouterTracer class that captures per-layer top-K routing decisions via forward hooks on TopKRouter modules, writing one JSONL record per (step, layer, rank). Works identically for training and inference.
  • Adds tools/moe_routing/ with two analysis scripts that work on traces from either training or inference:
    • analyze_routing_concentration.py — per-layer expert activation frequency, top-N coverage curves, and a heatmap of hot experts vs layers.
    • analyze_routing_predictability.py — applies layer L's router weights to L_prev's hidden states and measures cosine/Spearman similarity between predicted and actual per-expert token-count distributions. High scores here are the affirmative signal that distributional routing is predictable one layer ahead.

Notes on usability

  • Use --moe-routing-trace-max-iters / --moe-routing-trace-max-steps to bound the capture window; the tracer self-disables and removes its hooks after the limit is reached
  • Python forward hooks do not fire during CUDA graph replay. Run with --cuda-graph-impl none when collecting traces.
  • analyze_routing_predictability.py requires --moe-routing-trace-capture-hidden-states and --moe-routing-trace-dump-weights at collection time.

Follow-up items

  • Unify hook registration helpers: megatron/training/activation_logging.py has a generic _register_hooks(model, module_types, hook_factory) utility that walks model chunks and registers forward hooks. RouterTracer.register_hooks does the same thing with a loop. These can be consolidated with a shared helper into core/, but requires some more work to unify the hook registration pattern (_register_hooks in activation_logging.py does not currently live in core).

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
  • 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.

@mathemakitten
mathemakitten requested a review from a team as a code owner June 8, 2026 19:04
@mathemakitten
mathemakitten requested a review from a team June 8, 2026 19:04
@mathemakitten
mathemakitten requested review from a team as code owners June 8, 2026 19:04
@github-actions

github-actions Bot commented Jun 8, 2026

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.

@svcnvidia-nemo-ci
svcnvidia-nemo-ci marked this pull request as draft June 8, 2026 19:04
@copy-pr-bot

copy-pr-bot Bot commented Jun 8, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

Signed-off-by: Helen Ngo <helenn@nvidia.com>
Signed-off-by: Helen Ngo <helenn@nvidia.com>
Signed-off-by: Helen Ngo <helenn@nvidia.com>
Signed-off-by: Helen Ngo <helenn@nvidia.com>
Signed-off-by: Helen Ngo <helenn@nvidia.com>
Signed-off-by: Helen Ngo <helenn@nvidia.com>
Signed-off-by: Helen Ngo <helenn@nvidia.com>
Signed-off-by: Helen Ngo <helenn@nvidia.com>
Signed-off-by: Helen Ngo <helenn@nvidia.com>
Signed-off-by: Helen Ngo <helenn@nvidia.com>
Signed-off-by: Helen Ngo <helenn@nvidia.com>
Signed-off-by: Helen Ngo <helenn@nvidia.com>
@ericharper
ericharper requested a review from maanug-nv July 2, 2026 17:38

@maanug-nv maanug-nv Jul 2, 2026 •

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.

wouldn't LoggerConfig make more sense for these settings?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, moved.

Signed-off-by: Helen Ngo <helenn@nvidia.com>
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the Approved All necessary approvals have been made label Jul 7, 2026
@mathemakitten
mathemakitten added this pull request to the merge queue Jul 7, 2026
@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/28890461242

Merged via the queue into NVIDIA:main with commit c1560d9 Jul 7, 2026
90 of 92 checks passed
@mathemakitten
mathemakitten deleted the helenn-moe-metrics-basic branch July 7, 2026 20:13
sidsingh-nvidia pushed a commit to sidsingh-nvidia/Megatron-LM that referenced this pull request Jul 9, 2026
Signed-off-by: Helen Ngo <helenn@nvidia.com>
Signed-off-by: Siddharth Singh <sidsingh@nvidia.com>
jon-barker pushed a commit to jon-barker/Megatron-LM that referenced this pull request Jul 10, 2026
Signed-off-by: Helen Ngo <helenn@nvidia.com>
Signed-off-by: Jon Barker <jbarker@aws-cmh-slurm-1-vscode-02.cm.cluster>
sidsingh-nvidia pushed a commit to sidsingh-nvidia/Megatron-LM that referenced this pull request Jul 13, 2026
Signed-off-by: Helen Ngo <helenn@nvidia.com>
chochowski pushed a commit to chochowski/Megatron-LM that referenced this pull request Jul 20, 2026
Signed-off-by: Helen Ngo <helenn@nvidia.com>
Signed-off-by: mchochowski <mchochowski@nvidia.com>

This branch was previously deployed

2 inactive deployments
public — 177071be Deployed Jul 6, 2026 by copy-pr-bot[bot] via finalize / publish-docs #3292
test — 177071be Deployed Jul 6, 2026 by copy-pr-bot[bot] via cicd-wait-in-queue #16423
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approved All necessary approvals have been made complexity: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants