Skip to content

[Triton/Gluon] [QSA] Add qsa_select_paged_groups for row-sharded selection - #7815

Open
zovonoir wants to merge 4 commits into
ROCm:mainfrom
zovonoir:qsa-select-paged-groups
Open

zovonoir wants to merge 4 commits into
ROCm:mainfrom
zovonoir:qsa-select-paged-groups

Conversation

@zovonoir

@zovonoir zovonoir commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add qsa_select_paged_groups to aiter/ops/qsa.py: the score + top-k half of qsa_select_paged_tokens, returning [tokens, block_topk] int32 group ids and the token width qsa_expand_block_indices must emit for them. qsa_select_paged_tokens now calls it and then qsa_expand_block_indices; its signature and output are unchanged.

Source: ROCm/ATOM atom/model_ops/qwen4_exp/ops/qsa.py at ROCm/ATOM#2847 (cd2f5a0c). No kernel changes.

Motivation

aiter's QSA operators (#6469, #6515, #7008) were synced from ATOM at ROCm/ATOM#2652 (32a355f0). ROCm/ATOM#2847 then split the selection so each TP rank scores and selects only its own query rows; the ranks all-gather the group ids and expand every row locally. ATOM's qsa_attention.py calls qsa_select_paged_groups, so ROCm/ATOM#2679 (switch ATOM to aiter's QSA kernels) needs it from aiter.ops.qsa.

git diff 32a355f0 cd2f5a0c -- atom/model_ops/qwen4_exp/ops/qsa.py touches only qsa_select_paged_tokens and the new helpers; no @triton.jit code changed, so aiter/ops/triton/_triton_kernels/attention/qsa.py is unchanged here.

Changes

  • aiter/ops/qsa.py
    • _selection_widths(...) -> (scored columns, block_topk, output_width), factored out of qsa_select_paged_tokens (same power-of-two bucketing by max_seq_len).
    • qsa_select_paged_groups(...) -> (groups, output_width): chunked scoring + top_k_per_row_prefill(stable=True) into one [rows, block_topk] buffer. Same parameters as qsa_select_paged_tokens minus out, including allow_flydsl, compressed_k_scale and stacked_decode.
    • qsa_select_paged_tokens = qsa_select_paged_groups + one qsa_expand_block_indices over all rows (previously expanded per logits chunk; output is identical).
    • Exported in __all__, together with QSA_CACHE_FORMATS (ATOM imports the cache format names from aiter.ops.qsa).
  • op_tests/triton_tests/attention/test_qsa.py: test_qsa_row_sharded_groups_match_whole_batch.

Testing

  • Unit tests added or updated
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X (tested on MI308X, gfx942)

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. New function; qsa_select_paged_tokens keeps its signature and results.

PR Self-Review

Reviewed revision

c1749cb

Behavior and intent

New public op qsa_select_paged_groups for row-sharded selection. qsa_select_paged_tokens behavior unchanged; it now allocates the group ids for all rows at once (rows x block_topk int32) instead of per logits chunk, matching ATOM. QSA_CACHE_FORMATS is now importable from aiter.ops.qsa.

Validation and unverified scope

MI308X (gfx942), aiter main 023a9d147 + this change (c1749cb1d):

pytest op_tests/triton_tests/attention/test_qsa.py
112 passed

