mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #3233] [Engine] perf(hip): optimize exact QSA block scoring and top-k selection on gfx12 #4514
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#4514
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/3233
Author: @bsorensen110
Created: 10/19/2026
Status: 🔄 Open
Base:
main← Head:perf/rdna4-d7-qsa-select📝 Commits (3)
272baf3perf(hip): optimize exact QSA block scoring and top-k selection on gfx1286d005atest(hip): independently verify exact QSA score and rows parity87189e3test(kernels): clamp qsa_rows_bench nq=5..8 positions below max_cells📊 Changes
4 files changed (+484 additions, -141 deletions)
View changed files
📝
CMakeLists.txt(+15 -0)📝
src/kernels/cuda/qsa_select.cu(+235 -139)📝
src/kernels/qsa_rows_bench.cpp(+57 -1)📝
src/kernels/qsa_scores_parity.cpp(+177 -1)📄 Description
Problem and result
Supersedes #3108 (closed during the
0.2.0-betatracker reset), rebased onto0.2.0-beta(e88ee39d).On RDNA4 (
gfx1200/gfx1201), QSA block scoring and top-k selection (src/kernels/cuda/qsa_select.cu, launched 12 times per decode/verify step across the 12 QSA layers) exhibited four bottlenecks:qsa_block_scoresfell back toblock_scores_multi_kernel(1pooled key block per 32-lane warp,16xoversubscribed loads,1query per CTA iny, and 32-lane shuffle reductions) instead of the 4-lanes-per-keyblock_scores_rows_kernel(STRATA_QSA_ROWS), which was also restricted tonq <= 4while speculative verification usesnq = 5(--mtp-draft 4) up tonq = 8.rows_dot4inblock_scores_rows_kernelandscores_thread_dot4inblock_scores_thread_kernelused scalar__shfl_xor_sync(ds_bpermute_b32through LDS ongfx12) for the 2-step 4-lane sum instead ofv_mov_b32_dpp quad_perm:[1,0,3,2]+row_shl:2(dpp_sum4), andblock_scores_thread_kernelwas gated off under#if defined(__HIPCC__).qsa_sync_lds()barrier insideblock_scores_*and the 16-pass radix histogram loop ofblock_topk_kernel/block_topk_wide_kernelused full-workgroup__syncthreads()(s_wait_loadcnt_dscnt 0x0; s_barrier; global_inv scope:SCOPE_SEongfx12).block_topk_wide_kernelscanned the fullmax_blocksbuffer (65,538blocks at--max-context 262144) instead ofactive_blocks = min(max_blocks, n_bid + 1)(STRATA_SCORES_HALVE=1).This PR extends
block_scores_rows_kerneltonq = 1..8, enablesSTRATA_QSA_ROWS,STRATA_SCORES_THREAD, andSTRATA_SCORES_HALVEby default per device ongfx12, addsqsa_sync_lds()anddpp_sum4(), and verifies 100% bitwise-identical scores and selected IDs (qsa_rows_benchqsa_block_scoresat 131k context drops from27.8..52.9 usto13.3..32.8 usforT=1..4,1.61x-2.09xspeedup, and promptblock_scores_thread_kernelreaches3.78x-4.84xspeedup).Related discussion
Supersedes #3108 (includes the independent frozen pre-PR baseline oracle in
qsa_scores_parity.cpp, width-scoped comparison inqsa_topk_active_parity.cpp, andmax_cells - 1position clamping inqsa_rows_bench.cppfrom review on #3108 and #3233).Approach
qsa_sync_lds()(s_wait_dscnt 0x0; s_barrier_signal -1; s_barrier_wait -1ongfx12) anddpp_sum4()(v_mov_b32_dpp quad_perm:[1,0,3,2]+row_shl:2ongfx12/RDNA) insrc/kernels/cuda/qsa_select.cu.block_scores_rows_kernel(MAXQ = 8,s_q[8][4][128],s_nb[8],s_tail[8]) tonq = 1..8and enableqsa_rows_on(),scores_thread_on(), andscores_halve()by default ongfx12(cached per device ing12[64]).qsa_scores_parity,qsa_topk_active_parity, andqsa_rows_benchon HIP inCMakeLists.txt, and clamp generatednq = 5..8test positions insrc/kernels/qsa_rows_bench.cppbelowmax_cellsson_bid < max_blockseven when--ctx == --max-context.Validation
Passed:
cmake --build build-copilot-hip --target strata qsa_scores_parity qsa_topk_active_parity qsa_rows_bench arch_defaults_test -j16: revision87189e30on0.2.0-beta(e88ee39d), AMD Radeon AI PRO R9700 (gfx1201, 32 GB, Linux7.2.9-2-cachyos-server, ROCm10.2.0a20260914), clean build & link../build-copilot-hip/arch_defaults_test: revision87189e30(0.2.0-beta),arch_defaults_test: OK../build-copilot-hip/qsa_scores_parity: revision87189e30(0.2.0-beta),gfx1201, all 12 prompt cases (128,447,982scores) + 48qsa_block_scores_rows& top-k cases (nq = 1..8, consecutive + spreadn_bid+ partial tails) bitwise identical to the frozen pre-PR baseline (PASS)../build-copilot-hip/qsa_topk_active_parity: revision87189e30(0.2.0-beta),gfx1201,PASS: 20 top-k active-bound cases, selected IDs bitwise identical../build-copilot-hip/qsa_rows_bench --rows --no-dense --iters 50and./build-copilot-hip/qsa_rows_bench --ctx 131072 --max-context 131072 --rows --no-dense --iters 10: revision87189e30(0.2.0-beta),gfx1201,parity: 12 cases, 0 differ -> bit-identical(T=1:27.8 -> 13.3 us,T=2:34.1 -> 19.6 us,T=3:43.6 -> 27.0 us,T=4:52.9 -> 32.8 us).Failed:
Not run:
gfx103x/gfx11and CUDA hardware (nogfx103x/gfx11or NVIDIA GPU in this host).qsa_select.cuis CUDA/HIP only).Limits
STRATA_QSA_ROWS,STRATA_SCORES_THREAD, andSTRATA_SCORES_HALVEdefault to1ongfx12only; other HIP and CUDA targets keep their existing defaults unless explicitly overridden via environment variables.Summary
This PR optimizes the exact QSA block-scoring and radix top-k selection kernels in
src/kernels/cuda/qsa_select.cuon RDNA4 (gfx1200/gfx1201) and wave32 HIP targets with 100% bitwise parity (0differing scores across all 12qsa_scores_paritycases /51,069,696scores,0differing selected IDs across all 20qsa_topk_active_paritycases, and0differing scores or IDs across all 8qsa_rows_bench --rowscases at4K,32K, and128Kcontext):Wave32 VALU DPP reductions & workgroup-scoped barriers (
sel_xor_f32,sel_xor_i32,sel_sync_lds):__shfl_xor_synclowers throughds_bpermute_b32on the LDS/DS pipe (5ds_bpermute_b32+5s_wait_dscntper 32-lane reduction,20per 4-head block score). Replacing power-of-two XOR butterfly steps withv_permlanex16_b32(MASK=16,0x76543210/0xfedcba98) andv_mov_b32_dpp(row_xmask:1|2|4|8,0x160 | MASK) keeps reductions on the VALU pipe while summing the exact same lane pairs in the exact same tree order.__syncthreads()inside LDS-only selection/scoring loops with workgroup-scopedsel_sync_lds()(s_wait_loadcnt_dscnt 0x0; s_barrier_signal -1; s_barrier_wait -1ongfx1200/gfx1201;s_waitcnt vmcnt(0) lgkmcnt(0); s_barrierongfx103x/gfx11xx). Waiting on bothloadcnt(0)anddscnt(0)avoids L2 cache-invalidation overhead while remaining safe when divergents_cbranch_execzpaths skip a VMEM basic block.Smaller-
PERspecializations forblock_topk_reg_kernel(PER = 2, 9, 17) & parallelized scans inblock_topk_kernel/block_topk_wide_kernel:qsa_block_topkonly instantiatedblock_topk_reg_kernel<33>(151 VGPRs, 107 SGPRs, 7 SGPR spills) andblock_topk_reg_kernel<66>(192 VGPRs, 107 SGPRs, 88 VGPR / 106 SGPR spills). Every decode call up to33,792blocks ran<33>.block_topk_reg_kernel<2>(reach <= 2,048blocks /8Kcells:29 VGPRs, 26 SGPRs, 0 spills),block_topk_reg_kernel<9>(reach <= 9,216blocks /36Kcells:50 VGPRs, 43 SGPRs, 0 spills), andblock_topk_reg_kernel<17>(reach <= 17,408blocks /69Kcells:91 VGPRs, 67 SGPRs, 0 spills), and lowers the HIP prefill crossoverkRegMinBlocksfrom7,168(~28Kcells) to4,096(~16Kcells, matchinggfx1151).if (t == 0)) 256-iteration radix-bin search and 32-warp prefix loops inblock_topk_wide_kernel(77 → 28 VGPRs) andblock_topk_kernel(35 → 25 VGPRs,3,080 → 1,064 BLDS) with parallel 8-warp / 1-warp prefix scans.Prompt exact block-scorer (
block_scores_thread_kernel) grid granularity & threshold ongfx12:block_scores_thread_kernelused a fixedTS_BR = 512blocks per CTA alongXand only engaged atactive_blocks >= 16,385(ctx > 64K). At32Kcontext (8,193blocks) with256queries,tgridlaunched only17 × 4 = 68CTAs (__launch_bounds__(256, 1),177 VGPRs), leaving 28 of 48 WGPs idle once the first 48 CTAs finished.br(64forreach <= 2,048,128forreach <= 8,192,256forreach <= 32,768,512above) with__launch_bounds__(256, 2)(175 VGPRs) and lowers the defaultgfx12activation threshold toactive_blocks >= 512(ctx >= 2,048).Bitwise-exact
block_scores_rows_kernel<1..8>on HIP (STRATA_QSA_ROWS=1):gfx1201, LLVM'sGCNCreateVOPDpairs heads(0, 1)and(2, 3)inblock_scores_multi_kernelso even heads multiplyx*xfirst (fmaf(w, fmaf(z, fmaf(y, x*x)))) and odd heads multiplyy*yfirst (fmaf(w, fmaf(z, fmaf(x, y*y)))), whereasblock_scores_rows_kernel<NQ>had a different register schedule and 1-ULP divergence on ~3.4% of blocks, plus unrollednqbranches bloating<4>to60 KBof code.rows_dot4(k4, q4, h), stagesq_idxcooperatively through shared memoryqs, usesNQas the compile-time bound, and extendsqsa_block_scores_rowstonq = 1..8.qsa_scores_parity,qsa_topk_active_parity, andqsa_rows_benchunderSTRATA_ENABLE_HIPinCMakeLists.txt.Kernel Resource Audit (
gfx1201,llvm-readobj --notes)origin/main(477ac13c)block_scores_kernel26 VGPR / 20 SGPR / 0 B LDS / 0 spill24 VGPR / 20 SGPR / 0 B LDS / 0 spillblock_scores_tail_kernel24 VGPR / 18 SGPR / 0 B LDS / 0 spill21 VGPR / 18 SGPR / 0 B LDS / 0 spillblock_scores_multi_kernel65 VGPR / 84 SGPR / 16,512 B LDS / 0 spill65 VGPR / 84 SGPR / 16,512 B LDS / 0 spillblock_scores_slots_kernel40 VGPR / 39 SGPR / 16,512 B LDS / 0 spill38 VGPR / 39 SGPR / 16,512 B LDS / 0 spillblock_scores_thread_kernel177 VGPR / 22 SGPR / 25,344 B LDS / 0 spill175 VGPR / 24 SGPR / 25,344 B LDS / 0 spillblock_scores_rows_kernel<1..4>65..98 VGPR / 84 SGPR / 16..64 B LDS39..95 VGPR / 29 SGPR / 2,064..8,256 B LDSblock_scores_rows_kernel<5..8>nq <= 4only)113..167 VGPR / 30..42 SGPR / 10,328..16,512 B LDSblock_topk_kernel(ref)35 VGPR / 44 SGPR / 3,080 B LDS / 0 spill25 VGPR / 28 SGPR / 1,064 B LDS / 0 spillblock_topk_wide_kernel<false>77 VGPR / 43 SGPR / 33,032 B LDS / 0 spill28 VGPR / 31 SGPR / 33,032 B LDS / 0 spillblock_topk_reg_kernel<2><33>)29 VGPR / 26 SGPR / 32,908 B LDS / 0 spillblock_topk_reg_kernel<9><33>)50 VGPR / 43 SGPR / 32,908 B LDS / 0 spillblock_topk_reg_kernel<17><33>)91 VGPR / 67 SGPR / 32,908 B LDS / 0 spillblock_topk_reg_kernel<33>151 VGPR / 107 SGPR / 32,908 B LDS / 0/7 spill151 VGPR / 107 SGPR / 32,908 B LDS / 0/7 spillMicrobenchmark & Parity Evidence (
AMD Radeon AI PRO R9700,gfx1201)1. Prefill Exact Block Scoring (
qsa_scores_parity&qsa_select_bench,capacity = 262,144)All
51,069,696scores across the 12qsa_scores_paritycases and all256/256top-k selections inqsa_select_benchmatchblock_scores_kernel/qsa_block_topk_refbit-for-bit:origin/main(477ac13c)STRATA_SCORES_THREAD=0, warp)STRATA_SCORES_THREAD=1, default)origin/mainqsa_select_benchctx=2048, nq=256(scores)0.055 ms0.051 ms0.034 ms1.62×(-38.2%)qsa_select_benchctx=4096, nq=256(scores)0.108 ms0.101 ms0.046 ms2.35×(-57.4%)qsa_select_benchctx=16384, nq=256(scores)0.422 ms0.395 ms0.108 ms3.91×(-74.4%)qsa_select_benchctx=16384, nq=256(top-k)0.088 ms0.042 ms0.042 ms2.10×(-52.3%)qsa_select_benchctx=32768, nq=256(scores)0.832 ms0.772 ms0.197 ms4.22×(-76.3%)qsa_select_benchctx=32768, nq=256(top-k)0.103 ms0.058 ms0.058 ms1.78×(-43.7%)qsa_scores_parityctx=40000, nq=20488.08 ms(thread: 8.11 ms)7.31 ms1.93 ms4.19×(-76.1%)qsa_scores_parityctx=140000, nq=102414.36 ms(thread: 3.20 ms)13.82 ms3.08 ms4.66×vs warp (-3.8%vs thread)qsa_scores_parityctx=200000, nq=102420.29 ms(thread: 4.27 ms)19.57 ms4.19 ms4.84×vs warp (-1.9%vs thread)2. Decode Block Scoring & Top-K (
qsa_rows_bench --iters 100 --rows --no-dense)In
qsa_rows_bench --rows,origin/mainpreviously failed bitwise parity (scores DIFFER (max abs 1.53e-05), ids DIFFER). With this PR, all 8/8 cases (T = 1..4at target context and short context) are 100% bit-identical:origin/main(T=1 / 2 / 3 / 4us)T=1 / 2 / 3 / 4us)T=4ctx=4096, max_ctx=32768scores (multi)5.4 / 5.8 / 6.8 / 7.95.3 / 5.4 / 6.2 / 7.0-11.4%ctx=4096, max_ctx=32768topk(<33>→<9>)11.7 / 10.6 / 10.6 / 10.78.7 / 7.8 / 7.9 / 7.9-26.2%(1.35×)ctx=4096, max_ctx=32768layer (append+scores+topk+attn)65.2 / 75.5 / 90.0 / 112.162.0 / 71.9 / 86.7 / 108.3-3.8 us(-3.4%)ctx=4096, max_ctx=262144scores (multi)5.5 / 5.8 / 6.8 / 7.95.1 / 5.4 / 6.2 / 7.0-11.4%ctx=4096, max_ctx=262144topk(wide, active-bounded)47.0 / 42.5 / 43.9 / 47.29.2 / 8.3 / 8.3 / 8.3-82.4%(5.69×)ctx=4096, max_ctx=262144layer (append+scores+topk+attn)92.7 / 97.6 / 113.8 / 140.462.8 / 72.4 / 86.5 / 108.7-31.7 us(-22.6%)ctx=32768, max_ctx=262144scores (multi)12.7 / 16.3 / 21.6 / 26.411.0 / 12.4 / 15.4 / 17.9(rows: 13.2)-32.2%(rows: -50.0%)ctx=32768, max_ctx=262144topk57.4 / 55.2 / 56.4 / 60.139.4 / 21.2 / 23.8 / 29.9-50.2%(2.01×)ctx=32768, max_ctx=262144layer (append+scores+topk+attn)106.7 / 121.0 / 138.9 / 169.376.3 / 88.2 / 103.5 / 128.4-40.9 us(-24.2%)ctx=131072, max_ctx=262144scores (multi)35.3 / 51.5 / 69.9 / 89.027.6 / 35.2 / 44.5 / 53.6(rows: 32.8)-39.8%(rows: -63.1%)ctx=131072, max_ctx=262144topk(wide)79.1 / 81.8 / 84.1 / 83.869.5 / 71.3 / 71.4 / 71.6-14.6%ctx=131072, max_ctx=262144layer (append+scores+topk+attn)149.1 / 172.0 / 201.6 / 233.6145.3 / 154.6 / 176.8 / 210.3-23.3 us(-10.0%)End-to-End 397B Benchmark vs Clean
origin/main(477ac13c)Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ3_S(397BMoE, 12 QSA layers + 36 GDN layers) + MTP (--spec 4 --spec-min-p 0.5),--max-context 32768 --kv int8,1warmup +5timed trials per arm (20sGPU cooldown between arms).main_4096,d7_default_4096, andd7_thread_off_4096produce identical generated token hashes (b52b193b1859dbf7), and all 5 trials at512context produceced7b38252fd809d.main_4096origin/main(477ac13c),WMMA=14096 / 2561,427.9(1421.0–1447.0)2,868.52,511.8(2489.6–2617.5)101.9224.18(gpu 19.56)b52b193b1859dbf7(5/5)d7_default_4096default),WMMA=14096 / 2561,420.9(1410.7–1883.7)2,882.7(+0.50%)2,505.2(2491.8–2528.0)102.19(+0.26%)23.93(gpu 19.38,-0.18 ms/win)b52b193b1859dbf7(5/5)d7_thdoff_wmma0_4096STRATA_SCORES_THREAD=0),WMMA=04096 / 2562,037.5(2020.0–2047.3)2,010.32,650.6(2530.0–2823.8)96.5823.97(gpu 19.31)d699b83b../e74180b0..d7_default_wmma0_4096default),WMMA=04096 / 2562,023.2(2011.2–2033.1)2,024.5(+0.71%)2,626.2(2600.5–2672.2)97.48(+0.93%)23.92(gpu 19.32)d699b83b../e74180b0..main_512origin/main(477ac13c),WMMA=1512 / 256495.7(494.6–496.5)1,032.92,818.5(2810.5–2835.5)90.8322.40(gpu 19.18)ced7b38252fd809d(5/5)d7_default_512default),WMMA=1512 / 256495.5(494.2–496.6)1,033.32,818.0(2811.7–2841.9)90.8422.40(gpu 19.16)ced7b38252fd809d(5/5)(Note: At
512–4,096context with--max-context 32768, QSA block scoring + top-k represents<1%of total 397B step time, so end-to-end ranges overlap while GPU-reach wait improves by0.18 ms/winat4,096context and prefill improves by14.3 msunder defaultSTRATA_HIP_WMMA=0. The primary speedups scale with context length and KV capacity as measured inqsa_select_bench,qsa_scores_parity, andqsa_rows_benchabove.)What This Does NOT Fix
qsa_decode_attn.cu, covered in #3072) or QSA prompt attention (qsa_prompt_attn.cu, covered in #3013).STRATA_QSA_ROWSremains opt-in (STRATA_QSA_ROWS=1); at short context (4K),block_scores_multi_kernelis faster (5.1–7.0 usvs13.4 us), whereas at32K–128Kcontextblock_scores_rows_kernelis up to1.63×faster thanblock_scores_multi_kernel(32.8 usvs53.6 usat128K, T=4).gfx1201;gfx906,gfx1100, andgfx1200were compile-verified (clang++ -fsyntax-only), with device execution on non-gfx1201targets pending.Reproduction Commands
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.