Skip to content

[FlyDSL] Add fused QSA sparse decode for gfx950 - #6670

Merged
zufayu merged 7 commits into
ROCm:mainfrom
amd-nprotaso:pr/flydsl_qsa
Oct 23, 2026
Merged

zufayu merged 7 commits into
ROCm:mainfrom
amd-nprotaso:pr/flydsl_qsa

Conversation

@amd-nprotaso

Copy link
Copy Markdown
Contributor

Motivation

Qwen3.8 decodes with Qwen sparse attention (QSA): an indexer picks up to 2051 KV positions per query row, then attention runs over that selection only.
This PR adds a fused FlyDSL kernel for gfx950 that reads the selected KV straight from the KV pool and splits each row across CTAs. It was developed in SGLang and moves here so it lives with the other FlyDSL attention kernels. SGLang will call it through aiter.ops.flydsl.qsa_sparse_decode.

Technical Details

API (aiter/ops/flydsl/qsa_sparse_decode.py)

flydsl_qsa_sparse_decode(
    q,                     # [rows, Hq, 256] bf16, contiguous
    k_cache, v_cache,      # [num_slots, Hkv, 256] bf16 or fp8 e4m3fn (no scales)
    req_to_token,          # [num_reqs, max_ctx] int32
    row_req_pool_indices,  # [rows] int32/int64
    indices,               # [rows, topk] int32 logical positions, -1 padded
    seq_lens,              # [rows] int32/int64
    softmax_scale,
    *, num_splits, workspace=None,
) -> out                   # [rows, Hq, 256] bf16
  • An entry is attended iff 0 <= pos < seq_lens[row], and maps to slot req_to_token[row_req_pool_indices[row], pos]. Rows with no valid entry produce zeros.
  • Speculative verify uses the same entry point: each row is its own length-one query.
  • can_use_flydsl_qsa_sparse_decode(q, k, v) reports whether the operands are supported: gfx950, head_dim 256, GQA group <= 16, contiguous pool.
  • recommended_num_splits(rows, Hkv, topk, device) picks the split count that fills the GPU at that batch. workspace_partials(max_rows, ...) sizes one workspace for every batch up to max_rows. A CUDA-graph caller allocates once at its captured maximum and reuses it, since graphs keep raw pointers.

Kernels (aiter/ops/flydsl/kernels/qsa/)

  • sparse_decode_attn.py: one CTA per (row, KV head, KV split). Four waves take 16-token tiles and each runs its own online softmax; the waves merge in LDS once, at the end.
    • QK: mfma_f32_16x16x32_bf16 with tokens on M and query heads on N. The K rows are read straight from the pool.
    • P: the QK accumulator is already the B-operand layout of mfma_f32_16x16x16bf16_1k, so P never leaves registers.
    • V: staged in a wave-private LDS tile and read back transposed with ds_read_b64_tr_b16.
    • Large pools: pool offsets are 64-bit, so pools beyond 4M tokens work.
    • Specialization: each kernel is compiled for its GQA group, top-k, scale, single vs multi split, index dtypes, and bf16 vs fp8 KV.
  • sparse_decode_reduce.py: merges the per-split partials (max, denominator, normalized output). It is skipped when num_splits == 1; the attention kernel then writes the output directly.

Test Plan

op_tests/test_flydsl_qsa_sparse_decode.py, following the op_test standard. Running python op_tests/test_flydsl_qsa_sparse_decode.py runs the correctness checks first, then the perf sweep:

  • Correctness (pytest, 18 cases) against an fp32 torch reference:
    • masking: varying valid prefixes, -1 padding, an entry at seq_len, an empty row, NaN in an unreferenced slot
    • single split and 33 splits; GQA groups 3, 12 and 16; small top-k (1, 64, 65, 512)
    • int32/int64 index dtypes; pool offsets beyond int32
    • CUDA-graph replay picking up new indices, lengths and request map
    • workspace sizing for every captured batch
    • which operands are accepted, and ValueError on bad arguments
  • Perf sweep with Qwen3.8 shapes: top-k 2051, (Hq, Hkv) = (24, 2) at TP1 and (3, 1) at TP8, rows 1–128, bf16 and fp8 KV. It uses the workspace and split count the SGLang backend uses, and rotates the KV pool so the selected KV is read cold from HBM.

Test Result

MI355X (gfx950). All 18 pytest cases pass; every sweep row has err 0.

rows Hq,Hkv splits bf16 us bf16 TB/s fp8 us fp8 TB/s
1 24,2 17 19.0 0.22 18.4 0.12
4 24,2 17 19.4 0.87 18.8 0.45
16 24,2 7 26.9 2.53 25.2 1.36
64 24,2 2 56.3 4.82 47.8 2.87
128 24,2 1 86.6 6.27 69.4 3.95
1 3,1 17 16.5 0.13 15.9 0.07
4 3,1 17 16.7 0.51 16.1 0.27
16 3,1 11 20.0 1.70 18.2 0.94
64 3,1 4 32.7 4.15 30.1 2.28
128 3,1 2 49.0 5.54 45.0 3.04

The op is gfx950-only; on other archs main() logs and skips, and the pytest cases are skipped.

Qwen sparse attention (QSA) decode and speculative verify straight from the
KV pool: each query row's top-k positions are mapped to pool slots through
req_to_token inside the kernel, so there is no gather pass and no packed
scratch, and each row's selection is split across CTAs with a separate
merge kernel. BF16 Q with BF16 or FP8 e4m3fn KV, head_dim 256, GQA group
up to 16. CUDA-graph safe given a caller-provided workspace.

Moved from SGLang (sglang.kernels.ops.attention.qsa_flydsl) so the kernel
lives next to the other FlyDSL attention kernels.

op_tests/test_flydsl_qsa_sparse_decode.py: pytest correctness cases (masking,
small top-k, index dtypes, >int32 pool offsets, CUDA-graph replay, workspace
sizing, operand checks) and a perf sweep over Qwen3.8 TP1/TP8 shapes.

Co-Authored-By: Claude <[email protected]>
@amd-nprotaso
amd-nprotaso requested a review from a team October 19, 2026 12:03
@github-actions

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:windows Windows JIT op smoke test on the gfx1201 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 6670 --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.

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

The existing 18 correctness tests pass on gfx950 with the pinned FlyDSL 0.3.4.1. Additional tests with holes in the selection and GQA groups 1, 7, and 16 also pass. However, the public wrapper has three reproducible issues involving workspace memory corruption, GPU device selection/caching, and accepting CPU inputs. Please address the inline findings before merging.

The failed MI35X CI shard stopped at pytest tests because that directory was missing, before running the operator tests; it did not exercise this new operator.

Comment thread aiter/ops/flydsl/qsa_sparse_decode.py Resolved
Comment thread aiter/ops/flydsl/qsa_sparse_decode.py Resolved
Comment thread aiter/ops/flydsl/qsa_sparse_decode.py Resolved
@zufayu

zufayu commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

One non-blocking test-cost question: est_slot_offsets_beyond_int32 allocates two FP8 tensors with approximately 4.2 million slots and shape [num_slots, 2, 256]. This is about 2 GiB per tensor, or approximately 4 GiB total before the other inputs.

Could you confirm that this cost is acceptable in the standard gfx950 op-test shard? If not, should this case be gated or marked as a targeted/slow test while retaining the 64-bit offset coverage?

@zufayu
zufayu merged commit 1cb414d into ROCm:main Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants