Repository navigation
[SM100] MegaMoE: support blockwise-128 fp8 - #150
Conversation
|
@Fridge003 Hi, could you please help to review this pr? |
0aed6e2 to
f50a897
Compare
|
@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; |
There was a problem hiding this comment.
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.
| 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, |
There was a problem hiding this comment.
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 wrotex,x_sfand 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 extragather_outputbytes), 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.
|
@Fridge003 Hi, I've addressed all the review comments. Could you please take another look? |
|
Could you help resolve the conflicts? |
b718f85 to
a34cbf5
Compare
a34cbf5 to
ea321ad
Compare
@zcnrex @Fridge003 Conflicts resolved, please take another look~ |
|
|
||
| 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: |
There was a problem hiding this comment.
[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.
| num_shared_experts=num_shared_experts, | ||
| mma_type=args.mma_type | ||
| mma_type=args.mma_type, | ||
| recipe=(1, 1, gran_k) |
There was a problem hiding this comment.
[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.
|
@zcnrex Thanks for your valuable feedback! I've fixed the check and test. Could you please take another look? |
…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]>
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 * 4andkNumSFStagesPerLoadis1forper-32 and
4for 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 transactionbarriers 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) viatransform_sf_into_required_layout(..., (128,128)), sothe kernel only ever sees
gran_mn == 1. A matching one-line fix incsrc/utils/layout.hppmakes the TMA-stride lower bound useceil_div(mn, gran_mn)(consistent with thesf.size(-2) == ceil_div(mn, gran_mn)shape assertion); it is a no-op for the existing
gran_mn == 1per-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
torch.equalagainst the DeepEP + TileLang baseline.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