Skip to content

[Triton] Optimize chunk_delta_attn performance. - #4683

Merged
Liang-jianhao97 merged 3 commits into
mainfrom
feature/chunk_delta_attn_flashkda
Aug 13, 2026
Merged

Liang-jianhao97 merged 3 commits into
mainfrom
feature/chunk_delta_attn_flashkda

Conversation

@Liang-jianhao97

@Liang-jianhao97 Liang-jianhao97 commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Optimize chunk delta attn performance.

Technical Details

  1. Add the fused two-kernel forward path, on by default for the varlen prefill
    shapes flash_kda_supported() admits and falling back to the default pipeline
    for every other shape or flag combination
  2. Cache the cu_seqlens readback and the chunk index tensor on argument identity,
    and add the segment count to K2's autotune key so the segmented and
    unsegmented schedules stop sharing one tuned config

Test Plan

  • UT test -- op_tests/triton_tests/chunk_delta_attn/test_flash_kda.py
  • Microbench -- op_tests/op_benchmarks/triton/bench_flash_kda.py

Test Result

All correctness tests pass (bfloat16, atol=1e-2, rtol=1e-2). Precision verification passed on GFX1250/GFX950/GFX942.
Kimi-K3 accuracy pass.
image

https://github.com/ROCm/ATOM/actions/runs/31656616128/job/94313083167

GFX950 Perf Data:

B T H K V chunk_delta_attn (ms) FlashKDA (ms) speedup
1 8192 32 128 128 1.0031 0.6091 1.65x
1 8192 64 128 128 1.6186 0.9148 1.77x
1 8192 96 128 128 2.3558 1.2243 1.92x
1 16384 32 128 128 1.9047 1.1722 1.62x
1 16384 64 128 128 3.1407 1.7967 1.75x
1 16384 96 128 128 4.6134 2.4802 1.86x

Submission Checklist

@Liang-jianhao97
Liang-jianhao97 requested a review from a team August 11, 2026 14:27
@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:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
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: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
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 4683 --add-label <label>

@zufayu
zufayu requested a review from vgokhale August 12, 2026 02:00
@Liang-jianhao97
Liang-jianhao97 force-pushed the feature/chunk_delta_attn_flashkda branch from 33c333d to d9f9d6d Compare August 12, 2026 02:47
@zufayu
zufayu removed the request for review from vgokhale August 12, 2026 05:01
@Liang-jianhao97
Liang-jianhao97 force-pushed the feature/chunk_delta_attn_flashkda branch 2 times, most recently from ef06e6e to 8545855 Compare August 12, 2026 07:03
@Liang-jianhao97 Liang-jianhao97 changed the title Feature/chunk delta attn flashkda [Triton] Optimize chunk_delta_attn performance. Aug 12, 2026
@Liang-jianhao97
Liang-jianhao97 force-pushed the feature/chunk_delta_attn_flashkda branch from 8545855 to 0b2d380 Compare August 12, 2026 07:59
1. Add the fused two-kernel forward path, on by default for the varlen prefill
   shapes flash_kda_supported() admits and falling back to the default pipeline
   for every other shape or flag combination
2. Cache the cu_seqlens readback and the chunk index tensor on argument identity,
   and add the segment count to K2's autotune key so the segmented and
   unsegmented schedules stop sharing one tuned config

Co-authored-by: Cursor <[email protected]>
@Liang-jianhao97
Liang-jianhao97 force-pushed the feature/chunk_delta_attn_flashkda branch from ed0cbc7 to c072a4a Compare August 13, 2026 01:05
The WY inverse was built by Neumann doubling across the whole 32-wide chunk.
That is exact in exact arithmetic, but L^k counts paths down the chunk, so once
the keys correlate and the gate stops damping L its entries reach 5e7 while the
inverse they sum to stays bounded by 1. Nothing survives that cancellation: the
flat doubling is 1e5 wrong in bf16 and still 400% wrong in fp32, and K2's
recurrence turns that into NaN. This is what took Kimi-K3 gsm8k to 0.2% with
every unit test here passing.

Invert the 16-wide diagonal blocks instead, then fold the sub-diagonal block
back in with a single exact term, which closes because INV @ L_off is nilpotent
at each merge level. The powers peak at 2e3, the matmul count is unchanged, and
the result lands within 2e-4 of an fp64 triangular solve.

Store it as fp16 rather than bf16. The inverse is bounded by 1, so what limits
the fused path is mantissa rather than range, and both are two bytes: bf16 here
scores 1.1e-2 on the correlated cases where fp16 scores 2.3e-3. fp32 would fix
the storage too but costs 1.3-1.5x, since K2 reads this twice per chunk on its
serial path.

Store the three C x K tiles before the inversion rather than after. Held live
across it, the extra tile pushed the prepare kernel to 274 VGPRs at H=64, over
the 256 that gfx9 needs for two waves per SIMD, costing 26% there and nothing
at any other head count.

Correlated keys with a weak gate now score 2.3e-3 against the fp32 recurrence
where the default pipeline scores 1.3e-2, and benchmarks are unchanged within
3% across 13 shapes. The new case fails with NaN on the old algorithm.

Co-authored-by: Cursor <[email protected]>
@Liang-jianhao97
Liang-jianhao97 force-pushed the feature/chunk_delta_attn_flashkda branch from c072a4a to e099f64 Compare August 13, 2026 02:19
An unannotated fp32 tl.dot is not fp32 on every target. tl.dot resolves
input_precision to "tf32" whenever the backend merely allows it, so gfx942
lowered the five dots that invert (I - L) to v_mfma_f32_32x32x4_xf32, which
truncates operands to 19 bits without rounding. On a doubling this
cancellation-prone that is bf16-grade: the inverse came out 2e1 off and K2's
recurrence went NaN on the correlated-key case. gfx950 has no xf32 MFMA and
resolves the same dots to ieee, which is why the test only failed on MI308X.

Verified on gfx942: the ISA switches to v_mfma_f32_32x32x2_f32, and rho=0.9
and rho=1.0 pass at 1.2e-2 relative error where they previously produced NaN.

Co-authored-by: Cursor <[email protected]>

@Dewei-Wang-sh Dewei-Wang-sh 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.

lgtm

@Liang-jianhao97
Liang-jianhao97 merged commit 3679a25 into main Aug 13, 2026
64 of 66 checks passed
@Liang-jianhao97
Liang-jianhao97 deleted the feature/chunk_delta_attn_flashkda branch August 13, 2026 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants