Repository navigation
[Triton/Gluon] Add config-aware repr to the attention kernels - #5097
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
PR title tags: |
There was a problem hiding this comment.
Pull request overview
Adds config-aware repr strings (via make_kernel_repr) to a set of Triton/Gluon attention-related kernels so compiled artifacts and traces encode key compile-time specialization parameters.
Changes:
- Imports
make_kernel_reprand defines per-kernel_..._reprcallables capturing relevanttl.constexpr/ compile-time keys. - Applies
repr=to multiple@triton.jit/@gluon.jitkernel entrypoints across attention kernels. - Extends kernel naming coverage to decode, unified attention, sparse attention, FP8 MQA logits, and related utilities.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| aiter/ops/triton/attention/mla_decode.py | Adds repr= for MLA decode kernels to improve specialization-aware naming. |
| aiter/ops/triton/_triton_kernels/attention/unified_attention.py | Adds config-aware repr= for unified attention 2D kernel. |
| aiter/ops/triton/_triton_kernels/attention/unified_attention_sparse_mla.py | Adds config-aware repr= for sparse MLA unified attention kernel. |
| aiter/ops/triton/_triton_kernels/attention/sparse_attention_dsv4.py | Adds repr= for the autotuned sparse attention prefill kernel. |
| aiter/ops/triton/_triton_kernels/attention/pa_mqa_logits.py | Adds repr= for paged MQA logits Triton kernels. |
| aiter/ops/triton/_triton_kernels/attention/lean_atten_paged.py | Adds repr= for Lean Attention paged decode kernel. |
| aiter/ops/triton/_triton_kernels/attention/fp8_mqa_logits.py | Adds repr= for FP8 MQA logits Triton kernel. |
| aiter/ops/triton/_triton_kernels/attention/fav3_sage_attention.py | Adds repr= for SAGE attention forward kernel. |
| aiter/ops/triton/_triton_kernels/attention/fav3_sage_attention_mxfp4.py | Adds repr= for MXFP4 SAGE attention forward kernel. |
| aiter/ops/triton/_triton_kernels/attention/block_lut.py | Adds repr= for block attention mask → LUT kernel. |
| aiter/ops/triton/_gluon_kernels/gfx950/attention/fp8_mqa_logits.py | Adds repr= for gfx950 Gluon FP8 MQA logits kernel. |
| aiter/ops/triton/_gluon_kernels/gfx1250/attention/fp8_mqa_logits.py | Adds repr= for gfx1250 Gluon FP8 MQA logits kernel. |
Suppressed comments (2)
aiter/ops/triton/attention/mla_decode.py:304
- _fwd_grouped_kernel_stage1 is launched with tuning kwargs (num_warps/num_stages and, on ROCm, waves_per_eu/matrix_instr_nonkdim/kpack), but the repr key list omits them. Add these keys so different tuned variants don’t share the same repr name.
_fwd_grouped_kernel_stage1_repr = make_kernel_repr(
"_fwd_grouped_kernel_stage1",
[
"kv_group_num",
"q_head_num",
"BLOCK_DMODEL",
aiter/ops/triton/attention/mla_decode.py:590
- _fwd_kernel_stage2 is launched with num_warps/num_stages and (on ROCm) waves_per_eu/matrix_instr_nonkdim/kpack, but the repr key list omits those compile-time variants. Include the tuning keys so repr names differentiate the compiled specializations.
_fwd_kernel_stage2_repr = make_kernel_repr(
"_fwd_kernel_stage2",
[
"NUM_KV_SPLITS",
"BLOCK_DV",
"Lv",
],
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.
Suppressed comments (4)
aiter/ops/triton/_triton_kernels/attention/unified_attention.py:59
kernel_unified_attention_2dis launched with a per-shape config that includesnum_warps,num_stages, andwaves_per_eu(seeaiter/ops/triton/attention/unified_attention.py:149-156), but those keys are not included in themake_kernel_repr(...)key list here. This can produce identical compiled artifact names for different kernel specializations, defeating the purpose of a config-awarerepr(compare tokernel_unified_attention_3d_reprincluding these keys in this file).
_kernel_unified_attention_2d_repr = make_kernel_repr(
"kernel_unified_attention_2d",
[
"num_query_heads",
"num_queries_per_kv",
aiter/ops/triton/_triton_kernels/attention/unified_attention_sparse_mla.py:47
- This kernel is launched with compile-time launch parameters
num_warpsandnum_stages(seeaiter/ops/triton/attention/unified_attention_sparse_mla.py:58-95), but the newmake_kernel_repr(...)list here does not include them. That can cause different specializations to share the samereprname, making profiling/debugging ambiguous.
_kernel_unified_attention_sparse_mla_2d_repr = make_kernel_repr(
"_kernel_unified_attention_sparse_mla_2d",
[
"num_query_heads",
"num_queries_per_kv",
aiter/ops/triton/_triton_kernels/attention/sparse_attention_dsv4.py:270
_sparse_attn_prefill_kernelis autotuned over multiple configurations that varywaves_per_eu,matrix_instr_nonkdim,num_warps, andnum_stages(see_get_prefill_autotune_configs()just above), but the newmake_kernel_repr(...)list does not include those tuning keys. This can lead to identicalreprnames for different autotuned variants.
_sparse_attn_prefill_kernel_repr = make_kernel_repr(
"_sparse_attn_prefill_kernel",
[
"HAS_ATTN_SINK",
"BLOCK_H",
aiter/ops/triton/_triton_kernels/attention/pa_mqa_logits.py:232
_deepgemm_fp8_paged_mqa_logits_stage1is launched with the compile-time kwargwaves_per_eu(seeaiter/ops/triton/attention/pa_mqa_logits.py:253-256), but the newmake_kernel_repr(...)key list here does not include it. IfWavePerEUchanges, different specializations will share the samereprname.
_deepgemm_fp8_paged_mqa_logits_stage1_repr = make_kernel_repr(
"_deepgemm_fp8_paged_mqa_logits_stage1",
[
"ChunkQ",
"ChunkK",
19 launchable attention kernels had no repr: the five real pa_mqa_logits kernels, the four defined inline in attention/mla_decode.py, block_lut, both fav3_sage variants, fp8_mqa_logits (triton plus both gluon arch copies), lean_atten_paged, sparse_attention_dsv4, unified_attention's 2d kernel, and unified_attention_sparse_mla. The three _gluon_deepgemm_* entries in _triton_kernels/attention/pa_mqa_logits.py are deliberately left alone: their bodies are just "pass" -- they exist only so an AOT load has a matching signature -- and both triton.compile call sites pass an options dict that already sets "name" to a literal, so the repr callback is never consulted on that path. Naming for that op is driven by options["name"], not by repr. kernel_unified_attention_2d does NOT get the 3d kernel's key list: six of those keys do not exist on the 2d kernel and would have rendered NONE. It uses its own constexpr set. MAX_SEQLENS_Q / MAX_SEQLENS_K are dropped from both sage reprs -- MAX_SEQLENS_Q is exactly the grid extent at the launch site, so including it would vary the trace name per sequence length while the tuned config stayed put. (Noting the other side of that argument: flash_attn_triton_amd lists both in its autotune keys for the same kernel family.) Key lists follow the neighbouring kernels in each subtree: tuned block/tile sizes and the meaningful compile-time flags, with runtime pointers, strides and grid-extent constexprs excluded (the launch site was checked before deciding on each one). A key that is not a real parameter renders as NONE in every trace name, so every list was re-parsed from the file and cross-checked against the kernel signature, then rendered through the real make_kernel_repr to confirm no NONE appears. Bare @triton.jit(repr=...) is used throughout, matching the majority of the reprs already in the tree. Full-tree ruff (0.16.0, the CI pin) and black report exactly the same findings as main. Keys whose value can be negative or a non-integral float are deliberately excluded. make_kernel_repr renders the value into the kernel name, and Triton then rejects the name -- "CompilationError: invalid function identifier" -- so e.g. a 2.5 becomes "..._2.5" and the name breaks at the dot, and a -448 breaks at the minus. Every key list here was checked against the values its launch sites actually pass. Dropped on that basis: logit_cap on the two mla_decode stage-1 kernels. Note the pre-existing reprs in _triton_kernels/attention/extend_attention.py and mla_decode_rope.py still list logit_cap; those are untouched here, but they carry the same latent failure if a caller ever passes a non-integral cap.
cb35f8b to
a78cbdc
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.
Suppressed comments (2)
aiter/ops/triton/attention/mla_decode.py:312
- The repr key list for _fwd_grouped_kernel_stage1 omits logit_cap even though it is a tl.constexpr argument. This makes the compiled artifact/trace name ambiguous across different logit_cap specializations.
"NUM_KV_SPLITS",
"PAGE_SIZE",
"Lk",
"Lv",
],
aiter/ops/triton/_triton_kernels/attention/sparse_attention_dsv4.py:273
- This kernel is autotuned, but the repr key list does not include the autotune-selected meta-parameters (e.g., matrix_instr_nonkdim, waves_per_eu, num_warps, num_stages). Without these, trace/compiled artifact names won't identify which tuned config was selected.
"HAS_ATTN_SINK",
"BLOCK_H",
"BLOCK_D",
"BLOCK_K",
],
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.
Suppressed comments (2)
aiter/ops/triton/_triton_kernels/attention/unified_attention.py:64
- kernel_unified_attention_2d has a tl.constexpr parameter
scale, which is passed fromsoftmax_scaleat the call site. Becausescaleis not included in the repr key list, differentsoftmax_scalevalues can produce the same compiled function identifier, risking specialization/name collisions.
"BLOCK_SIZE",
"TILE_SIZE",
"HEAD_SIZE",
"HEAD_SIZE_PADDED",
"USE_ALIBI_SLOPES",
aiter/ops/triton/_triton_kernels/attention/fp8_mqa_logits.py:12
- _fp8_mqa_logits_kernel specializes on several stride parameters marked tl.constexpr (e.g., stride_q_h/stride_q_d/stride_kv_d/stride_w_h) that are passed from tensor strides at the call site. Since these constexprs are not included in the repr key list, different layouts/strides can share the same compiled function identifier.
[
"NUM_HEADS",
"HEAD_SIZE",
"BLOCK_KV",
],
sage_fwd's name ran to 350 characters across 22 keys, and ten of them could not tell two kernels apart. Two of those ten were also breaking the build. make_kernel_repr puts each value straight into the compiled kernel name, and Triton checks that name against ^[a-zA-Z_][a-zA-Z0-9_]*$. WINDOW_SIZE_LEFT and WINDOW_SIZE_RIGHT are -1 whenever no sliding window is requested -- the common case, not an edge case; the kernel body itself branches on WINDOW_SIZE_LEFT < 0 -- so every non-windowed launch asked Triton to compile sage_fwd_..._USE_SLIDING_WINDOW_0_WINDOW_SIZE_LEFT_-1_WINDOW_SIZE_RIGHT_-1_... and the '-' is not legal in a function identifier. That is the 400 failures in test_fav3_sage.py. Nothing in the tree asks for a sliding window -- both public entry points default window_size to (-1, -1) and the bench passes it explicitly -- and USE_SLIDING_WINDOW already carries the bit that says which masking path compiled, so the two size keys go. The other eight carried no information to begin with: IS_VARLEN, ENABLE_DROPOUT, RETURN_SCORES, USE_ALIBI, USE_SEQUSED are passed False and USE_EXP2 is passed True, as literals, at the only launch site (aiter/ops/triton/attention/fav3_sage.py:514). Six identical segments in every name the kernel has ever produced. BLOCK_DMODEL_QK and BLOCK_DMODEL_V are max(16, next_pow2(ACTUAL_BLOCK_DMODEL_*)), so the ACTUAL keys already determine them. Keeping ACTUAL and dropping BLOCK is the direction that loses nothing: the mapping is many-to-one, and head sizes 1, 2, 4 and 16 all pad to 16, so the reverse choice would have merged four shapes the test matrix actually exercises. sage_fwd_mxfp4 gets the same treatment, 16 keys to 11. Q_DTYPE_STR and K_DTYPE_STR are both the literal "e2m1" at its only launch site, which the base name already says. Checked against the config space the wrappers can actually build -- every head-count pair, head-size pair, causal, sliding-window, bias, block-sparse, return-lse and tuned BLOCK_M/BLOCK_N/PRE_LOAD_V combination, 50,688 renders: every name is a legal identifier, and no two specializations share one. sage_fwd 22 keys / 350 chars -> 12 keys / 184 chars sage_fwd_mxfp4 16 keys / 253 chars -> 11 keys / 168 chars Both key lists now carry a comment saying that anything added to them has to render inside [A-Za-z0-9_], and that the window sizes in particular must not come back.
2f0bb59 to
282beee
Compare
Remove comments regarding constexprs for kernel specialization.
Removed comments about key rendering and dtype handling in sage_fwd.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.
Suppressed comments (1)
aiter/ops/triton/_triton_kernels/attention/sparse_attention_dsv4.py:273
- The autotuned prefill configs vary
matrix_instr_nonkdim(see_get_prefill_autotune_configs()), but the kernelreprsuffix currently omits it. That means different compiled specializations can show up under the same trace name, which defeats the purpose of config-aware reprs (seeaiter/ops/triton/README.md“Config-aware kernel names in traces”). Includematrix_instr_nonkdim(andwaves_per_eu, if you want full parity with other kernels) in themake_kernel_reprkey list.
_sparse_attn_prefill_kernel_repr = make_kernel_repr(
"_sparse_attn_prefill_kernel",
[
"HAS_ATTN_SINK",
"BLOCK_H",
"BLOCK_D",
"BLOCK_K",
],
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.
Suppressed comments (2)
aiter/ops/triton/attention/mla_decode.py:55
- The kernel repr for
_fwd_kernel_stage1omitslogit_cap, even though it is atl.constexprargument. This means differentlogit_capspecializations will produce the same repr string, making compiled artifact names/traces ambiguous.
"NUM_KV_SPLITS",
"PAGE_SIZE",
"Lk",
"Lv",
],
aiter/ops/triton/_triton_kernels/attention/sparse_attention_dsv4.py:273
_sparse_attn_prefill_kernelis autotuned overwaves_per_eu,matrix_instr_nonkdim,num_warps, andnum_stages(via thetriton.Configentries), but the repr only includes BLOCK/HAS keys. Add the autotune/compile-option keys so different tuned variants get distinct repr strings in compiled artifacts/traces.
"HAS_ATTN_SINK",
"BLOCK_H",
"BLOCK_D",
"BLOCK_K",
],
_fwd_kernel_stage1, _fwd_grouped_kernel_stage1, _fwd_kernel_stage2 and
_csr_to_dense_kernel say nothing about which op they belong to once they are
out of the source file and into a JIT artifact or a profiling trace.
_fwd_kernel_stage2 was not merely generic, it was ambiguous: two different
kernels shipped that base name, attention/mla_decode.py and
_triton_kernels/attention/mla_decode_rope.py, and their key lists open with the
same three entries (NUM_KV_SPLITS, BLOCK_DV, Lv), so a trace line had to be read
to the end to tell which op produced it. Prefixed both.
Only the base_name argument to make_kernel_repr changes. The Python functions
keep their names, so no import, launch site or test moves, and nothing else in
the tree reads these strings -- no tuned config file, AOT metadata path or cache
key is derived from a repr base name.
attention/mla_decode.py
_fwd_kernel_stage1 -> mla_decode_fwd_kernel_stage1
_fwd_grouped_kernel_stage1 -> mla_decode_fwd_grouped_kernel_stage1
_fwd_kernel_stage2 -> mla_decode_fwd_kernel_stage2
_csr_to_dense_kernel -> mla_decode_csr_to_dense_kernel
_triton_kernels/attention/mla_decode_rope.py
_fwd_kernel_stage2 -> mla_decode_rope_fwd_kernel_stage2
A scan of the 256 distinct make_kernel_repr base names in the tree finds seven
still shared by more than one file: _fwd_kernel (extend_attention, pa_prefill,
prefill_attention), _bwd_preprocess (the two MHA backward kernels), and five
arch or backend variants of a single logical kernel (_pa_decode_sparse,
_pa_decode_sparse_reduce, _mla_decode_fwd_kernel, _mla_decode_fwd_reduce_kernel,
_gluon_fp8_mqa_logits_kernel). This clears the eighth.
Reported by @brunomazzottiamd in review of #5097.
Conflicts were all in fp8_mqa_logits, where both sides had moved: - launcher: main replaced the byte-based buffer-op gate with an int32 element offset bound (#5121) and gated BLOCK_M=2 on use_buffer_store. Took main's bound, since it is the better general rule and the branch only overrode the result for gfx950; kept the branch's gfx950 tuning block, which supersedes main's. The BLOCK_M=2 guard is vacuous on gfx950 -- the rebasing makes buffer stores unconditional there -- so it is noted rather than applied. Note the auto-merge left both gate computations in place, with the old byte rule silently overwriting the new one; the old block is removed. - gluon kernel: kept main's config-aware repr (#5097) and added the branch's M_CHUNK, UNROLL and RELAXED_STORE to its field list. - tests: took main's file as-is. Its test_fp8_mqa_logits_logits_past_2gib covers the logits-past-2-GiB case; the branch's large_kv, long_query and kv_over_2gib tests are dropped. 146 tests pass; black and ruff clean; the gfx950 launcher still picks num_warps=1/BLOCK_KV=32 only at block_m == 1 and seq_len > 4096, and RELAXED_STORE only when clean_logits is False. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
* [Triton] Prefix the mla_decode kernel names with the module
_fwd_kernel_stage1, _fwd_grouped_kernel_stage1, _fwd_kernel_stage2 and
_csr_to_dense_kernel say nothing about which op they belong to once they are
out of the source file and into a JIT artifact or a profiling trace.
_fwd_kernel_stage2 was not merely generic, it was ambiguous: two different
kernels shipped that base name, attention/mla_decode.py and
_triton_kernels/attention/mla_decode_rope.py, and their key lists open with the
same three entries (NUM_KV_SPLITS, BLOCK_DV, Lv), so a trace line had to be read
to the end to tell which op produced it. Prefixed both.
Only the base_name argument to make_kernel_repr changes. The Python functions
keep their names, so no import, launch site or test moves, and nothing else in
the tree reads these strings -- no tuned config file, AOT metadata path or cache
key is derived from a repr base name.
attention/mla_decode.py
_fwd_kernel_stage1 -> mla_decode_fwd_kernel_stage1
_fwd_grouped_kernel_stage1 -> mla_decode_fwd_grouped_kernel_stage1
_fwd_kernel_stage2 -> mla_decode_fwd_kernel_stage2
_csr_to_dense_kernel -> mla_decode_csr_to_dense_kernel
_triton_kernels/attention/mla_decode_rope.py
_fwd_kernel_stage2 -> mla_decode_rope_fwd_kernel_stage2
A scan of the 256 distinct make_kernel_repr base names in the tree finds seven
still shared by more than one file: _fwd_kernel (extend_attention, pa_prefill,
prefill_attention), _bwd_preprocess (the two MHA backward kernels), and five
arch or backend variants of a single logical kernel (_pa_decode_sparse,
_pa_decode_sparse_reduce, _mla_decode_fwd_kernel, _mla_decode_fwd_reduce_kernel,
_gluon_fp8_mqa_logits_kernel). This clears the eighth.
Reported by @brunomazzottiamd in review of #5097.
* Fix mla_decode_rope stage1 repr name
No description provided.