Repository navigation
[Build] [MHA] Stop flash_attn_varlen_func from dropping sequences with seqlen_q <= min_seqlen_q - #7473
[Build] [MHA] Stop flash_attn_varlen_func from dropping sequences with seqlen_q <= min_seqlen_q#7473AndreasKaratzas wants to merge 4 commits into
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
NOT 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 |
ChuanLi1101
left a comment
There was a problem hiding this comment.
LGTM — correct fix for the CK min_seqlen_q skip leaving short-seq out/lse uninitialized (#2229). High-level API forcing 0 while keeping low-level mha_varlen_fwd skip is the right split; regression test covers it.
Process APPROVE from me — please have TEAM_AITER / MHA owners clear for merge.
6802b3f to
326ef26
Compare
…Cm#7473 Signed-off-by: Stefan Koncarevic <[email protected]>
|
The CI issues are unrelated to this PR |
|
@Zzz9990 @fsx950223 could you confirm the min_seqlen_q change described under Breaking Changes is acceptable for the MHA varlen API? ChuanLi1101 asked for an MHA owner to clear it before merge. |
…_q <= min_seqlen_q With min_seqlen_q != 0 the CK varlen path builds the skip variant, and CK returns early for every sequence with seqlen_q <= min_seqlen_q, leaving its out/lse rows uninitialized. asm v3 and opus ignore the argument, so results depended on dtype and head size. Pass 0 from _flash_attn_varlen_forward so every backend computes every sequence, prebuild the matching _nskip bf16 module, and add a mixed-length regression test. Call the test from __main__, because CI runs this file as a script. Fixes #2229 Signed-off-by: Andreas Karatzas <[email protected]>
326ef26 to
ee54960
Compare
…eqlen_q_no_skip Co-authored-by: Claude <[email protected]> Signed-off-by: Andreas Karatzas <[email protected]>
|
@AndreasKaratzas Thanks, the change looks right to me. Forcing 0 in the high-level API fixes the silently uninitialized rows and makes CK behave like v3 and OPUS, and keeping the skip in the low-level I also checked the current callers: vLLM's One question on perf: fp16 d128 at 4 × 2048 is +4.4% (237.2 → 247.5 us). Is that run-to-run noise or a real cost of the @Zzz9990 @fsx950223 since you added |
|
it's real, not noise. re-ran it with 10 alternated runs: 4 x 2048 is +3.3% every run (+3.0 to +3.3), while two runs of the same |
|
@aiter-bot review |
|
aiter-bot — queued behind 2 review(s); expect to start in about 40 min. The box reviews one PR at a time. |
aiter-bot
left a comment
There was a problem hiding this comment.
ROCm/aiter PR #7473 — [Build] [MHA] Stop flash_attn_varlen_func from dropping sequences with seqlen_q <= min_seqlen_q
Review (advisory): 🔴 HIGH RISK
Validation (deterministic): NOT RUN — REVIEW_AUTO_VALIDATE=0 (auto-validation disabled; the single test target op_tests/test_mha_varlen.py exists — the PR reports the new test passing 16/16 on gfx942 and gfx950 and failing 12/16 on main, unverified here)
Perf (advisory): NOT RUN — REVIEW_AUTO_VALIDATE=0 disabled the base-vs-head auto-run and all 8 GPUs hold 95–96% VRAM from other processes; the PR's own MI350X medians (+3.3% on fp16 d128 4×2048, −55/−82% on short-seq shapes) are unverified here
Size: 60 lines / 3 files
Design (advisory): the high-level API forces min_seqlen_q=0 while the low-level mha_varlen_fwd keeps CK's skip; the prebuild list follows to the four bf16 _nskip modules
Human: recommend Jim Guo review whether redefining min_seqlen_q as ignored on the stable flash_attn_varlen_func contract is right for vLLM/ATOM/SGLang callers, and whether the PR-reported +3.3% fp16 d128 long-seq cost (whose recovery belongs in CK tile dispatch, not here) is acceptable to merge
🔴 [verified] aiter/ops/mha.py changes the behavior of the stable, downstream-consumed flash_attn_varlen_func kwarg min_seqlen_q — a caller passing 1 (ATOM's vLLM plugin does, on prefill/extend) goes from silently dropped rows to computed rows on every CK-served call (fp16 any head dim, bf16 d64, bf16 with sink/SWA) — but the PR's only approval is dismissed and the 2026-11-20 ping to fsx950223/Zzz9990, who added the parameter in #470, is unanswered, so this contract change has no owner sign-off. Reviewer must obtain explicit sign-off from the de-facto aiter/ops/mha.py owner and the parameter's authors before merge, on top of the ci:all label already on the PR.
2 finding(s) posted inline, on the lines they are about:
aiter/ops/mha.py:4365— now documents min_seqlen_q as "Unusedsetup.py:354-356— drops the three bf16 _skip modules from the PREBUILD_KERNELS wheel list, so a wheel install no longer ships…
| max_seqlen_q: int. Maximum query sequence length in the batch. | ||
| max_seqlen_k: int. Maximum key sequence length in the batch. | ||
| min_seqlen_q: int. Minimum query sequence length for chunked prefill. | ||
| min_seqlen_q: int. Unused; kept for API compatibility. Every sequence is |
There was a problem hiding this comment.
📝 [verified] aiter/ops/mha.py:4365 now documents min_seqlen_q as "Unused; kept for API compatibility", but the same docstring's provider contract at aiter/ops/mha.py:4400 still says "how_v3_bf16_cvt and min_seqlen_q tune AITER's kernels and may be ignored" — a provider author reading the contract still believes the kwarg is a live tuning knob. Author must update the provider-contract sentence to match the new semantics.
There was a problem hiding this comment.
yeah, good catch. the contract sentence now names only how_v3_bf16_cvt as a tuning kwarg and says min_seqlen_q is unused.
| "mha_varlen_fwd_bf16_nlogits_nbias_mask_lse_ndropout_nskip_nqscale", | ||
| "mha_varlen_fwd_bf16_nlogits_nbias_nmask_nlse_ndropout_nskip_nqscale", | ||
| ] |
There was a problem hiding this comment.
📝 [verified] setup.py:354-356 drops the three bf16 _skip modules from the PREBUILD_KERNELS wheel list, so a wheel install no longer ships them and the first low-level aiter.mha_varlen_fwd(min_seqlen_q>0) call JIT-compiles at runtime instead of loading a prebuilt module; no such caller exists in aiter, vLLM, ATOM or SGLang (PR audit, confirmed by reviewer), and fp16 variants were never prebuilt, so JIT is the pre-existing norm. Reviewer should ask whether any external consumer of the low-level op relies on the prebuilt _skip modules before merge.
|
@rocking5566 could you help take a look at this one? It changes which CK varlen forward modules the high-level API uses. @Zzz9990 and @fsx950223, who added A summary:
Does this look reasonable to you from the CK side, including the change in meaning and the long-sequence cost? Thanks! |
The contract sentence still named min_seqlen_q as a tuning kwarg that may be ignored. Only how_v3_bf16_cvt is one now. Co-authored-by: Claude <[email protected]> Signed-off-by: Andreas Karatzas <[email protected]>
op_tests/test_mha_varlen.py: this branch's min_seqlen_q test and main's gfx950 hd256 THD test were added at the same spot; keep both. aiter/ops/mha.py: drop the min_seqlen_q == 0 condition main added to can_impl_fmha_v4_thd (and its docstring mention). It existed because CK skipped short sequences; this branch makes min_seqlen_q unused, so it no longer changes which kernel runs. Co-authored-by: Claude <[email protected]> Signed-off-by: Andreas Karatzas <[email protected]>
Summary
Fixes #2229.
flash_attn_varlen_funcnow computes every sequence on every backend, whatevermin_seqlen_qis. Before this change, any sequence withseqlen_q <= min_seqlen_qwas skipped on the CK path, and itsout/lserows were left uninitialized.Motivation
The FP16 decode error in #2229 isn't a numerics problem. Those rows were never written. With
min_seqlen_q != 0, the CK varlen path builds the_skipvariant, and CK returns early for every sequence withseqlen_q <= min_seqlen_q(added in ROCm/composable_kernel#2221 so vLLM's old backend could hand decodes to paged attention). The output then keeps whatevertorch.emptyleft there.The asm v3 and opus kernels ignore the argument. So the same call is right when it lands on v3 (bf16 hd128/192, or hd256 on gfx950 MHA, without sink/SWA/softcap) and wrong on CK: fp16 at any head size, bf16 hd64, and bf16 with sinks or a sliding window.
Every current caller passes the argument as a lower bound, which is how the name and docstring read (see also #1368). vLLM #25763 lost Qwen3-32B accuracy by passing the true minimum, and #28670 hard-coded 1. With 1, vLLM still drops a 1-token prompt that is batched with a longer prefill (verified at kernel level with vLLM's own batch split). ATOM passes 0 on its prefill paths and
min(extend_lens)in the Kimi bridge. I found no caller that relies on the skip.cc @Zzz9990 @fsx950223, since this changes the meaning of the parameter you added.
Changes
_flash_attn_varlen_forwardpassesmin_seqlen_q=0to the CK and asm v3 varlen kernels, and the gfx950 hd256 mha_v4 THD route ([MHA] Route flash_attn_varlen_func gfx950 hd256 BF16 THD forward to mha_v4 #8704) no longer requiresmin_seqlen_q == 0. This coversflash_attn_varlen_func, its autograd path andflash_attn_varlen_fp8_pertensor_func. The low-levelmha_varlen_fwdop keeps CK's skip.flash_attn_varlen_funcdocstring saysmin_seqlen_qis unused.setup.pyprebuilds the four bf16_nskipmask/lse modules this path now uses, in place of the three_skipones. That coversmask_lse_nskip, which vLLM's causal extend call needs and which was not prebuilt.test_mha_varlen.pyadds a mixed-length regression test.__main__calls it, because CI runs this file as a script and not through pytest.Performance (if applicable)
The
_nskipmodules can use CK'sqr_async_trloadinstances on gfx950 (codegen only emits them for skip=f). Numbers are from CKmha_varlen_fwd, causal, H=32/Hkv=8,min_seqlen_q=1before (_skip) vs 0 after (_nskip). Both runs are interleaved in one process. Each value is the median of 10 processes, and each process is the median of 15×200 iterations. The range is the min and max of the per-process change. As a noise check, the same_skipcall was run a second time in the same loop. d64 and the asm v3 path are unaffected. The d128 rows apply to fp16, and to bf16 calls that go to CK (sink/SWA).The cost on long sequences is real. With
_nskip, CK selects theqr_async_trload128×64 tile whenmax_seqlen_q > 32. With_skip, it selects theqr_async128×128 tile.rocprofv3shows the same split. Callers that passmin_seqlen_q=0already get the_nskipkernel today, so this PR adds no new kernel. To get this cost back, change the tile selection in CK. Short sequences become much faster, because_nskiphas the 16×32 and 32×32 tiles formax_seqlen_q <= 32.Testing
Documentation
Dependencies
Breaking Changes
flash_attn_varlen_func(min_seqlen_q=N)no longer skips sequences withseqlen_q <= Non the CK path. A caller that relied on the skip to leave rows of a sharedoutbuffer untouched will now get those rows computed and overwritten. No such caller was found in aiter, ATOM or vLLM. The low-levelaiter.mha_varlen_fwdop still skips.PR Self-Review
Reviewed revision
27bb920
Behavior and intent
The high-level varlen API stops forwarding
min_seqlen_qto the kernels. Sequences no longer silently disappear when the call lands on CK (fp16, bf16 hd64, bf16 with sink or sliding window), and every backend now gives the same result for the same call.Validation and unverified scope
On MI350X (gfx950), ROCm 7.15, torch 2.13:
pytest op_tests/test_mha_varlen.py::test_flash_attn_varlen_func_min_seqlen_q_computes_short_seqs: 16 passed in each of 10 consecutive runs. On main, 12 failed; the 4 that pass are bf16 d128, which runs on asm v3.flash_attn_varlen_funcincorrect results for FP16 + q_len=1 (decode) on gfx942 #2229 repro matches an fp32 reference for fp16/bf16 at hd 64/128/256 and q_len 1/2/8; before this change fp16 gave inf/nan.tests/kernels/attention/test_rocm_aiter_fa.pyagainst this branch: 94 passed, and its FP16 single-token decode xfail now passes (max abs 1.8e-4).ruff check(0.16.0) andblack --checkpass.Rebased again on 2026-11-20 onto main
4ba20eb, which includes #7984 (thetest_mha_v4_sparse.pygfx942 abort that failed the Aiter Test Gate here). No conflicts;git range-diffshows the commit unchanged. On MI355X (gfx950) atee54960, the same pytest command: 16 passed (one run, clean JIT cache). The MI300X results below are from before this rebase.On MI300X (gfx942), ROCm 7.15, torch 2.13, after the rebase onto
95d2112:326ef26. On95d2112without this change, 12 failed. The 4 that pass are bf16 d128, which runs on asm v3. This is the same split as on MI350X.python3 op_tests/test_mha_varlen.py(the CI command, which now also runs the new test): exit 0, in 533 s.tests/kernels/attention/test_rocm_aiter_fa.pyagainst this branch: 97 passed.The CI wheel build
PREBUILD_KERNELS=1,GPU_ARCHS=gfx942;gfx950(job 126786209948, on6802b3f) builds the four bf16_nskipmodules and no_skipmodule.Not tested: end-to-end model accuracy.
ci:allcovers it. The MI300X CI shards did not run on6802b3f, because the label came after the first run. They run on this push.Merged main
f90c562on 2026-12-03. The only conflict wasop_tests/test_mha_varlen.py, where this test and the hd256 THD test from #8704 were added at the same spot; both are kept. #8704 also kept THD calls off mha_v4 whenmin_seqlen_q != 0, because CK used to skip short sequences. That check is gone now that the argument is unused, somin_seqlen_qno longer changes which kernel runs. On MI350X, a THD-eligible hd256 bf16 batch with 1- and 2-token documents (MHA and GQA, causal or not) runsfmha_fwd_hd256_bf16_varlenformin_seqlen_q0, 1 and 2, with bitwise-equalout/lseand no NaN.test_flash_attn_varlen_func_min_seqlen_q_computes_short_seqsandtest_flash_attn_varlen_func_thd_hd256: 21 passed in each of 10 runs.python op_tests/test_mha_varlen.py: exit 0.PR-specific question
setup.pyonly changes which bf16 varlen modules are prebuilt. Three_skipnames become the_nskiptwins, so the list goes from six modules to four and no dependency changes. On a clean cache,mha_varlen_fwd_bf16_nlogits_nbias_mask_lse_ndropout_nskip_nqscaleand the fp16_nskipmodules were JIT-built throughget_mha_varlen_prebuild_variants_by_names, the same recipe the prebuild uses. The existing-cache path is the second and later run of every test above. The CI wheel build withPREBUILD_KERNELS=1andGPU_ARCHS=gfx942;gfx950(job 126786209948) prebuilt all four_nskipmodules, which includemask_lse_nskip, and no_skipmodule.Changed op tests
op_tests/test_mha_varlen.py: addstest_flash_attn_varlen_func_min_seqlen_q_computes_short_seqs. It runs a batch with q lens 1/2/8/1 into a NaN-filledoutwithmin_seqlen_q1 and 2, and requiresoutandlseto equal themin_seqlen_q=0call. On main it fails on every CK case. The existing varlen tests only parametrizemin_seqlen_q=0, so nothing covered the skip.__main__runs all 16 cases, so the CI script run covers the test.