Repository navigation
[FlyDSL] [QSA] Sparse prefill follow-ups: runtime softmax scale, shared helpers, opt-in batch invariance - #8148
Conversation
…ed helpers, opt-in batch invariance Follow-ups to the review of ROCm#6576. - The softmax scale is a kernel argument (FP32 softmax_scale * log2(e)): a new scale compiles nothing and can be graph-captured directly. - The QK, PV and rescale code and the direct key loop are defined once in sparse_prefill_common.py and shared by the union and both direct kernels. - The kernels use FlyDSL's stable API; what has no stable equivalent (empty-asm register pins, the target-features attribute, unexported ROCDL operations, volatile LDS reads and the lgkmcnt ledger) is gathered in one section of sparse_prefill_common.py with the reason it stays. - qsa_sparse_prefill gains a trailing keyword-only batch_invariant=False. The default keeps the call-wide union/direct decisions, with main's bits. With batch_invariant=True they are made per request (union planning rows, tile rows, no K3 routing by the others), so a request gives the same bits alone as in any batch. The workspace key includes the mode. - The non-MI308X warning says what is reused, and group 16 is tested. - The SGLang Triton code copied into the op test keeps SGLang's Apache-2.0 notice. Co-authored-by: Copilot <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
There was a problem hiding this comment.
🟡 Changes recommended
The advertised mixed_union_packed case is absent from every runnable suite, so explicitly selecting it exits successfully without running any validation.
1 open issue
What changed in this PR
This PR updates AITER’s FlyDSL QSA sparse prefill attention with runtime softmax scaling, shared kernel helpers, and an opt-in batch-invariance mode while retaining the default routing behavior.
Changes:
- Pass softmax scale at runtime rather than specializing kernels and workspaces for each value.
- Consolidate attention helpers and direct-kernel loops, and centralize low-level FlyDSL operations.
- Add per-request planning for batch invariance, expand tests, clarify hardware warnings, and preserve SGLang’s license notice.
| File | Description |
|---|---|
op_tests/test_flydsl_qsa_sparse_prefill.py |
Adds group-16, runtime-scale graph, and batch-invariance tests plus the SGLang notice. |
aiter/ops/qsa.py |
Exposes the keyword-only batch_invariant option. |
aiter/ops/flydsl/qsa_sparse_prefill.py |
Updates workspace caching, runtime scale binding, and mode selection. |
aiter/ops/flydsl/kernels/qsa/sparse_prefill_union.py |
Uses runtime scaling and shared pipeline helpers. |
aiter/ops/flydsl/kernels/qsa/sparse_prefill_prepare.py |
Adds per-request tile planning and direct-only tile handling. |
aiter/ops/flydsl/kernels/qsa/sparse_prefill_direct.py |
Adapts raw K/V access to the shared direct loop. |
aiter/ops/flydsl/kernels/qsa/sparse_prefill_direct_packed.py |
Defines the shared direct loop and packed K/V adapter. |
aiter/ops/flydsl/kernels/qsa/sparse_prefill_common.py |
Consolidates math helpers, low-level operations, and LDS tracking. |
Review effort: Balanced
Co-authored-by: Copilot <[email protected]>
READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
There was a problem hiding this comment.
🔵 Needs a closer look
The scheduler-sensitive gfx942 kernel and per-request routing changes lack native, head-matched correctness and performance validation, which this environment cannot independently perform.
No open issues 🎉
1 resolved since last review
Review effort: Balanced
fsx950223
left a comment
There was a problem hiding this comment.
No blocking defect found in head 40a96a6. Targeted MI308X testing passed runtime-scale graph capture, batch invariance, and group-16 coverage. Eight default-mode outputs were bitwise identical to the merge-base. Reviewed the complete diff, callers, prior feedback, and CI logs.
Validation:
- PASS on
local and hjbog-srdc-39.amd.com / sixifang_dev:git rev-parse HEAD
Isolated local and remote head checkouts verified as 40a96a6. Remote merge-base verified as 66be4f6. - PASS on
hjbog-srdc-39.amd.com / sixifang_dev:HIP_VISIBLE_DEVICES=0 flock -w 60 /workspace/codex-pr-review-8148-20261119/gpu.lock timeout 600 python3 op_tests/test_flydsl_qsa_sparse_prefill.py -c graph_new_scale_mixed_union_packed graph_new_scale_kv_slots_mixed_union_raw batch_invariance_h12 batch_invariance_full_prefill_h6 kv_slots_g16_hk2_ragged mixed_union_raw
Exit 0; six cases and 31 comparisons passed, none skipped. Graph outputs matched eager outputs bitwise; unseen scales added zero specializations; individual requests matched batch-invariant batch outputs bitwise. Verified gfx942 MI308X, 80 CUs, idle device 0, intended AITER imports, and isolated FlyDSL 0.5.0. Log: gfx942-focused-final.log. - PASS on
hjbog-srdc-39.amd.com / sixifang_dev:bash run-comparison.sh
Exit 0. compare_default.py ran against isolated merge-base and head trees with separate caches. All eight outputs matched bitwise: mixed union/raw direct H12, mixed union/packed direct H12, full prefill 4096 H6, and full prefill 8192 H3, each at scales 0.0625 and 0.0517. Commands and results: run-comparison.sh, compare_default.py, gfx942-comparison.log. - PASS on
hjbog-srdc-39.amd.com / sixifang_dev:HIP_VISIBLE_DEVICES=0 flock -w 60 /workspace/codex-pr-review-8148-20261119/gpu.lock timeout 180 python3 /workspace/codex-pr-review-8148-20261119/extra-h3.py
Exit 0; four comparisons passed for an 8192-row full H3 prefill batched with a 1024-row long-context extend, including exact solo-versus-batch equality. Log: gfx942-extra-h3.log. - SKIP on
dzm-mi355-gpu34-dev:ssh -o BatchMode=yes -o ConnectTimeout=10 -o RemoteCommand=none -o RequestTTY=no -o ControlMaster=no -o ControlPath=none dzm-mi355-gpu34-dev "squeue -h -u zhiming_ding_qle -o '%i|%N|%t'"
Allocation discovery exited 255: connection to aac16.amd.com port 22 timed out. No remote GPU test ran. This op supports gfx942 only. Log: gfx950-allocation.log.
Remaining validation:
- Other gfx942 CU counts, the union-scratch budget fallback, and pools exceeding 4 GiB were not independently tested.
- No controlled before/after performance benchmark was run by the reviewer.
- CI's MI35X job skipped this gfx942-only op; approval relies on the independent MI308X validation.
- Detailed commands, environment failures, and validation evidence are preserved in review-validation-notes.txt and the task-directory logs.
Automated Codex review of commit 40a96a6364b3.
Brings in #8148 (runtime softmax scale, shared QK/PV/rescale helpers, stable FlyDSL API, batch_invariant) and #8149 (FP8 K/V on gfx942, K3 runnable-first chunks, task splits, core tiles). gfx942 runs main's kernels; gfx950 runs this PR's kernels in main's forms (runtime SCALE_LOG2, the prim wrappers and counted LDS ledger of sparse_prefill_common, kernel_attrs()). - Raw direct (_Raw) takes K/V row bytes (KV_STRIDE * 2) where main passed KV heads; K3's route cost reads the first three terms of the 5-term route model (main unpacked exactly four) and _route_choice loops over len(limits). - K3 keeps the Int32 chunk-local sort key under main's CHUNKED, and both CHUNKED variants compile on a key's first compile. - One operand rule list (_check_call): main's compute, FP8 scale and batch_invariant rules beside the scale, stride and row rules; qsa_sparse_prefill_problem and the qsa.py wrappers take batch_invariant, k_scale, v_scale and compute. FP8 K/V of any FP8 dtype are rejected off gfx942, right after the device rule: #8149's kernels are E4M3FNUZ, and gfx950's dtypes.fp8 is E4M3FN. With compute="bf16" the per-request K span rule applies with a slot table too: the op then runs packed BF16 copies of the FP8 K/V, so qsa_sparse_prefill_problem reports what that call raises. - batch_invariant=True on gfx950: packed direct compiles the K16 loop raw direct runs, so packed and raw K/V give the same bits; the default mode keeps the K32 packed loop. - Platform: the gfx950 entry and route_all_or_none; FP8 routes on gfx942 only; overrides of a switch are written 0 or 1. - Tests: the op test is main's with this PR's suites appended (their layouts seeded by name; FP8 suites on gfx942, a rejection case elsewhere); the CPU tests cover main's rules. The warm-up does not compile main's batch-invariant and FP8 variants yet; the next commit extends it. Validated with FlyDSL 0.5.3: on gfx950 the op test, the output bits against this branch's head and K1-K3 against main's prepare module; on gfx942 compile-only, ISA and launch arguments against main's. On gfx950 an eager call takes 10 to 15 us more host time than on the head (main's operand rules and launch arguments); graph replay and kernel times are unchanged. Signed-off-by: Rony Leppänen <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>

Summary
Follow-ups to #6576 (QSA sparse prefill attention for MI308X), addressing its non-blocking review notes:
sparse_prefill_common.py, each item with the reason it stays.qsa_sparse_prefillgains an opt-inbatch_invariant=Truemode that makes every request's output independent of the other requests in the batch. The default behavior is unchanged.qsa_sparse_prefillonly gains the trailing keyword-onlybatch_invariant=False.Motivation
softmax_scalerecompiled all kernels (several seconds each), and graph capture with an unseen scale raised._qk/_pvexisted three times and_rescaletwice; the direct and direct-packed main loops were about 80% the same.flydsl._mlirAPIs, empty inline asm and hand-countedlgkmcntwaits were spread over the kernel files.--enable-deterministic-inference) had no way to avoid it.Changes
SCALE_LOG2: fx.Float32, computed on the host and rounded once to FP32 (the value the constant had). It is no longer part of the compile or workspace key. The union kernel keeps it in a VGPR with an empty-asm pin: left in an SGPR, the scale makes the union kernel spill 12 SGPRs and calls up to 4.4% slower (prefill 4096, H12)._rescale,_read_q_g2r,_qkand_pv_steplive insparse_prefill_common.py; the direct and direct-packed kernels share their key loop.flydsl._mlir. The low-level section ofsparse_prefill_common.pyholds the empty-asm register pins, the-packed-fp32-opstarget feature, the ROCDL operations FlyDSL does not export, volatile LDS reads and theLdsQueueledger that replaces hand-countedlgkmcntwaits. FP32-to-BF16 conversion stays the simplified round-to-nearest-even (bits + 0x7FFF + lsb, high halves joined byv_perm_b32), without inline asm.batch_invariant=False, a new trailing keyword-only argument.batch_invariant=Truethe decisions are taken per request: only requests of at least the union planning rows take union tiles, each request picks its own tile rows (BQ21/BQ42 for a full H6/H3 prefill; union compiles for the widest), and K3 routes no tile by the others. A request then gives the same bits alone as in any batch.op_tests/test_flydsl_qsa_sparse_prefill.py, and the SGLang notice (see Changed op tests).Performance (if applicable)
GPU time of one
qsa_sparse_prefillcall: the profiler's span from the first kernel's start to the last kernel's end, with the GPU kept busy while the call is queued (host time hidden). MI308X (gfx942, 80 CUs), FlyDSL 0.5.0, one KV head, T = 512; identical CPU-generated inputs. Each tree and mode runs in its own process and repeats the call (warm caches): median of 20 samples per process and of 3 alternating rounds. K/V are BF16.Default mode, main (
66be4f6d2) vs this PR; the outputs are bitwise equal:batch_invariant=True(opt-in), relative to the default mode of this PR:batch_invariant=True: per-request tile rows make batches with a full prefill 14–15% faster. Without call-wide routing, a long-context chunk runs every proposed tile union (+32%), and requests below the union planning rows run direct, so a batch of short extends, which the default mode runs union, takes 4.8 times as long. This is why it is opt-in.Testing
Documentation
Dependencies
Breaking Changes
None.
qsa_sparse_prefillonly gains the trailing keyword-onlybatch_invariant=False, and default-mode outputs are bitwise unchanged.PR Self-Review
Reviewed revision
40a96a6364b33261c184518c777dc220aedfa199Behavior and intent
batch_invariant=Truegives batch-invariant outputs for callers that need determinism, such as SGLang's--enable-deterministic-inference. It is not the default because per-request decisions make some batches much slower (see Performance).flydsl._mliruses.Validation and unverified scope
MI308X (gfx942, 80 CUs), FlyDSL 0.5.0.
python op_tests/test_flydsl_qsa_sparse_prefill.py: all rows pass, 238checkAllclosecomparisons, none skipped. WithCU_NUM=304(the plans of a 304-CU gfx942 such as MI300X) all rows pass too.batch_invariance_h12(5 requests, packed and raw direct, an empty request) andbatch_invariance_full_prefill_h6(BQ21 full prefill plus a short extend), every request alone withbatch_invariant=Trueis bitwise equal to its rows in the batch. Each batch also runs in the default mode, within tolerance and on a workspace of its own.batch_invariant=Truecalls interleaved on one stream (4 batch layouts, 3 calls each) give the same bits as each mode alone.black --check(26.10.1) andruff check(0.16.0) on the changed files are clean.CU_NUM. They log the warning above.PR-specific question
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? (the question the self-review rules select for changes under
op_tests/)batch_invariantargument, and its call-wide union planning and routing change the bits of a request's rows when other requests join the call. No existing row compares a request alone with the same request in a batch.Changed op tests
op_tests/test_flydsl_qsa_sparse_prefill.py: six rows, each run once at its own group (see PR-specific question): two group-16 rows, two runtime-scale graph rows (one through a slot table, so every kernel taking the scale runs), and two batch-invariance rows, which also run their batch in the default mode. The copied SGLang Triton code carries SGLang's Apache-2.0 notice, matching lines 9–309 ofsparse_attn.pyat SGLang 52fecfdf0908.