Skip to content

[MoE][RL] MegaMoE NVFP4: load each scale on its own, combine after loading - #65254

Open
hao-aaron wants to merge 11 commits into
vllm-project:mainfrom
hao-aaron:lp/l2-megamoe-nvfp4-scales
Open

hao-aaron wants to merge 11 commits into
vllm-project:mainfrom
hao-aaron:lp/l2-megamoe-nvfp4-scales

Conversation

@hao-aaron

@hao-aaron hao-aaron commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Purpose

Part of #64418 (load plan), item L2 (closes RFC item X5 for MegaMoE).

  • P1's loader is called with a meta tensor for every name the load plan copies direct. DeepGemmMegaMoEExperts skipped its _record_global_scale bookkeeping on meta, so after a planned update l2_activation_scale kept its old value.
  • This PR moves that bookkeeping out of the loader. Each NVFP4 scale lands in its own checkpoint slot through a view and copy_. process_weights_after_loading combines them, and it reruns after every load and reload.
  • The block-scale loader becomes a byte-view copy, so a load plan can replay it. Every routed MegaMoE name can then be planned direct.

Changes

All in vllm/model_executor/layers/fused_moe/deep_gemm_mega_moe.py:

  • w13_weight_scale_2 is now (2, local experts), one row for w1 and one for w3.
    • Row 0 is the kernel's l1_alpha.
    • EPLB moves both rows.
  • w2_input_scale is now (logical experts,) and is written on every rank, so all ranks agree on the L2 scale. EPLB does not move it.
  • New process_weights_after_loading:
    • it checks that w1 and w3 match, raising the same ValueError as before, now after loading;
    • it sets l2_activation_scale = max(w2_input_scale) or 1.0;
    • the loader-side state it replaces is gone: _w2_input_scales, _pending_l1_scales and _record_global_scale.
  • Block scales are copied as bytes.
    • The source is (rows, K/4, 4) uint8 and the destination is a uint8 view of the int32 kernel layout.
    • repeat_interleave runs only when the checkpoint scales are coarser than the kernel's, and the clone is gone.
    • Bytes are unchanged; this was checked against base on CPU for w1/w3/w2, shared-expert, coarse, non-contiguous and scale_k == 1 cases.
  • Layerwise reload:
    • block-scale params declare weight_loader_numel in bytes, which is what the byte-view copies count;
    • _l2_alpha declares 0, since no loader writes it.
  • Dummy loads give all three global scales positive values (1e-3, 1e-3, 1.0): w1 and w3 match and the L2 scale stays at 1.

Known limitations

  • CUDA graphs keep the old L2 scale (pre-existing). l2_activation_scale is still a host float passed by value to the prepare kernel and to DeepGEMM. A reload updates it, but graphs captured earlier keep the old value, along with the _l2_alpha they compute from it. This holds for RL starts from --load-format dummy. The fix is to pass it as a device tensor, which is [RFC]: Direct Weight Reload for RL Training #54477 F16 and needs [SM100] NVFP4 MegaMoE: take the L2 activation scale as a device tensor DeepGEMM#49. The graph row in the audit table shows this.
  • MiniMax-M3 with EP > 1 still fails plan_loads (pre-existing, outside this file).
    • The MiniMax-M3 adapter turns weight_global_scale into 1 / x, which makes those names fallbacks.
    • On ranks that do not own the expert, those fallbacks write nothing, and P1 refuses them as names that "write nothing themselves".
    • The audit below therefore ran with that refusal turned into a warning.
  • The sharded RDT backend cannot trace the byte-view block-scale copy. unflatten and dtype-changing destination views are not in its op allowlist. It fails loudly at engine init, and base failed there too (repeat_interleave).
  • Kernel-format shapes changed. w13_weight_scale_2 went from (e,) to (2, e) and w2_input_scale from (e,) to (logical,). Tensors exported in the old shapes do not reload into the new ones.

Test Plan

  • CPU and GB200 unit tests in tests/kernels/moe/test_deep_gemm_mega_moe.py:
    • the load plan is all direct for routed names, and both planned and unplanned updates match a cold start (MXFP4, NVFP4);
    • layerwise reload processes the layer at its last tensor, not earlier and not only at finalize;
    • l2_activation_scale covers every logical expert and follows a reload;
    • EPLB views move whole experts, including both w13_weight_scale_2 rows;
    • mismatched w1/w3 scales are rejected after loading;
    • post-load processing is skipped on meta.
  • GB200 kernel tests: tests/kernels/moe/test_deep_gemm_mega_moe.py and tests/models/test_minimax_m3_mega_moe.py.
  • Weight-sync audit on MiniMax-M3.1-Flash (MXFP8 + compressed-tensors NVFP4 experts, deep_gemm_mega_moe):
    • vLLM TP2 + EP on GPUs 0-1, NCCL trainer FSDP2 on GPUs 2-3;
    • VLLM_RELOAD_MODE=direct with the load plan on;
    • 3 syncs of perturbed checkpoints, in which every tensor changes and the scales are multiplied by 2 and 4;
    • then every registered and off-registry device tensor, plus each layer's l2_activation_scale float, is compared with a fresh server started on the last checkpoint.
  • The audit used two local patches, neither of which is in this PR:
    • the MiniMax-M3 w2 input scale is taken from the checkpoint as is (upstream fixes it at 1/16), so the name is planned direct and an update changes the L2 scale;
    • P1's "write nothing themselves" refusal becomes a warning (see Known limitations).

Test Result

  • GB200 (job 46676, head 4ba1650): tests/kernels/moe/test_deep_gemm_mega_moe.py + tests/models/test_minimax_m3_mega_moe.py, 129 passed.
  • Weight-sync audit (job 46624; the module as of 100e7bd, later commits are cleanups covered by the unit tests), after 3 syncs, compared with a fresh server. 3662 tensors per arm; 114 = 57 MoE layers × 2 ranks:
Arm CUDA graphs Plan per rank: direct / fallback / not needed Mismatched vs fresh l2_activation_scale (fresh 2.0)
this PR off 30741 / 21888 / 37003 0 (PASS) 2.0
main (P1, without this PR) off 15978 / 33003 / 40651 228: l2_activation_scale ×114, _l2_alpha ×114 1.0 (stale)
this PR on 30741 / 21888 / 37003 114: _l2_alpha (captured graphs, F16) 2.0
  • On main, w2.input_global_scale is planned direct, the loader sees meta, and the L2 scale stays at its cold-start value. With this PR the same name is direct and post-load processing recomputes the scale.
  • An earlier run used a probe that keeps the name a fallback ((1/16) / x). There the loader sees real values, so main's l2_activation_scale was refreshed too. This PR passed eager there as well, and with CUDA graphs both had the same 114 _l2_alpha mismatches.
  • Sync time with the plan (median of 2): 7.1 s with this PR, against 9.3 s on main, eager.

Cold-start A/B

Setup: A = main at this PR's base (10c8574), B = this PR (4ba1650). Both arms are Python overlays on the same nightly wheel (7cf31d2, 24 commits behind the base; the only C++ change in those commits is a ROCm binding). Each row runs on one node, with the arms back to back.

  • GSM8K: 5-shot, greedy, all 1319 questions.
  • Throughput: vllm bench serve, random dataset, 1024 in / 256 out, concurrency 64.
  • Backend check: each arm's server log must contain the line that a DeepGemmMegaMoEExperts was built with the expected weight format. A probe that is identical in both arms prints this line. All rows passed.
Model (hardware, parallelism) GSM8K A → B Other Throughput tok/s A → B KV cache A → B
MiniMax-M3.1-Flash, MXFP8 + NVFP4 experts on deep_gemm_mega_moe (GB200, TP4 EP) 0.9621 → 0.9606 4036 → 4047 2.490M → 2.490M
MiniMax-M3.1-Flash NVFP4 MegaMoE, after one NCCL sync of the same weights (GB200, TP2 EP, --gpu-memory-utilization 0.85) 0.9651 → 0.9613 free after sync 27.49 → 27.49 GiB; reserved 122.37 → 122.37 GiB; sync 50.5 → 50.0 s 2831 → 2822 377k → 377k
DeepSeek-V4-Flash, MXFP4 MegaMoE (GB200, TP2 EP) 0.9477 → 0.9454 3811 → 3797 6.162M → 6.162M
GLM-5.3-Flash, block-FP8 requantized to MXFP8 MegaMoE (GB200, DP4 EP, --gpu-memory-utilization 0.85 --max-num-seqs 256) 0.9204 → 0.9174 3486 → 3436 573k → 573k
  • How to read the noise. All GSM8K gaps are under 0.4 pt and all throughput gaps are under 1.5%, below the thresholds for a swapped rerun (1 pt, 3%), so no row was rerun in the other order. KV cache is identical in every row. There is no regression.
  • MiniMax GSM8K uses chat completions. The MiniMax rows run GSM8K with --use-chat-completions --max-tokens 2048 and reasoning_effort: low. Through plain completions, both arms degenerate on main at this base (0.08 on arm A), while chat answers correctly.
  • Sync row. One NCCL update pushes the whole checkpoint (89632 tensors, 250 GB) into the running server through the default layerwise reload, without the load plan, and then the measurements run. At the default --gpu-memory-utilization 0.9, arm A (main) runs out of memory during the update, so this row uses 0.85 in both arms.
  • Skipped:
    • Inferact/MiniMax-M3-NVFP4-V2 is a ModelOpt NVFP4 checkpoint, and MiniMax-M3 MegaMoE requires moe_quant_algo: NVFP4, which only the MXFP8-format checkpoints set, so it cannot select this backend.
    • Inferact/Minimax-M3-NVFP4 has the same format as M3.1-Flash and runs on MegaMoE, but it scores about 0.03 on GSM8K in both arms, so it was dropped for M3.1-Flash. One A/B pass gave 0.0281 → 0.0394, 4053 → 4026 tok/s and KV cache 2.490M in both arms.
    • Kimi-K3 (MXFP4 MegaMoE): no cached checkpoint has weights.
    • Step-5 and Muse Spark have their own MegaMoE expert classes (step5/nvidia/mega_moe.py, muse_spark/nvidia/mega_moe.py), which do not use DeepGemmMegaMoEExperts, and this PR does not change them.

AI assistance (Claude Code) was used; the submitter reviews every changed line.

🤖 Generated with Claude Code

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@mergify

mergify Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @hao-aaron.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify Bot added the needs-rebase label Nov 8, 2026
@hao-aaron
hao-aaron force-pushed the lp/l2-megamoe-nvfp4-scales branch from 4ba1650 to 60667e9 Compare November 11, 2026 00:45
@mergify mergify Bot removed the needs-rebase label Nov 11, 2026
@mergify

mergify Bot commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @hao-aaron.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify Bot added the needs-rebase label Nov 14, 2026
hao-aaron and others added 9 commits November 16, 2026 19:51
…ading

DeepGemmMegaMoEExperts combined NVFP4 global scales inside the weight
loader: the L2 activation scale was a running max over a CPU copy of
every expert's w2_input_scale, w1 and w3 weight_scale_2 were paired
through loader state and both written into one slot, and block scales
went through repeat_interleave and clone even when nothing is expanded.
A load-plan trace cannot replay any of that.

Each checkpoint tensor now lands in its own slot with a view and copy_:
w13_weight_scale_2 is (2, experts) for w1 and w3, so its w1 row is the
contiguous L1 alpha the kernel reads; w2_input_scale holds every
logical expert on every rank; block scales are a view chain unless the
checkpoint granularity is coarser than the kernel's. A new
process_weights_after_loading checks the w1/w3 pair and sets
l2_activation_scale from the max; it reruns on every reload. _l2_alpha
is marked weight_loader_numel = 0 so layerwise reload does not wait for
it. Passing the L2 activation scale to the kernels as a tensor is left
for later.

Kernel-facing tensors match base at cold start when the w1 and w3
global scales are equal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Aaron Hao <hao-aaron@users.noreply.github.com>
The load plan's trace treats a dtype view that changes the element size
as a copy, so packing the 1-byte block scales into int32 before the copy
made every routed weight_scale name a fallback. Both sides are now byte
views, (rows, K / 4, 4), and the copy moves the same bytes. The clone for
non-contiguous or misaligned scales is gone with it.

The routed-loader test now builds a load plan: every routed name is
direct, and planned and unplanned updates match a cold start. It replaces
the FakeRDTTensor trace and the direct-reload test.

Signed-off-by: Aaron Hao <hao-aaron@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ise reload

Layerwise reload counts the elements each loader copies to decide when a
layer is complete. Block scales are now copied through a uint8 view of
the int32 parameter, so each copy counted four times its int32 elements,
the layer was processed before its last loads arrived and those were
dropped. The scale parameters now declare their load size in bytes.

The layerwise test loads the block scales last and checks the whole
kernel layout, so an early processing fails it; it covers MXFP4 too. The
w1/w3 mismatch error names the local slot (EPLB may have moved experts),
and the meta test keeps only the post-load processing check.

Signed-off-by: Aaron Hao <hao-aaron@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The shuffled-load helper always runs post-load processing (no caller
skipped it). The layerwise test keeps the kernel layout and L2 scale
checks and drops the global-scale placement checks the load test makes;
the w1/w3 mismatch test drops its placement checks too.

Signed-off-by: Aaron Hao <hao-aaron@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- w2_weight_scale_2 gets a positive dummy value like the other scales.
- get_expert_weights builds the EPLB views in one loop.
- _pack_scale is now _expand_scale: it no longer packs into int32.
- The _create_weights docstring names w2_input_scale as not moved.
- Tests: w1 and w3 weight_scale_2 land in their own rows and the
  mismatch error names both values; the layerwise test checks
  can_load() instead of patching a private function.

Signed-off-by: Aaron Hao <hao-aaron@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…an test

Signed-off-by: Aaron Hao <hao-aaron@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An unknown shard id now raises instead of landing in the w3 row.

Signed-off-by: Aaron Hao <hao-aaron@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Aaron Hao <hao-aaron@users.noreply.github.com>
SM90 repacks MXFP4 routed shards in the loader, so those names are
fallbacks there; the new tests follow the SM90 kernel gate of the
other kernel-layout tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Aaron Hao <hao-aaron@users.noreply.github.com>
@hao-aaron
hao-aaron force-pushed the lp/l2-megamoe-nvfp4-scales branch from 60667e9 to c18b452 Compare November 16, 2026 19:56
@hao-aaron

Copy link
Copy Markdown
Collaborator Author

/ci run

@github-actions

Copy link
Copy Markdown

✅ Triggered Buildkite CI #103234 for commit 4bbb0b3fee96.

ℹ️ This PR is 3 commits behind upstream main. Merged upstream main into this branch as 4bbb0b3fee96 before starting CI. Pull the branch before pushing more changes.

@mergify mergify Bot removed the needs-rebase label Nov 16, 2026
@mergify

mergify Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @hao-aaron.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify Bot added the needs-rebase label Nov 19, 2026
Signed-off-by: Aaron Hao <hao-aaron@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mergify mergify Bot removed the needs-rebase label Nov 21, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants