Skip to content

[CK] [Bugfix] Preserve virtual sinks in single-query batch prefill - #6760

Merged
shay-li77 merged 2 commits into
ROCm:mainfrom
lunarwhite:lunarwhite/fix-causal-prefill-virtual-sink
Oct 27, 2026
Merged

shay-li77 merged 2 commits into
ROCm:mainfrom
lunarwhite:lunarwhite/fix-causal-prefill-virtual-sink

Conversation

@lunarwhite

@lunarwhite lunarwhite commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Single-query causal batch prefill with a virtual attention sink fails with no matching kernel found. The qlen=1 optimization clears causal, selecting a no-mask specialization that CK does not generate with sink support. SGLang's AITER NHD attention path uses this API with virtual sinks.

This addresses the remaining single-query case after #4976, which corrected argument binding and sink detection. Virtual sink semantics were introduced in #2794.

Technical Details

Keep causal dispatch when sink_ptr is present, in both Python specialization selection and the C++ entry point. This uses the existing sink-enabled CK path; no kernel arithmetic, dependency or API changes. The no-sink optimization is unchanged. Explicitly noncausal virtual-sink support is outside this fix.

Test Plan

Five numerical cases reuse the existing reference: single-query sinks with unlimited/covering-full/local windows, a multi-query sink control, and a no-sink control. They participate in pytest and the existing standalone CI script.

Focused reproduction:

python -m pytest -q op_tests/test_batch_prefill.py -k test_batch_prefill_virtual_sink

Test Result

MI355X, BF16/page1, head dimension 128; PyTorch 2.9.1+rocm7.2.0.git7e1940d4, HIP 7.2.26015-fc0010cf6a, CK 96afae1d54fc. Identical tests and separate fresh JIT builds: base 2dff26579a17 has two expected dispatch failures and three passing controls; patched passes all five numerical checks, zero skips.

Patched GPU log, seconds include JIT, not performance measurements
{"case": [1, -1, true], "status": "pass", "seconds": 106.14433819055557}
{"case": [1, 8, true], "status": "pass", "seconds": 0.019419414922595024}
{"case": [1, 1, true], "status": "pass", "seconds": 0.0009224573150277138}
{"case": [2, -1, true], "status": "pass", "seconds": 0.0010075466707348824}
{"case": [1, -1, false], "status": "pass", "seconds": 101.00728063751012}

Submission Checklist

Disclosure: Found by Hyperloom session analysis, reviewed and validated by human.

Keep causal dispatch when a virtual sink is present. CK does not
generate no-mask sink specializations, so clearing the causal flag for a
single query selects an unsupported kernel configuration.

Apply the same guard to Python specialization selection and the C++
entry point. Cover full and sliding windows, multiple queries, and a no-
sink control with numerical regression tests.

Signed-off-by: Yuedong Wu <[email protected]>
@lunarwhite
lunarwhite requested review from a team and a balanced review from Copilot October 21, 2026 06:25
@github-actions github-actions Bot changed the title [Bugfix] Preserve virtual sinks in single-query batch prefill [CK] [Bugfix] Preserve virtual sinks in single-query batch prefill Oct 21, 2026
@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 6760 --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.

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.

🟢 Approval recommended

The narrow, matching dispatch guards address the failure with focused regression coverage and no unresolved findings.

No open issues 🎉

What changed in this PR

Fixes single-query causal batch prefill with virtual sinks by preserving sink-enabled CK dispatch, completing the earlier sink-detection fix.

Changes:

  • Align Python and C++ guards to retain causal dispatch when a virtual sink is present.
  • Add five numerical regression cases while preserving the no-sink optimization.
File Description
op_tests/​test_batch_prefill.py Adds virtual-sink and control cases for pytest and standalone execution.
csrc/​py_itfs_ck/​mha_batch_prefill_kernels.cu Preserves causal dispatch for virtual sinks.
aiter/​ops/​mha.py Applies the matching Python specialization guard.

Review effort: Balanced

@lunarwhite

Copy link
Copy Markdown
Contributor Author

Hi @valarLip, may I request your review on this fix? CI is passing. Thanks in advance.

@valarLip

Copy link
Copy Markdown
Collaborator

@aiter-bot

@valarLip

Copy link
Copy Markdown
Collaborator

@aiter-bot review

@aiter-bot

Copy link
Copy Markdown

aiter-bot — queued behind 1 review(s); expect to start in about 50 min. The box reviews one PR at a time.

@aiter-bot

Copy link
Copy Markdown

[ROCm/aiter] PR #6760 — [CK] [Bugfix] Preserve virtual sinks in single-query batch prefill

The single-query "causal≡non-causal" optimization in batch prefill no longer clears causal when a virtual attention sink (sink_ptr) is present, so those calls dispatch to the existing sink-enabled CK kernel instead of failing with "no matching kernel found".

Review (advisory): 🔴 HIGH RISK
Validation (deterministic): NOT RUN — auto-validation disabled (REVIEW_AUTO_VALIDATE=0) and no idle GPU (all 8 gfx942 cards at 98–99% VRAM); the target op_tests/test_batch_prefill.py exists and the aiter op-test CI collects it on gfx942 and gfx950
Perf (advisory): NOT RUN — auto-validation disabled and no idle GPU; the changed path (max_seqlen_q==1 with sink_ptr) raises "no matching kernel found" on base, so no base-vs-head pair exists for it, and every previously-working call keeps an identical dispatch by construction

🔴 [verified] aiter/ops/mha.py:1548 changes the default dispatch of the public mha_batch_prefill_func for single-query calls with a virtual sink — previously a loud TORCH_CHECK "no matching kernel found", now a causal+sink kernel launch — and the PR's own motivation says SGLang's AITER NHD attention path consumes this API, yet the PR carries no ci:* label, so every downstream job (ci:sglang/vllm/atom) is skipped and a semantic mismatch in the newly-routed kernel would surface only after merge. Author must add ci:all (the activation cannot be traced to one specific labeled model from this repo) and require it green before merge.

🔴 [verified] aiter/ops/mha.py is a stable downstream-facing dispatch contract whose behavior this diff changes (error → kernel launch for qlen==1+sink calls), but the de-facto owner by commit count — slippedJim, 23 commits on the file vs valarLip's 3 — has not been asked to sign off; the author's review request went to valarLip. Reviewer should ask slippedJim (top committer of the path) for explicit sign-off on the dispatch change before merge, or confirm valarLip owns this path.

⚠️ [verified] No latency number exists for the newly-routed path in aiter/ops/mha.py: the PR's own timing log says "seconds include JIT, not performance measurements", the perf stage could not run (auto-validation disabled; all 8 gfx942 cards at 98–99% VRAM), and the changed path has no base to compare against because it raised on base. Reviewer should ask for a single-query causal+sink latency check at a production shape (e.g. kv_len ≥ 4k with TP-split head counts) to confirm the serving path is not pathologically slow, since no baseline exists.

📝 [verified] Explicitly non-causal calls with sink_ptr still raise "no matching kernel found" (the CK batch_prefill codegen emits no no-mask+sink instance), and for non-causal + sink + window_size_left ≥ max_seqlen_k the Python side of aiter/ops/mha.py selects _mask+_sink blobs while the C++ entry decodes no-mask after its unconditional window normalization — a loud, pre-existing, author-documented out-of-scope failure. Reviewer should ask the author to confirm SGLang's virtual-sink calls are always causal, or to file the non-causal case as a follow-up.

@lunarwhite

Copy link
Copy Markdown
Contributor Author

Following the bot review, I checked the referenced SGLang call site and ran an additional warmed-up, long-KV latency check.

The SGLang batch-prefill call explicitly passes both causal=True and sink_ptr=sinks.

On MI355X, using BF16/page1, batch=1, qlen=1, KV=4096 and Q8/KV1, all four patched cases passed numerical checks. After 30 warm-up calls, median eager API timings over 9 groups of 100 calls were:

Head dim Window Base Patched
64 Full Dispatch error 190.185 µs
64 Local128 16.454 µs 16.606 µs
128 Full Dispatch error 189.876 µs
128 Local128 16.657 µs 17.035 µs

JIT compilation and input preparation are excluded. Head dim 64 matches GPT-OSS TP8 per-rank dimensions; this is a single-GPU operator check, not an end-to-end serving test.

The local-window controls showed small differences (+0.92% / +2.27%) in this run. The full-window cases have no working base baseline, so their absolute timings do not establish a performance regression or serving-level performance acceptance.

shay-li77
shay-li77 previously approved these changes Oct 26, 2026

@shay-li77 shay-li77 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.

Thanks for the fix! I verified it on MI355X (gfx950, ROCm 10.1, torch 2.14) and it looks correct. Approving; the comments below are non-blocking.

What I checked

I ran 15 cases through mha_batch_prefill_func on base (45d7ef3) and on this PR (21472a9), comparing against an fp32 reference. Inputs: random Q/K/V, batch=2 with different kv lengths (37 and 100), GQA 8:2, a different sink logit per head, shuffled page indices.

  • The target case (qlen=1, causal=True, sink_ptr) fails on base with no matching kernel found and passes with this PR. Max abs error vs fp32 is ≤ 0.004.
  • No regressions. Every other case gives the same result on base and on this PR. The only previously working call whose path changes is qlen=1 + sink_ptr + a finite left window. It now goes through the causal mask instead of the local mask, and it is still numerically correct.
  • The 5 new tests pass.

Pre-existing failures nearby (not introduced by this PR)

These fail with no matching kernel found on both base and this PR:

case base this PR
qlen=1, causal=False, window (-1,-1), sink_ptr ❌ ❌
qlen=4, causal=False, window (-1,-1), sink_ptr ❌ ❌
qlen=4, causal=False, window (4096,-1) / (-1,4096) / (4096,4096), max_seqlen_k=100, no sink ❌ ❌
qlen=1, causal=False, window (4096,-1), sink_ptr ❌ ❌

The first row is mathematically identical to the case this PR fixes, because qlen=1 causal is full attention, but it still fails. The error hint about page_size and the 2GB KV cache is also misleading here. Root causes and a suggested single-point fix are in the inline comments. Happy for this to land as a follow-up.

Comment thread aiter/ops/mha.py Resolved
Comment thread op_tests/test_batch_prefill.py Resolved
Comment thread op_tests/test_batch_prefill.py Resolved
Comment thread op_tests/test_batch_prefill.py Resolved
Use random Q/K/V, unequal request lengths, shuffled pages, and distinct
per-head sink logits with the existing BF16 tolerances. Run the cases
from the existing sink script entry point.

Signed-off-by: Yuedong Wu <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 27, 2026 01: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.

🔵 Needs a closer look

The five regression cases need GPU validation on the exact PR head to confirm Python/CK specialization agreement and numerical behavior, since the supplied MI355X results were not independently verified.

No open issues 🎉

Review effort: Balanced

@shay-li77
shay-li77 merged commit 836b336 into ROCm:main Oct 27, 2026
97 of 125 checks passed
@lunarwhite
lunarwhite deleted the lunarwhite/fix-causal-prefill-virtual-sink branch October 27, 2026 06:30
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.

5 participants