Repository navigation
[Triton/Gluon] GEMM: compute GRID_MN at runtime instead of as a constexpr - #7591
Conversation
GRID_MN = cdiv(M, BLOCK_SIZE_M) * cdiv(N, BLOCK_SIZE_N) was filled in by @triton.heuristics and passed as tl.constexpr. Because it depends on M, every new cdiv(M, BLOCK_SIZE_M) triggered a full recompile, including in kernels that never read it. Drop the heuristic, the constexpr parameter and the kernel-name entry, and compute the value in the kernel where it is used (as gemm_afp4wfp4 already does). On gfx950 with gemm_a8w8 N=K=4096 and a fixed tuned config, 63 M values went from 63 compiles to 1. Kernel time is unchanged (60 tuned cases, geomean 0.998). The MFMA main loop has the same instruction sequence; the prologue gains about 24 scalar instructions.
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
WAITING ON AUTHORPR Self-ReviewRevision Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision. Questions to answer in the PR description
Edit the Missing or invalid answers
Shadow mode reports what would block but keeps the status successful. Set |
gemm_a8w8, fused_gemm_afp4wfp4_split_cat and fused_gemm_afp4wfp4_a16w16 computed the tile count for remap_xcd and then the same cdivs again as num_pid_m/num_pid_n. Compute num_pid_m/num_pid_n first and derive grid_mn = num_pid_m * num_pid_n from them. Use the same grid_mn variable in the other kernels touched by the previous commit, which passed num_pid_m * num_pid_n inline.
There was a problem hiding this comment.
🟡 Changes recommended
Both kernels unnecessarily overwrite the runtime grid_mn argument with a calculation already performed by the wrapper.
1 open issue
What changed in this PR
This PR reduces shape-driven recompilation in AITER’s Triton/Gluon GEMM kernels by removing GRID_MN from compile-time specialization.
Changes:
- Remove grid-size heuristics, constexpr parameters, and associated kernel-name fields across 25 kernels.
- Compute required grid sizes at runtime for program mapping.
- Keep public wrappers and tuning configs unchanged.
| File | Description |
|---|---|
aiter/ops/triton/_triton_kernels/gemm/fused/fused_gemm_afp4wfp4_split_cat.py |
Use runtime grid sizes in both FP4 split-cat variants. |
aiter/ops/triton/_triton_kernels/gemm/fused/fused_gemm_afp4wfp4_a16w16.py |
Compute combined FP4/BF16 grid sizes at runtime. |
aiter/ops/triton/_triton_kernels/gemm/fused/fused_gemm_a8w8_blockscale_split_cat.py |
Remove grid specialization from both split-cat variants. |
aiter/ops/triton/_triton_kernels/gemm/fused/fused_gemm_a8w8_blockscale_a16w16.py |
Compute the combined FP8/BF16 grid at runtime. |
aiter/ops/triton/_triton_kernels/gemm/feed_forward/ff_a16w16_fused_ungated.py |
Replace the ungated kernel’s constexpr grid with a runtime value. |
aiter/ops/triton/_triton_kernels/gemm/feed_forward/ff_a16w16_fused_gated.py |
Replace the gated kernel’s constexpr grid with a runtime value. |
aiter/ops/triton/_triton_kernels/gemm/batched/batched_gemm_a8w8.py |
Remove unused grid specialization and naming metadata. |
aiter/ops/triton/_triton_kernels/gemm/basic/gemm_a8wfp4.py |
Remove grid specialization from mixed-precision GEMM. |
aiter/ops/triton/_triton_kernels/gemm/basic/gemm_a8w8.py |
Use runtime grids for standard and preshuffled program remapping. |
aiter/ops/triton/_triton_kernels/gemm/basic/gemm_a8w8_per_token_scale.py |
Replace the per-token kernel’s constexpr grid. |
aiter/ops/triton/_triton_kernels/gemm/basic/gemm_a16wfp4.py |
Remove unused grid parameters from both variants. |
aiter/ops/triton/_triton_kernels/gemm/basic/gemm_a16w8_blockscale.py |
Remove unused grid specialization from both variants. |
aiter/ops/triton/_triton_kernels/gemm/basic/gemm_a16w16_gated.py |
Replace the gated GEMM’s constexpr grid. |
aiter/ops/triton/_triton_kernels/gemm/basic/gemm_a16w16_atomic.py |
Use a runtime grid for non-split-K remapping. |
aiter/ops/triton/_triton_kernels/fusions/fused_bmm_rope_kv_cache.py |
Use runtime grids for FP4/FP8 head and tile indexing. |
aiter/ops/triton/_gluon_kernels/gfx950/gemm/basic/gemm_a8w8.py |
Use runtime grids in both gfx950 Gluon variants. |
aiter/ops/triton/_gluon_kernels/gfx950/gemm/basic/gemm_a8w8_blockscale.py |
Remove constexpr grid specialization from gfx950 blockscale GEMM. |
Review effort: Balanced
The merge of main kept GRID_MN as a compile-time constant of the generic FP8 kernel. Each new token count compiled a new binary again, which upstream ROCm#7591 had removed. The generic kernel _fused_fp8_bmm_rope_cat_and_cache_mla_kernel and its launch are now the same as on main, byte for byte. The pure-prefill code is in the new kernel _fused_fp8_bmm_rope_prefill_kernel. The wrapper selects it before the generic config lookup. The new kernel has only the code that the dispatch can reach: - two projection variants (LDB) that call _fp8_quant_group and _fp8_mma_store - the block RoPE and KV-cache write for one KV head and no decode tokens The constant GRID_MN, the multiply-high division, B_PF, AQH, LPIN, PERM_N and the third projection branch are removed. BLOCK_SIZE_K and K_TAIL come from group_size and K. The wrapper checks the column tile, the K tail and the row tile of the config. Signed-off-by: Saptarshi Majumder <[email protected]>
Triton makes a new binary when an integer argument changes between "divisible by 16" and "not divisible by 16". For the prefill kernel, M, grid_mn and bmm_programs change with the token count of each call. On gfx950, 90 calls with 30 token counts compiled 17 binaries. With do_not_specialize for these three arguments, the same 90 calls compile 3 binaries, one for each config bucket. ROCm#7591 made the grid size a runtime value of the generic kernel for the same reason. Signed-off-by: Saptarshi Majumder <[email protected]>

Summary
GRID_MN = cdiv(M, BLOCK_SIZE_M) * cdiv(N, BLOCK_SIZE_N) was filled in by @triton.heuristics and passed as tl.constexpr. Because it depends on M, every new cdiv(M, BLOCK_SIZE_M) triggered a full recompile, including in kernels that never read it.
Drop the heuristic, the constexpr parameter and the kernel-name entry, and compute the value in the kernel where it is used (as gemm_afp4wfp4 already does).
On gfx950 with gemm_a8w8 N=K=4096 and a fixed tuned config, 63 M values went from 63 compiles to 1. Kernel time is unchanged (60 tuned cases, geomean 0.998). The MFMA main loop has the same instruction sequence; the prologue gains about 24 scalar instructions.
There are certain test failing (in main and this branch):
test_fused_gemm_a8w8_blockscale_a16w16.py::test_gemm[*-8192-512-256-7168],When using ToT triton compiler, failures disappear. Initial analysis shows this is a compiler bug exists in
triton 3.8.0+git669b31acMotivation
GRID_MNwas filled in by@triton.heuristicsand passed as a constexpr. Because it depends on M, each new value ofcdiv(M, BLOCK_SIZE_M)produced a new specialization: a full compile (~135 ms on gfx950 with a cold cache, ~1–2 ms cache load with a warm one) and another binary in the cache. In a server, variable prefill sizes keep hitting this.In 14 of the 25 kernels the constexpr bought nothing. It was never read (
gemm_a16wfp4,gemm_a16w8_blockscale,batched_gemm_a8w8), or it only fed aremap_xcd(...)call whose result is discarded. For example, base cached 5 identical binaries of_gemm_a16wfp4_kernelfor a single config.Changes
For each kernel: remove the
"GRID_MN": lambda args: ...heuristic, theGRID_MN: tl.constexprparameter and, where present, the"GRID_MN"entry ofmake_kernel_repr. Each kernel now computesgrid_mn = num_pid_m * num_pid_nfrom its existingnum_pid_m/num_pid_nand passes that toremap_xcd(or topid // grid_mninfused_bmm_rope_kv_cache). No wrapper or config changes. 25 kernels in 17 files, +59 / −139 across the two commits.GRID_MNvalue is used (now computed at runtime):gemm_a8w8(+ preshuffle)gemm_a16w16_atomicfused_gemm_afp4wfp4_a16w16(+ preshuffle)fused_gemm_afp4wfp4_split_cat(+ preshuffle)fused_bmm_rope_kv_cache(fp4 and fp8)gemm_a8w8(+ preshuffled)GRID_MNonly passed to aremap_xcdcall whose result is discarded (the call is kept, with the runtime value):gemm_a16w16_gatedgemm_a8w8_per_token_scalegemm_a8wfp4ff_a16w16_fused_gatedandff_a16w16_fused_ungatedfused_gemm_a8w8_blockscale_a16w16fused_gemm_a8w8_blockscale_split_cat(+ preshuffle)gemm_a8w8_blockscaleGRID_MNnever read:gemm_a16wfp4(+ preshuffle)gemm_a16w8_blockscale(+ preshuffle)batched_gemm_a8w8GRID_MN_<value>part, so one config now has one kernel name. No code in the repo matches on it; external profiling or tuning scripts that do will need updating.gemm_a8w8_blockscaleandgemm_mxfp8already compute it at runtime). There are no gfx942-specific GEMMs.Performance (if applicable)
Kernel device time from
torch.profiler, 512 MiB L2/MALL flush before every call, 50 warmup + 200 timed calls, median. Runs were ordered base, PR, base, PR; the table shows the mean of the two runs. Tuned gfx950 configs (wrapper called withconfig=None).Median base-vs-base run-to-run noise is 0.6%. The extremes are noise: the 1.040 case (atomic N=256 M=8192) had base itself move from 66.1 to 71.8 µs between its two runs, and 0.970 is a ~4 µs kernel.
Compile counts:
gemm_a8w8N=K=4096, one fixed tuned config (BLOCK_SIZE_M=128), 63 values of M (129, 257, …, 8065), each with a differentcdiv(M, 128). Counted withtriton.knobs.compilation.listener.The 3 compiles in the dense sweep are Triton's normal specialization of the runtime int
M(M == 1,M % 16 == 0, otherwise), so the count is bounded.Testing
Tested on MI355X (gfx950). gfx942 and gfx1250 were only compiled offline, not run.
Documentation
Internal kernel signature change only; the public wrappers are unchanged.
Dependencies
Breaking Changes
None for wrapper APIs. Compiled kernel names no longer contain
GRID_MN_<value>. Calling one of these_kernels directly withGRID_MN=would now fail, but nothing in the repo does that.PR Self-Review
Reviewed revision
1d07d64
Behavior and intent
GRID_MNchanges from a compile-time constant to a runtime value computed in the kernel. Results are the same: each program still computes the same output tile. What changes is that kernels no longer recompile when M changes the number of M tiles, and one config produces one binary and one kernel name instead of one per M bucket.The old behavior recompiled (~135 ms each with a cold cache) for every new
cdiv(M, BLOCK_SIZE_M), including in kernels that never read the value. That shows up as first-call latency spikes and cache growth in servers with variable batch and prefill sizes. Nothing was gained in exchange: kernel time is unchanged (see Performance).Validation and unverified scope
Hardware: MI355X (gfx950), triton 3.8.0+git669b31ac, torch 2.12.0+rocm10.0. A/B between origin/main 8e69f10 and this branch, selected via
PYTHONPATH, on one idle GPU.Op tests for every touched file:
test_gemm_a8w8,test_gemm_a8w8_blockscale(gluon path),test_gemm_a16w16_gated,test_gemm_a16w8_blockscale,test_gemm_a16wfp4,test_gemm_a8w8_per_token_scale,test_gemm_a8wfp4,test_batched_gemm_a8w8,test_ff_a16w16_fused,test_fused_gemm_a8w8_blockscale_a16w16,test_fused_gemm_a8w8_blockscale_split_cat,test_fused_gemm_afp4wfp4_a16w16,test_fused_gemm_afp4wfp4_split_cat,test_fused_bmm_rope_kv_cache, and the atomic cases oftest_gemm_a16w16. Result: 5615 passed, 310 skipped, 8 failed.test_fused_gemm_a8w8_blockscale_a16w16.py::test_gemm[*-8192-512-256-7168], and they fail the same way on origin/main without this change."any"config for that shape (num_warps=2,waves_per_eu=1,matrix_instr_nonkdim=32) gives wrong values at rows 112–127 × columns 12 and 44 of every 128×128 fp8 output tile.waves_per_euto 0 gives correct results with the same register budget, so this looks like a separate compiler issue, unrelated to this PR.Outputs, base vs PR: saved for all 60 benchmark cases. 40 of 60 are bit-identical. The other 20 are all
gemm_a16w16_atomicwith split-K; they differ by about 1e-7 relative, the same amount base differs from itself across repeat calls (fp32 atomic order). Atomic cases withNUM_KSPLIT=1are bit-identical.ASM on gfx950:
gemm_a8w8BM=BN=128gemm_a16w16_atomicNUM_KSPLIT=1gemm_a16wfp4The extra prologue instructions compute the tile count and the XCD remap at runtime, once per workgroup.
gfx942, compiled offline: across 5 configs, same pattern as gfx950. The prologue gains ~23 scalar instructions with no vector instructions, the main loop has the same instructions in the same order, and register counts are unchanged.
Unverified:
gemm_a8w8,gemm_a16w16_atomicandgemm_a16wfp4were benchmarked. The others are covered only by op tests.PR-specific question
Most important boundary or failure mode, and the evidence covering it: the tile-to-program mapping.
remap_xcd, andpid // grid_mninfused_bmm_rope_kv_cache, now receive a runtime value. If that value differed from the old constexpr, programs would compute the wrong tiles, or tiles would be skipped or written twice.Evidence:
fused_gemm_a8w8_blockscale_a16w16andfused_gemm_afp4wfp4_a16w16it equalsGRID_MN_FP8/FP4 + GRID_MN_BF16.NUM_KSPLIT=1and split-K configs.Secondary concern: kernel time. The new runtime scalar work could slow kernels down. The A/B shows no change: geomean 0.998, within noise. The ASM shows the extra work is scalar-only and once per workgroup, with the MFMA main loop unchanged.
Changed op tests
None.