Skip to content

[Fix] SM90 heuristic offers block M 16/32 to the FP8 1D1D kernel (JIT failure for M <= 32) - #455

Closed
LiRunGuo wants to merge 2 commits into
deepseek-ai:mainfrom
LiRunGuo:fix-sm90-1d1d-small-m
Closed

LiRunGuo wants to merge 2 commits into
deepseek-ai:mainfrom
LiRunGuo:fix-sm90-1d1d-small-m

Conversation

@LiRunGuo

@LiRunGuo LiRunGuo commented Sep 22, 2026 •

Copy link
Copy Markdown

Problem

On SM90, the layout heuristic (csrc/jit_kernels/heuristics/sm90.hpp) adds block M candidates of 16 and 32 when m <= 32 for Normal, Batched and KGroupedContiguous GEMMs. The SM90 FP8 1D2D and BF16 kernels support these small tiles, but the SM90 FP8 1D1D kernel (used for FP32-output / weight-gradient GEMMs and for the k-grouped GEMM) only supports BLOCK_M of 64 or 128:

deep_gemm/include/deep_gemm/impls/sm90_fp8_gemm_1d1d.cuh(61): error: static assertion failed with "Invalid block size"
      static_assert(BLOCK_M % WGMMA::M == 0, "Invalid block size");
deep_gemm/include/deep_gemm/impls/sm90_fp8_gemm_1d1d.cuh(264): error: static assertion failed with "Invalid block sizes"
      static_assert(BLOCK_M == WGMMA::M * (BLOCK_M <= 64 ? 1 : 2), "Invalid block sizes");

For m <= 32 the cost model always prefers the smaller tile (e.g. it picks block_m=32, block_n=56 for m=27, n=5744, k=7936), so JIT compilation fails and the call raises. This affects, on Hopper:

  • fp8_gemm_nt(a, b, d, c=d) with an FP32 d (the 1D1D wgrad path) whenever m <= 32
  • k_grouped_fp8_gemm_nt_contiguous whenever m <= 32

Neither shape was covered by the tests (the wgrad shapes only use m from nk_list, all >= 576, and the k-grouped shapes all have m >= 768). Found with a randomized shape fuzzer on an H200.

Fix

Only offer the 16/32 candidates to kernels that support them (kernel_type != Kernel1D1D). With the fix, the heuristic picks block_m=64 for these shapes. Shapes with m > 32 are unaffected, since the candidate list only changes when m <= 32.

Tests (tests/generators.py):

Testing (H200, CUDA 13.0, PyTorch 2.11.0+cu130, at the PR head)

Suite Result
tests/test_fp8_fp4.py pass
tests/test_bf16.py pass
tests/test_layout.py pass
tests/test_einsum.py pass
tests/test_legacy.py pass
tests/test_attention.py pass

New test lines:

# test_fp8_fp4.py (FP8 1D1D wgrad and k-grouped)
 > Perf (m=    16, n=  4096, k=  7168, 1D1D, layout=NT, FP32, acc=1):   19.2 us |   49 TFLOPS | 1610 GB/s | 0.81x cuBLAS speedup
 > Perf (m=    32, n=  4096, k=  7168, 1D1D, layout=NT, FP32, acc=1):   17.8 us |  106 TFLOPS | 1778 GB/s | 0.90x cuBLAS speedup
 > Perf (FP8, num_groups= 8, m=   16, n= 2048, k= 1927, gran_k=128, k_alignment=128, psum=0, acc=1, FP32):    7 us |   17 TFLOPS |  952 GB/s
# test_bf16.py
 > Perf (m=    16, n=  4096, k=  7168, layout=TN, FP32, acc=1):    32.7 us |   29 TFLOPS | 1817 GB/s | 0.76x cuBLAS
 > Perf (m=    32, n=  4096, k=  7168, layout=TN, FP32, acc=1):    26.7 us |   70 TFLOPS | 2254 GB/s | 0.93x cuBLAS
 > Perf (num_groups= 8, m=   16, n= 2048, k= 1927, align=128, psum=0, acc=1, FP32):    8 us |   16 TFLOPS | 1513 GB/s
# test_legacy.py (k-grouped)
 > Perf (num_groups=8, m=   16, n= 2048, k= 2304, layout=TN):    9 us |   16 TFLOPS | 1146 GB/s

Before the fix, the same shapes fail at JIT compile time with the static assertions quoted above.

I could only test on SM90. The k-grouped shape is gated to SM90 because I could not check whether the SM100 k-grouped kernel accepts the swap-AB layout its heuristic picks for small M. It could be enabled there if it does.

The SM90 layout heuristic adds block M candidates of 16 and 32 for small M,
but the SM90 FP8 1D1D kernel (FP32-output / k-grouped GEMMs) only supports
block M of 64 or 128 (single or double math warpgroups). For M <= 32, the
cost model prefers the smaller tile and the JIT compilation then fails on the
kernel's static assertion, so e.g. `fp8_gemm_nt` with an FP32 accumulator and
`k_grouped_fp8_gemm_nt_contiguous` cannot run for M <= 32 on Hopper.

Restrict the 16/32 candidates to the kernels that support them (1D2D and
BF16), and add small-M weight-gradient shapes to the test enumeration.

Signed-off-by: RunguoLi <[email protected]>
Comment thread tests/generators.py
# Wgrad with small M (e.g., narrow weights), where the heuristic may pick small block M
for small_m in (16, 32):
yield emit(override_kernel_type, quant_config, small_m, 4096, 7168, override_major, override_major, True, torch.float) # Wgrad

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.

🔵 suggestion: The comment says the heuristic "may pick small block M" for these wgrad shapes, but after this fix the SM90 1D1D kernel will never see block M < 64 — that is the point of the regression test. Consider rewording to something like: "Wgrad with small M: regression test for the 1D1D kernel, which only supports block M 64/128 (the heuristic must not pick 16/32 here)".

🤖 v5

Comment thread tests/generators.py
for small_m in (16, 32):
yield emit(override_kernel_type, quant_config, small_m, 4096, 7168, override_major, override_major, True, torch.float) # Wgrad

if collect_cublas_scores and scores:

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.

🔵 suggestion: The issue also reports k_grouped_fp8_gemm_nt_contiguous failing for m <= 32 on SM90, but the added coverage only exercises the fp8_gemm_nt FP32-accumulate path. Since KGroupedContiguous goes through the same block_m_candidates branch it is fixed by the same change, but adding a small-M entry (e.g. m=16 or m=32) to enumerate_k_grouped_contiguous would keep that path covered too. Non-blocking.

🤖 v5

Comment thread tests/generators.py Outdated
if dtype == torch.bfloat16 or get_arch_major() == 10:
yield emit(override_kernel_type, quant_config, n, m, k, override_major, override_major, False, torch.float) # Wgrad
# Wgrad with small M (e.g., narrow weights), where the heuristic may pick small block M
for small_m in (16, 32):

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.

🔵 suggestion: The new small-M weight-gradient cases hardcode n=4096, k=7168. This covers the reported regression, but the literals duplicate shape information that is otherwise centralized in nk_list. Consider naming these constants or deriving them from the existing shape tables so the small-M path stays in sync if the canonical shapes change.

🤖 v4

@ds-review-bot

Copy link
Copy Markdown
Collaborator

🤖 ds-review-bot Code Review

v6

未发现本次变更引入的缺陷。新增条件与 SM90 1D1D 内核的块尺寸约束一致,不影响其他内核的小块候选;新增测试也保留了全部既有用例。已静态检查 SM100 相关路径,但当前环境缺少 PyTorch/CUDA,未能执行 GPU 验证。

v5

