Skip to content

mega_moe: route pre-dispatch through DeepGEMM + FP4 acts opt-in - #24449

Closed
pranjalssh wants to merge 2 commits into
sgl-project:dsv4-rebasefrom
pranjalssh:dsv4-rebase-fp4-acts-clean
Closed

pranjalssh wants to merge 2 commits into
sgl-project:dsv4-rebasefrom
pranjalssh:dsv4-rebase-fp4-acts-clean

Conversation

@pranjalssh

Copy link
Copy Markdown
Collaborator

Summary

  • Add two opt-in env flags forwarded to DeepGEMM via DG_USE_* env vars at first symm-buffer alloc:
    • SGLANG_OPT_DEEPGEMM_MEGA_MOE_USE_FP4_ACTS — pack the symm-buffer x slot as E2M1 nibbles instead of FP8 E4M3 (halves x-slot footprint; SF slot unchanged).
    • 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).
  • Gate the pre-dispatch implementation on the flag inside python/sglang/srt/layers/moe/mega_moe.py:
    • use_fp4_acts=True → call deep_gemm.mega_moe_pre_dispatch (handles the E2M1 packing variant).
    • default (FP8) → keep calling the existing sglang.jit_kernel.deepseek_v4.mega_moe_pre_dispatch.

Companion PR

sgl-project/DeepGEMM#27 — adds the FP4 acts + MXF4 kind support and the fused mega_moe_pre_dispatch kernel that this PR calls into. Both must land together.

How to enable (e.g. for InferenceX CI)

Set both env flags before launching sglang:

export SGLANG_OPT_DEEPGEMM_MEGA_MOE_USE_FP4_ACTS=1
export SGLANG_OPT_DEEPGEMM_MEGA_MOE_USE_MXF4_KIND=1

These are off by default. They are read once at first mega-MoE symm-buffer allocation and forwarded into DG_USE_FP4_ACTS=1 / DG_USE_MXF4_KIND=1 via os.environ.setdefault, so explicit external DG_USE_* exports still win.

The full launch invocation we use on B300x8 with DeepSeek-V4-Pro:

export SGLANG_OPT_USE_DEEPGEMM_MEGA_MOE=1
export SGLANG_OPT_DEEPGEMM_MEGA_MOE_USE_FP4_ACTS=1
export SGLANG_OPT_DEEPGEMM_MEGA_MOE_USE_MXF4_KIND=1
# ... existing mega-MoE launch flags

Validation

End-to-end on 8x 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; TTFT/ITL within run-to-run variance.
  • GSM8K accuracy parity (95.6% FP4 vs FP8 baseline within variance over 3 runs).

Test plan

  • Unit test in DeepGEMM (tests/test_mega_moe_pre_dispatch.py) — bytewise equality vs per_token_cast_to_fp{8,4} host helpers
  • Sentinel agreement test under sglang serve
  • GSM8K + 8K-input bench on 8x B300

Notes on the move from deepseek_v2.py

This PR replaces #24444 (closed) which was based on deepseek_v4. On dsv4-rebase the mega-MoE forward path was extracted out of models/deepseek_v2.py into layers/moe/mega_moe.py, so the patch lives there now.

🤖 Generated with Claude Code

Rebased onto dsv4-rebase. Mega-MoE forward lives in
`python/sglang/srt/layers/moe/mega_moe.py` on this branch, so the patch
that previously sat in `models/deepseek_v2.py` moves here.

Two changes:

1. Add two opt-in env flags forwarded to DeepGEMM via `DG_USE_*` at first
   symm-buffer alloc:
   - `SGLANG_OPT_DEEPGEMM_MEGA_MOE_USE_FP4_ACTS` — pack the symm-buffer x
     slot as E2M1 nibbles instead of FP8 E4M3 (halves x-slot footprint;
     SF slot unchanged).
   - `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 via `setdefault` so explicit external `DG_USE_*`
   exports still win.

2. Gate the pre-dispatch implementation on the flag:
   - `use_fp4_acts=True` → call `deep_gemm.mega_moe_pre_dispatch` (handles
     the E2M1 packing variant).
   - default → call the existing
     `sglang.jit_kernel.deepseek_v4.mega_moe_pre_dispatch` (FP8 only).

Companion DeepGEMM PR: sgl-project/DeepGEMM#27 — adds the FP4 acts +
MXF4 kind support and the fused `mega_moe_pre_dispatch` kernel that this
PR calls into.

Validated end-to-end on 8x 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.
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

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
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