Skip to content

Add FP4 acts + MXF4 kind support and fused mega_moe_pre_dispatch kernel - #27

Merged
Fridge003 merged 1 commit into
sgl-project:dev-0426from
pranjalssh:fp4-mega-moe-pre-dispatch
May 6, 2026
Merged

Fridge003 merged 1 commit into
sgl-project:dev-0426from
pranjalssh:fp4-mega-moe-pre-dispatch

Conversation

@pranjalssh

Copy link
Copy Markdown

Summary

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

  • Env-gated FP4 acts (DG_USE_FP4_ACTS=1): halves the symmetric x slot in the symm buffer (E2M1 nibbles vs E4M3 bytes). SF slot is unchanged — still hidden/32 UE8M0 bytes since gran_k=32 for both FP4 and FP8 under kind::mxf8f6f4.
  • Env-gated MXF4 kind (DG_USE_MXF4_KIND=1): 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.
  • cumulative_local_expert_recv_stats is threaded 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 static-assert.
  • Multi-block_m support via kCandidateBlockM = {8,16,32,64,96,128,192} + LCM-aligned pool padding. Replaces the static block_m=192 heuristic 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_dispatch kernel (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 the per_token_cast_to_fp4 host helper.

New Python entrypoint:

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=32, use_fp4_acts=False,
)

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_arrive helper (used twice in sm100_fp8_fp4_mega_moe.cuh).
  • cvt_pack_f32_to_e2m1x2 / cvt_pack_f32x4_to_e2m1x4 PTX wrappers in common/math.cuh.
  • SM100_MMA_MXF4_2x1SM_SS 2-CTA cluster MMA wrapper in ptx/tcgen05.cuh.
  • red_add(int*, int) overload for the cumulative_local_expert_recv_stats counter.
  • st.L1::no_allocate.relaxed.sys.global.u64 — explicit .global state 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:

  • 8K-input bench (concurrency 2048): FP4 acts + MXF4 kind path produces matching tokens vs the FP8 baseline (rel-RMSE ≤ 0.5 sentinel agreement).
  • GSM8K accuracy parity within run-to-run variance.
  • Unit test 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:

export DG_USE_FP4_ACTS=1     # halve symm-buffer x-slot footprint
export DG_USE_MXF4_KIND=1    # switch mainloops to kind::mxf4 (2-CTA)

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

  • Build under develop.sh on B300 (CUDA 13, sm_100a)
  • tests/test_mega_moe_pre_dispatch.py (single-GPU, FP8 + FP4)
  • sentinel test in production sglang (FP4 acts vs FP8 acts agreement)
  • GSM8K + 8K-input bench on 8× B300 with DeepSeek-V4-Pro

🤖 Generated with Claude Code

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).
@Fridge003
Fridge003 merged commit bca278e into sgl-project:dev-0426 May 6, 2026
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
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)
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)
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.

3 participants