Skip to content

[SM100] Let the FP8 einsum epilogue and Mega mHC write FlashInfer's 128x4 SF layout - #136

Merged
zcnrex merged 3 commits into
sgl-project:devfrom
zcnrex:chunan/fi-swizzled-sf
Nov 4, 2026
Merged

zcnrex merged 3 commits into
sgl-project:devfrom
zcnrex:chunan/fi-swizzled-sf

Conversation

@zcnrex

@zcnrex zcnrex commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The FP8 (d, sfd) output of fp8_einsum("bhr,hdr->bhd", ...) and Mega mHC's y_gemm_sf can now be written directly in FlashInfer's 128x4 swizzled UE8M0 layout. A consumer running FlashInfer's mm_mxfp8 then needs no scale conversion.

  • Selecting it. Pass a contiguous 4D int32 SF tensor [ceil(rows / 128), K / 128, 32, 4] instead of the 2D MN-major one.
    • Element [t, w, i, j] packs the four SFs of row 128 t + 32 j + i for K words w.
    • Its .view(torch.uint8).view(-1) is exactly FlashInfer's swizzled a_descale.
    • Pad rows are not written.
  • 2D outputs are unchanged.
  • FP8 epilogue. EpilogueOperatorArgs::sfd_mn_tile_stride (0 = MN-major) selects the row offset in store_sf.
  • Mega mHC. A kSwizzledPrimarySF template argument plus NormArgs::y_primary_sf_stride_mtile select the layout at compile time.
    • A runtime branch here cost the 2D path 2-3% (sm_103), so the 2D instantiation keeps the original code.

Motivation

In DeepSeek-V4.1 prefill, two dense projections take DeepGEMM-packed (fp8, sf) pairs from these producers:

  • wo_b, from the wo_a einsum epilogue;
  • the fused wqkv_a, from Mega mHC.

On sm_107 (CC 10.7), FlashInfer's cute-dsl MXFP8 GEMM is faster than the DeepGEMM 1D1D GEMM for these shapes. Today the pair has to be permuted into FlashInfer's layout first, and that copy is 4-22% of the FlashInfer call. It erases the GEMM win for wqkv_a and for small-row wo_b.

FlashInfer cannot read the MN-major layout itself without a kernel change:

  • Each 16 B UTCCP (tcgen05.cp 32x128b.warpx4) row holds rows i, i+32, i+64 and i+96.
  • Turning MN-major words into that order is a 4-byte-granular transpose, which neither a TMA box nor the UTCCP smem descriptor can express.
  • That is why DeepGEMM's own 1D1D kernel runs a transposer warp. Emitting the final layout from the producer avoids that.

Validation

Correctness (bitwise equal to the 2D path followed by the pad + permute conversion):

  • New test cases:
    • test_einsum.py: the FP8 bhr,hdr->bhd case now also checks the 4D SFD.
    • test_mega_mhc.py: new test_mega_mhc_flashinfer_sf, normal and shifted, T = 1, 200, 1025, 4096.
  • Production shapes (wqkv_a: K = 5120, Mega mHC shifted; wo_b: K = 8192, einsum h = 8, r = 4096): SF bytes, FP8 data, and FlashInfer mm_mxfp8 output (cute-dsl backend).
    • Pad rows filled with NaN SFs do not change the valid output rows.
    • sm_103: rows 1 to 16384. sm_107: rows 1000 and 4100.
  • tests/test_einsum.py passes on sm_103 at bcf8ec5, and tests/test_mega_mhc.py at the current head (8b4cc78). The later Mega mHC commits are also bitwise-checked at production shapes: sm_103 rows 1 to 16384 (including 127, 128, 129), sm_107 rows 1, 129, 1000, 4100 and 16384.

Performance, sm_107 (FlashInfer cute-dsl mm_mxfp8, autotuned). The DeepGEMM side is the 1D1D GEMM at #133. Each arm is one CUDA graph; arms are interleaved over 5 rounds, and the table gives the median of per-round ratios. "e2e" = producer + GEMM (+ conversion), compared with producer + DeepGEMM GEMM.

shape rows FI + conversion / DG (GEMM) FI with 4D SF / DG (GEMM) e2e, 4D SF / DG
wqkv_a (N 1792, K 5120) 16384 1.00 0.92 1.00
8192 1.04 0.95 1.00
4096 1.01 0.88 0.98
2048 1.04 0.86 0.97
1024 1.09 0.90 0.99
512 1.08 0.82 0.92
256 1.03 0.82 0.92
wo_b (N 5120, K 8192) 16384 0.85 0.82 0.91
8192 0.95 0.95 0.97
4096 0.94 0.85 0.91
2048 1.04 0.97 0.99
1024 1.05 0.87 0.92
512 1.02 0.88 0.93
256 0.99 0.81 0.89

Producer cost of the 4D layout:

  • Mega mHC, swizzled instantiation, vs its 2D instantiation:
    • Measured producer-only: each arm is its own CUDA graph, arms alternate over 15 rounds, median of per-round ratios.
    • sm_103: 0.986-0.999 at 1K-16K tokens. At bcf8ec5 it was 1.006-1.021. The second commit moves the offset math to 32 bits and derives the shifted task's second token from the first.
    • sm_107: 0.99-1.00 at 512-8K tokens, 1.008-1.011 at 16K. At bcf8ec5 it was 1.004-1.017.
      • The second commit did not move sm_107. There the cost was the store order: a variant with the same instructions but 2D-like token order measured about 1.00.
      • The third commit permutes token pairs inside each 128-token tile, so each 32 B SF sector has two writer CTAs, as in the MN-major layout. In FlashInfer's order it had four.
      • Orders that make neighbouring CTAs alternate between a tile's two 64-token m-blocks cost 6-8%.
    • The table above predates the second and third commits.
  • Mega mHC, 2D instantiation: vs the base header, 0.997-1.001 (sm_103, ABBA x2).
  • Einsum epilogue, 2D path: vs base, 0.999-1.007.
  • Einsum, 4D vs 2D: about 1.00, except 0.95 at 1024 rows on sm_107 (three runs). That point is unexplained.

🤖 Generated with Claude Code

zcnrex and others added 3 commits November 3, 2026 19:24
…28x4 SF layout

A 4D int32 SF output [ceil(rows / 128), K / 128, 32, 4] selects FlashInfer's
128x4 swizzle; its flat uint8 view is FlashInfer's mm_mxfp8 a_descale, so a
FlashInfer consumer needs no layout conversion. 2D outputs are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Compute the FlashInfer 128x4 row offset in 32 bits, and derive the shifted
task's second token from the first (+4 words, since the first index is even).
The 2D instantiation is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
In the FlashInfer 128x4 layout a 32 B SF sector holds token pairs p,
p+16, p+32 and p+48 of a 128-token tile, so the shifted-norm task order
had four CTAs writing each sector. Permute token pairs inside the tile
so a CTA takes p and p+16, matching the two writers per sector of the
MN-major layout. Only the swizzled instance changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@zcnrex
zcnrex merged commit d91ce2d into sgl-project:dev Nov 4, 2026
@zcnrex
zcnrex deleted the chunan/fi-swizzled-sf branch November 4, 2026 16:38
Jzz1943 pushed a commit to Jzz1943/DeepGEMM that referenced this pull request Dec 1, 2026
The upstream merge (sgl-project#177) added a 2D-only `check_sf_layout` to
`FP8CastWithClampEpilogue::check`, which rejects the 4D
`[ceil(m / 128), n / 128, 32, 4]` SFD that `fp8_einsum` accepts since sgl-project#136.
DeepSeek V4.1's FP8 `wo_a` einsum (FlashInfer `wo_b` handoff) asserted at
server warmup. Validate the 4D shape there as `fp8_einsum` does, and add
the 4D case to the sgl einsum test.

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.

1 participant