Repository navigation
Add FP4 acts + MXF4 kind support and fused mega_moe_pre_dispatch kernel - #27
Merged
Fridge003 merged 1 commit intoMay 6, 2026
Merged
Conversation
Two related additions for the DeepSeek-V4-Pro mega-MoE path:
1. **FP4 (E2M1) activations + `kind::mxf4` mainloop opt-in** for `fp8_fp4_mega_moe`.
- `DG_USE_FP4_ACTS=1` halves the symm-buffer x-slot footprint (E2M1 nibbles
vs E4M3 bytes); SF slot unchanged (still `hidden/32` UE8M0 bytes under
gran_k=32).
- `use_mxf4_kind=true` switches the L1+L2 mainloops to `cta_group::2 kind::mxf4`
(2-CTA cluster) with dense FP4 smem layout (`_ALIGN8B`, 2 nibbles/byte).
Per-stage A/B byte footprint halves → num_stages doubles for the same
smem budget.
- Threads `cumulative_local_expert_recv_stats` through the public mega-MoE
API for per-rank expert counters used by sglang's expert-distribution
recorder.
- Block-m heuristic: under `use_mxf4_kind`, bumps `block_m=16 → 32` for the
smallest-tokens-per-expert bucket so `load_block_m * block_k / 2` meets
the 1024-byte smem alignment.
- Multi-block_m support via `kCandidateBlockM` array + LCM-aligned pool
padding; replaces the static `block_m=192` heuristic with token-density
dispatch (8/16/32/64/96/128/192).
2. **`mega_moe_pre_dispatch` kernel**: BF16 → quant + topk-copy + pad-fill in
one launch, gated on `kUseFp4Acts` + `kUsePDL`. Templated on
`(kGroupSize, kUseFp4Acts, kUsePDL)`. Uses bucketize-style E2M1 encoder for
byte-exact match against the `per_token_cast_to_fp4` host helper.
- New: `deep_gemm.mega_moe_pre_dispatch(x, topk_idx, topk_weights, buf_x,
buf_x_sf, buf_topk_idx, buf_topk_weights, num_tokens, group_size, use_fp4_acts)`
- Test: `tests/test_mega_moe_pre_dispatch.py` — single-GPU bytewise check
against host `per_token_cast_to_fp{8,4}` + pad-fill assertion.
Validated end-to-end on 8× B300 with DeepSeek-V4-Pro at 8K input bench:
- FP4 acts + MXF4 kind path produces matching tokens vs the FP8 baseline
(rel-RMSE ≤ 0.5 sentinel; GSM8K accuracy parity within run-to-run variance).
PR also includes existing FP4-mega-MoE supporting changes that are required
by the kernel:
- `cluster_sync_with_relaxed_arrive` helper (used twice in `sm100_fp8_fp4_mega_moe.cuh`).
- `cvt_pack_f32_to_e2m1x2` / `cvt_pack_f32x4_to_e2m1x4` PTX wrappers.
- `SM100_MMA_MXF4_2x1SM_SS` 2-CTA cluster MMA wrapper.
- Generalized `red_add(int*, int)` for the `cumulative_local_expert_recv_stats`
counter.
- `st.L1::no_allocate.relaxed.sys.global.u64` (correctness fix: previous
generic-address variant could miss the global state space).
pranjalssh
pushed a commit
to pranjalssh/sglang
that referenced
this pull request
May 5, 2026
…ts opt-in
Two related changes for the FP4 mega-MoE path:
1. Swap the `sglang.jit_kernel.deepseek_v4.mega_moe_pre_dispatch` call for
the new `deep_gemm.mega_moe_pre_dispatch` kernel (BF16→quant + topk-copy
+ pad-fill, fused into one launch). The DeepGEMM path additionally
handles the FP4 (E2M1) packing variant; the sgl-kernel one only emits
FP8.
2. Add two opt-in env flags that forward into DeepGEMM via `DG_USE_*` env
vars (read by DeepGEMM at host-function call time):
- `SGLANG_OPT_DEEPGEMM_MEGA_MOE_USE_FP4_ACTS` — pack symm-buffer x slot
as E2M1 nibbles instead of FP8 E4M3. Halves x-slot footprint; SF slot
unchanged (still UE8M0 at gran_k=32).
- `SGLANG_OPT_DEEPGEMM_MEGA_MOE_USE_MXF4_KIND` — switch DeepGEMM's L1+L2
mainloops to `kind::mxf4` (2-CTA cluster, dense FP4 smem). Requires the
FP4-acts flag (DeepGEMM asserts host-side).
Forwarding happens once at first symm-buffer alloc via `setdefault`, so
explicit external `DG_USE_*` exports still win.
The non-fused fallback branch is also extended to handle FP4 acts via
`deep_gemm.utils.per_token_cast_to_fp4` so a launcher can disable
`SGLANG_OPT_MEGA_MOE_FUSED_PRE_DISPATCH` and still exercise the FP4 path.
Validated end-to-end on 8× B300 with DeepSeek-V4-Pro:
- Sentinel test (FP4 acts vs FP8 acts agreement): rel-RMSE ≤ 0.5 across
layers.
- 8K-input bench (concurrency 2048): tokens match the FP8 baseline.
- GSM8K accuracy parity within run-to-run variance.
Companion PR: sgl-project/DeepGEMM#27 (FP4 acts + MXF4 kind support and
fused mega_moe_pre_dispatch kernel).
3 tasks done
pranjalssh
pushed a commit
to pranjalssh/sglang
that referenced
this pull request
May 6, 2026
Forwards `DG_USE_FP8_COMBINE=1` to DeepGEMM so the L2 epilogue ships FP8 E4M3 + per-(token, N=128) UE8M0 SF over NVLink instead of BF16. Halves NVLink bytes/token on the second a2a. Independent of `DG_USE_FP4_ACTS` / `DG_USE_MXF4_KIND` (those control the dispatch a2a + mainloops). Combinable: enable all three for the maximum bandwidth savings. Companion DeepGEMM PR: sgl-project/DeepGEMM#27 (commit ports the FP8 combine path into `sm100_fp8_fp4_mega_moe.cuh`). Validated on 8x B300, DeepSeek-V4-Pro, b=8192 input, 1024 output: b=512: 91.9 s (FP8 acts) -> 78.4 s (FP4+MXF4+FP8combine) — +17.3% b=2048: 259.4 s -> 238.2 s — +8.9% b=4096: 489.5 s -> 444.2 s — +10.2% Sentinel + GSM8K parity preserved. How to enable (combined with the existing FP4 stack): export SGLANG_OPT_DEEPGEMM_MEGA_MOE_USE_FP4_ACTS=1 export SGLANG_OPT_DEEPGEMM_MEGA_MOE_USE_MXF4_KIND=1 export SGLANG_OPT_DEEPGEMM_MEGA_MOE_USE_FP8_COMBINE=1
5 tasks done
Fridge003
pushed a commit
that referenced
this pull request
May 12, 2026
…el (#27) Two related additions for the DeepSeek-V4-Pro mega-MoE path: 1. **FP4 (E2M1) activations + `kind::mxf4` mainloop opt-in** for `fp8_fp4_mega_moe`. - `DG_USE_FP4_ACTS=1` halves the symm-buffer x-slot footprint (E2M1 nibbles vs E4M3 bytes); SF slot unchanged (still `hidden/32` UE8M0 bytes under gran_k=32). - `use_mxf4_kind=true` switches the L1+L2 mainloops to `cta_group::2 kind::mxf4` (2-CTA cluster) with dense FP4 smem layout (`_ALIGN8B`, 2 nibbles/byte). Per-stage A/B byte footprint halves → num_stages doubles for the same smem budget. - Threads `cumulative_local_expert_recv_stats` through the public mega-MoE API for per-rank expert counters used by sglang's expert-distribution recorder. - Block-m heuristic: under `use_mxf4_kind`, bumps `block_m=16 → 32` for the smallest-tokens-per-expert bucket so `load_block_m * block_k / 2` meets the 1024-byte smem alignment. - Multi-block_m support via `kCandidateBlockM` array + LCM-aligned pool padding; replaces the static `block_m=192` heuristic with token-density dispatch (8/16/32/64/96/128/192). 2. **`mega_moe_pre_dispatch` kernel**: BF16 → quant + topk-copy + pad-fill in one launch, gated on `kUseFp4Acts` + `kUsePDL`. Templated on `(kGroupSize, kUseFp4Acts, kUsePDL)`. Uses bucketize-style E2M1 encoder for byte-exact match against the `per_token_cast_to_fp4` host helper. - New: `deep_gemm.mega_moe_pre_dispatch(x, topk_idx, topk_weights, buf_x, buf_x_sf, buf_topk_idx, buf_topk_weights, num_tokens, group_size, use_fp4_acts)` - Test: `tests/test_mega_moe_pre_dispatch.py` — single-GPU bytewise check against host `per_token_cast_to_fp{8,4}` + pad-fill assertion. Validated end-to-end on 8× B300 with DeepSeek-V4-Pro at 8K input bench: - FP4 acts + MXF4 kind path produces matching tokens vs the FP8 baseline (rel-RMSE ≤ 0.5 sentinel; GSM8K accuracy parity within run-to-run variance). PR also includes existing FP4-mega-MoE supporting changes that are required by the kernel: - `cluster_sync_with_relaxed_arrive` helper (used twice in `sm100_fp8_fp4_mega_moe.cuh`). - `cvt_pack_f32_to_e2m1x2` / `cvt_pack_f32x4_to_e2m1x4` PTX wrappers. - `SM100_MMA_MXF4_2x1SM_SS` 2-CTA cluster MMA wrapper. - Generalized `red_add(int*, int)` for the `cumulative_local_expert_recv_stats` counter. - `st.L1::no_allocate.relaxed.sys.global.u64` (correctness fix: previous generic-address variant could miss the global state space). Co-authored-by: pranjalssh <[email protected]> (cherry picked from commit bca278e)
This was referenced Jun 15, 2026
Fridge003
pushed a commit
that referenced
this pull request
Jun 27, 2026
…el (#27) Two related additions for the DeepSeek-V4-Pro mega-MoE path: 1. **FP4 (E2M1) activations + `kind::mxf4` mainloop opt-in** for `fp8_fp4_mega_moe`. - `DG_USE_FP4_ACTS=1` halves the symm-buffer x-slot footprint (E2M1 nibbles vs E4M3 bytes); SF slot unchanged (still `hidden/32` UE8M0 bytes under gran_k=32). - `use_mxf4_kind=true` switches the L1+L2 mainloops to `cta_group::2 kind::mxf4` (2-CTA cluster) with dense FP4 smem layout (`_ALIGN8B`, 2 nibbles/byte). Per-stage A/B byte footprint halves → num_stages doubles for the same smem budget. - Threads `cumulative_local_expert_recv_stats` through the public mega-MoE API for per-rank expert counters used by sglang's expert-distribution recorder. - Block-m heuristic: under `use_mxf4_kind`, bumps `block_m=16 → 32` for the smallest-tokens-per-expert bucket so `load_block_m * block_k / 2` meets the 1024-byte smem alignment. - Multi-block_m support via `kCandidateBlockM` array + LCM-aligned pool padding; replaces the static `block_m=192` heuristic with token-density dispatch (8/16/32/64/96/128/192). 2. **`mega_moe_pre_dispatch` kernel**: BF16 → quant + topk-copy + pad-fill in one launch, gated on `kUseFp4Acts` + `kUsePDL`. Templated on `(kGroupSize, kUseFp4Acts, kUsePDL)`. Uses bucketize-style E2M1 encoder for byte-exact match against the `per_token_cast_to_fp4` host helper. - New: `deep_gemm.mega_moe_pre_dispatch(x, topk_idx, topk_weights, buf_x, buf_x_sf, buf_topk_idx, buf_topk_weights, num_tokens, group_size, use_fp4_acts)` - Test: `tests/test_mega_moe_pre_dispatch.py` — single-GPU bytewise check against host `per_token_cast_to_fp{8,4}` + pad-fill assertion. Validated end-to-end on 8× B300 with DeepSeek-V4-Pro at 8K input bench: - FP4 acts + MXF4 kind path produces matching tokens vs the FP8 baseline (rel-RMSE ≤ 0.5 sentinel; GSM8K accuracy parity within run-to-run variance). PR also includes existing FP4-mega-MoE supporting changes that are required by the kernel: - `cluster_sync_with_relaxed_arrive` helper (used twice in `sm100_fp8_fp4_mega_moe.cuh`). - `cvt_pack_f32_to_e2m1x2` / `cvt_pack_f32x4_to_e2m1x4` PTX wrappers. - `SM100_MMA_MXF4_2x1SM_SS` 2-CTA cluster MMA wrapper. - Generalized `red_add(int*, int)` for the `cumulative_local_expert_recv_stats` counter. - `st.L1::no_allocate.relaxed.sys.global.u64` (correctness fix: previous generic-address variant could miss the global state space). Co-authored-by: pranjalssh <[email protected]> (cherry picked from commit bca278e)
zcnrex
pushed a commit
to zcnrex/DeepGEMM
that referenced
this pull request
Aug 31, 2026
…el (sgl-project#27) Two related additions for the DeepSeek-V4-Pro mega-MoE path: 1. **FP4 (E2M1) activations + `kind::mxf4` mainloop opt-in** for `fp8_fp4_mega_moe`. - `DG_USE_FP4_ACTS=1` halves the symm-buffer x-slot footprint (E2M1 nibbles vs E4M3 bytes); SF slot unchanged (still `hidden/32` UE8M0 bytes under gran_k=32). - `use_mxf4_kind=true` switches the L1+L2 mainloops to `cta_group::2 kind::mxf4` (2-CTA cluster) with dense FP4 smem layout (`_ALIGN8B`, 2 nibbles/byte). Per-stage A/B byte footprint halves → num_stages doubles for the same smem budget. - Threads `cumulative_local_expert_recv_stats` through the public mega-MoE API for per-rank expert counters used by sglang's expert-distribution recorder. - Block-m heuristic: under `use_mxf4_kind`, bumps `block_m=16 → 32` for the smallest-tokens-per-expert bucket so `load_block_m * block_k / 2` meets the 1024-byte smem alignment. - Multi-block_m support via `kCandidateBlockM` array + LCM-aligned pool padding; replaces the static `block_m=192` heuristic with token-density dispatch (8/16/32/64/96/128/192). 2. **`mega_moe_pre_dispatch` kernel**: BF16 → quant + topk-copy + pad-fill in one launch, gated on `kUseFp4Acts` + `kUsePDL`. Templated on `(kGroupSize, kUseFp4Acts, kUsePDL)`. Uses bucketize-style E2M1 encoder for byte-exact match against the `per_token_cast_to_fp4` host helper. - New: `deep_gemm.mega_moe_pre_dispatch(x, topk_idx, topk_weights, buf_x, buf_x_sf, buf_topk_idx, buf_topk_weights, num_tokens, group_size, use_fp4_acts)` - Test: `tests/test_mega_moe_pre_dispatch.py` — single-GPU bytewise check against host `per_token_cast_to_fp{8,4}` + pad-fill assertion. Validated end-to-end on 8× B300 with DeepSeek-V4-Pro at 8K input bench: - FP4 acts + MXF4 kind path produces matching tokens vs the FP8 baseline (rel-RMSE ≤ 0.5 sentinel; GSM8K accuracy parity within run-to-run variance). PR also includes existing FP4-mega-MoE supporting changes that are required by the kernel: - `cluster_sync_with_relaxed_arrive` helper (used twice in `sm100_fp8_fp4_mega_moe.cuh`). - `cvt_pack_f32_to_e2m1x2` / `cvt_pack_f32x4_to_e2m1x4` PTX wrappers. - `SM100_MMA_MXF4_2x1SM_SS` 2-CTA cluster MMA wrapper. - Generalized `red_add(int*, int)` for the `cumulative_local_expert_recv_stats` counter. - `st.L1::no_allocate.relaxed.sys.global.u64` (correctness fix: previous generic-address variant could miss the global state space). Co-authored-by: pranjalssh <[email protected]> (cherry picked from commit bca278e)
Helicoplacoidea
pushed a commit
to Helicoplacoidea/DeepGEMM
that referenced
this pull request
Aug 31, 2026
…el (sgl-project#27) Two related additions for the DeepSeek-V4-Pro mega-MoE path: 1. **FP4 (E2M1) activations + `kind::mxf4` mainloop opt-in** for `fp8_fp4_mega_moe`. - `DG_USE_FP4_ACTS=1` halves the symm-buffer x-slot footprint (E2M1 nibbles vs E4M3 bytes); SF slot unchanged (still `hidden/32` UE8M0 bytes under gran_k=32). - `use_mxf4_kind=true` switches the L1+L2 mainloops to `cta_group::2 kind::mxf4` (2-CTA cluster) with dense FP4 smem layout (`_ALIGN8B`, 2 nibbles/byte). Per-stage A/B byte footprint halves → num_stages doubles for the same smem budget. - Threads `cumulative_local_expert_recv_stats` through the public mega-MoE API for per-rank expert counters used by sglang's expert-distribution recorder. - Block-m heuristic: under `use_mxf4_kind`, bumps `block_m=16 → 32` for the smallest-tokens-per-expert bucket so `load_block_m * block_k / 2` meets the 1024-byte smem alignment. - Multi-block_m support via `kCandidateBlockM` array + LCM-aligned pool padding; replaces the static `block_m=192` heuristic with token-density dispatch (8/16/32/64/96/128/192). 2. **`mega_moe_pre_dispatch` kernel**: BF16 → quant + topk-copy + pad-fill in one launch, gated on `kUseFp4Acts` + `kUsePDL`. Templated on `(kGroupSize, kUseFp4Acts, kUsePDL)`. Uses bucketize-style E2M1 encoder for byte-exact match against the `per_token_cast_to_fp4` host helper. - New: `deep_gemm.mega_moe_pre_dispatch(x, topk_idx, topk_weights, buf_x, buf_x_sf, buf_topk_idx, buf_topk_weights, num_tokens, group_size, use_fp4_acts)` - Test: `tests/test_mega_moe_pre_dispatch.py` — single-GPU bytewise check against host `per_token_cast_to_fp{8,4}` + pad-fill assertion. Validated end-to-end on 8× B300 with DeepSeek-V4-Pro at 8K input bench: - FP4 acts + MXF4 kind path produces matching tokens vs the FP8 baseline (rel-RMSE ≤ 0.5 sentinel; GSM8K accuracy parity within run-to-run variance). PR also includes existing FP4-mega-MoE supporting changes that are required by the kernel: - `cluster_sync_with_relaxed_arrive` helper (used twice in `sm100_fp8_fp4_mega_moe.cuh`). - `cvt_pack_f32_to_e2m1x2` / `cvt_pack_f32x4_to_e2m1x4` PTX wrappers. - `SM100_MMA_MXF4_2x1SM_SS` 2-CTA cluster MMA wrapper. - Generalized `red_add(int*, int)` for the `cumulative_local_expert_recv_stats` counter. - `st.L1::no_allocate.relaxed.sys.global.u64` (correctness fix: previous generic-address variant could miss the global state space). Co-authored-by: pranjalssh <[email protected]> (cherry picked from commit bca278e)
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.
Summary
Two related additions for the DeepSeek-V4-Pro mega-MoE path:
1. FP4 (E2M1) activations +
kind::mxf4mainloop opt-in forfp8_fp4_mega_moeDG_USE_FP4_ACTS=1): halves the symmetricxslot in the symm buffer (E2M1 nibbles vs E4M3 bytes). SF slot is unchanged — stillhidden/32UE8M0 bytes sincegran_k=32for both FP4 and FP8 underkind::mxf8f6f4.DG_USE_MXF4_KIND=1): switches the L1 + L2 mainloops tocta_group::2 kind::mxf4(2-CTA cluster) with dense FP4 smem layout (_ALIGN8B, 2 nibbles/byte). Per-stage A/B byte footprint halves → num_stages doubles for the same smem budget.cumulative_local_expert_recv_statsis threaded through the public mega-MoE API for per-rank expert counters used by sglang's expert-distribution recorder.use_mxf4_kind, bumpsblock_m=16 → 32for the smallest-tokens-per-expert bucket soload_block_m * block_k / 2meets the 1024-byte smem alignment static-assert.kCandidateBlockM = {8,16,32,64,96,128,192}+ LCM-aligned pool padding. Replaces the staticblock_m=192heuristic with token-density dispatch (smaller blocks for low-tpe routes; production-validated on prefill 192-tpe and decode 8-tpe regimes).2.
mega_moe_pre_dispatchkernel (new)BF16 → quant + topk-copy + pad-fill in one launch, gated on
kUseFp4Acts+kUsePDL. Templated on(kGroupSize, kUseFp4Acts, kUsePDL). The FP4 path uses a bucketize-style E2M1 encoder so the GPU output is byte-exact vs theper_token_cast_to_fp4host helper.New Python entrypoint:
New test:
tests/test_mega_moe_pre_dispatch.py— single-GPU bytewise equality vs host helpers (FP8 + FP4) plus pad-fill correctness check.Supporting changes (required by the kernels above)
cluster_sync_with_relaxed_arrivehelper (used twice insm100_fp8_fp4_mega_moe.cuh).cvt_pack_f32_to_e2m1x2/cvt_pack_f32x4_to_e2m1x4PTX wrappers incommon/math.cuh.SM100_MMA_MXF4_2x1SM_SS2-CTA cluster MMA wrapper inptx/tcgen05.cuh.red_add(int*, int)overload for thecumulative_local_expert_recv_statscounter.st.L1::no_allocate.relaxed.sys.global.u64— explicit.globalstate space (correctness fix; the previous generic-address variant could miss the global state space).Validation
End-to-end on 8× B300 with DeepSeek-V4-Pro:
tests/test_mega_moe_pre_dispatch.py: PASS for both FP8 and FP4 paths (M=512, P=576, H=1024, K=8, G=32).How to use
The two new env flags are off by default; opt in via:
The fused pre-dispatch kernel is unconditional once the symm buffer is allocated; the sglang side calls
deep_gemm.mega_moe_pre_dispatch(...)instead of the prior sgl-kernel pre-dispatch path.Test plan
develop.shon B300 (CUDA 13, sm_100a)tests/test_mega_moe_pre_dispatch.py(single-GPU, FP8 + FP4)🤖 Generated with Claude Code