Skip to content

[Triton/Gluon] [Perf] [gfx1250] unified attention 3D decode: hoist the K/V page offsets - #8885

Draft
akao-amd wants to merge 18 commits into
ROCm:mainfrom
akao-amd:pr-gfx1250-ua3d-decode-hoist
Draft

akao-amd wants to merge 18 commits into
ROCm:mainfrom
akao-amd:pr-gfx1250-ua3d-decode-hoist

Conversation

@akao-amd

@akao-amd akao-amd commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On top of #8884, HOIST_KV_ADDR computes invariant K/V in-page offsets before the KV loop. Each tile reads one scalar block-table page and adds its base to those offsets. Shipped 3D rows use address hoisting with the normal masked loop.

Stack: #7743 -> #8883 -> #8884 -> #8885. This PR adds address preparation, two opt-ins and independent hoist parity/config checks.

Motivation and changes

For plain NHD KV with one page per tile, the former path rebuilt [64, 64] integer offsets from a repeated block-table gather each iteration. The in-page portion is invariant.

The shared AttentionConfig/KVLoader prepares K/V offsets once and reads one scalar page base per tile. Wrapper and kernel guards require all-decode, plain NHD, no window, page=tile and power-of-two Q/V head sizes. Loads and apply_mask_qk keep their masks. Only gfx1250 D_LEQ_64.DT_bf16_fp8.Q_LEQ_1 and D_LEQ_64.DT_fp8_fp8.Q_LEQ_1 enable hoisting.

SPLIT_UNMASKED_LOOP on the 3D grid failed exact parity with non-finite outputs and is not enabled; upstream's existing 2D use is unchanged.

At the measured gpt-oss TP1/TP2 serving batches, every full-attention decode call has more than one segment and takes #8883's producer; sliding-window layers use the 2D kernel. These 3D changes act with AITER_UA_DECODE_2H=0, one-segment calls or other supported shapes, and were inactive in the default base1130_v3 serving measurements.

Performance

Earlier measurement (not re-run on this head): gpt-oss-120b TP2 on one MI455X (two 96-CU partitions), 1400 W, FP8 KV page 64 and FP8 decode Q, image rocm/sgl-dev:v0.5.23-rocm101-mi45x-20261127, Triton d436c49e2f, bench_one_batch. Arms used the same build, fresh servers and bracketing measurements.

At 32768/1024×168, with 2h disabled and head-first enabled, 21.00 -> 20.18 ms (-3.9%) measured the earlier combined address hoist and full-tile mask skip. Current address-only performance has not been measured.

The base1130_v3 stack (MI450 gpt-oss measurements, December 1, 2026) carried this change as 0b6896441, whose HOIST_KV_ADDR also skipped the causal mask on full tiles. Current code retains masking despite identical HOIST row edits, so its loop/code generation differs. V1/v3 contain the old change in both A/B arms, so that comparison does not measure this PR alone. The older whole-stack greedy-identical claim is not current validation: base1130_v3 contains a separate dense-FP8 numerical change, and serving accuracy has not been checked on this head.

Testing

GPU setup: MI455X (gfx1250) 96-CU partition, image rocm/sgl-dev:v1.0.0-rocm101-mi45x-20261202, torch 2.12.0+rocm10.1.0, Triton 3.9.0. At the reviewed revision on HIP 0 / CU_NUM=96:

  • python -m pytest -q op_tests/triton_tests/attention/test_unified_attention_config.py: 12 passed / 68 subtests.
  • python -m pytest -q op_tests/triton_tests/attention/test_unified_attention.py -k "decode_hoist_kv_addr or decode_two_heads or decode_kv_head_first or triton_3d_launch_grid": 147 passed with AITER_UA_DECODE_2H=1 and 147 passed with =0, each in a fresh process with a separate initially empty TRITON_CACHE_DIR. This includes 80 independent hoist cases, parent producer/grid parity and 12 CPU grid checks.
  • All four 65535/65536-slot × BF16/FP8 Q boundary cases passed with another fresh Triton cache, four live rows with metadata padding, eager and graph replay.

All steps returned zero. This validates compilation and execution starting from isolated cold Triton caches; tests may reuse compiled kernels within each step.

The broader selection -k "gfx1250 or decode" previously passed 630 cases / 96 skipped under each switch at 9b1d6e4db9e53828b07f648c7bf95cc1fce02268, HIP 2, with a warm Triton cache. The reviewed revision changes only commit history and has an identical complete source tree. The earlier 31 failures are gone. These are correctness checks; current performance has not been measured.

Current CPU config/metadata checks: 26 passed / 68 subtests. Hoist parity covers 4/8 KV heads, BF16/FP8 Q, sinks, both grid orders, partial/full tiles, empty slots and graph replay, with split disabled in reference and candidate. Config tests pin both HOIST opt-ins and the split key's absence. Changed-file Black/Ruff and whitespace checks pass. Other architectures, unfiltered attention and current serving performance/accuracy remain unverified.

Documentation and dependencies

README/config/review guidance document address-only behavior and absent 3D split opt-ins. No new dependencies or public API changes. Rebased onto #8884 c5c9bdb29; root #7743 merges main ecfa616d8.

PR Self-Review

Reviewed revision

bf1846c198756eabf67d535c85157898c71d34d9

Behavior and intent

Hoist invariant K/V addresses under the plain-NHD decode contract. Shipped 3D rows retain ordinary masking and omit the unsafe split flag; tests steer reference and candidate independently to preserve a valid comparison.

Validation and unverified scope

At this SHA, separate cold Triton caches passed 147 selected cases under each producer switch and all four boundary cases; config passed 12 tests / 68 subtests. The identical source tree previously passed 630 broad cases / 96 skips per switch. Current performance, serving accuracy and other architectures remain unverified.

PR-specific question

Only the two FP8-KV decode rows carry HOIST_KV_ADDR. Wrapper/kernel guards enforce layout, page/tile, window and head-size invariants. Config tests pin split-key absence; hoist comparisons disable split in both arms. Review-guidance edits add no workflow execution or permissions.

Changed op tests

  • test_unified_attention.py adds the 80-case independent hoist matrix and split steering; explicitly disabling split in both arms prevents a failing shared reference from contaminating parity. Parent producer/grid tests remain inherited.
  • test_unified_attention_config.py pins HOIST opt-ins and split-key absence; output comparisons do not identify accidental config-key reintroduction.
  • mha_test_utils.py is inherited from [Triton/Gluon] [Perf] [gfx1250] unified attention: two-KV-head split-KV decode producer (GQA 8, head 64) #8883 for valid cache geometry and eager/replay comparison, avoiding redundant setup across the stack.

akao-amd and others added 8 commits November 10, 2026 19:08
…A3D_NHD_CFG

Cherry-picked from perf1101 (no upstream PR yet).

Co-Authored-By: Claude <[email protected]>
(cherry picked from commit 1502c52)
…, head_dim <= 64

The plain (non-shuffled) bf16 KV cache fell to the 'any' entry (waves_per_eu 1). Occupancy 2 cuts the gpt-oss-120b full-attention decode kernel by ~35% in situ; outputs bitwise identical (only the launch's occupancy hint changes).

Co-Authored-By: Claude <[email protected]>
(cherry picked from commit 37d3490)
…ault

The env AITER_GFX1250_UA3D_NHD_CFG still overrides it. waves_per_eu 2 alone was the old bf16 entry; the measured config is waves 2, num_stages 3, SEGMENTS_PER_CU 16, MIN_SEGMENTS 4, for bf16 and fp8 q/KV.

Co-authored-by: Cursor <[email protected]>
The D<=64 entries matched every query length, so short prefill and verify batches that still launch the 3D kernel took the decode tuning. Key them on Q_LEQ_1 with Q after SHUF so shuffled entries still win, and drop the env override so this change is only the retune.
The env override is gone, and this branch does not need the dtype-tag edits that landed on main after it forked.
…r fp8-KV decode

The all-decode rows this PR adds for head dim <= 64 run num_stages 3. Over an fp8 KV
cache (DT_bf16_fp8 and DT_fp8_fp8 Q_LEQ_1) num_stages 4 is faster at every context we
measured and stores the same bits (num_warps 1, waves_per_eu 2 unchanged); bf16 KV
keeps 3.

Per call on one MI455X partition (96 CUs), gpt-oss TP2 decode shapes, 2.2 GHz:
- bf16 Q: 32k c168 579 -> 513 us, 128k (graph batch 48) 595 -> 555 us, 1M c8 974 -> 871 us
- fp8 Q:  32k 585 -> 480 us, 128k 579 -> 538 us, 1M 898 -> 786 us
At the 1400 W serving cap, TP2 decode step: -0.37 / -0.39 / -0.28 ms at 32k / 128k / 1M.

These are Triton attn_3d rows. The Gluon 3D kernel's num_stages == 2 requirement
(ROCm#6730) reads the gluon table and is not affected.

Upstream-PR: ROCm#7743

Co-Authored-By: Claude <[email protected]>
@akao-amd
akao-amd requested a review from a team November 28, 2026 22:18
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

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

Extended tests (opt-in via labels):

Label Tests
ci:mi300x Run Aiter standard tests on MI300X in PRs; main branch always runs them
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 Radeon R9700 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 8885 --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.

Reviews:
A reviewer is usually assigned within one working day after the PR is ready for review (see Code Review Process). If nobody has responded for about a week, mention the area contact listed in CONTACTS.md. Want to help review? See REVIEWERS.md.

@aiter-bot

aiter-bot commented Nov 28, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision 392eb1f9cf3f · 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 permissions and untrusted-input boundary does this CI change have, and can fork PR code or text reach a privileged execution path?
  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.

@akao-amd
akao-amd force-pushed the pr-gfx1250-ua3d-decode-hoist branch from 4b2d2d0 to a1da727 Compare November 28, 2026 22:25
@Boss2002n
Boss2002n requested a balanced review from Copilot November 28, 2026 22:33

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

Outstanding findings require consolidating duplicated kernel and test logic and documenting the new producer-selection contract, input limits, disable switch, and guards.

3 open issues
What changed in this PR

Optimizes AITER’s gfx1250 Triton 3D decode path by reusing K/V page offsets and skipping causal masks on full tiles. It builds on stacked changes for decode-specific tuning and KV-head scheduling.

Changes:

  • Adds guarded HOIST_KV_ADDR support for plain, one-page-per-tile decode.
  • Carries the table-selected two-KV-head producer, KV-head-major launch option, and tuned decode settings.
  • Adds exact-parity, graph-replay, dispatch-gate, and config-lookup tests.
File Description
op_tests/​triton_tests/​attention/​test_unified_attention.py Adds decode-path comparisons, gate checks, and graph-replay coverage.
op_tests/​triton_tests/​attention/​test_unified_attention_config.py Checks decode tuning selection and generic fallback behavior.
aiter/​ops/​triton/​utils/​unified_attention_utils.py Adds producer-table detection and the KV-length lookup axis.
aiter/​ops/​triton/​configs/​gfx1250/​triton/​attention/​unified_attention/​DEFAULT.json Adds decode tuning rows and enables the optimized launch options.
aiter/​ops/​triton/​attention/​unified_attention.py Selects decode producers and guards the new 3D launch options.
aiter/​ops/​triton/​_triton_kernels/​attention/​unified_attention.py Implements address hoisting, full-tile mask skipping, and paired-head decode.

Review effort: Balanced

Comment on lines +1167 to +1171
m_j = tl.maximum(M, tl.max(S, axis=1))
m_j = tl.where(m_j > float("-inf"), m_j, 0.0)
P = tl.math.exp2(S - m_j[:, None])
l_j = tl.sum(P, axis=1)
alpha = tl.math.exp2(M - m_j)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We'd prefer not to do this in this PR. The two-head producer keeps a [16, 128] accumulator for a block-diagonal Q (two KV heads per program), so its online-softmax/PV update works on different tile shapes and register layout than the 3D kernel's; a shared helper would either be generic over those shapes or change the 3D kernel's codegen, and either needs a fresh performance measurement of both producers. #6337 is introducing shared attention-loop helpers; once that lands we'd port both producers onto its helpers in one follow-up and re-measure. Happy to revisit if you see a lighter way.


_CONFIG_NAME = "UNIFIED-ATTENTION"
_OPS = ("attn_2d", "attn_3d", "reduce", "kv_split")
_OPS = ("attn_2d", "attn_3d", "reduce", "kv_split", "attn_decode_2h")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed: the three docs now cover the attn_decode_2h table, its opt-in, input limits and AITER_UA_DECODE_2H, and both attn_3d launch keys, including the HOIST_KV_ADDR guard (all-decode, plain NHD, no window, tile == page, power-of-two head sizes). The review instructions ask for a parity case when that guard widens.

Comment on lines +728 to +732
seq_lens = [
(1, max(1, max_kv_len - (i % 7) * 63 - (i % 3))) for i in range(batch_size)
]
# row 0 keeps max_kv_len; rows 1-3: exactly one page, one page + 1, one token
seq_lens[1:4] = [(1, 64), (1, 65), (1, 1)]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed: the shared setup and replay logic now lives in the existing attention test-utility module, op_tests/triton_tests/attention/mha_test_utils.py: paged_decode_case builds the batch and assert_steered_parity runs the eager and graph-replay exact checks. Each test passes its data generator, the unified_attention launch and its steering callable, and states its reference and candidate choices explicitly.

@akao-amd

Copy link
Copy Markdown
Contributor Author

Stacked on #8884. Refreshed onto current main (e22831c83). The Triton shard 7 failure on the previous head is the same one reported on #8883.

@akao-amd
akao-amd force-pushed the pr-gfx1250-ua3d-decode-hoist branch from 708f50a to 392eb1f Compare December 3, 2026 02:23
@akao-amd
akao-amd marked this pull request as draft December 3, 2026 02:38
akao-amd and others added 5 commits December 3, 2026 05:31
…KV decode producer (GQA 8, head 64)

Stacked on ROCm#7743 (pr-gfx1250-ua3d-nhd). Carries only the two-KV-head producer and its dispatch. The tuned-split and num_stages commits this branch used to repeat are dropped: ROCm#7743 already has them.

Split-KV decode on gfx1250 runs kernel_unified_attention_3d with BLOCK_M 16. With 8
query heads per KV head (gpt-oss), half of every 16-row block is padding.

kernel_unified_attention_decode_2h handles two adjacent KV heads per program, so the 16
rows are 8 query heads x 2 KV heads and none are padding. QK multiplies a block-diagonal
Q [16, 128] (each row's query in its own head's 64 columns) by both heads' K at once. PV
accumulates [16, 128], and each row keeps its own head's half. It writes the same
split partials (segm_output / segm_max / segm_expsum) as the 3D kernel, so the same
reduce follows and the results are bit-identical to the 3D kernel.

Selection:
- The producer is picked per call in _select_triton_split_decode, once the split
  (NUM_SEGMENTS, TILE_SIZE) is fixed and before its scratch is allocated; the 3D
  launch site calls the chosen launcher. It is a small producer table, so
  kernel_unified_attention_3d stays the default.
- decode_2h_supported() checks only what the kernel computes: all-decode split KV
  (NUM_SEGMENTS > 1), GQA 8, head 64, page = tile = 64, an even KV-head count, plain
  NHD fp8 K/V, bf16 or fp8 Q, optional sinks, no window, softcap, alibi, qq_bias or
  output scale.
- An arch opts in by shipping an attn_decode_2h section in its triton DEFAULT.json
  (has_unified_attention_config). gfx1250 ships one row: num_warps 1, num_stages 3,
  waves_per_eu 2, at every context length.
- AITER_UA_DECODE_2H=0 turns the producer off (read at import, like
  AITER_UA2D_FUSED_EPILOGUE).

Depends on ROCm#7743. Its all-decode split (MIN_SEGMENTS 4) is what gives gpt-oss TP2
4 / 8 / 64 segments at 32k c168 / 128k c44 / 1M c8. Main's split gives 1 / 2 / 16, and
one segment never takes this producer.

Tests (op_tests/triton_tests/attention/test_unified_attention.py):
- exact parity with the 3D producer for 4 and 8 KV heads, bf16 and fp8 Q, sinks on and
  off, lengths 1/64/65 and two empty trailing slots;
- a CUDA graph replay that moves tiles between partial and full;
- a gate test on metadata mocks: predicate, table opt-in, kill switch, and the shipped
  gfx1250 / gfx950 tables.

gpt-oss-120b TP2 decode step on one MI455X (1400 W), before the launch table:
-2.42 / -1.37 / -3.30 ms at 32k / 128k / 1M; num_stages 3 adds -1.02 / -2.37 ms
at 32k / 128k. At 1M, serving also runs faster with 3 stages than with the 4 that
per-call sweeps had picked, so the table has one row. To be re-measured on this
base for the PR.

Co-Authored-By: Claude <[email protected]>
Co-authored-by: Cursor <[email protected]>
…major grid from the attn_3d table

Stacked on ROCm#8883 (two-KV-head producer).

The Triton 3D decode grid is (q_blocks, kv_heads, segments), so the programs of one
sequence's adjacent KV heads are launched far apart. The 2D grid is already
(kv_heads, q_blocks).

An attn_3d row can now set KV_HEAD_FIRST: all-decode launches then use
(kv_heads, q_blocks, segments), and the kernel swaps its first two program ids. Each
program computes and stores the same values; only the launch order changes. The
gfx1250 rows for head dim <= 64 decode over an fp8 KV cache (DT_bf16_fp8 and
DT_fp8_fp8 Q_LEQ_1) set it. No other row changes.

This is the 3D producer, so it applies where the two-KV-head producer does not (one
segment, other GQA ratios or head sizes, AITER_UA_DECODE_2H=0).

Test: bit-exact parity of the KV-head-major and default grids for 4 and 8 KV heads,
bf16 and fp8 Q, unequal lengths and empty trailing slots. Each table row covers
seqused_k, so no read goes past the table.

gpt-oss-120b TP2 32k c168 decode step on one MI455X (3D producer): -3.9 ms. To be
re-measured on this base.

Co-Authored-By: Claude <[email protected]>
Co-authored-by: Cursor <[email protected]>
…e K/V page offsets

Stacked on ROCm#8884. For all-decode plain NHD KV with one page per tile,
HOIST_KV_ADDR computes the invariant K/V in-page offsets before the KV loop
and adds one scalar page base per tile. The public wrapper and kernel guard
require no window and power-of-two head sizes.

Only the two gfx1250 fp8-KV decode rows enable hoisting. They retain the
normal masked loop: the 3D SPLIT_UNMASKED_LOOP use failed exact parity and
produced non-finite outputs, so it is not enabled. Existing upstream split
uses are unchanged.

Tests independently steer address hoisting with splitting disabled, pin
both opt-ins and the absence of the split key, and cover sinks, both grid
orders, bf16/fp8 Q, partial/full tiles and graph replay. GPU validation on
the identical source tree passed 147 selected cases and 630 broad cases
(96 skips) under each import-time producer switch on gfx1250/Triton 3.9.
Current address-only performance has not been measured.
@akao-amd akao-amd changed the title [Triton/Gluon] [Perf] [gfx1250] unified attention 3D decode: hoist the K/V page offsets, skip the causal mask on full tiles [Triton/Gluon] [Perf] [gfx1250] unified attention 3D decode: hoist the K/V page offsets Dec 3, 2026
@akao-amd
akao-amd force-pushed the pr-gfx1250-ua3d-decode-hoist branch from 392eb1f to bf1846c Compare December 3, 2026 07:29
@zufayu
zufayu removed request for a team and brunomazzottiamd December 3, 2026 23:06

This branch has not been deployed

No deployments
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.

4 participants