Skip to content

[SM100] Mega MoE: support shared experts with blockwise-128 FP8 SFs - #171

Merged
zcnrex merged 1 commit into
sgl-project:devfrom
yz-wqf:qfw/mega-moe-blockwise-128-shared
Nov 28, 2026
Merged

zcnrex merged 1 commit into
sgl-project:devfrom
yz-wqf:qfw/mega-moe-blockwise-128-shared

Conversation

@yz-wqf

@yz-wqf yz-wqf commented Nov 26, 2026

Copy link
Copy Markdown

Motivation

The blockwise-128 FP8 recipe (recipe=(1, 1, 128): 1x128 activation and 128x128 weight SFs, #150) rejects shared experts in both host entry points (get_symm_buffer_size_for_mega_moe and fp8_fp4_mega_moe). Models with a shared expert therefore run it as separate GEMMs next to MegaMoE when their routed experts use 128x128 block scales.

The kernel already handles shared tiles on the blockwise-128 path the same way as routed ones:

  • The L1 epilogue's blockwise branch (kL2AmaxPer128) merges the 1x128 amax across the 2-CTA cluster. It stores one SF byte per group, into the shared L2 SF buffer for shared tasks (shared_l2_sf_buffer, kNumSharedSFTokens stride).
  • The activation and weight loaders, and the MMA's sf_id, reuse each SF word across four 128-wide K blocks for SharedLinear1/2 exactly as for Linear1/2. SharedLinear2 starts only after every shared L1 block is done, so all bytes of a reused word are written before it is loaded.

Changes

  • csrc/apis/mega_moe.hpp: remove the two blockwise-128 SFs (recipe gran_k=128) do not support shared experts asserts.
  • tests/test_mega_moe.py: --gran-k {32,128}. With 128 (fp8xfp8 only):
    • inputs are cast with 1x128 SFs, and routed and shared weights with 128x128 block SFs;
    • the legacy per-32 baseline is skipped;
    • the shared expert is checked against an FP32 reference of the kernel's quantization. The shared output is taken as the fused output minus a routed-only run on the same inputs. The reference uses 1x128 inputs and 128x128 weights, rounds the L1 output to BF16 and clamps it, applies SwiGLU, and re-quantizes per 1x128 with UE8M0 SFs.

Tests

8x B300, EP8, tests/test_mega_moe.py --num-processes 8 --num-correctness-tests 3 --num-max-tokens-per-rank 4096, default shapes (hidden 7168, intermediate 3072, 384 experts, top-6):

Case Result
--mma-type fp8xfp8 --gran-k 128, 4096 tokens per rank shared expert vs FP32 reference: worst calc_diff 1.15e-4
same, 32 tokens per rank 1.20e-4
same, 1000 tokens, --masked-ratio 0.3 8.28e-5
same, --num-shared-experts 2 5.87e-5
--gran-k 128 --num-shared-experts 0 runs clean
--mma-type fp8xfp8 (per-32), 1 shared expert 3/3 correctness tests vs the legacy baseline pass
--mma-type fp8xfp4 (per-32), 1 shared expert 3/3 pass

The blockwise-128 recipe (1x128 activation and 128x128 weight SFs, sgl-project#150) rejected shared experts
in both host entry points. The kernel already handles shared tiles on that path like routed ones:
the L1 epilogue merges the 1x128 amax across the 2-CTA cluster and stores one SF byte per group
into the shared L2 SF buffer, and the loaders and MMA reuse each SF word across four K blocks for
SharedLinear1/2 as for Linear1/2. This lifts the two asserts.

tests/test_mega_moe.py gains `--gran-k 128` (`fp8xfp8` only). The legacy baseline runs per-32 SFs
only, so with blockwise-128 the shared expert (the fused output minus a routed-only run on the
same inputs) is checked against an FP32 reference of the kernel's quantization: 1x128 inputs,
128x128 weights, the L1 output rounded to BF16 and clamped, SwiGLU, the result re-quantized per
1x128 with UE8M0 SFs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@zcnrex
zcnrex merged commit 12be8d3 into sgl-project:dev Nov 28, 2026
zcnrex added a commit that referenced this pull request Nov 28, 2026
Git carried the three PRs' edits through the *_inference renames.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
aryatsc added a commit to aryatsc/DeepGEMM that referenced this pull request Nov 28, 2026
Brings in sgl-project#170, sgl-project#165, sgl-project#169, sgl-project#178 and sgl-project#171.

Conflicts:
- csrc/jit_kernels/impls/sm100_fp8_fp4_mega_moe.hpp: the K=64 switch is now
  `uses_packed_fp4_weights(config.mainloop)` (sgl-project#165); the row-shuffled FP8 x FP4
  layout asserts that mainloop instead of `config.use_sm107_k64`. The kernel
  template takes the mainloop in place of the two K=64 / K=96 flags, followed by
  this branch's four parameters.
- deep_gemm/include/deep_gemm/impls/sm100_fp8_fp4_mega_moe.cuh: sgl-project#165's mainloop
  plan and validity check replace the K=64 static assert; the base MMA loop is
  taken from dev (now under `run_base_loop`) with this branch's SFB word order
  for row-shuffled packed FP4 weights re-applied in it.
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