Skip to content

[SM100] MegaMoE: support blockwise-128 fp8 - #150

Merged
zcnrex merged 4 commits into
sgl-project:devfrom
Wanglongzhi2001:blockwise128_dev
Nov 19, 2026
Merged

zcnrex merged 4 commits into
sgl-project:devfrom
Wanglongzhi2001:blockwise128_dev

Conversation

@Wanglongzhi2001

@Wanglongzhi2001 Wanglongzhi2001 commented Nov 13, 2026 •

Copy link
Copy Markdown

Motivation

Many production LLMs ship their FP8 weights and activations with blockwise-128
quantization
(one scale per 128x128 element block) rather than the fine-grained
per-32 (MX) layout. Until now the SM100 fused MegaMoE kernel only accepted the
per-32 MXFP8/MXFP4 recipe, so blockwise-128 checkpoints had no fast fused
inference path and had to fall back to unfused GEMMs.

This PR adds a true blockwise-128 FP8 path to the fused MegaMoE kernel, giving
blockwise-128 models a high-performance, single-kernel MoE inference route on
Blackwell while leaving the existing per-32 path byte-for-byte unchanged.

What this PR does

1. Blockwise-128 through the per-32 path by re-reading

The kernel keeps the existing per-32 SF/UTCCP consumption machinery and simply has
the threads re-read the same packed SF word across the four sub-groups it
covers. Concretely, kSFChunkK = kGranK * 4 and kNumSFStagesPerLoad is 1 for
per-32 and 4 for blockwise-128: the per-32 path advances the SF word every stage,
the blockwise path points all four stages at the same word.

2. Cross-CTA reduction of the L2-GEMM pre-activation scale

The kernel runs as a 2-CTA cluster (tcgen05/UMMA), so each GEMM's N dimension is
split across the two CTAs of the cluster. For per-32 quantization a scale block is
narrow enough to live entirely inside one CTA, so the L1 epilogue that quantizes the
L2 input can compute its amax locally. Blockwise-128 breaks that assumption: a single
per-128 group of the L2 activation now spans both CTAs of the cluster, so neither
CTA sees the full group on its own.

To keep the per-128 scale correct, the L1 epilogue reduces the amax across the
cluster
before writing the SF byte. Each CTA publishes its local amax to its peer
over DSMEM with st.async.shared::cluster, synchronized by two ping-pong transaction
barriers per warpgroup, then both CTAs combine the two partials into the same
cluster-wide per-128 amax. With both CTAs now holding an identical scale, the leader
writes the group's single SF byte, and the L2 GEMM dequantizes consistently on both
halves of the N split.

3. Host-side layout

Block (128,128) weight scales are host-expanded to per-row
(gran_mn = gran_n = 1) via transform_sf_into_required_layout(..., (128,128)), so
the kernel only ever sees gran_mn == 1. A matching one-line fix in
csrc/utils/layout.hpp makes the TMA-stride lower bound use
ceil_div(mn, gran_mn) (consistent with the sf.size(-2) == ceil_div(mn, gran_mn)
shape assertion); it is a no-op for the existing gran_mn == 1 per-32 path.

Test results

Config: --num-processes 4 --hidden 7168 --intermediate-hidden 3072 --num-experts 8 --num-topk 6 --num-max-tokens-per-rank 8192 --num-correctness-tests 1, over tokens-per-rank
{8, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192} on 4×Blackwell.

Accuracy — existing per-32 paths are byte-for-byte unchanged

  • W4A8 and fp8 paths are bitwise torch.equal against the DeepEP + TileLang baseline.
  • Both FP4-activation arms(mxfp4xmxfp4/nvfp4xnvfp4) additionally pass the test_mega_moe_l1_fp4_accuracy.py.

Performance — no regression on existing paths

w4a8/mxfp4xmxfpx/nvfp4xnvfp4/mxfp8xmxfp8 latency is unchanged within ±2% run-to-run noise.

Performance — blockwise-128 fused kernel vs. the DeepEP + DeepGEMM baseline

tokens fused us baseline us speedup fused TFLOPS
8 423.0 580.4 1.37× 18
32 789.2 1035.8 1.31× 32
64 892.9 1156.6 1.30× 58
128 909.6 1182.2 1.30× 111
256 921.3 1200.6 1.30× 215
512 936.4 1240.8 1.33× 429
1024 970.6 1366.8 1.41× 841
2048 1085.0 1558.2 1.44× 1502
4096 1812.0 2603.8 1.44× 1797
8192 2953.0 4370.3 1.48× 2201

@Wanglongzhi2001

Copy link
Copy Markdown
Author

@Fridge003 Hi, could you please help to review this pr?

@Wanglongzhi2001

Copy link
Copy Markdown
Author

@zcnrex @Fridge003 Hi, could you please help to review this pr?

constexpr uint32_t kNumMMAsPerSFChunk = kSFChunkK / UMMA_K;
// UMMAs per 128-wide K block: independent of the SF granularity (a blockwise-128 word is
// reused across four blocks, each still issuing its own UMMAs)
constexpr uint32_t kNumMMAsPerSFChunk = BLOCK_K / UMMA_K;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks the FP4-activation variants (nvfp4xnvfp4, mxf4xmxf4).

BLOCK_K / UMMA_K only equals the old kSFChunkK / UMMA_K when BLOCK_K == kSFChunkK, i.e. on the FP8 paths (BLOCK_K = 128). For FP4 activations the heuristic picks block_k = 128 * 8 / 4 = 256 and UMMA_K = 64:

  • NVFP4 (gran 16, kSFChunkK = 64): old value 1, new value 4
  • MXFP4 (gran 32, kSFChunkK = 128): old value 2, new value 4

So the unchanged static assert a few lines below (kNumMMAsPerSFChunk == (kPackedFP4Weights ? 2u : kIsNVFP4 ? 1u : kIsMXFP4 ? 2u : 4u)) now fails and these kernels no longer JIT-compile. Even with the assert relaxed, the MMA loop (sf_chunk_idx < kNumSFWordsPerStage x k < kNumMMAsPerSFChunk) would issue 4x too many UMMAs and walk past the K block.

The current tests only cover fp8xfp4 / fp8xfp8, so this isn't caught. Suggested fix:

constexpr uint32_t kNumMMAsPerSFChunk = math::constexpr_min(kSFChunkK, BLOCK_K) / UMMA_K;

It would also be good to add an NVFP4 or MXFP4 MegaMoE case to the tests.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

shared_l2_weights: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
cumulative_local_expert_recv_stats: Optional[torch.Tensor] = None,
recipe: Tuple[int, int, int] = (1, 1, 32),
recipe: Optional[Tuple[int, int, int]] = None,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The recipe passed here isn't tied to the recipe the SymmBuffer was laid out with, but the kernel's MegaMoEBuffer region offsets depend on gran_k.

  • Buffer allocated with the default (gran 32), call made with recipe=(1, 1, 128): the required-bytes check passes (the gran-128 layout is smaller), but the kernel computes the SF/activation region offsets with gran 128, while Python wrote x, x_sf and the top-k data into views sliced with gran 32. The result is silently wrong.
  • Buffer allocated with recipe=(1, 1, 128), call made with the default (None, which resolves to gran 32): this normally fails with a confusing "symmetric buffer is N bytes" error. If the buffer happens to be large enough (e.g. the extra gather_output bytes), it passes and the data is corrupted.

Suggestion: store the resolved recipe / gran_k on SymmBuffer at construction, default to it here when recipe/recipe_a/recipe_b are all None, and assert that an explicitly passed recipe matches. That would also replace the self.gran_k = hidden // (4 * self.x_sf.shape[1]) back-derivation, which is currently unused.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@Wanglongzhi2001

Copy link
Copy Markdown
Author

@Fridge003 Hi, I've addressed all the review comments. Could you please take another look?

@zcnrex

zcnrex commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Could you help resolve the conflicts?

@Wanglongzhi2001

Copy link
Copy Markdown
Author

Could you help resolve the conflicts?

@zcnrex @Fridge003 Conflicts resolved, please take another look~

Comment thread deep_gemm/mega/__init__.py Outdated

if recipe is None and recipe_a is None and recipe_b is None:
recipe, recipe_a, recipe_b = sym_buffer.recipe, sym_buffer.recipe_a, sym_buffer.recipe_b
elif recipe is not None:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Paired recipes bypass buffer-layout validation

A default gran-32 buffer accepts recipe_a=(1,128), recipe_b=(1,128) because only recipe is checked. The native size check passes for the smaller gran-128 layout, but the kernel then reads SF and top-k data at offsets different from Python's views. Validate the resolved granularity for both recipe forms.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread sgl_deep_gemm/tests/test_mega_moe.py Outdated
num_shared_experts=num_shared_experts,
mma_type=args.mma_type
mma_type=args.mma_type,
recipe=(1, 1, gran_k)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] BF16 test now fails during buffer allocation

The unconditional recipe=(1,1,gran_k) supplies gran-32 for --mma-type bf16xbf16. The allocator accepts only gran-0 or gran-128 for BF16, so the existing BF16 case in run_tests.sh aborts before testing. Pass None for BF16.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@Wanglongzhi2001

Copy link
Copy Markdown
Author

@zcnrex Thanks for your valuable feedback! I've fixed the check and test. Could you please take another look?

@zcnrex
zcnrex merged commit 169d4d5 into sgl-project:dev Nov 19, 2026
zcnrex pushed a commit that referenced this pull request Nov 28, 2026
…171)

The blockwise-128 recipe (1x128 activation and 128x128 weight SFs, #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]>
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