Skip to content

[Spec][MegaMoE] Let the speculative draft choose its own W4A4 MXFP4 MegaMoE MMA type - #42022

Merged
kpham-sgl merged 6 commits into
sgl-project:mainfrom
nvpohanh:spec-w4a4-megamoe-draft
Oct 6, 2026
Merged

kpham-sgl merged 6 commits into
sgl-project:mainfrom
nvpohanh:spec-w4a4-megamoe-draft

Conversation

@nvpohanh

@nvpohanh nvpohanh commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

[by Claude Code]

Motivation

--enable-w4a4-mxfp4-megamoe is read process-wide, so a speculative draft whose MoE layers run through MegaMoE always uses the same MMA type as the target. For example, the DSpark head bundled in deepseek-ai/DeepSeek-V4-Pro-0813 has 3 stages x 384 MXFP4 routed experts. DSparkWorkerV2 builds and runs it under the target's megamoe A2A backend, and --speculative-moe-a2a-backend is ignored for DSpark. Users therefore cannot run the target with W4A4 (mxf4xmxf4) while keeping the draft on W4A8 (fp8xfp4), or the reverse.

Modifications

  • New tri-state server arg --speculative-enable-w4a4-mxfp4-megamoe / --no-speculative-enable-w4a4-mxfp4-megamoe (spec namespace). When unset (default None), the draft inherits --enable-w4a4-mxfp4-megamoe, so existing deployments are unchanged. This mirrors how --speculative-moe-runner-backend / --speculative-moe-a2a-backend inherit from the target.
  • draft_model_build_scope() now also applies the draft's value to exec.moe.enable_w4a4_mxfp4_megamoe while the draft is constructed. It uses the same scoped config-bag override it already applies to boundary_reduction. Every draft worker (DSpark, DFlash, EAGLE/MTP, multi-layer EAGLE, frozen-KV MTP, standalone) builds its draft inside this scope.
  • FusedMoE pins the choice at construction (_mega_moe_w4a4). _mega_moe_mma_type(experts) reads it from the layer, so weight repacking (build_mega_moe_experts_weights, Mxfp4MoEMethod), the symmetric-buffer key, and the forward kernel all use the layer's own MMA type after the build scope exits. _mega_moe_mma_type requires the layer and _get_mega_moe_symm_buffer requires mma_type, so no caller can fall back to the process-wide flag. Kimi K3's MegaMoE forward now passes its layer's MMA type to the buffer lookup as well.
  • Help text and docs row in server_arguments.mdx, including the memory cost of a draft value that differs from the target.

Mixed configurations allocate one additional DeepGEMM MegaMoE symmetric buffer, because the buffer cache is keyed by MMA type (see memory numbers below).

Accuracy Tests

Setup: 8x B300 (SM103), lmsysorg/sglang:dev-cu13 (2026-10-01) with this branch on PYTHONPATH (head b71c8568b2, base 5733bcaad3), deepseek-ai/DeepSeek-V4-Pro-0813 (HF 72e1d32). The recipe is the cookbook section 3.6 DEP8 DSpark + MegaMoE W4A4 command without the HiCache flags, which are unrelated to this change, run on B300 instead of B200:

SGLANG_OPT_DEEPGEMM_MEGA_MOE_NUM_MAX_TOKENS_PER_RANK=8320 \
python3 -m sglang.launch_server \
  --model-path deepseek-ai/DeepSeek-V4-Pro-0813 --trust-remote-code \
  --tp 8 --attn-dp-size 8 --enable-dp-lm-head --ep-size 8 \
  --moe-a2a-backend megamoe --enable-w4a4-mxfp4-megamoe \
  --enable-deepseek-v4-fp4-indexer --disable-shared-experts-fusion \
  --disable-flashinfer-autotune --mem-fraction-static 0.88 \
  --swa-full-tokens-ratio 0.02 --chunked-prefill-size 49152 \
  --speculative-algorithm DSPARK --speculative-dspark-block-size 6 \
  [--no-speculative-enable-w4a4-mxfp4-megamoe]   # arm B only

python3 -m sglang.test.few_shot_gsm8k --num-questions 1319 --parallel 128   # run twice per server
A: new flag unset (target + draft W4A4) B: --no-speculative-enable-w4a4-mxfp4-megamoe (target W4A4, draft W4A8)
Resolved speculative_enable_w4a4_mxfp4_megamoe None False
Target MoE layers (61) built with mxf4xmxf4 mxf4xmxf4
DSpark MoE stages (3) built with mxf4xmxf4 fp8xfp4
MegaMoE symmetric buffers 1 (mxf4xmxf4) 2 (mxf4xmxf4, plus fp8xfp4 with the draft's FP8 shared expert fused)
GSM8K accuracy, run 1 / run 2 (1319 q) 0.948 / 0.946 0.945 / 0.944
DSpark accept length, mean over 8 DP ranks, run 1 / run 2 4.347 / 4.342 4.349 / 4.348
GSM8K output throughput, run 2 (tok/s) 2686 2673
max_total_num_tokens 14,749,184 14,748,672
Free memory on GPU0 after draft verify graph capture 31.16 GB 23.21 GB

The MMA-type and buffer rows come from a temporary debug print in build_mega_moe_experts_weights / _get_mega_moe_symm_buffer; that print is not part of this PR. Arm A matches the pre-PR behavior: one buffer, and every MoE layer on mxf4xmxf4.

Memory note: the mixed configuration allocates a second DeepGEMM MegaMoE symmetric buffer, about 7.8 GiB per GPU at SGLANG_OPT_DEEPGEMM_MEGA_MOE_NUM_MAX_TOKENS_PER_RANK=8320 for V4-Pro. It is allocated lazily during draft CUDA-graph capture, after the KV pool is sized. KV capacity is therefore unchanged, but post-capture headroom drops by that amount, so lower --mem-fraction-static if capture runs out of memory.

Unit tests

Run in the same container on the B300 node at b71c8568b2. All pass except TestDcpCommBackendDefault::test_a5_npu_supports_dcp in test_server_args.py, which fails the same way on base 5733bcaad3 and is unrelated (NPU DCP).

  • test/registered/unit/layers/moe/test_mega_moe_deepgemm_api.py: new test_draft_layers_keep_their_own_w4a4_choice builds real FusedMoE layers inside and outside draft_model_build_scope. It fails ('mxf4xmxf4' != 'fp8xfp4') when _mega_moe_mma_type reads the process-wide flag instead of the layer's pinned value.
  • test/registered/unit/server_args/test_server_args.py: new test_speculative_w4a4_mxfp4_megamoe_is_tri_state checks unset -> None (inherit), --speculative-enable-... -> True, --no-speculative-enable-... -> False.
  • test_server_args_namespaces.py, test_runtime_context_config_bags.py, test_moe_runner_extensions.py: pass.

Benchmarking and Profiling

Not a performance change by itself. The accept-length and throughput rows above show that W4A8 for the DSpark draft does not change accept length or GSM8K accuracy on this workload. The flag lets users trade the draft's W4A4 speedup for W4A8 numerics, or the reverse, without touching the target.

Checklist

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ✅ Run #37454010899
Latest PR Test (Extra): ❌ Run #37454010504
Latest PR Test (AMD ROCm 10): ❌ Run #37454010878

--enable-w4a4-mxfp4-megamoe was read process-wide, so a draft model that
runs MegaMoE (e.g. the DSpark head of DeepSeek-V4-Pro-0813) always used the
same mxf4xmxf4 kernel as the target.

Add --[no-]speculative-enable-w4a4-mxfp4-megamoe. When unset it inherits
the target flag, so existing deployments are unchanged. draft_model_build_scope
applies the draft value while the draft is constructed, and FusedMoE pins the
choice on the layer so weight repacking, the symmetric buffer and the forward
kernel all use the layer's own MMA type after the scope exits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Oct 1, 2026
@nvpohanh
nvpohanh marked this pull request as ready for review October 1, 2026 07:31

@nvpohanh nvpohanh left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

[by Claude Code] Two inline style comments. No functional issues found in the traced draft-build, weight-repack, symmetric-buffer, and forward paths.

_mega_moe_mma_type() and _get_mega_moe_symm_buffer() no longer fall back
to the process-wide --enable-w4a4-mxfp4-megamoe: a caller that omitted
the layer or mma_type would silently get the target's MMA type for a
draft layer. Every production caller already passes it.

The --speculative-enable-w4a4-mxfp4-megamoe help text and docs row now
note that a value different from the target allocates a second MegaMoE
symmetric buffer after the KV pool is sized.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nvpohanh

nvpohanh commented Oct 1, 2026

Copy link
Copy Markdown
Collaborator Author

/rerun-test test/registered/unit/layers/moe/test_mega_moe_deepgemm_api.py test/registered/unit/server_args/test_server_args.py test/registered/unit/test_server_args_namespaces.py test/registered/e2e/models/test_deepseek_v4_flash_fp4_megamoe_b200.py test/registered/e2e/models/test_kimi_k3_b300.py test/registered/unit/spec/test_draft_construction_isolation.py test/registered/cp/test_deepseek_v4_flash_fp4_b200_cp.py

@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/unit/layers/moe/test_mega_moe_deepgemm_api.py test/registered/unit/server_args/test_server_args.py test/registered/unit/test_server_args_namespaces.py test/registered/e2e/models/test_deepseek_v4_flash_fp4_megamoe_b200.py test/registered/e2e/models/test_kimi_k3_b300.py test/registered/unit/spec/test_draft_construction_isolation.py test/registered/cp/test_deepseek_v4_flash_fp4_b200_cp.py:

🚀 ubuntu-latest (4 tests): ✅ View workflow run

cd test/ && python3 registered/unit/layers/moe/test_mega_moe_deepgemm_api.py
cd test/ && python3 registered/unit/server_args/test_server_args.py
cd test/ && python3 registered/unit/test_server_args_namespaces.py
cd test/ && python3 registered/unit/spec/test_draft_construction_isolation.py

🚀 4-gpu-b200 (2 tests): ✅ View workflow run

cd test/ && python3 registered/e2e/models/test_deepseek_v4_flash_fp4_megamoe_b200.py
cd test/ && python3 registered/cp/test_deepseek_v4_flash_fp4_b200_cp.py

🚀 8-gpu-b300 (1 test): ✅ View workflow run

cd test/ && python3 registered/e2e/models/test_kimi_k3_b300.py

@nvpohanh

nvpohanh commented Oct 1, 2026

Copy link
Copy Markdown
Collaborator Author

@Fridge003 @ch-wan could you help to review this? Thanks!

@nvpohanh

nvpohanh commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Oct 2, 2026
@nvpohanh

nvpohanh commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

1 similar comment
@nvpohanh

nvpohanh commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

@nvpohanh

nvpohanh commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator Author

All NV pipelines passed

@kpham-sgl kpham-sgl left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Three nonblocking suggestions on documentation and test coverage. Ruff lint, Ruff formatting, and isort checks pass for all nine changed Python files.

boundary_reduction=get_spec().speculative_boundary_reduction
),
_draft_w4a4_mxfp4_megamoe_override(),
):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nonblocking documentation nit: could we update draft_model_build_scope()'s docstring to describe the boundary-reduction and W4A4 overrides as well as shared-expert fusion? The scope now controls several construction-time settings; documenting what is restored on exit and what layers retain would make its contract easier to follow.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

[by Claude Code] Done in 35a5d48. The draft_model_build_scope() docstring now lists all three construction-time settings: shared-experts fusion, boundary_reduction, and enable_w4a4_mxfp4_megamoe. It says that all three return to the target's values on exit, including on error. It also says what layers built in the scope keep: boundaries keep the resolved reduction, and FusedMoE layers keep the draft's MegaMoE MMA type.

"--no-speculative-enable-w4a4-mxfp4-megamoe to keep the draft on "
"fp8xfp4 (W4A8) while the target runs W4A4. Same as "
"--enable-w4a4-mxfp4-megamoe if unset. A value different from "
"--enable-w4a4-mxfp4-megamoe allocates a second MegaMoE symmetric "

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nonblocking documentation accuracy nit: differing flag values do not necessarily allocate a second buffer. The draft must actually use MXFP4 MegaMoE; for example, a dense draft does not allocate one because of this flag. Could we qualify this sentence here and in server_arguments.mdx, e.g. "A draft using a different MXFP4 MegaMoE MMA type may allocate an additional symmetric buffer after the KV pool is sized; lower --mem-fraction-static if CUDA-graph capture runs out of memory"?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

[by Claude Code] Done in 35a5d48. The help text and the server_arguments.mdx row now read: "A draft that runs MXFP4 MegaMoE with a different MMA type from the target may allocate an additional MegaMoE symmetric buffer after the KV pool is sized; lower --mem-fraction-static if CUDA-graph capture runs out of memory."

The draft forward runs outside draft_model_build_scope, so a layer that
re-read the global flag would pair draft weights with the wrong kernel.
"""
for draft_flag, expected_draft in (

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nonblocking test coverage suggestion: could we also cover target W4A8 (enable_w4a4_mxfp4_megamoe=False) with draft W4A4 (speculative_enable_w4a4_mxfp4_megamoe=True)? The current cases exercise the negative override and inheritance with a W4A4 target, while the reverse mixed configuration is also advertised. A nested scope that raises would additionally verify that the target setting is restored on exceptional exit.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

[by Claude Code] Done in 35a5d48. test_draft_layers_keep_their_own_w4a4_choice now covers all four target/draft combinations: W4A4/W4A8, W4A4/inherit, W4A8/W4A4, and W4A8/inherit. It asserts the target flag is restored after each scope. The new test_draft_build_scope_restores_w4a4_on_exception raises inside the scope with a W4A8 target and a W4A4 draft. It checks that the flag returns to False and that a layer built afterward uses fp8xfp4. Both pass locally, along with test_server_args.py, test_server_args_namespaces.py, and test_draft_construction_isolation.py.

nvpohanh and others added 2 commits October 5, 2026 04:08
…note, and test W4A8 target with W4A4 draft

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nvpohanh

nvpohanh commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

1 similar comment
@nvpohanh

nvpohanh commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

@nvpohanh

nvpohanh commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

@nvpohanh

nvpohanh commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator Author

[by Claude Code] All NV pipelines have passed.

@kpham-sgl kpham-sgl left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Off by default so should be safe to merge

@kpham-sgl
kpham-sgl merged commit 3eec178 into sgl-project:main Oct 6, 2026
165 of 187 checks passed
cursor Bot pushed a commit to cipheraxat/sglang that referenced this pull request Oct 9, 2026
…egaMoE MMA type (sgl-project#42022)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants