Repository navigation
Mha v4: kv_splits="auto" from a wave-rounding cost model - #7942
Conversation
…a wave Grids between one and a few waves end on a nearly idle round. MI355X BF16, 50 DiT per-rank shapes, CUDA-graph replay: regret vs the best count mean 0.1% / max 1.1% (was 4.6% / 33.2%); Qwen-Image-Edit 2048 U8 824 -> 622 us.
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
…s it min() with a key function is not traceable by Dynamo under fullgraph. The compile test now uses 8192 keys, where the cost model splits (3), so it exercises that branch.
…equence Under torch.compile with a dynamic sequence, auto's cost model ran on symbolic sizes: divmod on a SymInt broke the graph (Dynamo fell back to eager for the caller), and the fold and range lengths guarded on the key length, recompiling per length. On Qwen-Image-Edit 2048 (CFG passes of different lengths) that made auto 18% slower end to end than a fixed count. Under compile, the pick, fold, launch and merge now run inside one functional custom op (aiter::mha_v4_split_kv), where sizes are concrete; auto resolves to "pick at launch". Eager calls run the same body directly. The op returns its output rather than writing the caller's buffer: the mutating form failed in Inductor's scheduler on that graph. Edit 2048 on 8x MI355X: no graph breaks; a new test compiles mha_v4 once with dynamic=True over lengths whose picks differ and matches eager.
The cost model's merge term was fit on BF16 at batch 1. The INT8/FP8 and FP8/FP8 rows walk a key faster, so the same merge is worth more of their keys, and past batch 1 every split also copies the queries. Scale the term 1.5x for 8-bit Q/K and 2x past batch 1. 27 shapes per recipe on one GPU under graph replay, mean regret of the picked count (shapes at least 5% off the best): MI355X: FP8/FP8 1.65% (3) -> 0.17% (0), INT8/FP8 1.17% (2) -> 0.39% (0), BF16/FP8 0.74% (2) -> 0.21% (0), BF16 0.62% (0) -> 0.52% (0). MI350X: FP8/FP8 2.00% (4) -> 0.10% (0), INT8/FP8 1.32% (2) -> 0.21% (0), BF16/FP8 1.15% (2) -> 0.37% (0), BF16 0.71% (2) -> 0.27% (0).
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 |
|
Nit: No test covers a caller-provided out= under compile, which is the out.copy_() branch. Adding out= (and maybe FP8) to Nit: 8-bit merge factor. q_format != AttentionFormat.BF16 gives the 1.5x factor to any non-BF16 Q. Only INT8 and FP8 were measured, and the public mha_v4_kv_splits() accepts any format. |
…ut under compile The 1.5x merge factor was measured on INT8 and FP8 Q/K only; other formats now keep BF16's merge instead of inheriting it. mha_v4 itself only splits BF16 and those 8-bit recipes, so its picks are unchanged. The dynamic-sequence compile check now also runs FP8 and passes out=, which takes the copy from the compiled split.
|
@JohnNikolay84 thanks, both taken in ce8ba24:
Both changed checks pass on MI355X (3 cases). I haven't rerun the full suites at this head. |
The FP8 case added in ce8ba24 compiled more than once under torch 2.10 in CI (it passed on 2.13). The BF16 case, which passes a caller's out= through the compiled split, stays.
|
Follow-up: CI's torch 2.10 compiled the new FP8 case of the dynamic-sequence check more than once (it passed on 2.13, where I'd run it), so f63d42a keeps that check on BF16. The |
…-KV (#962) * perf(aiter): compute FAv3 rows past the last full wave split-K The AITER FAv3 hd128 bf16 forward on gfx942/gfx950 gives each CU one 256-row query tile. Qwen-Image-Edit at Ulysses 8 launches 258 tiles on the 256-CU MI355X, so a second wave of 2 tiles costs as much as the first. Run the rows past the last full wave against K slices as varlen segments over all CUs and merge them by log-sum-exp. * perf(qwen): default Qwen-Image(-Edit) to AITER MHA v4 BF16 with split-KV Where AITER's mha_v4 takes kv_splits (ROCm/aiter#6560) on gfx950, Qwen-Image and Qwen-Image-Edit default to AITER_BF16. Edit uses a fixed 4-way split, because the joint edit sequence leaves too few query tiles for the split auto picks; Qwen-Image uses auto. An explicit --attention_backend or --mha_v4_kv_splits keeps precedence. Replaces the FAv3 last-wave split from the previous commit. * fix(config): keep an explicit --mha_v4_kv_splits 1 over the model default --mha_v4_kv_splits defaulted to 1, so the runner could not tell an explicit 1 from an omitted flag and replaced it with the model's split count. The flag now defaults to unset; the model default fills it in, and the runtime config resolves what is still unset to 1. * fix(qwen): only default to split-KV MHA v4 on gfx950 AITER exports the MHA v4 LSE, and so accepts kv_splits > 1, only on gfx950; on gfx942 AITER_BF16 is available but an explicit split count raises. Gate the Qwen default on gfx950 so MI300-series GPUs keep the global default. The test now drives the real arch check and patches only Qwen's split-KV gate. * perf(qwen): default Qwen-Image-Edit to MHA v4 kv_splits auto Needs ROCm/aiter#7942: its auto picks Edit's split from a wave-rounding cost model and runs the split in one op, so torch.compile neither breaks the graph nor recompiles across Edit's two CFG sequence lengths. With it, auto matches the fixed 4 on Edit 2048 (8x MI355X: 11.07 vs 11.10 s); without it, auto leaves Edit unsplit. * fix(mha-v4): regroup batched packed keys without a graph break A batch of several packed sequences is scattered back into a padded [batch, max_k] buffer before the MHA v4 launch. The row and slot of each packed key came from repeat_interleave over the lengths, whose output shape depends on their values, so torch.compile split the graph at this call in every transformer block. Search the cumulative boundaries instead: the output has the shape of the packed rows and the indices are the same, empty sequences included. Qwen-Image-Edit with batched true CFG on AITER_BF16 (8x MI355X, 2048x2048, measured on top of #962, where that backend is the default): 104 graph breaks and 41 recompiles go to 0, and a step batch goes from 12.14 s to 9.88 s.
…#966) * perf(aiter): compute FAv3 rows past the last full wave split-K The AITER FAv3 hd128 bf16 forward on gfx942/gfx950 gives each CU one 256-row query tile. Qwen-Image-Edit at Ulysses 8 launches 258 tiles on the 256-CU MI355X, so a second wave of 2 tiles costs as much as the first. Run the rows past the last full wave against K slices as varlen segments over all CUs and merge them by log-sum-exp. * perf(qwen): default Qwen-Image(-Edit) to AITER MHA v4 BF16 with split-KV Where AITER's mha_v4 takes kv_splits (ROCm/aiter#6560) on gfx950, Qwen-Image and Qwen-Image-Edit default to AITER_BF16. Edit uses a fixed 4-way split, because the joint edit sequence leaves too few query tiles for the split auto picks; Qwen-Image uses auto. An explicit --attention_backend or --mha_v4_kv_splits keeps precedence. Replaces the FAv3 last-wave split from the previous commit. * fix(config): keep an explicit --mha_v4_kv_splits 1 over the model default --mha_v4_kv_splits defaulted to 1, so the runner could not tell an explicit 1 from an omitted flag and replaced it with the model's split count. The flag now defaults to unset; the model default fills it in, and the runtime config resolves what is still unset to 1. * fix(qwen): only default to split-KV MHA v4 on gfx950 AITER exports the MHA v4 LSE, and so accepts kv_splits > 1, only on gfx950; on gfx942 AITER_BF16 is available but an explicit split count raises. Gate the Qwen default on gfx950 so MI300-series GPUs keep the global default. The test now drives the real arch check and patches only Qwen's split-KV gate. * perf(qwen): default Qwen-Image-Edit to MHA v4 kv_splits auto Needs ROCm/aiter#7942: its auto picks Edit's split from a wave-rounding cost model and runs the split in one op, so torch.compile neither breaks the graph nor recompiles across Edit's two CFG sequence lengths. With it, auto matches the fixed 4 on Edit 2048 (8x MI355X: 11.07 vs 11.10 s); without it, auto leaves Edit unsplit. * perf(flux): default FLUX.1-Kontext to AITER MHA v4 BF16 with split-KV Where AITER's mha_v4 takes kv_splits (ROCm/aiter#6560) on gfx950, FLUX.1-Kontext-dev defaults to AITER_BF16 with kv_splits auto. At 1024x1024 with Ulysses 8 each rank attends 3 heads over ~8.7k tokens, too few query tiles to fill the GPU, which the split fills. An explicit --attention_backend or --mha_v4_kv_splits keeps precedence. prefer_mha_v4 moves from the Qwen runner to base_model so both use it.
) * perf(aiter): compute FAv3 rows past the last full wave split-K The AITER FAv3 hd128 bf16 forward on gfx942/gfx950 gives each CU one 256-row query tile. Qwen-Image-Edit at Ulysses 8 launches 258 tiles on the 256-CU MI355X, so a second wave of 2 tiles costs as much as the first. Run the rows past the last full wave against K slices as varlen segments over all CUs and merge them by log-sum-exp. * perf(qwen): default Qwen-Image(-Edit) to AITER MHA v4 BF16 with split-KV Where AITER's mha_v4 takes kv_splits (ROCm/aiter#6560) on gfx950, Qwen-Image and Qwen-Image-Edit default to AITER_BF16. Edit uses a fixed 4-way split, because the joint edit sequence leaves too few query tiles for the split auto picks; Qwen-Image uses auto. An explicit --attention_backend or --mha_v4_kv_splits keeps precedence. Replaces the FAv3 last-wave split from the previous commit. * fix(config): keep an explicit --mha_v4_kv_splits 1 over the model default --mha_v4_kv_splits defaulted to 1, so the runner could not tell an explicit 1 from an omitted flag and replaced it with the model's split count. The flag now defaults to unset; the model default fills it in, and the runtime config resolves what is still unset to 1. * fix(qwen): only default to split-KV MHA v4 on gfx950 AITER exports the MHA v4 LSE, and so accepts kv_splits > 1, only on gfx950; on gfx942 AITER_BF16 is available but an explicit split count raises. Gate the Qwen default on gfx950 so MI300-series GPUs keep the global default. The test now drives the real arch check and patches only Qwen's split-KV gate. * perf(qwen): default Qwen-Image-Edit to MHA v4 kv_splits auto Needs ROCm/aiter#7942: its auto picks Edit's split from a wave-rounding cost model and runs the split in one op, so torch.compile neither breaks the graph nor recompiles across Edit's two CFG sequence lengths. With it, auto matches the fixed 4 on Edit 2048 (8x MI355X: 11.07 vs 11.10 s); without it, auto leaves Edit unsplit. * perf(flux): default FLUX.1-Kontext to AITER MHA v4 BF16 with split-KV Where AITER's mha_v4 takes kv_splits (ROCm/aiter#6560) on gfx950, FLUX.1-Kontext-dev defaults to AITER_BF16 with kv_splits auto. At 1024x1024 with Ulysses 8 each rank attends 3 heads over ~8.7k tokens, too few query tiles to fill the GPU, which the split fills. An explicit --attention_backend or --mha_v4_kv_splits keeps precedence. prefer_mha_v4 moves from the Qwen runner to base_model so both use it. * perf(flux2): default FLUX.2-dev to AITER MHA v4 BF16 with split-KV Where AITER's mha_v4 takes kv_splits on gfx950, FLUX.2-dev defaults to AITER_BF16 with kv_splits auto, except under PipeFusion, whose pipeline was not measured. An explicit --attention_backend or --mha_v4_kv_splits keeps precedence.
…KV (#968) * perf(aiter): compute FAv3 rows past the last full wave split-K The AITER FAv3 hd128 bf16 forward on gfx942/gfx950 gives each CU one 256-row query tile. Qwen-Image-Edit at Ulysses 8 launches 258 tiles on the 256-CU MI355X, so a second wave of 2 tiles costs as much as the first. Run the rows past the last full wave against K slices as varlen segments over all CUs and merge them by log-sum-exp. * perf(qwen): default Qwen-Image(-Edit) to AITER MHA v4 BF16 with split-KV Where AITER's mha_v4 takes kv_splits (ROCm/aiter#6560) on gfx950, Qwen-Image and Qwen-Image-Edit default to AITER_BF16. Edit uses a fixed 4-way split, because the joint edit sequence leaves too few query tiles for the split auto picks; Qwen-Image uses auto. An explicit --attention_backend or --mha_v4_kv_splits keeps precedence. Replaces the FAv3 last-wave split from the previous commit. * fix(config): keep an explicit --mha_v4_kv_splits 1 over the model default --mha_v4_kv_splits defaulted to 1, so the runner could not tell an explicit 1 from an omitted flag and replaced it with the model's split count. The flag now defaults to unset; the model default fills it in, and the runtime config resolves what is still unset to 1. * fix(qwen): only default to split-KV MHA v4 on gfx950 AITER exports the MHA v4 LSE, and so accepts kv_splits > 1, only on gfx950; on gfx942 AITER_BF16 is available but an explicit split count raises. Gate the Qwen default on gfx950 so MI300-series GPUs keep the global default. The test now drives the real arch check and patches only Qwen's split-KV gate. * perf(qwen): default Qwen-Image-Edit to MHA v4 kv_splits auto Needs ROCm/aiter#7942: its auto picks Edit's split from a wave-rounding cost model and runs the split in one op, so torch.compile neither breaks the graph nor recompiles across Edit's two CFG sequence lengths. With it, auto matches the fixed 4 on Edit 2048 (8x MI355X: 11.07 vs 11.10 s); without it, auto leaves Edit unsplit. * perf(flux): default FLUX.1-Kontext to AITER MHA v4 BF16 with split-KV Where AITER's mha_v4 takes kv_splits (ROCm/aiter#6560) on gfx950, FLUX.1-Kontext-dev defaults to AITER_BF16 with kv_splits auto. At 1024x1024 with Ulysses 8 each rank attends 3 heads over ~8.7k tokens, too few query tiles to fill the GPU, which the split fills. An explicit --attention_backend or --mha_v4_kv_splits keeps precedence. prefer_mha_v4 moves from the Qwen runner to base_model so both use it. * perf(minimax-h3): default MiniMax-H3 to AITER MHA v4 BF16 with split-KV Where AITER's mha_v4 takes kv_splits on gfx950, the base MiniMax-H3 runner defaults to AITER_BF16 with kv_splits auto. Its subclasses (FastH3, Turbo, VDN, Ref2VA) keep their own defaults since they were not measured, and so does --use_hybrid_attn_schedule, which refuses a set --attention_backend. An explicit --attention_backend or --mha_v4_kv_splits keeps precedence.
* perf(aiter): compute FAv3 rows past the last full wave split-K The AITER FAv3 hd128 bf16 forward on gfx942/gfx950 gives each CU one 256-row query tile. Qwen-Image-Edit at Ulysses 8 launches 258 tiles on the 256-CU MI355X, so a second wave of 2 tiles costs as much as the first. Run the rows past the last full wave against K slices as varlen segments over all CUs and merge them by log-sum-exp. * perf(qwen): default Qwen-Image(-Edit) to AITER MHA v4 BF16 with split-KV Where AITER's mha_v4 takes kv_splits (ROCm/aiter#6560) on gfx950, Qwen-Image and Qwen-Image-Edit default to AITER_BF16. Edit uses a fixed 4-way split, because the joint edit sequence leaves too few query tiles for the split auto picks; Qwen-Image uses auto. An explicit --attention_backend or --mha_v4_kv_splits keeps precedence. Replaces the FAv3 last-wave split from the previous commit. * fix(config): keep an explicit --mha_v4_kv_splits 1 over the model default --mha_v4_kv_splits defaulted to 1, so the runner could not tell an explicit 1 from an omitted flag and replaced it with the model's split count. The flag now defaults to unset; the model default fills it in, and the runtime config resolves what is still unset to 1. * fix(qwen): only default to split-KV MHA v4 on gfx950 AITER exports the MHA v4 LSE, and so accepts kv_splits > 1, only on gfx950; on gfx942 AITER_BF16 is available but an explicit split count raises. Gate the Qwen default on gfx950 so MI300-series GPUs keep the global default. The test now drives the real arch check and patches only Qwen's split-KV gate. * perf(qwen): default Qwen-Image-Edit to MHA v4 kv_splits auto Needs ROCm/aiter#7942: its auto picks Edit's split from a wave-rounding cost model and runs the split in one op, so torch.compile neither breaks the graph nor recompiles across Edit's two CFG sequence lengths. With it, auto matches the fixed 4 on Edit 2048 (8x MI355X: 11.07 vs 11.10 s); without it, auto leaves Edit unsplit. * perf(flux): default FLUX.1-Kontext to AITER MHA v4 BF16 with split-KV Where AITER's mha_v4 takes kv_splits (ROCm/aiter#6560) on gfx950, FLUX.1-Kontext-dev defaults to AITER_BF16 with kv_splits auto. At 1024x1024 with Ulysses 8 each rank attends 3 heads over ~8.7k tokens, too few query tiles to fill the GPU, which the split fills. An explicit --attention_backend or --mha_v4_kv_splits keeps precedence. prefer_mha_v4 moves from the Qwen runner to base_model so both use it. * perf(flux): default FLUX.1-dev to AITER MHA v4 BF16 with split-KV Where AITER's mha_v4 takes kv_splits on gfx950, FLUX.1-dev defaults to AITER_BF16 with kv_splits auto, except under PipeFusion, which runs FLUX.1 in FP16. An explicit --attention_backend or --mha_v4_kv_splits keeps precedence.
…it-KV (#972) * perf(aiter): compute FAv3 rows past the last full wave split-K The AITER FAv3 hd128 bf16 forward on gfx942/gfx950 gives each CU one 256-row query tile. Qwen-Image-Edit at Ulysses 8 launches 258 tiles on the 256-CU MI355X, so a second wave of 2 tiles costs as much as the first. Run the rows past the last full wave against K slices as varlen segments over all CUs and merge them by log-sum-exp. * perf(qwen): default Qwen-Image(-Edit) to AITER MHA v4 BF16 with split-KV Where AITER's mha_v4 takes kv_splits (ROCm/aiter#6560) on gfx950, Qwen-Image and Qwen-Image-Edit default to AITER_BF16. Edit uses a fixed 4-way split, because the joint edit sequence leaves too few query tiles for the split auto picks; Qwen-Image uses auto. An explicit --attention_backend or --mha_v4_kv_splits keeps precedence. Replaces the FAv3 last-wave split from the previous commit. * fix(config): keep an explicit --mha_v4_kv_splits 1 over the model default --mha_v4_kv_splits defaulted to 1, so the runner could not tell an explicit 1 from an omitted flag and replaced it with the model's split count. The flag now defaults to unset; the model default fills it in, and the runtime config resolves what is still unset to 1. * fix(qwen): only default to split-KV MHA v4 on gfx950 AITER exports the MHA v4 LSE, and so accepts kv_splits > 1, only on gfx950; on gfx942 AITER_BF16 is available but an explicit split count raises. Gate the Qwen default on gfx950 so MI300-series GPUs keep the global default. The test now drives the real arch check and patches only Qwen's split-KV gate. * perf(qwen): default Qwen-Image-Edit to MHA v4 kv_splits auto Needs ROCm/aiter#7942: its auto picks Edit's split from a wave-rounding cost model and runs the split in one op, so torch.compile neither breaks the graph nor recompiles across Edit's two CFG sequence lengths. With it, auto matches the fixed 4 on Edit 2048 (8x MI355X: 11.07 vs 11.10 s); without it, auto leaves Edit unsplit. * perf(flux): default FLUX.1-Kontext to AITER MHA v4 BF16 with split-KV Where AITER's mha_v4 takes kv_splits (ROCm/aiter#6560) on gfx950, FLUX.1-Kontext-dev defaults to AITER_BF16 with kv_splits auto. At 1024x1024 with Ulysses 8 each rank attends 3 heads over ~8.7k tokens, too few query tiles to fill the GPU, which the split fills. An explicit --attention_backend or --mha_v4_kv_splits keeps precedence. prefer_mha_v4 moves from the Qwen runner to base_model so both use it. * perf(flux2-klein): default FLUX.2-klein to AITER MHA v4 BF16 with split-KV Where AITER's mha_v4 takes kv_splits on gfx950, FLUX.2-klein-9B and -4B default to AITER_BF16 with kv_splits auto, except under PipeFusion, which runs them in FP16. An explicit --attention_backend or --mha_v4_kv_splits keeps precedence.
* perf(aiter): compute FAv3 rows past the last full wave split-K The AITER FAv3 hd128 bf16 forward on gfx942/gfx950 gives each CU one 256-row query tile. Qwen-Image-Edit at Ulysses 8 launches 258 tiles on the 256-CU MI355X, so a second wave of 2 tiles costs as much as the first. Run the rows past the last full wave against K slices as varlen segments over all CUs and merge them by log-sum-exp. * perf(qwen): default Qwen-Image(-Edit) to AITER MHA v4 BF16 with split-KV Where AITER's mha_v4 takes kv_splits (ROCm/aiter#6560) on gfx950, Qwen-Image and Qwen-Image-Edit default to AITER_BF16. Edit uses a fixed 4-way split, because the joint edit sequence leaves too few query tiles for the split auto picks; Qwen-Image uses auto. An explicit --attention_backend or --mha_v4_kv_splits keeps precedence. Replaces the FAv3 last-wave split from the previous commit. * fix(config): keep an explicit --mha_v4_kv_splits 1 over the model default --mha_v4_kv_splits defaulted to 1, so the runner could not tell an explicit 1 from an omitted flag and replaced it with the model's split count. The flag now defaults to unset; the model default fills it in, and the runtime config resolves what is still unset to 1. * fix(qwen): only default to split-KV MHA v4 on gfx950 AITER exports the MHA v4 LSE, and so accepts kv_splits > 1, only on gfx950; on gfx942 AITER_BF16 is available but an explicit split count raises. Gate the Qwen default on gfx950 so MI300-series GPUs keep the global default. The test now drives the real arch check and patches only Qwen's split-KV gate. * perf(qwen): default Qwen-Image-Edit to MHA v4 kv_splits auto Needs ROCm/aiter#7942: its auto picks Edit's split from a wave-rounding cost model and runs the split in one op, so torch.compile neither breaks the graph nor recompiles across Edit's two CFG sequence lengths. With it, auto matches the fixed 4 on Edit 2048 (8x MI355X: 11.07 vs 11.10 s); without it, auto leaves Edit unsplit. * perf(flux): default FLUX.1-Kontext to AITER MHA v4 BF16 with split-KV Where AITER's mha_v4 takes kv_splits (ROCm/aiter#6560) on gfx950, FLUX.1-Kontext-dev defaults to AITER_BF16 with kv_splits auto. At 1024x1024 with Ulysses 8 each rank attends 3 heads over ~8.7k tokens, too few query tiles to fill the GPU, which the split fills. An explicit --attention_backend or --mha_v4_kv_splits keeps precedence. prefer_mha_v4 moves from the Qwen runner to base_model so both use it. * perf(z-image): default Z-Image to AITER MHA v4 BF16 with split-KV
…#977) * perf(aiter): compute FAv3 rows past the last full wave split-K The AITER FAv3 hd128 bf16 forward on gfx942/gfx950 gives each CU one 256-row query tile. Qwen-Image-Edit at Ulysses 8 launches 258 tiles on the 256-CU MI355X, so a second wave of 2 tiles costs as much as the first. Run the rows past the last full wave against K slices as varlen segments over all CUs and merge them by log-sum-exp. * perf(qwen): default Qwen-Image(-Edit) to AITER MHA v4 BF16 with split-KV Where AITER's mha_v4 takes kv_splits (ROCm/aiter#6560) on gfx950, Qwen-Image and Qwen-Image-Edit default to AITER_BF16. Edit uses a fixed 4-way split, because the joint edit sequence leaves too few query tiles for the split auto picks; Qwen-Image uses auto. An explicit --attention_backend or --mha_v4_kv_splits keeps precedence. Replaces the FAv3 last-wave split from the previous commit. * fix(config): keep an explicit --mha_v4_kv_splits 1 over the model default --mha_v4_kv_splits defaulted to 1, so the runner could not tell an explicit 1 from an omitted flag and replaced it with the model's split count. The flag now defaults to unset; the model default fills it in, and the runtime config resolves what is still unset to 1. * fix(qwen): only default to split-KV MHA v4 on gfx950 AITER exports the MHA v4 LSE, and so accepts kv_splits > 1, only on gfx950; on gfx942 AITER_BF16 is available but an explicit split count raises. Gate the Qwen default on gfx950 so MI300-series GPUs keep the global default. The test now drives the real arch check and patches only Qwen's split-KV gate. * perf(qwen): default Qwen-Image-Edit to MHA v4 kv_splits auto Needs ROCm/aiter#7942: its auto picks Edit's split from a wave-rounding cost model and runs the split in one op, so torch.compile neither breaks the graph nor recompiles across Edit's two CFG sequence lengths. With it, auto matches the fixed 4 on Edit 2048 (8x MI355X: 11.07 vs 11.10 s); without it, auto leaves Edit unsplit. * perf(flux): default FLUX.1-Kontext to AITER MHA v4 BF16 with split-KV Where AITER's mha_v4 takes kv_splits (ROCm/aiter#6560) on gfx950, FLUX.1-Kontext-dev defaults to AITER_BF16 with kv_splits auto. At 1024x1024 with Ulysses 8 each rank attends 3 heads over ~8.7k tokens, too few query tiles to fill the GPU, which the split fills. An explicit --attention_backend or --mha_v4_kv_splits keeps precedence. prefer_mha_v4 moves from the Qwen runner to base_model so both use it. * perf(qwen): default Qwen-Image-2.1 to AITER MHA v4 BF16 with split-KV * fix(config): defer torch import in attention output codecs
Summary
mha_v4(kv_splits="auto")only splits a grid that fits in half a wave. MHA v4 runs one 512-thread work-group per CU, so a grid between one and a few waves ends on a nearly idle round that still costs about a full one: the Qwen-Image-Edit 2048x2048 per-rank attention at Ulysses 8 ([1, 21890, 3, 128]) is 258 work-groups, two rounds for 1.01 rounds of work, and auto leaves it unsplit at 824 us where 5-6 splits take 619-622 us.This keeps today's rule up to half a wave and, past it, picks the count with the least modelled time: rounds (a partial last round at 0.40 of a full one plus its fill) times the keys of the longest range, plus the merge. The divisor and ragged rules decide the candidates as before; the head dim cancels out of the comparison, so one model covers hd128 and hd256. The constants are fit on MI355X BF16 and hold on MI350X. The merge counts 1.5x for 8-bit Q/K (INT8/FP8 and FP8/FP8, whose rows walk a key faster than BF16's) and 2x past batch 1 (each split also copies the queries);
mha_v4_kv_splits()takes the Q/K format as a new keyword,q_format, which defaults to BF16.It also makes auto safe under
torch.compilewith a dynamic sequence. Traced, the pick ran on symbolic sizes:divmodon a SymInt broke the graph, so Dynamo ran the caller eagerly, and the fold and range lengths guarded on the key length, recompiling per length. Under compile, the pick, fold, launch and merge now run inside one functional custom op (aiter::mha_v4_split_kv), where the sizes are concrete; eager calls run the same body directly. The op returns its output rather than writing into the caller's buffer, because Inductor's scheduler failed on the mutating form in such a graph.Performance
Kernel sweep
CUDA-graph replay, every count from 1 to 12 and 16 timed per shape. Regret = time of the picked count over the best measured count, minus one; means are geometric.
BF16, 144 shapes, head dim 128: 3, 6, 7, 12, 14 and 24 heads at 21 lengths from 2048 to 65536 keys, batch 1, plus batch 2 for 3, 6 and 12 heads.
The best count is the same on both SKUs for 120 of the 144 shapes. A finer MI350X grid (3 and 6 heads, every 128 keys from 12288 to 26624, 226 shapes) puts this PR at 0.2% mean regret, 1 shape >= 5% off (main: 5.8%, 109 shapes). Fitting the constants to both SKUs jointly did not improve on these.
Per recipe, 27 shapes on one GPU: 3 to 40 heads at 4096 to 32768 keys, batch 1 and 2, and the Qwen-Image-Edit (Ulysses 8) and Qwen-Image (Ulysses 4) per-rank shapes. The quantized recipes' times include their quantizers, which do not depend on the count. Mean regret (shapes >= 5% off):
With this PR the worst of these shapes is 4.1% off its best count. The two merge factors come from this set: without them the batch-2 shapes split too far, e.g. FP8/FP8 [2, 21890, 3, 128] took 5 splits and ran 16% (MI355X) and 18% (MI350X) slower than unsplit, which is what main picks there.
An earlier 50-shape set of per-rank DiT shapes (Qwen-Image(-Edit), FLUX.1, FLUX.1-Kontext at 1024-2048 and Ulysses 1-8, plus head-dim-256 shapes) on MI355X: main 4.6% mean / 33.2% max regret, this PR 0.1% / 1.1%. Examples (us):
End to end
xDiT, 8x MI355X, Ulysses 8,
torch.compile, BF16 MHA v4 attention; seconds per output, two interleaved rounds. Measured at 3d6dd4c: this attention is BF16 at batch 1 per rank, where the last commit leaves the pick as it was.Qwen-Image-Edit on 8x MI350X: 12.62 with auto, no graph breaks.
PR Self-Review
Reviewed revision
f63d42aBehavior and intent
Only
kv_splits="auto"changes its pick, and only for grids above half a wave; explicit counts and smaller grids pick as before. Every picked count is one an explicitkv_splitsalready accepts (a divisor, or any count at batch 1 for operands with per-batch key lengths), capped at 16 and at ranges of at least 512 keys.mha_v4_kv_splits()gains the keywordq_format; without it a caller gets the BF16 pick, and only INT8 and FP8 Q/K (the measured 8-bit recipes) scale the merge, so other formats get BF16's pick too. Undertorch.compileevery split (auto or explicit) goes through the one custom op, so a compiled graph neither guards on the key length nor sees the count; eager calls are unchanged apart from running the split through a helper.Validation and unverified scope
On this branch merged with main (
fe40b01), on MI355X and on MI350X:python3 op_tests/test_mha_v4.pyruns 346 checks, 0 failed, andpython3 op_tests/test_mha_v4_sparse.py609 with 5 skipped, 0 failed. With #7939 (hd256 BF16) merged as well,test_mha_v4.pyruns 360 checks, 0 failed (MI350X).Eager calls: main and this branch in three interleaved rounds at [1, 4352, 3, 128] (BF16 at 4 splits, BF16/FP8 with auto and unsplit) take 54 to 120 us per call and differ by at most 4 us; the unsplit call, whose path this PR does not touch, differs by up to 2 us.
[1, 16400, 6, 128] with 16398 keys (Qwen-Image 2048 at Ulysses 4) had measured 12.7% slower at auto's 3 splits in an 8-GPU run. Re-timed on one GPU with 15 neighbouring shapes, 3 splits take 652 us against 683 unsplit on MI355X and 829 against 827 on MI350X, and auto is within 0.3% of the best count on all 16 shapes on both SKUs.
At
f63d42a(review nits only: the 8-bit merge factor lists INT8/FP8 explicitly, and the dynamic-sequence compile check passes a caller'sout), the two changed checks pass on MI355X. An FP8 case of the compile check, tried ince8ba24, compiled more than once under CI's torch 2.10 (it passed on 2.13) and was dropped. The full suites were not rerun locally afterf575999.Unverified:
f575999.PR-specific question
test_mha_v4_kv_splits_auto_shrinks_a_partial_last_roundfails on main, which returns 1 for every grid past half a wave, and its batch-2 and 8-bit cases fail at 3d6dd4c (5 splits instead of 2, and noq_format). The existingtest_mha_v4_kv_splits_auto_fills_one_wavestops at half a wave.test_mha_v4_kv_splits_auto_cost_model_compilesfailed at 87378e6, where the pick usedmin()with a key function that a fullgraph trace rejects; the existing compile parity test passes an explicit count.test_mha_v4_kv_splits_auto_compiles_once_for_a_dynamic_sequencefails at 7f9f1a4 (divmodon a symbolic size inside the trace) and would fail on a recompile per length; it is the only MHA v4 test that compiles withdynamic=True.Changed op tests
op_tests/test_mha_v4.py: the three tests above are new (the dynamic-sequence one fills a caller'sout; the partial-round one checks that an unmeasured Q format keeps BF16's pick), andbenchmark_mha_v4passes the Q/K format when it reports auto's count.