Skip to content

[Triton/Gluon] GEMM: compute GRID_MN at runtime instead of as a constexpr - #7591

Merged
vgokhale merged 3 commits into
mainfrom
cagri/triton_gemm_const_grid
Nov 13, 2026
Merged

vgokhale merged 3 commits into
mainfrom
cagri/triton_gemm_const_grid

Conversation

@cagrikymk

@cagrikymk cagrikymk commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

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+git669b31ac

Motivation

GRID_MN was filled in by @triton.heuristics and passed as a constexpr. Because it depends on M, each new value of cdiv(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 a remap_xcd(...) call whose result is discarded. For example, base cached 5 identical binaries of _gemm_a16wfp4_kernel for a single config.

Changes

For each kernel: remove the "GRID_MN": lambda args: ... heuristic, the GRID_MN: tl.constexpr parameter and, where present, the "GRID_MN" entry of make_kernel_repr. Each kernel now computes grid_mn = num_pid_m * num_pid_n from its existing num_pid_m / num_pid_n and passes that to remap_xcd (or to pid // grid_mn in fused_bmm_rope_kv_cache). No wrapper or config changes. 25 kernels in 17 files, +59 / −139 across the two commits.

  • GRID_MN value is used (now computed at runtime):
    • gemm_a8w8 (+ preshuffle)
    • gemm_a16w16_atomic
    • fused_gemm_afp4wfp4_a16w16 (+ preshuffle)
    • fused_gemm_afp4wfp4_split_cat (+ preshuffle)
    • fused_bmm_rope_kv_cache (fp4 and fp8)
    • gluon gfx950 gemm_a8w8 (+ preshuffled)
  • GRID_MN only passed to a remap_xcd call whose result is discarded (the call is kept, with the runtime value):
    • gemm_a16w16_gated
    • gemm_a8w8_per_token_scale
    • gemm_a8wfp4
    • ff_a16w16_fused_gated and ff_a16w16_fused_ungated
    • fused_gemm_a8w8_blockscale_a16w16
    • fused_gemm_a8w8_blockscale_split_cat (+ preshuffle)
    • gluon gfx950 gemm_a8w8_blockscale
  • GRID_MN never read:
    • gemm_a16wfp4 (+ preshuffle)
    • gemm_a16w8_blockscale (+ preshuffle)
    • batched_gemm_a8w8
  • Compiled kernel names lose the GRID_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.
  • Not touched: MoE, attention, gfx1250 gluon GEMMs (gemm_a8w8_blockscale and gemm_mxfp8 already 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 with config=None).

Hardware Workload/configuration Before (µs) After (µs) Change
MI355X (gfx950) gemm_a8w8 fp8 N=4096 K=4096, M=1 / 256 / 8192 5.22 / 11.94 / 155.82 5.32 / 11.90 / 156.10 +1.9% / −0.3% / +0.2%
MI355X (gfx950) gemm_a8w8 fp8 N=2112 K=7168, M=1 / 256 / 8192 9.25 / 20.30 / 167.29 9.20 / 20.30 / 167.22 −0.5% / 0.0% / 0.0%
MI355X (gfx950) gemm_a16w16_atomic N=896 K=3584, M=1 / 256 / 8192 3.94 / 12.66 / 206.06 3.84 / 12.72 / 206.40 −2.5% / +0.5% / +0.2%
MI355X (gfx950) gemm_a16wfp4 N=7168 K=2048, M=1 / 256 / 8192 8.98 / 21.46 / 374.25 8.91 / 21.54 / 374.93 −0.8% / +0.4% / +0.2%
MI355X (gfx950) All 60 cases (5 shapes, M = 1…8192) geomean 0.998 (min 0.970, max 1.040)

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_a8w8 N=K=4096, one fixed tuned config (BLOCK_SIZE_M=128), 63 values of M (129, 257, …, 8065), each with a different cdiv(M, 128). Counted with triton.knobs.compilation.listener.

Hardware Triton cache Before After
MI355X cold 63 compiles, 9.07 s total first-call time (135 ms per new M) 1 compile, 0.58 s (0.12 ms per new M)
MI355X warm on disk 63 cache loads (1.2 ms per new M) 1 cache load (0.12 ms per new M)
MI355X dense M = 1…1024 17 compiles 3 compiles
gfx942, compiled offline 32 values of M, BLOCK_SIZE_M=128 32 compiles 1 compile
gfx1250, compiled offline 32 values of M, BLOCK_SIZE_M=256 32 compiles 1 compile

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

  • Unit tests added or updated
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X

Tested on MI355X (gfx950). gfx942 and gfx1250 were only compiled offline, not run.

Documentation

  • Docstrings updated
  • User guide updated
  • Performance guide updated
  • No documentation change is needed, with the reason stated below

Internal kernel signature change only; the public wrappers are unchanged.

Dependencies

  • No new third-party dependencies added
  • New dependencies are listed and justified below

Breaking Changes

None for wrapper APIs. Compiled kernel names no longer contain GRID_MN_<value>. Calling one of these _kernels directly with GRID_MN= would now fail, but nothing in the repo does that.

PR Self-Review

Reviewed revision

1d07d64

Behavior and intent

GRID_MN changes 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 of test_gemm_a16w16. Result: 5615 passed, 310 skipped, 8 failed.

    • The 8 failures are 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.
    • Cause: the tuned gfx950 "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.
    • Changing waves_per_eu to 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_atomic with split-K; they differ by about 1e-7 relative, the same amount base differs from itself across repeat calls (fp32 atomic order). Atomic cases with NUM_KSPLIT=1 are bit-identical.

  • ASM on gfx950:

    Kernel / config Prologue MFMA main loop Epilogue Registers, LDS, spills
    gemm_a8w8 BM=BN=128 +24 scalar instructions same instructions, same order; only register names and branch labels differ identical identical
    gemm_a16w16_atomic NUM_KSPLIT=1 +25 scalar instructions same instructions, same order identical identical
    gemm_a16wfp4 byte-identical byte-identical byte-identical identical

    The 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:

  • Runtime on other GPUs: nothing was run on MI300X (gfx942), MI250X or gfx1250, so correctness and performance there are untested.
  • gfx1250 ASM: offline compiles show changes I have not analyzed: vector instructions in the prologue, small main-loop and epilogue differences in some split-K configs, and different register counts.
  • Unbenchmarked kernels: of the touched kernels, only gemm_a8w8, gemm_a16w16_atomic and gemm_a16wfp4 were 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, and pid // grid_mn in fused_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:

  • Same value by construction: the runtime expression is identical to the old heuristic. For fused_gemm_a8w8_blockscale_a16w16 and fused_gemm_afp4wfp4_a16w16 it equals GRID_MN_FP8/FP4 + GRID_MN_BF16.
  • Bit-identical outputs: base and PR outputs match bit for bit on every deterministic benchmark case, across M = 1…8192 and both NUM_KSPLIT=1 and split-K configs.
  • Op tests pass: for all touched files, except the failure that also fails on main.

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.

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.
@cagrikymk
cagrikymk requested a review from a team November 6, 2026 19:34
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the gfx1201 ROCm runner
ci:multi-gpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 7591 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

@github-actions github-actions Bot changed the title [Triton] GEMM: compute GRID_MN at runtime instead of as a constexpr [Triton/Gluon] GEMM: compute GRID_MN at runtime instead of as a constexpr Nov 6, 2026
@aiter-bot

aiter-bot commented Nov 6, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision 5eb0579001a0 · mode shadow

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

  1. What behavior does this PR change, and why is the existing behavior insufficient?
  2. What did you validate, and what remains unverified? Include commands, hardware/configuration and results.
  3. What is the most important boundary or failure mode introduced by this change, and what evidence covers it?
  4. For every changed op_tests/ file, what behavior does it cover and why is it not redundant? Write None if no op test changed.

Edit the PR Self-Review section in the PR description. Include the current revision SHA; this comment refreshes automatically.

Missing or invalid answers

  • FAIL — Reviewed revision must contain at least the first 7 characters of the current head SHA

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

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.
@lburzawa
lburzawa self-requested a review November 6, 2026 20:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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

@zufayu
zufayu requested review from a team and Dewei-Wang-sh November 7, 2026 06:46
@vgokhale
vgokhale merged commit a35ba88 into main Nov 13, 2026
@vgokhale
vgokhale deleted the cagri/triton_gemm_const_grid branch November 13, 2026 21:05
iraj465 added a commit to iraj465/aiter that referenced this pull request Nov 18, 2026
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]>
iraj465 added a commit to iraj465/aiter that referenced this pull request Nov 18, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants