Repository navigation
Add NVFP4 mega MoE support - #80
Merged
Merged
Conversation
NVFP4 becomes a third MMA kind (`nvfp4xnvfp4`) alongside `fp8xfp4` and `mxf4xmxf4`: it reuses the dense FP4 operand layout and changes only the block-scale grid — UE4M3 per 16 elements instead of UE8M0 per 32. The kernel's MMA kind now comes from the symmetric buffer's `mma_type` rather than from the recipe's K granularity. The buffer's activation slots are sized for the kind it was allocated with, so deriving the kind anywhere else lets the two disagree; the recipe is cross-checked against it instead. Unimplemented and mismatched combinations fail by name instead of tripping an opaque byte-count assertion. This replaces the `DG_USE_FP4_ACTS` / `DG_USE_MXF4_KIND` environment switches, which selected the same behaviour without the buffer agreeing. NVFP4 also carries the outer scales its dynamic range needs: per-token FP32 input scales on the L1 (fc13) accumulator, per-expert `l1_alphas` / `l2_alphas` weight global scales, and per-expert `l2_act_scales` for the fc2 input. `mega_moe_pre_dispatch` gains a matching per-token NVFP4 acts mode, taking `mma_type` plus optional `buf_x_scales` / `expert_scales`. `get_block_m_for_mega_moe` is exported to Python — callers laying out shared-expert activation SFs need the kernel's chosen `BLOCK_M`. Co-Authored-By: Claude Opus 5 <[email protected]>
Collaborator
|
Please register the newly added tests to https://github.com/zcnrex/DeepGEMM/blob/328a35cd2ddfb31910eaa5a7e93281f5ed136862/sgl_deep_gemm/run_tests.sh, so they can be covered at release workflow |
Fridge003
added a commit
that referenced
this pull request
Aug 31, 2026
Fridge003
added a commit
that referenced
this pull request
Aug 31, 2026
Co-authored-by: zcnrex <[email protected]>
Fridge003
pushed a commit
that referenced
this pull request
Aug 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Added
nvfp4xnvfp4MMA kind. A third kind alongsidefp8xfp4andmxf4xmxf4. Reuses the dense FP4 operand layout and changes only the block-scale grid — UE4M3 per 16 elements instead of UE8M0 per 32.NVFP4 outer scales. The dynamic range needs scales the other kinds don't carry:
l1_alphas(gate, up) andl2_alphasweight global scalesl2_act_scalesfor the fc2 inputNVFP4 mode in
mega_moe_pre_dispatch. Takesmma_typeplus optionalbuf_x_scales/expert_scales, replacing theuse_fp4_actsboolean.get_block_m_for_mega_moeexported to Python. Callers laying out shared-expert activation SFs need the kernel's chosenBLOCK_M; only the raw tvm-ffi symbol existed, sodeep_gemm.get_block_m_for_mega_moeraisedAttributeError.Refactored
MMA kind now comes from the symmetric buffer's
mma_type, not the recipe's K granularity. The buffer's activation slots are sized for the kind it was allocated with, so deriving the kind anywhere else lets the two disagree; the recipe is cross-checked against it instead. Unimplemented and mismatched combinations fail by name rather than tripping an opaque byte-count assertion.This replaces the
DG_USE_FP4_ACTS/DG_USE_MXF4_KINDenvironment switches, which selected the same behaviour without the buffer agreeing. One combination they could express is no longer selectable: FP4 activations on themxf8f6f4kind — same numerics, different instruction path, covered bymxf4xmxf4.Buffer and block sizing are byte-denominated.
MegaMoEBuffersizes activation slots fromget_element_bits(mma_kind)instead of a per-kind flag, and the block heuristic scalesBLOCK_KsoBLOCK_K_BYTESis kind-independent.kSwizzleAModeis a fixed 128 B atom andtma::copytilesBLOCK_K_BYTES / 128atoms per row, which removes themin(block_k, 128)clamp and theuse_mxf4_kindblock-config special case.SM100 FP8/FP4 kernel rebased onto the upstream SharedStorage implementation.
kUseFullPoolFP8FP4Path/kRingCoversFullPoolandDG_USE_FP8_COMBINEare reimplemented on top of it; both are verified below.DG_USE_FP8_COMBINEremains off by default.Tests
All on 8×B300, 2 ranks,
sgl_deep_gemm/testsunless noted.test_mega_moe_nvfp4_alphas.pyl1_alphas/l2_alphas/l2_act_scalesroll-by-one controls all have teethDG_USE_FP8_COMBINE=1mxf4xmxf4vsfp8xfp4control, identical inputsnvfp4xnvfp4vsfp8xfp4control, identical inputsswiglu/swigluoai/situswiglu|y|1859 → 2175test_mega_moe_pre_dispatch.py --dtype bothtest_attention.py(DG_MQA_NUM_CASES=24)BLOCK_M=192) and small (BLOCK_M=16,BLOCK_K_BYTES=256) pass for all three kindsFull-pool path equivalence.
kUseFullPoolFP8FP4Pathis a synchronization change, so output must be unchanged. Dumpedyforfp8xfp4under both implementations at the shape where the gate arms (BLOCK_K == BLOCK_N == 128, ring == pool == 138240 tokens, no shared experts,L2_SHAPE_K/BLOCK_K = 24 ≤ 32):Same at 1024 tokens/rank, where the ring wraps and the path is off.
Bench
fp8xfp4prefill, 8192 tokens/rank, hidden 7168, intermediate 3072, 384 experts, top-6, 2 ranks. Both builds measured in the same session:Unchanged, as intended — the full-pool fast path was confirmed armed in the benched kernel, and showed no measurable gain at 2 ranks.
Not covered
tests/test_mega_moe.pyfollows the upstream version and does not carry shared-expert arms; that coverage lives in the probes above. SM90 and SM120 paths are untouched by this change and were not re-run — everysm90,sm120,mqa_logits,attentionandw4a4file is byte-identical todev.