Skip to content

Add NVFP4 mega MoE support - #80

Merged
Fridge003 merged 2 commits into
sgl-project:devfrom
zcnrex:nvfp4-release
Aug 30, 2026
Merged

Fridge003 merged 2 commits into
sgl-project:devfrom
zcnrex:nvfp4-release

Conversation

@zcnrex

@zcnrex zcnrex commented Aug 29, 2026

Copy link
Copy Markdown
Collaborator

Added

nvfp4xnvfp4 MMA kind. A third kind alongside fp8xfp4 and mxf4xmxf4. 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:

  • per-token FP32 input scales applied to the L1 (fc13) accumulator
  • per-expert l1_alphas (gate, up) and l2_alphas weight global scales
  • per-expert l2_act_scales for the fc2 input

NVFP4 mode in mega_moe_pre_dispatch. Takes mma_type plus optional buf_x_scales / expert_scales, replacing the use_fp4_acts boolean.

get_block_m_for_mega_moe exported to Python. Callers laying out shared-expert activation SFs need the kernel's chosen BLOCK_M; only the raw tvm-ffi symbol existed, so deep_gemm.get_block_m_for_mega_moe raised AttributeError.

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_KIND environment switches, which selected the same behaviour without the buffer agreeing. One combination they could express is no longer selectable: FP4 activations on the mxf8f6f4 kind — same numerics, different instruction path, covered by mxf4xmxf4.

Buffer and block sizing are byte-denominated. MegaMoEBuffer sizes activation slots from get_element_bits(mma_kind) instead of a per-kind flag, and the block heuristic scales BLOCK_K so BLOCK_K_BYTES is kind-independent. kSwizzleAMode is a fixed 128 B atom and tma::copy tiles BLOCK_K_BYTES / 128 atoms per row, which removes the min(block_k, 128) clamp and the use_mxf4_kind block-config special case.

SM100 FP8/FP4 kernel rebased onto the upstream SharedStorage implementation. kUseFullPoolFP8FP4Path / kRingCoversFullPool and DG_USE_FP8_COMBINE are reimplemented on top of it; both are verified below. DG_USE_FP8_COMBINE remains off by default.

Tests

All on 8×B300, 2 ranks, sgl_deep_gemm/tests unless noted.

test result
test_mega_moe_nvfp4_alphas.py pow2 global scales cancel bitwise; realistic rel-L2 0.18253 (tol 0.30); l1_alphas/l2_alphas/l2_act_scales roll-by-one controls all have teeth
same, DG_USE_FP8_COMBINE=1 rel-L2 0.18627
mxf4xmxf4 vs fp8xfp4 control, identical inputs rel-L2 0.211
nvfp4xnvfp4 vs fp8xfp4 control, identical inputs rel-L2 0.467
activations swiglu / swigluoai / situ all finite and non-zero, each differs from swiglu
same, with 1 shared expert all pass; |y| 1859 → 2175
test_mega_moe_pre_dispatch.py --dtype both FP8 and FP4 bytewise equal vs host helper, pad-fill correct
test_attention.py (DG_MQA_NUM_CASES=24) pass, both prefill and paged MQA sections
block regimes normal (BLOCK_M=192) and small (BLOCK_M=16, BLOCK_K_BYTES=256) pass for all three kinds

Full-pool path equivalence. kUseFullPoolFP8FP4Path is a synchronization change, so output must be unchanged. Dumped y for fp8xfp4 under 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):

rank0: bitwise_equal=True  maxdiff=0
rank1: bitwise_equal=True  maxdiff=0

Same at 1024 tokens/rank, where the ring wraps and the path is off.

Bench

fp8xfp4 prefill, 8192 tokens/rank, hidden 7168, intermediate 3072, 384 experts, top-6, 2 ranks. Both builds measured in the same session:

EP 0 EP 1
before 2569 TFLOPS 2541 TFLOPS
after 2570 TFLOPS 2547 TFLOPS

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.py follows 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 — every sm90, sm120, mqa_logits, attention and w4a4 file is byte-identical to dev.

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]>
@Fridge003

Copy link
Copy Markdown
Collaborator

@Fridge003
Fridge003 merged commit b2aece8 into sgl-project:dev Aug 30, 2026
Fridge003 added a commit that referenced this pull request Aug 31, 2026
Fridge003 added a commit that referenced this pull request Aug 31, 2026
Fridge003 pushed a commit that referenced this pull request Aug 31, 2026
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.

2 participants