Skip to content

perf(ck_tile/fmha): [gfx1250 opt 5] optimize qr_tdm_pingpong, extend it to every head dim and simplify the dispatch - #15122

Draft
Yanxing-Shi wants to merge 2 commits into
developfrom
users/yanxishi/ck/qr-tdm-pingpong-opt
Draft

Yanxing-Shi wants to merge 2 commits into
developfrom
users/yanxishi/ck/qr-tdm-pingpong-opt

Conversation

@Yanxing-Shi

@Yanxing-Shi Yanxing-Shi commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

What

Optimizes qr_tdm_pingpong (#13471, #13484) on gfx1250 fp16/bf16 and makes it the default forward pipeline there:

  • Head dims: 32, 64, 96, 128, 160, 192/128, 256 and 512 (was 128 only).
  • Faster main loop: a second V buffer issued a trip ahead, 128-row tiles for long keys, and LDS reads issued ahead of the WMMAs, tuned per head dim (GFX125_QR_TDM_PINGPONG_LDS_READ_SCHEDULE).
  • Features: logits soft cap, bias/ALiBi, dropout, sink tokens and column-major V, now also in qr_tdm and qr_tdm_sched.
  • Dispatch: qr_tdm_pingpong takes every query of these head dims; only d128 on a small grid (fewer than two 128-row query blocks per CU) goes to qr_tdm_sched. qr_tdm_sched and fp16/bf16 qr_tdm are therefore generated for d128 only (gfx1250 fwd instances 77005 -> 49357).
  • Fixes: the generated dispatch API uses flat ifs (the else if chain overflowed clang's stack on a full build); LdsShuffle2DEpilogue stores whole vectors for 512-column rows; tile_example_fmha_fwd skips Pack-GQA where it would cost parallelism on gfx125 (short queries on small grids).
  • README: qr_tdm_pingpong section.

Performance

gfx1250, bf16, batch mode, bshd, TFLOPS, median of 3 runs. "causal" is -mask=b. The three pipeline columns force one pipeline; "develop dispatch" and "this PR dispatch" are the default builds of develop (c6542db) and this PR. This PR vs develop: geomean +23.8% (min +1.7%, max +55.9%) over the rows above 5 TFLOPS.

b s s_k q_total kv_total h h_k d mask qr_tdm qr_tdm_sched qr_tdm_pingpong develop dispatch this PR dispatch
1152 128 1 147456 1152 2 2 64 dense 6.8 7.6 9.3 7.6 9.3
1152 128 1 147456 1152 2 2 64 causal 0.1 0.1 0.1 0.1 0.1
1152 128 2 147456 2304 2 2 64 dense 13.6 15.1 18.6 15.1 18.6
1152 128 2 147456 2304 2 2 64 causal 0.1 0.1 0.2 0.1 0.2
1152 128 6 147456 6645 2 2 64 dense 40.5 44.9 55.3 45.0 54.6
1152 128 6 147456 6645 2 2 64 causal 1.0 1.0 1.4 1.0 1.4
1152 128 12 147456 13290 2 2 64 dense 81.0 89.4 110.2 89.3 109.5
1152 128 12 147456 13290 2 2 64 causal 3.6 3.6 5.2 3.6 5.2
1152 128 53 147456 60885 2 2 64 dense 334.2 370.2 457.9 370.6 452.0
1152 128 53 147456 60885 2 2 64 causal 62.9 63.3 92.1 63.2 92.3
1152 128 61 147456 69750 2 2 64 dense 378.2 417.5 522.4 420.4 522.5
1152 128 61 147456 69750 2 2 64 causal 82.4 82.0 121.4 82.5 121.2
1152 128 99 147456 113594 2 2 64 dense 485.9 538.4 635.3 535.3 636.9
1152 128 99 147456 113594 2 2 64 causal 157.2 148.4 211.4 147.3 210.9
1152 128 99 147456 114382 2 2 64 dense 480.1 541.1 634.3 536.0 629.8
1152 128 99 147456 114382 2 2 64 causal 154.2 146.7 210.2 147.9 211.1
1152 128 106 147456 121770 2 2 64 dense 503.5 563.6 662.9 558.2 662.5
1152 128 106 147456 121770 2 2 64 causal 175.1 167.2 238.8 165.6 238.2
1152 128 121 147456 139500 2 2 64 dense 578.1 606.0 769.8 641.0 764.4
1152 128 121 147456 139500 2 2 64 causal 225.9 207.4 310.1 219.1 311.4
1152 128 167 147456 192422 2 2 64 dense 622.5 689.7 812.4 706.9 806.0
1152 128 167 147456 192422 2 2 64 causal 313.1 273.3 443.4 283.9 435.5
1152 128 169 147456 194292 2 2 64 dense 643.6 673.5 818.0 719.0 828.0
1152 128 169 147456 194292 2 2 64 causal 315.9 285.7 451.0 293.5 457.6
1152 128 191 147456 220556 2 2 64 dense 685.6 743.2 897.5 770.9 895.9
1152 128 191 147456 220556 2 2 64 causal 396.8 387.3 563.7 395.9 560.1
1152 128 193 147456 222226 2 2 64 dense 619.1 675.2 793.8 709.6 778.3
1152 128 193 147456 222226 2 2 64 causal 338.5 317.4 457.4 318.8 475.1
1152 128 197 147456 227188 2 2 64 dense 600.9 687.4 783.0 699.3 802.3
1152 128 197 147456 227188 2 2 64 causal 343.3 319.1 472.1 332.9 477.2
1152 128 199 147456 228764 2 2 64 dense 614.3 683.4 800.1 707.9 816.1
1152 128 199 147456 228764 2 2 64 causal 345.6 328.1 489.4 335.7 491.6
1152 128 334 147456 384844 2 2 64 dense 637.7 733.0 746.1 729.5 812.1
1152 128 334 147456 384844 2 2 64 causal 468.6 471.6 580.0 467.0 611.7
1152 128 337 147456 388584 2 2 64 dense 648.4 725.3 779.0 730.9 833.8
1152 128 337 147456 388584 2 2 64 causal 471.3 469.0 614.8 478.2 640.3
1152 128 383 147456 441112 2 2 64 dense 684.6 783.4 833.6 776.5 856.1
1152 128 383 147456 441112 2 2 64 causal 536.4 563.7 729.3 550.0 732.7
1152 128 386 147456 444452 2 2 64 dense 659.1 713.5 812.0 737.4 808.2
1152 128 386 147456 444452 2 2 64 causal 498.3 486.9 626.5 508.1 627.3
1152 128 772 147456 889193 2 2 64 dense 764.6 804.3 811.7 811.5 825.0
1152 128 772 147456 889193 2 2 64 causal 669.2 703.2 749.7 705.2 757.1
1152 128 930 147456 1071000 2 2 64 dense 796.0 866.0 869.7 853.5 870.7
1152 128 930 147456 1071000 2 2 64 causal 697.3 751.5 780.7 755.2 791.2
1152 128 1544 147456 1778386 2 2 64 dense 867.4 912.6 1001.2 921.3 998.6
1152 128 1544 147456 1778386 2 2 64 causal 818.4 838.5 940.0 868.7 928.7
1152 128 1859 147456 2142000 2 2 64 dense 888.5 956.5 1028.4 955.9 1037.8
1152 128 1859 147456 2142000 2 2 64 causal 857.0 895.0 945.3 891.2 970.0
768 512 5252 393216 4033639 3 3 128 dense 1484.7 1713.5 1755.2 1375.6 1753.3
768 512 5252 393216 4033639 3 3 128 causal 1480.1 1212.1 1690.0 1337.1 1688.6
768 512 12288 393216 9437184 3 3 128 dense 1426.9 1752.6 1776.0 1332.7 1775.0
768 512 12288 393216 9437184 3 3 128 causal 1468.3 1237.8 1655.7 1283.3 1740.1

Tests

  • tile_example_fmha_fwd -v=1 (default build, gfx1250): all 8 head dims x batch/group x masks x bias x lse x sink, plus soft cap, dropout and column-major V: 1584 passed, 0 failed.
  • test_fmha_fwd_codegen.py (45 tests) and test_lds_shuffle_2d_epilogue.cpp pass.

…line and extend it to d64

- qr_tdm_pingpong serves hdim 64 as well as 128 (fp16/bf16, gfx1250).
- Second V buffer (is_qr_tdm_pingpong_double_v_v): V_{i+1} is issued a whole
  trip ahead of its PV gemm on the 128-row tiles and the d64 masked 64-row
  tile; Q moves onto the last V buffer.
- 128-row tiles for long keys, halving the K/V LDS reads per query row.
- SchedReadsAheadOfWmma: the B-operand LDS reads of both gemms run ahead of
  their WMMAs instead of a dscnt-0 wait before each; three fragments ahead on
  the d128 and masked 128-row tiles, one elsewhere.
- The next K tile is issued at the loop head together with V; the last trip
  issues no K/V.
- Dispatch: d64/d128 queries in the pingpong range go to qr_tdm_pingpong,
  with its 128-row tile from 128 query rows for long keys or dense 512-row
  queries; d128 dense and d64 causal long-key grids stay on qr_tdm_sched.
@github-actions github-actions Bot added project: composablekernel ck: attention-moe Used to tag composablekernel PRs that require approval from Attention/MOE review team. labels Nov 5, 2026
@therock-pr-bot

therock-pr-bot Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

✅ All Checks Passed — Ready for Review

Check Status Details
📝 PR Description ✅ Pass —
⛔ Forbidden Files ✅ Pass —
🧪 Unit Test ✅ Pass —
🔎 pre-commit ✅ Pass —
🚫 Draft PR 🔜 To Be Enabled —
🚩 Feature Flag 🔜 To Be Enabled —
📊 Code Coverage 🔜 To Be Enabled —
🤖 therock-pr-bot ✅ Pass —

🎉 All checks passed! This PR is ready for review.

📖 Need help? See the Policy FAQ for details on every check and how to fix failures.

🙋 Wish to Override Policy?

@therock-pr-bot

therock-pr-bot Bot commented Nov 5, 2026

Copy link
Copy Markdown

🎉 All checks passed! This PR is ready for review.

…m, LDS read schedule and simpler dispatch

qr_tdm_pingpong (fp16/bf16, gfx1250):
- Head dims 32, 96, 160, 192/128, 256 and 512 besides 64 and 128. Non-power-of-2 rows
  take padded LDS rows; d32 stages Q/K at 32 columns instead of padding them to 64.
- LDS read schedule per head dim (QrTdmPingPongLdsReadSchedule, codegen
  GFX125_QR_TDM_PINGPONG_LDS_READ_SCHEDULE): prefetching the next kK0/kK1 slice of K/V
  while the current slice's WMMAs run, and reading V's first slice before the softmax,
  each on the tiles where its VGPRs leave the occupancy alone (_sp<rows> / _vbs<rows>
  instance suffixes where they take effect).
- d96/d160/d192: the 128-row tile from 128 query rows at any key length.
- Logits soft cap, elementwise bias and ALiBi, dropout, sink tokens and column-major V.

qr_tdm and qr_tdm_sched take the same features (soft cap, bias, dropout, sink tokens;
column-major V for qr_tdm). The three TDM pipelines share one soft-cap helper and refuse
a CK_TILE_ATTENTION_LOGITS_SOFT_CAP_DEFAULT other than tanh.

Dispatch on gfx1250: qr_tdm_pingpong serves every fp16/bf16 query of its head dims at
any query and key length, except d128 with at least 128 query rows and fewer than two
128-row query blocks per CU, which goes to qr_tdm_sched. qr_tdm_sched and the fp16/bf16
qr_tdm instances are generated for d128 only. qr_tdm_pingpong's unpadded-V check takes
the exact V row (bn1). The generated API returns from flat ifs per candidate instead of
one else-if chain, which overflowed clang's parser stack on a full build.

LdsShuffle2DEpilogue keeps whole, lane-contiguous vectors when a row is longer than a
wavefront of them (MakeODramTileDistribution).

tile_example_fmha_fwd: on gfx125, Pack-GQA is skipped for 2..16 fp16/bf16 query rows
(hdim_v >= 64) that it would fold into more than 16 rows on a grid under two
(batch, head) pairs per CU.

README: qr_tdm_pingpong section; qr_tdm_sched and Pack-GQA notes updated.
@Yanxing-Shi Yanxing-Shi changed the title perf(ck_tile/fmha): [gfx1250 opt 5] optimize the qr_tdm_pingpong pipeline and extend it to d64 perf(ck_tile/fmha): [gfx1250 opt 5] optimize qr_tdm_pingpong, extend it to every head dim and simplify the dispatch Nov 7, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ck: attention-moe Used to tag composablekernel PRs that require approval from Attention/MOE review team. documentation organization: ROCm project: composablekernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant