Skip to content

[Triton/Gluon] pa_prefill_sparse: single-pass gluon kernel for gfx950 at H=32 and 64 - #7680

Open
eppaneamd wants to merge 8 commits into
mainfrom
eppaneamd/gluon-pa-prefill-sparse-gfx950
Open

eppaneamd wants to merge 8 commits into
mainfrom
eppaneamd/gluon-pa-prefill-sparse-gfx950

Conversation

@eppaneamd

@eppaneamd eppaneamd commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On gfx950, pa_prefill_sparse gets a single-pass Gluon kernel for bf16 q and KV with 32 or 64 heads of a 512-wide
latent. It takes every call it serves, at any number of query rows.

  • H=32: 0.53–0.59× the time of mla_gluon, which serves these calls today.
  • H=64: it replaces mla_gluon's bh64 path, which asserts unless T % 64 == 0 and gets rows of up to 128 keys wrong.
    • Where bh64 is correct: 0.4–1.6 % slower on the in-tree benchmark, 12–13 % with 1–2 GiB pools (Performance).
  • Rows with no slots get zeros.

Motivation

pa_prefill_sparse on gfx950 calls mla_gluon(..., min_kv_seq_len=float("inf"), has_pe=False). On that path (MI355X,
main 1eab5ae12c):

  • H=32 (bh16bn64, mla_gluon.py:26-27): heads are tiled into 16-head blocks, so two programs per query row gather the
    same keys.
  • The split count is max(1, CUs // (T · head blocks)) (:962): one split from T=65 at H=32 on 256 CUs, and at smaller
    T with fewer CUs (CU_NUM, compute partitions).
    • With one split, a row with no slots returns early (:155-156) and its output is never written.
    • With more, the sink is added once per split that has keys (:675-686). T=1, q=0, 128 keys of ones, sink log 128:
      every output is 0.334, not 0.5.
  • H=64 (bh64, :900):
    • asserts unless T % 64 == 0 (:932-933);
    • gets rows with up to 128 keys wrong: at T=1,024 with row t holding t + 1 keys, exactly the 128 rows of 1–128 keys
      (rel L2 0.31; H=128 the same).
    • min_kv_seq_len=inf disables the guard (:939-945) for the kernel's gl.assume(num_iter >= 3) (:475). That this
      assumption causes the wrong rows is a hypothesis.

The new kernel runs one program per query row for 32 heads:

  • each 64-key tile is copied once from the pool straight into LDS (128 bits per lane) and feeds the QK and PV MFMAs;
  • with 70,656 B of LDS, two 4-warp programs share a CU;
  • at H=64, two programs share each row, one per half of the heads, in one launch:
    • at program ids L and L + 8 (the XCD count), as sparse_mla_fp8.py places a row's head blocks, so the second one's
      KV gathers can hit the first one's in L2 (Performance);
    • one 64-head program measured slower.

It is a separate kernel, not a regime of mla_gluon: mla_gluon is a split-KV decode kernel with a second reduce
stage; this one is single-pass over one CSR index list.

Scope: no caller reaches gfx950's pa_prefill_sparse today. #7681 (stacked) makes it mla_sparse_prefill's default
for GLM-5.3-Flash at attention TP 2 (32 heads per rank); at TP 1 (64) only with backend="triton".

Changes

  • aiter/ops/triton/_gluon_kernels/gfx950/attention/pa_prefill_sparse.py (new):
    • the kernel, with no torch import and a repr over H, HEAD_GROUPS, BLOCK_K and HAS_SINK;
    • HEAD_GROUPS programs per query row, H heads each. A row's programs are NUM_XCDS program ids apart; the last
      T % NUM_XCDS rows run theirs at adjacent ids;
    • 64-bit offsets for q, out and KV rows;
    • optional fp32 sink.
  • aiter/ops/triton/configs/gfx950/gluon/attention/pa_prefill_sparse/DEFAULT.json (new): BLOCK_K 64, num_warps 4.
  • aiter/ops/triton/attention/pa_prefill_sparse.py, gfx950 branch:
    • takes the kernel when H is 32 or 64, D=512, q and KV are bf16, unified_kv is 2-D, both inner strides are 1 and
      has_invalid is not True (_single_pass_unsupported), with or without a sink, at any T;
    • launches it once: one head group of 32 at H=32, two at H=64 with NUM_XCDS from get_num_xcds();
    • otherwise calls mla_gluon, as before;
    • logs the launch at info level;
    • module and function docstrings updated.
  • Second commit: the QK mask's column index is built inside the tile loop instead of before it. The output is bitwise
    equal; the effect on time is under "Validation and unverified scope".
  • dcbb9eaf7a: softmax_scale=0 gave NaN in every row whose last tile is partial.
    • Cause: the kernel masks the last tile's unused key columns to -inf before scaling, and -inf * 0 is NaN.
    • Fix: _qk_scale passes the smallest normal fp32 for a product in [0, that value), so masked scores stay -inf and
      every key weighs 1, as at 0. The kernel and the value passed at any other scale are unchanged.
  • 03e16bc83e: H=64 runs as one launch with two head groups instead of one launch per 32 heads.
    • H=32 without a sink compiles to the same instructions as before (Triton 3.8.0; not compared with a sink); the
      kernel name gains HEAD_GROUPS_1.
  • Tests: see "Changed op tests".

Performance (if applicable)

bench_sparse_attention_dsv4.py --shapes prefill, bf16, no sink; slots drawn uniformly at random. Each backend's time
is the mean of 50 back-to-back launches.

Hardware Workload/configuration Before (main, ms) After (ms) Change
MI355X H 32, Q 2,048, Kv 65,536, top-k 2,048 0.6797 0.4026 −40.8 %
MI355X H 32, Q 8,192, Kv 65,536, top-k 2,048 2.615 1.430 −45.3 %
MI355X H 32, Q 16,384, Kv 131,072, top-k 2,048 5.422 2.856 −47.3 %
MI355X H 64, Q 2,048, Kv 65,536, top-k 2,048 0.6753 0.6807 +0.8 %
MI355X H 64, Q 8,192, Kv 65,536, top-k 2,048 2.485 2.514 +1.1 %
MI355X H 64, Q 16,384, Kv 131,072, top-k 2,048 4.851 4.889 +0.8 %
python op_tests/op_benchmarks/triton/bench_sparse_attention_dsv4.py --shapes prefill \
    --prefill_cfgs 2048,32,65536,2048 8192,32,65536,2048 16384,32,131072,2048
python op_tests/op_benchmarks/triton/bench_sparse_attention_dsv4.py --shapes prefill \
    --prefill_cfgs 2048,64,65536,2048 8192,64,65536,2048 16384,64,131072,2048

H=64:

  • Before: the bench's gluon column (mla_gluon called with main's arguments). After: its pa_prefill_sparse
    column. Both from one process; means of two processes.
  • bh64 is correct on these inputs: T is a multiple of 64 and every row has 2,048 keys. Per process the kernel takes
    1.004–1.016× bh64's time.
  • 1 and 2 GiB pools (Kv 1,048,576 and 2,097,152; Q 8,192 and 16,384): 2.635 → 2.977 ms (+13.0 %), 5.460 → 6.146 ms
    (+12.6 %).
  • Against two launches, one per 32 heads (dcbb9eaf7a), with the same commands: 0.85–0.88× the time with the table's
    pools, 0.71–0.77× with 1–2 GiB pools.
  • Fabric reads per call (rocprofv3, TCC_EA0_RDREQ_{32B,64B,128B}), Q 8,192 on a 1 GiB pool, in multiples of the
    listed keys' bytes: two launches 2.03×, one launch 1.03×, one launch with a row's programs at adjacent ids 2.03×.
  • On index lists captured from GLM-5.3-Flash prefill (T 8,499 and 16,179, rows of 1 key and more), bh64 asserts; the
    kernel takes 1.11× the time of main's OPUS there ([Triton/Gluon] mla_sparse_prefill: triton backend on gfx950, the default at H=32 without a sink #7681).