_expand_selected_groups offset widening: qsa_expand_block_indices over 2200 rows with out.stride(0) = 1_000_000, so the last row starts at element 2,199,000,000 (past int32): rows 0, 2147, 2148, 2149 and 2199 all match the reference. Before the cast the same script trips Memory access fault / GPU Hang. Suite caveat on the verification box: 47 FP4/MXFP4 cases fail there identically with and without this change (the image's Triton rejects ROUNDING_MODE in _mxfp4_quant_op); the 11 tests it ran before this PR all pass.

End to end with ROCm/ATOM#2679 (49251b2f, ATOM on these ops instead of its vendored copies): SGLang 0.5.22, Qwen3.8-Flash-Next-PTPC-FP8, TP4/EP4, MTP, BF16 KV, against the same image with ATOM's vendored QSA kernels. 4096 x 32 input throughput 29,978 vs 30,101 tok/s (mean of 6 runs each, -0.4%, within run-to-run spread), 30720 x 8 28,426 vs 28,663 tok/s, decode 4096 x 16 TPOT 9.63 vs 10.16 ms, GSM8K 400 0.975 vs 0.9675. Every rank logged the FlyDSL prefill attention line and no QSA fallback warning.

Not run: gfx950.

PR-specific question

Before this PR there is no qsa_select_paged_groups and no test that scores and selects a subset of rows, so the new test_qsa_row_sharded_groups_match_whole_batch fails on import/attribute lookup rather than on a wrong value: it is the only coverage of shard assembly.

test_qsa_multi_chunk_selection_matches_unchunked would fail on the pre-PR code only if chunk offsets were wrong, which the old per-chunk expand hid. It passes on main, and exists because the new one-call expand changed that: with the budget derived from _selection_widths, 1100 rows run as 400 + 400 + 300 and 1500 as 400 + 400 + 400 + 300, so a nonzero first-row offset and a short final chunk both occur. Writing every chunk at offset 0 makes both cases fail.

The int64 widening in _expand_selected_groups is the case that is not covered by a canonical test: 2200 rows x 2,051 columns only exceeds int32 with a out.stride(0) near 1e6, which no existing test builds. With row * out_s0 left in int32 it faults the GPU instead of failing an assert, so no default-shape test can catch it.

Changed op tests

op_tests/triton_tests/attention/test_qsa.py::test_qsa_row_sharded_groups_match_whole_batch[2|4]: 1500 rows after a 2000-token prefix, split into 2 / 4 shards (all > 256 rows, so every call uses the same scorer as the whole batch). Each shard runs qsa_select_paged_groups; the concatenated groups expanded by qsa_expand_block_indices must equal whole-batch qsa_select_paged_tokens exactly, and every shard must report the same width. Not redundant: no existing test calls the split API or checks shard assembly.

op_tests/triton_tests/attention/test_qsa.py::test_qsa_multi_chunk_selection_matches_unchunked[1100|1500]: with a 400-row logits_workspace_bytes, 1100 rows run as 400 + 400 + 300 and 1500 as 400 + 400 + 400 + 300, so a nonzero first-row offset and a short final chunk both occur. Groups, reported width and expanded tokens must equal an unchunked call exactly. Not redundant: the other selection tests fit in one chunk (the default workspace holds 32,768 rows at 256 columns) and so never write at a nonzero offset.

Port of ROCm/ATOM#2847 (cd2f5a0c), atom/model_ops/qwen4_exp/ops/qsa.py.
Scoring + top-k (qsa_select_paged_groups) is split from token expansion
(qsa_expand_block_indices) so a caller can select a row shard and expand
the gathered rows. qsa_select_paged_tokens keeps its signature and output.
No kernel changes.
@zovonoir
zovonoir requested review from a team and a balanced review from Copilot November 11, 2026 08:00
@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 7815 --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 11, 2026 •

Copy link
Copy Markdown

NOT READY FOR HUMAN REVIEW

PR Self-Review

Revision c1749cb1d179 · 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

New public op `qsa_select_paged_groups` for row-sharded selection. `qsa_select_paged_tokens` behavior unchanged; it now allocates the group ids for all rows at once (`rows x block_topk` int32) instead of per logits chunk, matching ATOM. `QSA_CACHE_FORMATS` is now importable from `aiter.ops.qsa`.

Validation and unverified scope

MI308X (gfx942), aiter main `023a9d147` + this change (`c1749cb1d`):

```
pytest op_tests/triton_tests/attention/test_qsa.py
112 passed
```

`_expand_selected_groups` offset widening: `qsa_expand_block_indices` over 2200 rows with `out.stride(0) = 1_000_000`, so the last row starts at element 2,199,000,000 (past int32): rows 0, 2147, 2148, 2149 and 2199 all match the reference. Before the cast the same script trips `Memory access fault` / `GPU Hang`. Suite caveat on the verification box: 47 FP4/MXFP4 cases fail there identically with and without this change (the image's Triton rejects `ROUNDING_MODE` in `_mxfp4_quant_op`); the 11 tests it ran before this PR all pass.

End to end with ROCm/ATOM#2679 (`49251b2f`, ATOM on these ops instead of its vendored copies): SGLang 0.5.22, Qwen3.8-Flash-Next-PTPC-FP8, TP4/EP4, MTP, BF16 KV, against the same image with ATOM's vendored QSA kernels. 4096 x 32 input throughput 29,978 vs 30,101 tok/s (mean of 6 runs each, -0.4%, within run-to-run spread), 30720 x 8 28,426 vs 28,663 tok/s, decode 4096 x 16 TPOT 9.63 vs 10.16 ms, GSM8K 400 0.975 vs 0.9675. Every rank logged the FlyDSL prefill attention line and no QSA fallback warning.

Not run: gfx950.

PR-specific question

Before this PR there is no `qsa_select_paged_groups` and no test that scores and selects a subset of rows, so the new `test_qsa_row_sharded_groups_match_whole_batch` fails on import/attribute lookup rather than on a wrong value: it is the only coverage of shard assembly.

`test_qsa_multi_chunk_selection_matches_unchunked` would fail on the pre-PR code only if chunk offsets were wrong, which the old per-chunk expand hid. It passes on main, and exists because the new one-call expand changed that: with the budget derived from `_selection_widths`, 1100 rows run as 400 + 400 + 300 and 1500 as 400 + 400 + 400 + 300, so a nonzero first-row offset and a short final chunk both occur. Writing every chunk at offset 0 makes both cases fail.

The int64 widening in `_expand_selected_groups` is the case that is not covered by a canonical test: 2200 rows x 2,051 columns only exceeds int32 with a `out.stride(0)` near 1e6, which no existing test builds. With `row * out_s0` left in int32 it faults the GPU instead of failing an assert, so no default-shape test can catch it.

Changed op tests

`op_tests/triton_tests/attention/test_qsa.py::test_qsa_row_sharded_groups_match_whole_batch[2|4]`: 1500 rows after a 2000-token prefix, split into 2 / 4 shards (all > 256 rows, so every call uses the same scorer as the whole batch). Each shard runs `qsa_select_paged_groups`; the concatenated groups expanded by `qsa_expand_block_indices` must equal whole-batch `qsa_select_paged_tokens` exactly, and every shard must report the same width. Not redundant: no existing test calls the split API or checks shard assembly.

`op_tests/triton_tests/attention/test_qsa.py::test_qsa_multi_chunk_selection_matches_unchunked[1100|1500]`: with a 400-row `logits_workspace_bytes`, 1100 rows run as 400 + 400 + 300 and 1500 as 400 + 400 + 400 + 300, so a nonzero first-row offset and a short final chunk both occur. Groups, reported width and expanded tokens must equal an unchunked call exactly. Not redundant: the other selection tests fit in one chunk (the default workspace holds 32,768 rows at 256 columns) and so never write at a nonzero offset.

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply

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
  • FAIL — Aiter Test Gate: failed
  • Required CI failed; fix it before requesting human review.

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

The expansion kernel’s int32 row-offset arithmetic overflows for the reported large input, potentially causing out-of-bounds writes.

1 open issue
What changed in this PR

Adds qsa_select_paged_groups so ATOM can select QSA groups on separate query-row shards, gather the group IDs, and expand them locally.

Changes:

  • Extracts shared width calculation and exports the new group-selection API.
  • Refactors qsa_select_paged_tokens to select groups first, then expand all rows in one call.
  • Adds two- and four-shard tests that check exact agreement with whole-batch selection.
File Description
op_tests/​triton_tests/​attention/​test_qsa.py Tests shard assembly, output widths, and whole-batch equivalence.
aiter/​ops/​qsa.py Adds group selection and shared width calculation; updates token selection to use them.

Review effort: Balanced

Comment thread aiter/ops/qsa.py Resolved
ATOM imports the cache format names from the consumer entry.
Copilot AI balanced review requested due to automatic review settings November 11, 2026 08:18

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.

🔵 Needs a closer look

The selection tests cover only one logits chunk, leaving nonzero-offset group-buffer writes and partial final chunks untested against an unchunked call.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Test multi-chunk selection with a partial final chunk

aiter/​ops/​qsa.py:212

The selection tests use only one logits chunk (this new case permits 32,768 rows per chunk but uses 1,500), so writes to the shared groups buffer with a nonzero row offset are not tested. Add a small-workspace case that forces multiple chunks, including a partial final chunk, and compare its groups and expanded tokens with an unchunked call.

Review effort: Balanced

zovonoir added a commit to mengfei-jiang/ATOM that referenced this pull request Nov 11, 2026
…ked decode scorer

* import qsa_select_paged_groups from aiter.ops.qsa (ROCm/aiter#7815); the
  row-sharded indexer selection from ROCm#2847 calls it.
* aiter scores decode-sized calls with the stacked kernel only when asked:
  pass stacked_decode=ATOM_QSA_STACKED_DECODE_SCORER at every selection
  call, as ATOM's own scorer read the flag before.
* drop the verification test that now lives in aiter's test_qsa.py.
@zufayu
zufayu requested review from a team and vgokhale November 12, 2026 01:10
`qsa_select_paged_tokens` now expands every selected row in one call instead
of per logits chunk, so `_expand_selected_groups` can see the whole batch: at
1,048,576 rows x 2051 output columns the last row's `row * out_s0` is
2,150,627,325 elements, past int32. Cast the row index to int64 before the
multiply, as the neighbouring kernels already do.

Also add `test_qsa_multi_chunk_selection_matches_unchunked`, which forces a
400-row logits workspace so nonzero chunk offsets and a partial final chunk
are exercised against an unchunked call.
Copilot AI balanced review requested due to automatic review settings November 19, 2026 08:56

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

The workspace budgets use the wrong scored width, producing 100-row chunks and leaving the intended partial-final-chunk coverage untested.

2 open issues

Review effort: Balanced

The scored width is rounded up to a power of two, so a page table with 256
columns and 4 compressed tokens per page scores 1024 columns, not 256. The
400-row budget was computed from the raw column count and therefore produced
100-row chunks with no partial final chunk. Use `_selection_widths` so the
chunks are the intended 400 + 400 + 300 and 400 + 400 + 400 + 300.
Copilot AI balanced review requested due to automatic review settings November 19, 2026 09:01

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.

🔵 Needs a closer look

Launch-size and pointer-offset changes require human confirmation with head-matched GPU validation, particularly for large output strides and the untested gfx950 path.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

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