Skip to content

[Triton/Gluon] Restructure Unified Attention Kernel (merging 2d/3d and modularization) - #6337

Merged
cagrikymk merged 20 commits into
mainfrom
cagri/unify_UA_2D_3D
Dec 2, 2026
Merged

cagrikymk merged 20 commits into
mainfrom
cagri/unify_UA_2D_3D

Conversation

@cagrikymk

@cagrikymk cagrikymk commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

[Triton] unified_attention: merge the 2D and 3D kernels into one modular kernel, and allow a shuffled-KV tile that differs from the page size.

RDNA performance is verified by @amd-xavierwang , no big delta for unshuffled cases (the path that is taken for that arch).

Motivation

  • Two copies of one kernel. The Triton unified attention has two kernels,
    kernel_unified_attention_2d (prefill and unsplit decode) and kernel_unified_attention_3d
    (split-KV decode). They are near-copies of each other, about 500 and 400 lines, and have to be
    kept in sync by hand. Fixes landed on one and not the other: main's 3D split fixes are ported
    here.
  • The shuffled tile was pinned to the page. With a pre-shuffled KV cache, the wrapper forced
    TILE_SIZE = block_size, because the kernels read a shuffled tile as one contiguous page. The
    tuned tile was therefore ignored for shuffled layouts. That is very slow for head-256 prefill,
    where the tuned tile is 32 and the page is 64.

Technical Details

Only two files change: aiter/ops/triton/_triton_kernels/attention/unified_attention.py and its
wrapper, aiter/ops/triton/attention/unified_attention.py.

  • One kernel. kernel_unified_attention replaces kernel_unified_attention_2d and
    kernel_unified_attention_3d.
    • The grid is selected by the GRID_3D constexpr, and NUM_SEGMENTS_PER_SEQ sets the split.
    • The shared steps are helpers: initial_row_max, attention_loop, and the KV tile
      address and load steps.
    • Kernel names in profiles keep the _2d / _3d suffix, via the kernel repr.
  • Split vs unsplit. A 3D launch with NUM_SEGMENTS > 1 writes fp32 partials
    (segm_output / segm_max / segm_expsum), and reduce_segments applies output_scale.
    Unsplit, it writes output directly and applies out_scale. The 2D launch passes None for
    the segment pointers.
  • KV loader. It is split into an address step and a load step for K and for V. A shuffled
    tile may now span pages: it is addressed flat, and V keeps its page rows.
  • TILE_SIZE for shuffled KV.
    • The wrapper no longer pins TILE_SIZE = block_size on either grid.
    • The fp8 shuffled guard changes from block_size >= 32 to TILE_SIZE >= 32.
    • The page must still be a power of 2.
  • Masking. The causal mask is built before the QK dot on both grids, and the sliding window
    is folded into the causal mask.
  • Rebase onto main 1eab5ae. There was one conflict, with the gfx12 FILL_MIN_SEGMENTS
    block ([Triton/Gluon] [GFX12] Add SPEC_DECODE mode to UA with tunned Qwen35 config #7100). main's block was kept and reads the config tile size, now named TILE_SIZE.
    The shuffled pin was dropped.

Test Plan

A/B of main 1eab5ae (baseline) against this branch rebased onto it (pr). The scripts and full
data are in the A/B folder: REPORT.md, RUN_DETAILS.md and RESULTS_DETAIL.md.

  • GPUs: MI355X (gfx950) and MI300X (gfx942); both machines run torch 2.12.0+rocm10.0.0,
    HIP 7.15 and triton 3.8.0. backend="triton" is forced; Gluon is not touched.
  • Matrix: 416 cases per arm per GPU.
    • Configs: bf16 at head 64 / 128 / 256, and fp8 at head 128 (e4m3fn on gfx950, e4m3fnuz
      on gfx942).
    • 64 query and 8 KV heads; page 64.
    • Full causal, and sliding windows 128 / 512 / 1024.
    • Decode: batch 4 / 32 / 128 × kv 1K / 8K / 32K. Prefill: 8×1K, 16×1K, 1×8K and 2×8K, with
      q = kv.
    • Plain and pre-shuffled KV.
  • Setup: k and v are each over 2 GiB (the production non-buffer-load path), with a scattered
    block table.
  • Timing: 25 warm-up calls, then 100 calls, each after a 512 MiB cache flush. The time is the
    median profiler device time of the attention kernel plus reduce_segments. Both arms run back
    to back on the same GPU, one shard at a time, and noisy cases are retried.
  • Accuracy: max |out − fp32 ref_paged_attn| per arm, plus max |pr − baseline| on a 64K
    output sample per case.
  • Noise: MI300X was run twice (mi300_r1, mi300_r2).
  • Codegen (gfx942): the two arms' Triton cache entries were compared for all 96 kernel
    variants built by both: launch config, VGPRs, spills, scratch, LDS and instruction count.
  • Not run yet: op_tests/triton_tests/attention/test_unified_attention.py (5,145 tests).

Test Result

  • Correctness is unchanged on both GPUs.

    • pr's error against the fp32 reference is never worse than baseline's.
    • The largest pr-vs-baseline difference is 3.9e-3, bf16 rounding.
    • There are no NaNs, and every case takes the same grid in both arms.
    • There were 0 errors.
  • Same specialization: 400 of 416 cases per GPU launch the same kernel constexprs in both
    arms. The other 16 are the h256 shuffled prefills, which now use the tuned 32-token tile
    instead of the 64-token page.

  • Performance, as geomean of pr / baseline − 1 (negative = pr faster):

    MI355X MI300X
    h256 shuffled prefill −34% to −44% −58% to −70%
    sliding-window prefill (per group, outside h256) up to −16% up to −11%
    full-attention prefill −0.5% to +1.0% −2.1% to +1.0%
    decode, most groups −0.3% to −4% −2% to −6%

    For example, h256 shuffled 1×8K×8K full-attention prefill goes from 57.0 to 16.7 ms on MI300X.

  • MI300X-only regressions. Both reproduce in the repeat run. The same rows are flat or faster
    on MI355X.

    • bf16 h128 shuffled decode, window 512 / 1024: +6% to +16% at batch 4 and 32.
      For example, B4 at 8K with window 1024 goes from 29.4 to 33.7 µs. Batch 128 is flat.
    • bf16 h256 plain windowed decode: +1% to +5%, for example B128 with window 1024:
      1469 → 1540 µs.
  • Codegen on gfx942.

    • Across all 96 paired kernel variants, warps, stages and LDS are identical.
    • pr's 2D kernels have 0.90–1.02× baseline's instructions (median 0.94), and 3D kernels
      0.95–1.00× (median 0.98). reduce_segments is identical.
    • VGPRs change by −20 to +34. No variant spills in pr that didn't spill in baseline, and 4
      stop spilling.
    • The two regressed kernels have fewer instructions, and the same MFMA, load and branch
      counts, as baseline. Head 256 spills heavily in both arms; pr reports 239 spilled VGPRs vs
      226 but emits slightly fewer scratch stores (281 vs 288).
    • Static stats therefore don't explain either regression. The next step is an instruction-level
      profile of the h128 shuffled window-1024 B4 kernel.
  • Run-to-run noise on MI300X: per-row Δ moves by a median of 0.8 points (p90 3.0), and by up
    to 11 points on kernels under 20 µs.

    MI355 Results

    Geomean pr / baseline − 1 (negative = pr faster)

config phase layout full w128 w512 w1024 all
bf16 h64 decode plain +0.3% -2.3% -2.0% -1.8% -1.5%
bf16 h64 decode shuffled -0.3% -1.3% -1.3% -1.3% -1.0%
bf16 h64 prefill plain -0.1% -7.4% -11.8% -15.7% -8.9%
bf16 h64 prefill shuffled +1.0% -2.4% -7.1% -9.7% -4.6%
bf16 h128 decode plain -0.4% -3.0% -1.8% -1.5% -1.7%
bf16 h128 decode shuffled -0.3% -2.6% -1.6% -1.1% -1.4%
bf16 h128 prefill plain +0.6% -3.8% -6.4% -7.6% -4.3%
bf16 h128 prefill shuffled +0.6% +1.5% -3.7% -5.0% -1.7%
bf16 h256 decode plain -2.3% -1.6% -0.8% -0.4% -1.3%
bf16 h256 decode shuffled +0.2% -1.6% +0.1% +1.3% -0.0%
bf16 h256 prefill plain -0.5% -1.6% -2.4% -2.8% -1.8%
bf16 h256 prefill shuffled -33.9% -44.3% -37.8% -37.0% -38.4%
fp8 h128 decode plain -0.2% -4.2% -4.3% -4.3% -3.3%
fp8 h128 decode shuffled +0.3% -3.5% -1.8% -0.8% -1.4%
fp8 h128 prefill plain +0.2% -3.3% -8.8% -11.3% -5.9%
fp8 h128 prefill shuffled +0.2% -0.8% -3.8% -6.6% -2.8%

MI300 Results

Geomean pr / baseline (negative = pr faster)

config phase layout full w128 w512 w1024 all
bf16 h64 decode plain +0.4% -3.2% -2.8% -2.2% -2.0%
bf16 h64 decode shuffled +0.0% -5.8% -3.2% -4.1% -3.3%
bf16 h64 prefill plain -0.3% -5.2% -7.4% -5.5% -4.6%
bf16 h64 prefill shuffled -2.1% -3.8% -4.9% -8.3% -4.8%
bf16 h128 decode plain -10.1% -3.4% -5.5% -6.2% -6.3%
bf16 h128 decode shuffled -3.6% -5.3% +4.4% +7.6% +0.6%
bf16 h128 prefill plain -0.5% -10.7% -9.0% -10.1% -7.7%
bf16 h128 prefill shuffled +1.0% -4.7% -7.5% -8.4% -5.0%
bf16 h256 decode plain -5.3% +3.5% +2.8% +3.1% +1.0%
bf16 h256 decode shuffled +1.7% -2.1% -4.4% -5.3% -2.6%
bf16 h256 prefill plain -1.2% -0.1% -0.8% -1.1% -0.8%
bf16 h256 prefill shuffled -69.5% -58.4% -60.0% -60.5% -62.4%
fp8 h128 decode plain +0.7% -5.3% -3.7% -4.2% -3.1%
fp8 h128 decode shuffled +0.7% -6.5% -4.4% -3.7% -3.5%
fp8 h128 prefill plain -1.1% -4.6% -5.6% -6.4% -4.4%
fp8 h128 prefill shuffled -0.4% -3.4% -2.8% -4.0% -2.7%

Submission Checklist

PR Self-Review

Reviewed revision

31fb65704f01: the branch rebased onto main 1eab5ae. It has 14 commits; the net diff touches
aiter/ops/triton/_triton_kernels/attention/unified_attention.py and
aiter/ops/triton/attention/unified_attention.py only.

Behavior and intent

  • One kernel. kernel_unified_attention_2d and kernel_unified_attention_3d are replaced by
    one modular kernel_unified_attention, built from AttentionConfig, KVLoader and
    AttentionProgram aggregates, with one attention_loop for unmasked and masked tiles.
    • GRID_3D selects the split-KV program order.
    • NUM_SEGMENTS_PER_SEQ > 1 writes partials for reduce_segments.
    • Configs, launch sections (attn_2d / attn_3d) and the 2d / 3d kernel names are unchanged.
    • The existing behavior meant two copies of the same algorithm that had drifted: the masking
      order differed between them, and main's 3d split fixes landed in only one. That made every fix
      a double change.
  • Shuffled KV tiles no longer have to equal the page.
    • Before, the wrapper pinned TILE_SIZE = block_size for the shuffled layout, so tuned shuffled
      configs (e.g. gfx950 SHUF entries) were ignored.
    • A tile inside one page now reads its slice of the page. A tile spanning pages reads each page
      as a contiguous chunk, addressed flat so the K and V loads keep their 16-byte direct-to-LDS
      copies on gfx950.
    • The fp8 shuffled requirement moves from block_size >= 32 to TILE_SIZE >= 32, so 16-token
      pages work with fp8.
  • Other changes.
    • The sliding window is folded into the causal mask: one unsigned compare, built before the QK
      dot. Output is unchanged.
    • A single-segment 3d launch now stores the output like the 2d launch rather than writing the
      padded segment layout into it. This fixes out-of-bounds writes for non-power-of-2 value heads
      and adds the fp8 clamp.
    • Main's 3d split fixes are ported into the merged kernel:

Validation and unverified scope

Validated: an A/B against main 1eab5ae on MI355X (gfx950), torch 2.12.0+rocm10.0.0,
HIP 7.15, triton 3.8.0, backend="triton" forced.

  • Matrix: 416 cases per arm.
    • Configs: bf16 at head 64 / 128 / 256, and fp8 × fp8 at head 128. All use 64 query / 8 KV heads
      and a page size of 64.
    • Windows: full causal, and sliding windows 128 / 512 / 1024.
    • Decode: C = 4 / 32 / 128 × KV length 1K / 8K / 32K. Prefill: 8×1K×1K, 16×1K×1K, 1×8K×8K,
      2×8K×8K.
    • KV layout: plain and pre-shuffled.
    • Every case scatters the block table over a KV pool where k and v each exceed 2 GiB.
  • Timing: 25 warm-up calls, then 100 timed calls, each after a 512 MiB L2 flush.
    torch.profiler device time of the unified_attention and reduce_segments kernels only. Both
    arms run on the same GPU, one shard at a time.
  • Results:
    • Accuracy: 0 errors and 0 NaNs. The max error against an fp32 ref_paged_attn is never
      worse than main's. The largest pr-vs-main output difference is 3.9e-3. Every case selects
      the same grid in both arms.
    • Shuffled h256 prefill: 32–45% faster.
    • Sliding-window prefill: faster as the window grows. For example, h64 plain is −7% / −12% /
      −16% at windows 128 / 512 / 1024, and fp8 h128 plain is −3% / −9% / −11%.
    • Decode: mostly 0.3–4% faster.
    • Full-attention prefill: within ±1%.
    • Worst rows: +3% to +5.6%, all on 10–100 µs decode kernels. Across two runs, the same case
      varied by a median of 0.6 points (p90 1.8), and kernels under 20 µs by up to 14 points, so
      these rows are within noise.

Unverified:

  • Shuffled tiles spanning several pages (TILE_SIZE > page): no A/B case selected one. All
    shuffled cases used page 64, with tile 64 or 32.
  • Page sizes other than 64.
  • Variable-length batches and chunked prefill: prefill was fresh, with no cached context, and
    decode batches had uniform lengths.
  • Feature flags: sinks, softcap, ALiBi, qq_bias, output_scale, uint8 / FP4 KV caches and
    non-power-of-2 value heads.

PR-specific question

The most important boundary: a shuffled KV tile whose TILE_SIZE differs from the page size.
This is the new addressing path. A wrong K or V un-shuffle there would silently return wrong
attention, not crash.

  • Tile smaller than the page: covered by the A/B. With page 64, the tuned gfx950 config gives
    TILE_SIZE=32 for bf16 h256 shuffled prefill; main pinned it to 64. All 16 such cases (4
    shapes × 4 windows):
    • match main's fp32-reference error exactly, where the reference fits;
    • match main's output to 3.9e-3;
    • are where the 32–45% speedup comes from.
  • Tile spanning several pages: has no A/B coverage. It relies on the shuffled-tile test added
    in commit dd0db30 (2d grid, lengths that end mid-page, fp8 on both tile cases). That test was
    later removed from this PR, so this path currently has no test in the tree.
  • Secondary boundary: the single-segment 3d store change. It is covered by the A/B's 3d
    decode cases (same grid, same reference error), but not for non-power-of-2 value heads.

Changed op tests

None. The branch's commits added op tests and then removed them again (2a899c3, 7ff8d2e),
so the net diff against main changes no file under op_tests/.

@github-actions

github-actions Bot commented Oct 9, 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: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: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
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 6337 --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.

@aiter-bot

aiter-bot commented Nov 5, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision a52dedc25269 · 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 behavior would each changed op test fail on before this PR, and why is that coverage not already provided by an existing canonical test?
  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
  • FAIL — changed op test not explained in Changed op tests: op_tests/triton_tests/attention/test_unified_attention.py

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

@cagrikymk
cagrikymk force-pushed the cagri/unify_UA_2D_3D branch 2 times, most recently from cd3c9e0 to c12be61 Compare November 6, 2026 22:07
@cagrikymk
cagrikymk marked this pull request as ready for review November 9, 2026 19:18
@cagrikymk
cagrikymk requested a review from a team November 9, 2026 19:18
@github-actions github-actions Bot changed the title [TRITON] Restructure Unified Attention Kernel (merging 2d/3d and modularization) [Triton/Gluon] Restructure Unified Attention Kernel (merging 2d/3d and modularization) Nov 9, 2026
kernel_unified_attention replaces kernel_unified_attention_2d/_3d. GRID_3D
picks the (q_block, kv_head, segment) program order of the split-KV launch;
NUM_SEGMENTS_PER_SEQ > 1 writes partials for reduce_segments, otherwise the
output. The body is split into AttentionConfig, KVLoader and AttentionProgram
aggregates plus one attention_loop used for both the unmasked and the masked
tiles, along the lines of the gfx950 gluon kernel.

Configs and launch names are unchanged (attn_2d / attn_3d sections, 2d and 3d
kernel reprs). The 2d kernel compiles to the same instruction stream as before
on gfx942/gfx950/gfx1100/gfx1201/gfx1250; 3d loops only differ in the softmax
scale, now a constexpr as in 2d.

A 3d launch with a single segment used to write the padded segment layout into
the output; it now stores the output like the 2d launch (fixes out-of-bounds
writes for non power-of-2 value heads, adds the fp8 clamp).
The triton kernels read a shuffled tile as one contiguous page, so the
wrapper pinned TILE_SIZE to block_size and the tuned tile (e.g. the gfx950
SHUF entries) was ignored. A tile inside one page now reads its slice of the
page (one block id per tile), a tile over several pages reads each page as a
contiguous chunk; both use the existing un-shuffle. One-page tiles keep the
old load and codegen. The fp8 shuffled assert moves from block_size >= 32 to
TILE_SIZE >= 32, so 16-token pages work with fp8.
The prefill batch was small enough to take the 3d grid, so the 2d shuffled
paths went untested; use a batch that takes the 2d grid, lengths that end
mid-page, fp8 on both tile cases, and no random descales, which flattened the
softmax enough to hide a wrong K layout.
…n both grids

The mask only depends on key and query positions. The old 3d kernel built it
before the dot, the 2d kernel after; use one order. Outputs are bitwise
unchanged; the 2d loop schedule changes on some archs.
… steps

KVLoader.load_tile now looks the tile up once (block_ids) and builds the K and
V offsets and masks with separate methods before load_k / load_v issue the
loads, so the two loads can be reordered independently. A shuffled tile that
is not one page is described as NUM_SLOTS slots of SLOT_SIZE keys, each inside
one page, instead of two separate gather paths. Drops the forwarding
KVLoader.initialize and folds the tensor-or-constexpr field wrapping into one
helper.

The plain layout and one-page shuffled tiles compile to the same instructions
as before on gfx942/gfx950/gfx1100/gfx1101/gfx1150/gfx1151/gfx1200/gfx1201/
gfx1250.
…pans pages

A shuffled V page is [BLOCK_SIZE // W, HEAD_SIZE, W]. Addressing a multi-page
tile as [pages, BLOCK_SIZE * HEAD_SIZE] made the un-shuffle split that axis,
and the layout derived back to the load spread a lane's W-run over pages: on
gfx950 the direct-to-LDS copy of V fell to 4 bytes per lane. Keep the rows
axis when a page has more than one row, so the un-shuffle only merges pages.
Pages of a single row are one contiguous run and keep the flat form.
Build the offsets of a shuffled tile that spans pages in the one-page order
and flatten them before the load: K as [HEAD_SIZE // W, pages, page * W],
V as concatenated pages. The load and the un-shuffle are then the one-page
ones, so K and V keep their 16-byte direct-to-LDS copies on gfx950; the
4-D K un-shuffle and the page-rows V form are gone.
…kernel

The rebase onto main dropped what main changed in the old 2d/3d kernels:
- with a sliding window the split carves only the window's tiles
  (split_window_tiles), as reduce_segments now expects; without it the
  split outputs were wrong or NaN
- rows past BLOCK_Q tokens are masked, so two q blocks no longer both
  store segment stats for the same token (#7472)
- the qq_bias load masks padded query rows
The window was a second select over S after the causal one. Build both
from the key distance before the QK dot, with one unsigned compare for
0 <= dist < window. Bitwise identical output; on gfx950 sliding-window
prefill and chunked prefill run 7-11% faster at head size 64 and 128,
decode and head size 256 are unchanged.
…w_max

Fixes ruff SIM102 under the CI-pinned ruff 0.16.0.

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

Single-segment 3D decode can access tensors out of bounds for zero-KV padding sequences with no query rows, while int64 sliding-window metadata causes compilation failures and the shuffled loader lacks Triton regression coverage.

3 open issues
What changed in this PR

This PR merges AITER’s Triton 2D and 3D unified attention kernels to reduce duplicated logic and let shuffled KV caches use tuned tile sizes independently of page size.

Changes:

  • Introduces shared configuration, KV loading, attention-loop, and output helpers while preserving 2D/3D profile names.
  • Supports shuffled tiles that cover part of a page or span several pages.
  • Updates wrapper launches and separates split-KV partial writes from direct output stores.
File Description
aiter/​ops/​triton/​attention/​unified_attention.py Launches the merged kernel and removes page-sized tile restrictions.
aiter/​ops/​triton/​_triton_kernels/​attention/​unified_attention.py Consolidates attention logic and adds flexible shuffled-KV tile addressing.

Review effort: Balanced

@zufayu
zufayu requested a review from azaidy November 10, 2026 01:26
@github-actions

Copy link
Copy Markdown
Contributor

No activity for 15 days, so this is now labelled stale. A push or a comment clears it; keep-open exempts it. Nothing is closed automatically.

@github-actions github-actions Bot added the stale The PR hasn't been updated in 2 weeks label Nov 25, 2026
@akao-amd

Copy link
Copy Markdown
Contributor

Heads-up so nothing collides by surprise: #8883 adds a split-KV decode producer for gfx1250, GQA 8 / head 64 (two KV heads per program, so no padding rows). It writes the same segm_output / segm_max / segm_expsum partials as the 3D kernel and is selected at the call site that launches _unified_attention_3d_triton, through a one-entry producer table, so the 3D kernel stays the default. #8884 and #8885 add two attn_3d launch keys (KV_HEAD_FIRST, HOIST_KV_ADDR) to the 3D kernel; after your restructure those would become a grid-order flag and a KV-address variant in your helpers. Whichever lands second, we'll do the rebase; nothing in your design needs to change for ours.

🤖 Generated with Claude Code

…t 3D grid

A 3D launch with one segment read Q and wrote output for padding
sequences with no KV and no query rows. Run the empty-segment return on
every 3D launch, as the 3D kernel on main did.

Signed-off-by: Mehmet Cagri Kaymak <[email protected]>
With int64 seqused_k or cu_seqlens_q, the sliding-window bitcast compare
did not compile. Narrow the loads to int32: positions fit, and addresses
already widen to int64. int64 callers now run the int32 code.

Signed-off-by: Mehmet Cagri Kaymak <[email protected]>
@azaidy

azaidy commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

@cagrikymk

The new cross-page shuffled KVLoader addressing, the PR's core feature, still has no Triton-backend regression test. Path: aiter/ops/triton/_triton_kernels/attention/unified_attention.py:95-237. No op_tests/ file is touched by this PR at any reviewed head, confirmed by git diff --stat against op_tests/. Suggested fix: add a forced-backend="triton" test with TILE_SIZE != BLOCK_SIZE for a shuffled cache, covering a tile spanning several pages and a scattered block table.

…nd on gfx950

The shuffled Triton loader was only tested on gfx942, where the tile
mostly equals the page. Run the 2D and 3D shuffled tests on the Triton
backend on gfx950 too, which covers tiles larger and smaller than a page.

Signed-off-by: Mehmet Cagri Kaymak <[email protected]>

@azaidy azaidy 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.

LGTM!

@cagrikymk
cagrikymk merged commit b85783e into main Dec 2, 2026
63 of 65 checks passed
@cagrikymk
cagrikymk deleted the cagri/unify_UA_2D_3D branch December 2, 2026 17:43
akao-amd added a commit to akao-amd/aiter that referenced this pull request Dec 3, 2026
Resolved against the kernel_unified_attention restructure (ROCm#6337): this PR's kernel changes are ported onto the merged 2d/3d kernel (GRID_3D).
akao-amd added a commit to akao-amd/aiter that referenced this pull request Dec 3, 2026
Resolved against the kernel_unified_attention restructure (ROCm#6337): this PR's kernel changes are ported onto the merged 2d/3d kernel (GRID_3D).
The updated parent PR branch is merged in as well.
akao-amd added a commit to akao-amd/aiter that referenced this pull request Dec 3, 2026
Resolved against the kernel_unified_attention restructure (ROCm#6337): this PR's kernel changes are ported onto the merged 2d/3d kernel (GRID_3D).
The updated parent PR branch is merged in as well.
klin-amd added a commit that referenced this pull request Dec 3, 2026
…dim-256 prefill entry on the merged kernel

After #6337 merged the 2D and 3D kernels, main's 64-row / tile-32 value got
about 20% faster on the head_dim-256 bf16 prefill shapes and the entry
picked on the old kernel (64 / 4 / tile 16 / waves_per_eu 0) fell to
0.88x-0.97x of it. Re-searched on the new kernel: main's tile with
waves_per_eu 2 is 1.05x-1.08x faster than main's value on all 8 shapes.
The other 13 changed or added entries keep their gain on the new kernel;
whole table 1.24x (bf16 KV) and 1.23x (fp8 KV) over 7 models x 17 calls.

Signed-off-by: Lin, KuanTing <[email protected]>
klin-amd added a commit that referenced this pull request Dec 3, 2026
… lost to main on the restructured kernel

With the 2D / 3D kernel merge (#6337) on main, several entries of this
table fell behind main's values on gfx1151 (TheRock 10.3.0a20261202,
torch 2.17.0a0, Triton 3.9.0; CUDA graph, every arm checked against
ref_paged_attn): attn_2d.Q_GEQ_256.DT_any_bf16.D_LEQ_128 on mixed
prefill-plus-decode batches (0.93x-0.97x), attn_2d.Q_LEQ_1 and
Q_LEQ_1.DT_fp8_fp8 on single-sequence 256-token decode of every model and
on 16 / 8-head head_dim-256 decode batches (0.79x-0.87x; these were
already behind on the previous kernel), Q_GEQ_256.DT_any_fp8.D_LEQ_512
on head_dim-512 fp8-KV prefill (0.95x-0.97x) and kv_split.D_EQ_512 on
32-sequence 1k-token decode (0.96x).

Each of those keys was re-swept on the merged kernel. Rule: one value per
key that is at least 0.98x of main's value on every shape of the key, best
geomean among those; where no value qualifies the key goes back to main's
value.

- attn_2d.Q_GEQ_256.DT_any_bf16.D_LEQ_128: 128 / 8 / 1 / 0, tile 32 ->
  64 / 4 / 1 / 0, tile 32. Llama-3-8B and Qwen2.5-7B prefill 512-4096,
  chunked prefill and mixed batches: 1.05x-1.17x of main (was 0.93x-1.19x).
- attn_2d.Q_LEQ_1.DT_any_bf16 (new): 16 / 8 / 1 / 2, tile 32-64 for
  head_dim 256 and 512 bf16 decode: 1.05x-2.3x of main (was 0.79x-1.47x).
- attn_2d.Q_LEQ_1.DT_any_bf16.D_LEQ_128 (new), attn_2d.Q_LEQ_1 and
  attn_2d.Q_LEQ_1.DT_fp8_fp8: main's values. No single value serves both
  single-sequence and 32-sequence decode (one wants 4-8 warps and a 32-64
  tile, the other 2 warps and a 16 tile) and the batch size is not a
  lookup axis.
- attn_2d.Q_GEQ_256.DT_any_fp8.D_LEQ_512: main's value. No candidate beat
  it on the chunked-prefill shape.
- kv_split.D_EQ_512.DT_any_bf16: SEGMENTS_PER_CU 16 -> 4. Gemma-4 bf16
  decode 1.01x-4.55x of main (was 0.96x-4.55x).

Whole table on the merged kernel, 6 models x 17 shapes, bf16: geomean
1.35x of main, no shape under 0.99x; 1.06x of this PR's previous table.
fp8 KV and fp8 / fp8 variants: every entry still changed by this PR is at
or above main's value on its shapes.

Signed-off-by: Lin, KuanTing <[email protected]>
klin-amd added a commit that referenced this pull request Dec 3, 2026
… lost to main on the restructured kernel

With the 2D / 3D kernel merge (#6337) on main, several entries of this
table fell behind main's values on gfx1150 (12 CU; TheRock
10.3.0a20261202, torch 2.17.0a0, Triton 3.9.0; CUDA graph, every arm
checked against ref_paged_attn): attn_2d.Q_GEQ_256.DT_any_bf16.D_LEQ_256
on head_dim-256 bf16 prefill, chunked prefill and mixed batches
(0.82x-0.97x), attn_2d.Q_LEQ_1 for fp8 KV and Q_LEQ_1.DT_fp8_fp8 on
single-sequence 256-token decode (0.70x-0.85x; already behind on the
previous kernel), attn_2d.any for fp8-KV speculative decode at head_dim
128 (0.80x), attn_2d.DT_fp8_fp8 on fp8 / fp8 speculative decode at
head_dim 256 (0.86x), attn_3d.D_LEQ_1024.DT_any_fp8 with kv_split.any on
8-sequence 1k-token fp8-KV decode (0.88x), and kv_split.D_LEQ_256 on
8-sequence head_dim-256 decode (0.97x).

Each was re-swept on the merged kernel. Rule: a value is taken only if it
is at least 0.98x of main's value on every shape of its key, best geomean
among those; otherwise the key keeps main's value. Where single-sequence
and many-sequence decode, or head_dim 128 and larger heads, want different
values, the key is split by head_dim.

- attn_2d.Q_GEQ_256.DT_any_bf16.D_LEQ_256: back to main's value; the best
  candidate was within 2% of it.
- attn_2d.Q_LEQ_1.DT_fp8_fp8: 16 / 4 / 1 / 2, tile 16-32 for head_dim
  256 and 512 (1.28x of main, min 1.02x); new .D_LEQ_128: 16 / 4 / 1 / 8,
  tile 16 (1.09x, min 1.00x).
- new attn_2d.Q_LEQ_1.DT_any_fp8: 16 / 4 / 1 / 2, tile 16-32 for head_dim
  256 and 512 (1.91x of main, min 1.27x); new .D_LEQ_128: 16 / 2 / 1 / 0,
  tile 32-64 (1.34x, min 1.02x). These calls fell through to Q_LEQ_1.
- new attn_2d.DT_any_fp8: main's catch-all value for head_dim 128 fp8-KV
  speculative decode; new .D_GEQ_256: 32 / 4 / 1 / 4, tile 16 (1.09x).
- attn_2d.DT_fp8_fp8: 32 / 2 / 1 / 2, tile 16 for head_dim 256 and 512
  (1.12x of main, min 1.06x); new .D_LEQ_128: main's value.
- new kv_split.D_EQ_256.DT_any_bf16: 4 / 2 / 128 / 32, tile 32 for
  head_dim-256 bf16 decode (1.20x of main, min 1.03x); D_LEQ_256 keeps its
  value for head_dim 128.
- new kv_split.D_EQ_512.DT_any_fp8: 2 / 4 / 128 / 16, tile 16, with the
  unchanged attn_3d.D_LEQ_1024.DT_any_fp8: Gemma-4 fp8-KV decode 1.03x-1.90x
  of main (was 0.78x-1.88x).

Signed-off-by: Lin, KuanTing <[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.

5 participants