The change is correct and minimal. csrc/jit_kernels/heuristics/sm90.hpp now only offers the 16/32 block-M candidates when desc.kernel_type != KernelType::Kernel1D1D, which exactly matches the kernel-side constraints: sm90_fp8_gemm_1d1d.cuh statically requires BLOCK_M % WGMMA::M == 0 (line 61) and BLOCK_M == WGMMA::M * (BLOCK_M &lt;= 64 ? 1 : 2) (line 264), i.e. 64 or 128 only, whereas the 1D2D (sm90_fp8_gemm_1d2d.cuh:69) and BF16 (sm90_bf16_gemm.cuh:62) kernels explicitly allow BLOCK_M &lt; WGMMA::M, so their behavior is unchanged. Both affected SM90 entry points (fp8_gemm_nt with FP32 d and k_grouped_fp8_gemm_nt_contiguous) build a Kernel1D1D GemmDesc and go through get_best_config&lt;SM90ArchSpec&gt;, so both reported failures are covered. The rest of SM90ArchSpec (store_block_m = 64 for 1D1D, 128 vs 256 math threads derived from block_m &lt;= 64) is already consistent with block M in {64,128}. In tests/generators.py, hoisting override_major/override_kernel_type out of the m_bwd_list loop is behavior-preserving (neither depends on m), and the new m=16/32, n=4096, k=7168 FP32-accumulate wgrad shapes exercise the 1D1D path on SM90. For SM100 the added shapes are safe by inspection: with MN-major A the non-swap path requires block_m % 64 == 0, so the heuristic falls back to swap-AB (block_m multiple of 16, block_n = 128), which sm100_fp8_fp4_gemm_1d1d.cuh:84 accepts. I could not run the GPU tests in this environment; the review is based on static analysis of the heuristics and kernel static assertions. Two minor, non-blocking suggestions below (k-grouped small-M coverage and a slightly stale test comment). Approve.

v4

This MR fixes a real SM90 JIT failure. The layout heuristic in csrc/jit_kernels/heuristics/sm90.hpp unconditionally offered block M candidates of 16/32 for small M, but the FP8 1D1D kernel (FP32-output weight-gradient and k-grouped paths) only supports BLOCK_M of 64 or 128 and fails its static assertions. The fix correctly scopes the small-tile candidates behind 'desc.kernel_type != KernelType::Kernel1D1D', preserving the small tiles for the 1D2D and BF16 kernels that do support them. This matches the kernel's static asserts exactly and the explanatory comment is valuable for maintenance. The test change adds small-M weight-gradient coverage to enumerate_normal, and hoisting override_major / override_kernel_type out of the inner 'for n, k in nk_list' loop is a clean, behavior-preserving refactor (these values depend only on arch and dtype). On SM100 the new shapes exercise the swap-AB path, as intended. The change is small, well-commented, and I found no functional concerns.

Files reviewed: 2
Issues found: 🔵 3 suggestion
Inline comments posted: 3

…case

- Name the small-M weight-gradient shape next to the other shape tables
  and reword its comment (the heuristic must not pick block M 16/32).
- Add a small-M (m=16) shape to the k-grouped enumeration on SM90, with 8
  groups so that TMA multicast stays disabled.

Signed-off-by: RunguoLi <[email protected]>
@LiRunGuo

Copy link
Copy Markdown
Author

Thanks for the review. Pushed 95880a3 to address the suggestions:

Re-ran the SM90 suites at the PR head on an H200 (CUDA 13.0): test_fp8_fp4, test_bf16, test_layout, test_einsum, test_legacy and test_attention all pass. The PR description is updated with the results.

@LiRunGuo

Copy link
Copy Markdown
Author

@LyricZhao @zheanxu Could you take a look at this when you have time? It only limits the block M 16/32 candidates in the SM90 heuristic to the kernels that support them, so FP32-output fp8_gemm_nt and k_grouped_fp8_gemm_nt_contiguous compile again for M <= 32 on Hopper. All SM90 test suites pass at the PR head on an H200. Thanks!

@LiRunGuo

Copy link
Copy Markdown
Author

Hi @RayWang96, a friendly follow-up on this one. The SM90 heuristic on main is unchanged since this PR was opened, so FP32-output fp8_gemm_nt and k_grouped_fp8_gemm_nt_contiguous still fail to JIT-compile on Hopper for M <= 32. The fix only limits the block-M 16/32 candidates to the kernels that support them, and the PR still merges cleanly. Happy to rebase or adjust anything. Thanks!

@LiRunGuo

LiRunGuo commented Nov 4, 2026

Copy link
Copy Markdown
Author

Hi @LyricZhao, could you review this when you have time? It changes the SM90 heuristic lines you last updated: the block-M 16/32 candidates are now restricted to kernels that support them. The PR merges cleanly, and the SM90 test suites pass at the PR head on an H200. Thanks!

kevmo314 added a commit to kevmo314/DeepGEMM that referenced this pull request Nov 11, 2026
SM90 grouped 1D2D heuristics, thread-local alignment, MoE precompile, and deepseek-ai#455/deepseek-ai#478
@LiRunGuo LiRunGuo closed this Nov 18, 2026
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