Testing

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

Documentation

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

Dependencies

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

Breaking Changes

None in the API. On gfx950:

  • rows with no slots get zeros instead of being left unwritten;
  • H=64 calls run at any T instead of asserting unless T % 64 == 0;
  • the outputs of the calls that move to the kernel differ from mla_gluon's within the test tolerance, where
    mla_gluon's are correct.

PR Self-Review

Reviewed revision

2718e0df2b928c4092f8784d119f1154622e508c

Behavior and intent

  • On gfx950, calls with bf16 q and KV, H=32 or 64, D=512, a 2-D unified_kv, unit inner strides and has_invalid not
    True run the single-pass kernel instead of mla_gluon, at any T, with or without a sink. Other calls keep
    mla_gluon.
  • For these calls mla_gluon is slower (H=32), counts the sink once per key split, leaves rows without slots unwritten
    at one split, and at H=64 asserts unless T % 64 == 0 and gets rows of up to 128 keys wrong.

Validation and unverified scope

  • Hardware: MI355X (gfx950, 256 CUs).

  • Image: vllm/vllm-openai-rocm:nightly-rocm100-1648fefab91204f3b15bf8062217316f07f2c4bb (Triton 3.8.0). AITER is
    this branch or main (1eab5ae12c) from source.

  • This revision is 03e16bc83e with main 85ccf1794d merged; this PR's diff is unchanged. Results below were taken
    at 03e16bc83e unless stated.

  • python -m pytest op_tests/triton_tests/attention/test_pa_prefill_sparse.py:

    • this branch: 29 passed, 43 skipped (other architectures), at 03e16bc83e and at this revision (with [Triton/Gluon] mla_sparse_prefill: triton backend on gfx950, the default at H=32 without a sink #7681 on top,
      which does not change the files tested);
    • this branch before dcbb9eaf7a, with this test file: the 8 [*-zero-scale] cases fail (83–85 % of the elements
      mismatch), the other 21 pass;
    • main with this PR's test file: the 16 test_pa_prefill_sparse_gfx950_single_pass cases fail, the other 13 pass.
      • H=32: 114,688 of 786,432 (T 48) and 704,512 of 4,915,200 (T 300) elements mismatch: the empty rows stay NaN.
      • H=64: AssertionError: mla_gluon[bh64] requires batch_size divisible by 64, got 48 (and 300).
  • Mutations of this branch, each failing only the named cases of test_pa_prefill_sparse_gfx950_single_pass:

    • the second head group reads the first one's sinks: [300-64-True-*], [48-64-True-*];
    • the second head group reads the first one's queries: [*-64-*-scaled] (at softmax_scale 0 every key weighs 1,
      so the output does not depend on q);
    • the second head group writes the first one's output: the eight H=64 cases;
    • the last T % 8 rows run the first head group twice: [300-64-*] (T=300 leaves 4 such rows, T=48 none);
    • the kernel only from 64 query rows, as in this PR's first commits: the eight T=48 cases;
    • sink-free H=32 calls below 64 rows kept on mla_gluon: [48-32-False-*];
    • _qk_scale passing 0 through, as before dcbb9eaf7a: the eight [*-zero-scale] cases (run at dcbb9eaf7a).
  • The mla_gluon values in Motivation come from probes of main's pa_prefill_sparse against a reference softmax.

  • Benchmark revisions:

    • H=32 rows: main and this branch at d25bc29d22, one after the other on one GPU. At this revision those calls
      compile to the same instructions and launch configuration.
    • H=64 rows and the notes under them: 03e16bc83e, on another MI355X; the OPUS ratio with this revision's kernel
      against main 85ccf1794d's OPUS.
  • Second commit (column index per tile) against the first commit's kernel, H=32:

    • rows 1–2: a harness outside this PR, both kernels interleaved in one process, 512 MiB written before each call;
    • row 3: the in-tree commands, one run per kernel, on one GPU.
    Triton Index lists Time against the first commit
    3.8.0 captured from GLM-5.3-Flash prefill (8,499 and 16,179 rows); synthetic, up to 512 runs of 4 adjacent slots per row (8,192 and 16,384 rows) 0.944–0.952×
    3.7.0 the same 0.994–1.002×
    3.8.0 the tables of bench_sparse_attention_dsv4.py and test_mla_sparse_prefill.py 0.992–1.010×
  • dcbb9eaf7a's alternative, scaling the scores before the mask, also removes the NaN, but took 1.066–1.074× the
    kernel's time at H=32 (the same harness as the first two rows; Triton 3.8.0, 3 processes per input set).

  • Unverified:

    • time against mla_gluon for calls below 64 query rows, which now take the kernel;
    • calls the kernel does not serve keep mla_gluon and its behaviour above (other H, non-unit inner strides);
    • gfx942 and gfx1250, whose branches are unchanged;
    • MI250X and MI300X;
    • at H=64, the L2 sharing on other devices and partition modes: the kernel assumes that program i runs on XCD
      i % get_num_xcds(). The output does not depend on it (bitwise equal with a row's programs at adjacent ids);
    • Triton versions other than 3.8; 3.7 only in the second commit's timings above.

PR-specific question

The only new key is _pa_prefill_sparse (BLOCK_K 64, num_warps 4) in
aiter/ops/triton/configs/gfx950/gluon/attention/pa_prefill_sparse/DEFAULT.json.

  • Only _single_pass_config() reads it, for the single-pass kernel's launch; it does not decide which calls take the
    kernel.
  • _single_pass_unsupported decides: gfx950, H=32 or 64, D=512, bf16 q and unified_kv, a 2-D unified_kv of width
    D, unit inner strides for both and has_invalid not True.
  • Every other call takes the branch it took before: other H (the H=16 cases of test_pa_prefill_sparse_gfx950 run
    mla_gluon and pass), other dtypes or strides and has_invalid=True run mla_gluon; gfx942 and gfx1250 are
    untouched.
  • No test asserts which kernel an unrelated shape reaches; a spy on mla_gluon for H=16 would pin it.

Changed op tests

op_tests/triton_tests/attention/test_pa_prefill_sparse.py:

  • test_pa_prefill_sparse_gfx950 runs H=16, 32 and 64 (was 64):
    • H=32 and 64 run the new kernel; H=16 keeps covering mla_gluon;
    • at T=16,384 with 512 slots per row the pool is 8 GiB, so KV offsets need 64 bits;
    • it passes on main: T is a multiple of 64 and no row is empty or short.
  • test_pa_prefill_sparse_gfx950_single_pass (new): H=32 and 64, T=300 and 48, with and without a sink, at
    softmax_scale 512^-0.5 and 0.
    • Empty, short and partial rows are written into a NaN-filled out.
    • Sinks are drawn from N(5, 1), near the rows' log-denominators, so that a wrong sink moves the output past the
      tolerance.
    • mla_gluon sees 64 CUs (get_num_sms patched), as under CU_NUM or a compute partition: at H=32 it then runs one
      key split and leaves the empty rows unwritten, and at H=64 it asserts.
    • All 16 cases fail on main. No existing test has empty rows on gfx950.
  • _ragged_rows is moved out of test_pa_prefill_sparse_single_source, unchanged, so that both tests use it.

AI assistance (Claude) was used for this PR; the change, tests and measurements were reviewed and run by the submitter.

… at H=32

On gfx950, pa_prefill_sparse calls mla_gluon. For bf16 q and KV with 32
heads of a 512-wide latent, a new single-pass kernel now serves the call.
One program computes one query row for all 32 heads. It copies each
64-key tile from the pool straight into LDS and runs the QK and PV MFMAs
on it. mla_gluon still serves other shapes, has_invalid=True and calls
with fewer than MIN_TOKENS rows.

- Kernel: _gluon_kernels/gfx950/attention/pa_prefill_sparse.py, without
  a torch import.
- Config: configs/gfx950/gluon/attention/pa_prefill_sparse/DEFAULT.json
  (MIN_TOKENS, BLOCK_K, num_warps).
- At H=32, a row with no slots now writes zeros. mla_gluon does not
  write such rows.
- Tests: H=32 in test_pa_prefill_sparse_gfx950 (an 8 GiB pool at
  T=16384), and test_pa_prefill_sparse_gfx950_single_pass (empty, short
  and partial rows into a NaN-filled out, with and without a sink).

Signed-off-by: Rony Leppänen <[email protected]>
…er tile

The QK mask's column index is built inside the tile loop instead of
before it. The compiled kernel changes in instruction order and register
count (6 more VGPRs, still 2 waves per SIMD); the output is bitwise
equal.

On MI355X at H=32 the kernel takes, against its previous time:
- 0.944-0.952x with Triton 3.8 and 0.994-1.002x with Triton 3.7, on
  index lists captured from GLM-5.3-Flash prefill in SGLang (8,499 and
  16,179 rows) and on synthetic lists whose rows hold up to 512 runs of
  4 adjacent slots (8,192 and 16,384 rows);
- 0.992-1.010x with Triton 3.8 on the generated tables of
  bench_sparse_attention_dsv4.py and test_mla_sparse_prefill.py.

Signed-off-by: Rony Leppänen <[email protected]>
@github-actions

github-actions Bot commented Nov 9, 2026

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 7680 --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.

…oo, and for small calls mla_gluon gets wrong

The gfx950 single-pass kernel now also serves H=64, as two launches of its
32-head program, and calls with a sink below MIN_TOKENS rows. mla_gluon
keeps the sink-free H=32 calls below MIN_TOKENS rows, which it gets right,
and the shapes the kernel does not serve.

On MI355X, mla_gluon on the calls that move:
- H=64 (its bh64 regime) asserts unless T is a multiple of 64. With one
  key split it returns wrong output for every row of at most 128 keys
  (H=64 and 128, T=1024, rows of 1 to 2,046 keys).
- With a sink and more than one key split, it adds the sink once per
  split. At T=1, H=32, with 128 keys of ones and a sink of log(128), it
  returns 1/3 where the answer is 1/2. At T=48 every row is wrong.

At H=64 the two launches take 0.76-0.78x OPUS's time on index lists
captured from GLM-5.3-Flash prefill (8,499 and 16,179 rows) and on
synthetic lists (8,192 and 16,384 rows, and 8,192 after an 8,192-row
prefix). On the one input of these that mla_gluon gets right (8,192 rows
after the prefix), it takes 0.75-0.76x OPUS's time against the two
launches' 0.78x.

- _single_pass_unsupported(): why the kernel cannot serve a call, or None.
- Tests: test_pa_prefill_sparse_gfx950_single_pass runs H=32 and 64 at
  T=300 and 48, with and without a sink; test_pa_prefill_sparse_gfx950
  adds H=16 for the mla_gluon branch.

Signed-off-by: Rony Leppänen <[email protected]>
…g-denominators

test_pa_prefill_sparse_gfx950_single_pass's T=48 sink cases check that
the kernel, not mla_gluon, serves them: mla_gluon splits each row's keys
in two there and adds the sink once per split. With sinks from randn(H),
that error is small next to rows of hundreds of keys. In a CPU estimate
over 8 draws of the test's inputs, it exceeded the tolerance on 0 to 17
of the 48 rows. With randn(H) + 5 it does so on 26 to 33 rows in every
draw.

Signed-off-by: Rony Leppänen <[email protected]>
… every call it serves, at any T

Sink-free H=32 calls below 64 query rows stayed on mla_gluon, which splits
each row's keys over the CUs. On a device with fewer CUs (a CU_NUM setting
or a compute partition) such a call runs in one key split, where mla_gluon
leaves rows without keys unwritten. Every call the single-pass kernel serves
now takes it, so MIN_TOKENS leaves the gfx950 config.

The single-pass test runs with mla_gluon seeing 64 CUs, so a call routed
back to mla_gluon fails it at any T.

Signed-off-by: Rony Leppänen <[email protected]>
@eppaneamd
eppaneamd marked this pull request as ready for review November 10, 2026 12:18
@eppaneamd
eppaneamd requested review from a team and a balanced review from Copilot November 10, 2026 12:18
@aiter-bot

aiter-bot commented Nov 10, 2026 •

Copy link
Copy Markdown

READY FOR HUMAN REVIEW

PR Self-Review

Revision 2718e0df2b92 · mode shadow

Author self-review accepted. Readiness report:

Author answers

  • PASS — current-SHA answers are present and structurally specific
Show the author's self-review answers

Behavior and intent

- On gfx950, calls with bf16 q and KV, H=32 or 64, D=512, a 2-D `unified_kv`, unit inner strides and `has_invalid` not
  `True` run the single-pass kernel instead of `mla_gluon`, at any T, with or without a sink. Other calls keep
  `mla_gluon`.
- For these calls `mla_gluon` is slower (H=32), counts the sink once per key split, leaves rows without slots unwritten
  at one split, and at H=64 asserts unless T % 64 == 0 and gets rows of up to 128 keys wrong.

Validation and unverified scope

- Hardware: MI355X (gfx950, 256 CUs).
- Image: `vllm/vllm-openai-rocm:nightly-rocm100-1648fefab91204f3b15bf8062217316f07f2c4bb` (Triton 3.8.0). AITER is
  this branch or `main` (`1eab5ae12c`) from source.
- This revision is `03e16bc83e` with `main` `85ccf1794d` merged; this PR's diff is unchanged. Results below were taken
  at `03e16bc83e` unless stated.
- `python -m pytest op_tests/triton_tests/attention/test_pa_prefill_sparse.py`:
  - this branch: 29 passed, 43 skipped (other architectures), at `03e16bc83e` and at this revision (with #7681 on top,
    which does not change the files tested);
  - this branch before `dcbb9eaf7a`, with this test file: the 8 `[*-zero-scale]` cases fail (83–85 % of the elements
    mismatch), the other 21 pass;
  - `main` with this PR's test file: the 16 `test_pa_prefill_sparse_gfx950_single_pass` cases fail, the other 13 pass.
    - H=32: 114,688 of 786,432 (T 48) and 704,512 of 4,915,200 (T 300) elements mismatch: the empty rows stay NaN.
    - H=64: `AssertionError: mla_gluon[bh64] requires batch_size divisible by 64, got 48` (and 300).
- Mutations of this branch, each failing only the named cases of `test_pa_prefill_sparse_gfx950_single_pass`:
  - the second head group reads the first one's sinks: `[300-64-True-*]`, `[48-64-True-*]`;
  - the second head group reads the first one's queries: `[*-64-*-scaled]` (at `softmax_scale` 0 every key weighs 1,
    so the output does not depend on q);
  - the second head group writes the first one's output: the eight H=64 cases;
  - the last T % 8 rows run the first head group twice: `[300-64-*]` (T=300 leaves 4 such rows, T=48 none);
  - the kernel only from 64 query rows, as in this PR's first commits: the eight T=48 cases;
  - sink-free H=32 calls below 64 rows kept on `mla_gluon`: `[48-32-False-*]`;
  - `_qk_scale` passing 0 through, as before `dcbb9eaf7a`: the eight `[*-zero-scale]` cases (run at `dcbb9eaf7a`).
- The `mla_gluon` values in Motivation come from probes of `main`'s `pa_prefill_sparse` against a reference softmax.
- Benchmark revisions:
  - H=32 rows: `main` and this branch at `d25bc29d22`, one after the other on one GPU. At this revision those calls
    compile to the same instructions and launch configuration.
  - H=64 rows and the notes under them: `03e16bc83e`, on another MI355X; the OPUS ratio with this revision's kernel
    against `main` `85ccf1794d`'s OPUS.
- Second commit (column index per tile) against the first commit's kernel, H=32:
  - rows 1–2: a harness outside this PR, both kernels interleaved in one process, 512 MiB written before each call;
  - row 3: the in-tree commands, one run per kernel, on one GPU.

  | Triton | Index lists | Time against the first commit |
  |---|---|---:|
  | 3.8.0 | captured from GLM-5.3-Flash prefill (8,499 and 16,179 rows); synthetic, up to 512 runs of 4 adjacent slots per row (8,192 and 16,384 rows) | 0.944–0.952× |
  | 3.7.0 | the same | 0.994–1.002× |
  | 3.8.0 | the tables of `bench_sparse_attention_dsv4.py` and `test_mla_sparse_prefill.py` | 0.992–1.010× |

- `dcbb9eaf7a`'s alternative, scaling the scores before the mask, also removes the NaN, but took 1.066–1.074× the
  kernel's time at H=32 (the same harness as the first two rows; Triton 3.8.0, 3 processes per input set).
- Unverified:
  - time against `mla_gluon` for calls below 64 query rows, which now take the kernel;
  - calls the kernel does not serve keep `mla_gluon` and its behaviour above (other H, non-unit inner strides);
  - gfx942 and gfx1250, whose branches are unchanged;
  - MI250X and MI300X;
  - at H=64, the L2 sharing on other devices and partition modes: the kernel assumes that program i runs on XCD
    i % `get_num_xcds()`. The output does not depend on it (bitwise equal with a row's programs at adjacent ids);
  - Triton versions other than 3.8; 3.7 only in the second commit's timings above.

PR-specific question

The only new key is `_pa_prefill_sparse` (`BLOCK_K` 64, `num_warps` 4) in
`aiter/ops/triton/configs/gfx950/gluon/attention/pa_prefill_sparse/DEFAULT.json`.
- Only `_single_pass_config()` reads it, for the single-pass kernel's launch; it does not decide which calls take the
  kernel.
- `_single_pass_unsupported` decides: gfx950, H=32 or 64, D=512, bf16 q and `unified_kv`, a 2-D `unified_kv` of width
  D, unit inner strides for both and `has_invalid` not `True`.
- Every other call takes the branch it took before: other H (the H=16 cases of `test_pa_prefill_sparse_gfx950` run
  `mla_gluon` and pass), other dtypes or strides and `has_invalid=True` run `mla_gluon`; gfx942 and gfx1250 are
  untouched.
- No test asserts which kernel an unrelated shape reaches; a spy on `mla_gluon` for H=16 would pin it.

Changed op tests

`op_tests/triton_tests/attention/test_pa_prefill_sparse.py`:
- `test_pa_prefill_sparse_gfx950` runs H=16, 32 and 64 (was 64):
  - H=32 and 64 run the new kernel; H=16 keeps covering `mla_gluon`;
  - at T=16,384 with 512 slots per row the pool is 8 GiB, so KV offsets need 64 bits;
  - it passes on `main`: T is a multiple of 64 and no row is empty or short.
- `test_pa_prefill_sparse_gfx950_single_pass` (new): H=32 and 64, T=300 and 48, with and without a sink, at
  `softmax_scale` 512^-0.5 and 0.
  - Empty, short and partial rows are written into a NaN-filled `out`.
  - Sinks are drawn from N(5, 1), near the rows' log-denominators, so that a wrong sink moves the output past the
    tolerance.
  - `mla_gluon` sees 64 CUs (`get_num_sms` patched), as under `CU_NUM` or a compute partition: at H=32 it then runs one
    key split and leaves the empty rows unwritten, and at H=64 it asserts.
  - All 16 cases fail on `main`. No existing test has empty rows on gfx950.
- `_ragged_rows` is moved out of `test_pa_prefill_sparse_single_source`, unchanged, so that both tests use it.

AI assistance (Claude) was used for this PR; the change, tests and measurements were reviewed and run by the submitter.

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply
  • ADVISORY — Copilot did not review the current head SHA

Code and op-test hygiene

  • PASS — no committed artifact or conflict marker detected

Required CI

  • PASS — Black: passed
  • PASS — Ruff: passed
  • PASS — Repository dependency: passed
  • PASS — Aiter Test Gate: passed

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

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

Accepted bf16 KV views can violate the new copy path’s alignment requirements, and zero softmax scaling can produce NaN outputs from masked scores.

2 open issues
What changed in this PR

Adds a single-pass Gluon path to pa_prefill_sparse on gfx950 for bf16 inputs with 32 heads and dimension 512. The public API stays unchanged, and other calls retain the existing dispatch.

Changes:

  • Adds a kernel that shares KV loads across all 32 heads, supports an optional sink, and writes zeros for empty rows.
  • Adds JSON launch settings and dispatch conditions for the new path.
  • Extends tests for H=32, large KV offsets, and empty, short, and partial rows.
File Description
op_tests/​triton_tests/​attention/​test_pa_prefill_sparse.py Adds H=32 and ragged-row coverage; shares the row-generation helper.
aiter/​ops/​triton/​configs/​gfx950/​gluon/​attention/​pa_prefill_sparse/​DEFAULT.json Defines the row threshold, tile size, and warp count.
aiter/​ops/​triton/​attention/​pa_prefill_sparse.py Adds configuration loading, dispatch, and launch support.
aiter/​ops/​triton/​_gluon_kernels/​gfx950/​attention/​pa_prefill_sparse.py Implements single-pass sparse attention with 64-bit row offsets.

Review effort: Balanced

eppaneamd added a commit that referenced this pull request Nov 10, 2026
…kernel serves, H=64 too, and opus when pa_prefill_sparse cannot load

backend="triton" now takes exactly the calls pa_prefill_sparse's gfx950
single-pass kernel serves (_single_pass_unsupported) within its own
contract (one KV source, no split-K, out None or contiguous), and raises
RuntimeError for the rest. Before, it also accepted q or unified_kv with
an inner stride other than 1, and a unified_kv that is not 2-D.
pa_prefill_sparse sent those to mla_gluon, which ignores those strides:
on MI355X every row of a T=256, H=32 call came back wrong. Calls with a
sink below MIN_TOKENS rows now run the kernel inside pa_prefill_sparse
(merged from #7680), where mla_gluon added the sink once per key split.

With #7680's H=64 support the triton backend serves H=64 too, and
backend=None takes it for sink-free H=64 calls with at least MIN_TOKENS
rows, as at H=32. Its choice at H=32 is unchanged.

backend=None raised ImportError where pa_prefill_sparse's module cannot
be imported (its gluon kernels need a Triton with gluon). It now runs
opus there, and logs why once.

Tests: test_triton_backend_rejects (q with inner stride 2, a 3-D
unified_kv), test_default_backend_without_gluon, test_default_backend_gfx950
at H=32 and 64, and an H=64 one-source case in the sweep.

Signed-off-by: Rony Leppänen <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 10, 2026 12:35

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

Scaling masked -inf scores produces NaN attention weights on partial tiles when softmax_scale=0, rather than valid attention outputs.

4 open issues

Review effort: Balanced

@eppaneamd eppaneamd changed the title [Triton/Gluon] pa_prefill_sparse: single-pass gluon kernel for gfx950 at H=32 [Triton/Gluon] pa_prefill_sparse: single-pass gluon kernel for gfx950 at H=32 and 64 Nov 10, 2026
@eppaneamd
eppaneamd marked this pull request as draft November 10, 2026 12:53
… key equally, not NaN

The single-pass kernel masks the scores of a partial key tile to -inf and then
multiplies them by qk_scale, so softmax_scale=0 gave -inf * 0 = NaN, and a NaN
output for every row with a partial last tile. _qk_scale now passes the
smallest normal fp32 for a product in [0, that value): masked scores stay -inf
and every key weighs 1, as at scale 0. Other scales pass the same value as
before, to an unchanged kernel. Scaling the scores before the mask instead
measured 1.07x the kernel time at H=32 on MI355X.

test_pa_prefill_sparse_gfx950_single_pass also runs at softmax_scale=0.

Signed-off-by: Rony Leppänen <[email protected]>
…f a row in one launch

At H=64 the single-pass kernel ran twice, once per half of the heads, and
each launch gathered every key. It now runs once with T * 2 programs: a
row's two 32-head programs sit at program ids L and L + NUM_XCDS, on one
XCD, so the second one's KV gathers can hit the first one's in L2. The
last T % NUM_XCDS rows put their halves at adjacent ids. HEAD_GROUPS and
NUM_XCDS are constexprs and T comes from the grid, so there is one
compile per head count at any T; at H=32 (HEAD_GROUPS 1, NUM_XCDS 1) the
instruction stream is unchanged. HEAD_GROUPS joins the kernel's name.

Signed-off-by: Rony Leppänen <[email protected]>
eppaneamd added a commit that referenced this pull request Nov 13, 2026
…d/mla-sparse-prefill-triton-backend

Brings in main up to 85ccf17 through #7680.

Conflicts:
- aiter/ops/mla_sparse_prefill.py: main routes the op through the plugin
  interface (#7791). mla_sparse_prefill now dispatches as
  mla_sparse_prefill_v41 does: enabled providers first, then a native
  fallback that picks triton or opus from backend=. The routed op
  (_mla_sparse_prefill_routed, opus) and its contract are unchanged.
- op_tests/test_mla_sparse_prefill.py: docstrings. main replaced
  --total_tokens with --window and passes total_tokens=n, so the gfx950
  triton candidate now keys on an empty kv_indices_extend (what the triton
  backend requires) instead of total_tokens == 0.

tests/test_plugin_routed_ops.py: the backend-pin test also covers
backend="triton".

Signed-off-by: Rony Leppänen <[email protected]>
@eppaneamd
eppaneamd marked this pull request as ready for review November 13, 2026 18:50
Copilot AI balanced review requested due to automatic review settings November 13, 2026 18:50

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

Negative softmax scales turn masked scores into +inf and invalidate the denominator for partial tiles, requiring scaling before masking and taking the maximum of the scaled scores.

1 open issue
4 resolved since last review

Review effort: Balanced

Comment on lines +187 to +189
s = gl.where((j * BLOCK_K + cols_c < n)[None, :], s, float("-inf"))
m_new = gl.maximum(m, gl.max(s, axis=1) * qk_scale)
p = gl.exp2(s * qk_scale - m_new[:, None])
@zufayu
zufayu requested a review from Dewei-Wang-sh November 17, 2026 13:29
@zufayu
zufayu requested a review from a team November 17, 2026 13:29
@github-actions

github-actions Bot commented Dec 3, 2026

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 Dec 3, 2026

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

Labels

stale The PR hasn't been updated in 2 weeks Triton/Gluon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants