Skip to content

fix(mmlu): shard batches over expert-DP group for MoE models#1966

Closed
ChenhanYu wants to merge 1 commit into
mainfrom
chenhany/mmlu-moe-ep-shard-fix
Closed

fix(mmlu): shard batches over expert-DP group for MoE models#1966
ChenhanYu wants to merge 1 commit into
mainfrom
chenhany/mmlu-moe-ep-shard-fix

Conversation

@ChenhanYu

@ChenhanYu ChenhanYu commented Jul 12, 2026

Copy link
Copy Markdown
Collaborator

Problem

megatron_mmlu shards whole batches across the dense data-parallel group (get_data_parallel_world_size()), giving each rank a disjoint subset (if batch_idx % dp_size != dp_rank: continue) and all-reducing per-subject counts over the DP group at the end.

That is correct for dense models — DP ranks are independent replicas and the forward has no cross-rank collective. It deadlocks for MoE models with expert parallelism (EP>1): megatron_prefill's forward runs an expert all-to-all across the EP group. When EP overlaps the dense-DP group (e.g. EP=4, TP=1, PP=1 on 4 GPUs — all four ranks are both EP peers and dense-DP ranks), each rank runs prefill on a different batch, so their expert all-to-alls desync. Uneven batch counts (n_batches % dp_size != 0) and per-batch padded seq-lengths leave trailing ranks blocked at NCCL communicator creation until the c10d store times out (600 s).

Reproduced with a NemotronH-class MoE (Nano-30B-A3B) quantize → MMLU at EP=4: ranks 2 & 3 wait on rank 0's ncclUniqueId and time out.

Fix

Shard over the expert-data-parallel group when EP>1. Ranks within one EP group then evaluate every batch in lockstep (so the expert all-to-all always has all participants), and only true expert-DP replicas — each holding a full expert set — take disjoint batches. Dense models (EP==1) keep the standard DP sharding unchanged.

  • EP=4 on 4 GPUs → expert-DP=1 → no sharding, all ranks lockstep. Final all-reduce becomes a size-1 no-op (correct).
  • EP=4 on 8 GPUs → expert-DP=2 → two replicas shard batches, each replica's 4 EP ranks lockstep.

Test plan

  • MoE MMLU (EP>1) completes instead of hanging — validated on Nano-30B-A3B EP=4.
  • Dense MMLU sharding unchanged (EP==1 path is identical to before).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved evaluation stability when expert model parallelism is enabled.
    • Ensured batches are sharded consistently across expert-parallel replicas, preventing potential evaluation deadlocks.

megatron_mmlu shards whole batches across the dense data-parallel group and has
each rank run megatron_prefill on a disjoint subset. That is correct for dense
models, but for MoE models with expert parallelism (EP>1) the prefill forward
runs an expert all-to-all across the EP group. When EP overlaps the dense-DP
group, the per-rank disjoint batches desync that all-to-all — uneven batch
counts and differing padded seq-lengths leave trailing ranks blocked at NCCL
communicator creation until the store timeout.

Shard over the expert-data-parallel group instead when EP>1, so ranks within an
EP group evaluate every batch in lockstep and only true expert-DP replicas take
disjoint batches. Dense models (EP==1) keep the standard DP sharding.

Signed-off-by: Chenhan Yu <chenhany@nvidia.com>
@ChenhanYu ChenhanYu requested a review from a team as a code owner July 12, 2026 01:24
@ChenhanYu ChenhanYu requested a review from AAnoosheh July 12, 2026 01:24
@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 52edd5cb-6547-4314-b9d7-4defcb722ef0

📥 Commits

Reviewing files that changed from the base of the PR and between 7ed19b2 and c4bc3dc.

📒 Files selected for processing (1)
  • modelopt/torch/utils/plugins/megatron_mmlu.py

📝 Walkthrough

Walkthrough

The MMLU plugin now selects expert-data-parallel sharding state when expert model parallelism is active and retains standard data-parallel sharding otherwise.

Changes

MMLU sharding

Layer / File(s) Summary
EP-aware data-parallel selection
modelopt/torch/utils/plugins/megatron_mmlu.py
Data sharding derives size, rank, and group from expert-data-parallel replicas for EP greater than one, while EP equal to one uses the regular data-parallel state. The comment documents the EP deadlock risk.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 6
✅ Passed checks (6 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: MMLU batch sharding now uses the expert-DP group for MoE models.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Security Anti-Patterns ✅ Passed Only megatron_mmlu.py changed; it adds no torch.load/numpy.load/trust_remote_code/eval/exec/nosec patterns or new dependencies.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch chenhany/mmlu-moe-ep-shard-fix

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-07-12 01:35 UTC

@codecov

codecov Bot commented Jul 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 57.14286% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.29%. Comparing base (7ed19b2) to head (c4bc3dc).

Files with missing lines Patch % Lines
modelopt/torch/utils/plugins/megatron_mmlu.py 57.14% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1966      +/-   ##
==========================================
- Coverage   77.92%   77.29%   -0.64%     
==========================================
  Files         519      519              
  Lines       58217    58221       +4     
==========================================
- Hits        45366    45002     -364     
- Misses      12851    13219     +368     
Flag Coverage Δ
examples 43.29% <57.14%> (-0.16%) ⬇️
gpu 57.93% <57.14%> (-0.64%) ⬇️
regression 15.00% <0.00%> (+0.07%) ⬆️
unit 55.46% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ChenhanYu

Copy link
Copy Markdown
Collaborator Author

Superseded by #1967, which combines the launcher example with the MMLU expert-DP sharding fix (the example is the fix's end-to-end test, so they ship together).

@ChenhanYu ChenhanYu closed this Jul 12, 2026
ChenhanYu added a commit that referenced this pull request Jul 14, 2026
#1967)

Combines the NemotronH MoE PTQ launcher example with the MMLU
expert-parallel sharding fix it exercises. The example is the end-to-end
test for the fix — quantize + **MMLU EP=4** + export + vLLM smoke on
Nano-30B-A3B — so they ship together.

## Fix — `megatron_mmlu.py` shards over the expert-DP group for MoE
models

`megatron_mmlu` shards whole batches across the **dense data-parallel
group** and runs `megatron_prefill` per-rank on a disjoint subset.
Correct for dense models, but for MoE with **EP>1** the prefill forward
runs an **expert all-to-all across the EP group**. When EP overlaps the
dense-DP group (e.g. `EP=4,TP=1,PP=1` on 4 GPUs), each rank is on a
different batch, so the all-to-alls desync (uneven batch counts +
differing padded seq-lengths) → trailing ranks block at NCCL
communicator creation until the c10d store times out (600 s). Reproduced
on Nano-30B-A3B at `EP=4`: ranks 2 & 3 wait on rank 0's `ncclUniqueId`.

The fix shards over the **expert-data-parallel** group when `EP>1`, so
EP peers evaluate every batch in lockstep and only true expert-DP
replicas take disjoint batches. Dense models (`EP==1`) are byte-for-byte
unchanged.

## Example —
`examples/nvidia/NVIDIA-Nemotron-3-Nano-30B-A3B-BF16/megatron_lm_ptq.yaml`

Quantize (EP=4) + MMLU gate (EP=4) → export (PP=4) → vLLM smoke, modeled
on the Super-120B example. Uses `NVFP4_DEFAULT_CFG` (no Nano-30B recipe)
and sets `modelopt_install_path` to the nemo-container venv so the
mounted modelopt overrides the container copy. Passes
`test_examples_resolve.py`.

## Test plan

- [x] MoE MMLU (`EP>1`) completes instead of hanging — validated
end-to-end via this example on Nano-30B-A3B `EP=4` (nmm-sandbox CI).
- [x] `test_examples_resolve.py` green (example parses/resolves).
- [ ] Dense MMLU sharding unchanged (`EP==1` path identical).

Supersedes #1964 (example) and #1966 (fix).

🤖 Generated with [Claude Code](https://claude.com/claude-code)


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Improved MMLU evaluation for expert-parallel MoE models by aligning
batch sharding with expert-parallel execution for more consistent
results.
* **Bug Fixes**
* Dense (non-MoE) models retain standard data-parallel batch sharding,
while MoE handling is corrected.
* **Documentation**
* Updated the Nemotron-3-Nano BF16 PTQ example to clarify the quantize →
MMLU gate → export flow and why the vLLM smoke is intentionally omitted.
* Adjusted the Llama-3.2-1B-Instruct PTQ/MMLU gate lower-bound threshold
(0.40 → 0.36).
* **Tests**
* Marked the homogeneous compressed sharded state-dict test to skip on
Blackwell to avoid a known flaky issue.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: Chenhan Yu <chenhany@nvidia.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
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