mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #3220] [Engine] perf(hip): vectorize HC_Q8 prefill dequant and enable gfx12 Q8_0 gr_upmix #4503
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#4503
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/3220
Author: @bsorensen110
Created: 10/19/2026
Status: 🔄 Open
Base:
main← Head:new/pr2988📝 Commits (4)
b536ccfperf(rdna4): vectorize and pair-fuse STRATA_HC_Q8 prefill dequantization (P1)bf1ff2eperf(rdna4): port gr_upmix to gfx12 and fuse Q8_0 w_up dequantization (P1b)c581819fix(prefill): address review feedback for gr_upmix FP16 I/O, Q8 parity checks, and BF16 retry67d3b10fix(prefill): update SYCL gr_upmix signature and Metal stubs for Q8_0 dequant pair📊 Changes
13 files changed (+464 additions, -93 deletions)
View changed files
📝
CMakeLists.txt(+11 -7)📝
include/strata/core/native_dense.hpp(+3 -0)📝
include/strata/kernels/fused_gr.hpp(+2 -0)📝
include/strata/prefill/kernels.hpp(+3 -2)📝
metal/src/engine/stubs.cpp(+2 -1)📝
src/core/layer.cpp(+10 -6)📝
src/core/native_dense.cpp(+53 -2)📝
src/kernels/cuda/fused_gr.cu(+79 -2)📝
src/kernels/gr_parity.cpp(+64 -0)📝
src/prefill/gr_upmix_parity.cu(+92 -33)📝
src/prefill/kernels.cu(+85 -14)📝
src/prefill/prefill.cpp(+56 -23)📝
sycl/src/prefill/kernels.dp.cpp(+4 -3)📄 Description
Problem and result
Supersedes #2988 (closed during the
0.2.0-betatracker reset), rebased onto0.2.0-beta(e88ee39d).When
STRATA_HC_Q8=1is active (the default on RDNA4), each prefill chunk previously launched 192 scalargr_dequant_q8_0_kernelcalls (1int8element per thread, 2-byte stores,601.5 GB/songfx1201) and dequantized bothwdandwubeforegr_norm_rs, evictingwd(6.55 MB) from the 8 MB L2 / 64 MB Infinity Cache beforebf16_proj(wd)read it. In addition, the opt-inSTRATA_HC_UPMIX=1path was gated off ongfx12(gfx1200/gfx1201) and required materializingwuin global BF16 scratch before running.This PR:
gr_dequant_q8_0_kernel(8int8elements per thread,uint416-byte stores, bitwise identical across all16,777,216(fp16 d, int8 q)pairs), adds pair-fusedgr_dequant_q8_0_pair_kernel(3.204 ms -> 1.646 ms,1.95xacross 192 matrices), elides same-streamcudaStreamWaitEventcalls, and moveshc_bf16_view(*wd)/hc_bf16_view(*wu)immediately before their consumers.STRATA_HC_UPMIX=1(gr_upmix_kernel) togfx1200/gfx1201(__builtin_amdgcn_wmma_f32_16x16x16_bf16_w32_gfx12,1.57x-1.63xvshipBLASLt + gr_mix_r) and fuses directQ8_0w_updequantization into shared memorysB(bitwise identical to pre-dequantized BF16gr_upmixacross all 21 token countsT = 1..32768), reducing 397B--serveprefill wall time ongfx1201by-14.0 ms(T=2048) and-21.8 ms(T=4096).Related discussion
Supersedes #2988. Scope is limited to
STRATA_HC_Q8=1prefill dequantization (bitwise-identical default path) and opt-inSTRATA_HC_UPMIX=1ongfx12.Approach
gr_dequant_q8_0_kernel(64Q8_0blocks per 256-thread CTA) and addfused_gr_dequant_q8_0_pair+NativeDense::hc_bf16_view_pair/hc_bf16_release_pair.gr_upmix_kernelandgr_upmixtogfx1200/gfx1201with optional directQ8_0w_up_q8shared-memory dequantization, while preserving the ten/eleven-argument signature across SYCL (sycl/src/prefill/kernels.dp.cpp) and Metal (metal/src/engine/stubs.cpp).can_upmixwith!m.f16_iosoSTRATA_HIP_PROMPT_F16=1remains on the FP16 GEMM path, and retrygr_upmixwith materialized BF16 weights when directQ8_0upmix is declined (preserving CUDAsm_80+fused upmix underSTRATA_HC_Q8=1).Validation
Passed:
cmake --build build-copilot-hip --target strata gr_parity gr_upmix_parity arch_defaults_test -j16: revision67d3b105on0.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: revision67d3b105(0.2.0-beta),arch_defaults_test: OK../build-copilot-hip/gr_parity --selftest: revision67d3b105(0.2.0-beta),gfx1201,fused GR Q8_0 dequant (vec8 + pair), finite/Inf/NaN scales: pass,gr_read/gr_write: 0 failures,gr_parity OK.STRATA_HIPBLASLT_TUNING=tools/hip/gfx1201-hipblaslt-100500.txt ./build-copilot-hip/gr_upmix_parity --bench: revision67d3b105(0.2.0-beta),gfx1201, all 21 token counts (T = 1..32768) pass (gr_upmix_parity: 0 failures, 0 bitwise diffs between directQ8_0w_up_q8and pre-dequantized BF16w_up;T=20480.508 -> 0.324 ms1.57x,T=81921.935 -> 1.207 ms1.60x,T=327687.794 -> 4.784 ms1.63x).clang++ -std=c++20 -fsyntax-only -Iinclude/strata/hip_compat -Iinclude -I/opt/rocm/include -D__HIP_PLATFORM_AMD__=1 metal/src/engine/stubs.cpp: syntax check passed (0 errors).Failed:
Not run:
gfx11and CUDAsm_80+hardware (nogfx11or NVIDIA GPU in this host).sycl/src/prefill/kernels.dp.cppandmetal/src/engine/stubs.cppsignatures verified via header/stub syntax check).Limits
hc_down(wd) in prefill still dequantizesQ8_0 -> BF16into scratch slot 0 (6.55 MBper half) and runshipBLASLt.STRATA_HC_UPMIX=1remains opt-in (hc_upmix()) because its 16-element WMMA accumulation order differs slightly fromhipBLASLt(mixed max |diff| / max |value| ~ 1.1e-7..1.9e-7).Summary
Optimizes the
STRATA_HC_Q8=1hyper-connection (hc_down/hc_up) prefill read path on RDNA4 (gfx1200/gfx1201) and CUDA/HIP in two complementary steps:Default path (
STRATA_HC_Q8=1, bitwise-identical):gr_dequant_q8_0_kernel(src/kernels/cuda/fused_gr.cu) from 1int8element per lane (global_load_u8/i8+global_store_b16) to 8int8elements per lane (dequant_q8_0_vec8_item+bf16x2_from_f32_finitewithuint416-byte stores, 64Q8_0blocks per 256-thread CTA), and adds pair-fusedgr_dequant_q8_0_pair_kernel(fused_gr_dequant_q8_0_pair) plusNativeDense::hc_bf16_view_pair/hc_bf16_release_pair(src/core/native_dense.cpp).g_hc_ev_stream[dev][slot]) so same-streamcudaStreamWaitEventcalls are elided during single-stream prefill chunks.hc_bf16_view(*wd)aftergr_norm_rs(immediately beforebf16_proj(wd)) andhc_bf16_view(*wu)aftergr_silu(immediately beforebf16_proj(wu)) insrc/prefill/prefill.cppsogr_norm_rs(125.8 MBatT=2048,251.7 MBatT=4096) andwudo not evictwd(6.55 MB) from the 8 MB L2 / 64 MB Infinity Cache beforebf16_proj(wd)reads it.Opt-in
STRATA_HC_UPMIX=1path ongfx12(gfx1200/gfx1201) + fusedQ8_0w_updequantization:gr_upmix_kernelandgr_upmix()(src/prefill/kernels.cu) togfx1200/gfx1201(__builtin_amdgcn_wmma_f32_16x16x16_bf16_w32_gfx12, 8-elementum_b16fragments per lane,t = t0 + 8 * hi + iwave32 output row mapping, ands_wait_dscnt 0x0; s_barrier_signal -1; s_barrier_wait -1).const uint8_t* w_up_q8 = nullptrparameter togr_upmixso that whenSTRATA_HC_Q8=1andSTRATA_HC_UPMIX=1are active (wu->data == nullptr && wu->hc_q8 != nullptr),gr_upmix_kerneldequantizes each CTA's 64w_uprows (64 × 320= 640Q8_0blocks) directly into shared memorysBonce per CTA.prefill.cppskipshc_bf16_view(*wu)entirely, eliminating all 96wufull-matrix dequantizations (1.26 GBof BF16 scratch write+read per chunk) in addition to eliminating them.gated(T × 10,240FP32 =16.1 GBatT=2048,32.2 GBatT=4096) global memory round-trip across all 96 halves.gr_upmix_parity(src/prefill/gr_upmix_parity.cu,CMakeLists.txt) on HIP (gfx11/gfx12) and adds a bitwise parity check verifyinggr_upmix(..., d_wu_q8)matchesfused_gr_dequant_q8_0 + gr_upmix(..., d_wu_deq)bit-for-bit across all 21 token counts (T = 1 .. 32768).Root cause
STRATA_HC_Q8=1prefill dequantization overhead and cache eviction (src/core/native_dense.cpp,src/kernels/cuda/fused_gr.cu,src/prefill/prefill.cpp):STRATA_HC_Q8=1is set (NativeDense::add_hc_skip), the BF16 hyper-connection matrices (blk.*.attn_hc_down/up.weight,blk.*.ffn_hc_down/up.weight) are skipped at load (w.data == nullptr), leaving onlyw.hc_q8(Q8_0,320 × 10,240=3,276,800elements /102,400blocks per matrix).prefill.cpp:2943), each of the 96 halves calledNativeDense::hc_bf16_viewtwice (192launches ofgr_dequant_q8_0_kernel), where each thread loaded a singleint8quant (global_load_u8,global_load_u8,global_load_i8) and issued a 2-byteglobal_store_b16(601.5 GB/songfx1201).prefill.cppdequantized bothwd(6.55 MBBF16) andwu(6.55 MBBF16) beforegr_norm_rs. Runninggr_norm_rs(251.7 MBofm.Rread +m.xn16write atT=4096) between dequantization andbf16_proj(wd)evicted the freshly dequantized BF16 scratch from the 8 MB L2 / 64 MB Infinity Cache beforebf16_projread it.STRATA_HC_UPMIX=1gated off ongfx12(src/prefill/kernels.cu:2112, 2398):gr_upmix()checked onlygfx_arch_is_gfx11_wmma(p.gcnArchName), returningfalseongfx1200/gfx1201. Furthermore,prefill.cpp:2972requiredwu->data != nullptrbefore callinghc_bf16_view, or else required dequantizingwu(6.55 MBper half) into global BF16 scratch first.gfx1201always fell back tohc_bf16_view(wu)+hipBLASLt(wuGEMM) +gr_mix_r, writing and re-readingT × 10,240FP32m.gated(80 MBper half =7.68 GBread +7.68 GBwrite atT=2048;30.7 GBtotal atT=4096).The change
include/strata/kernels/fused_gr.hpp,src/kernels/cuda/fused_gr.cu:bf16x2_from_f32_finiteanddequant_q8_0_vec8_item(8 elements per thread, handling both 2-byte aligned and arbitrary unaligned pointers, using round-to-nearest-even finite packing when(d_u16 & 0x7C00u) != 0x7C00uand falling back tobf16_from_f32for±Inf/NaNscales).gr_dequant_q8_0_kernel(64Q8_0blocks per 256-thread CTA,uint416B stores) and addgr_dequant_q8_0_pair_kernel(fused_gr_dequant_q8_0_pair).include/strata/core/native_dense.hpp,src/core/native_dense.cpp,src/core/layer.cpp:NativeDense::hc_bf16_view_pairandNativeDense::hc_bf16_release_pair, plusg_hc_ev_stream[64][2]tracking to elide redundant same-streamcudaStreamWaitEventcalls.include/strata/prefill/kernels.hpp,src/prefill/kernels.cu,src/prefill/prefill.cpp:gr_upmix_kernelandgr_upmixtogfx1200/gfx1201(__builtin_amdgcn_wmma_f32_16x16x16_bf16_w32_gfx12, wave32gfx12fragment/row layout,s_wait_dscnt 0x0; s_barrier_signal -1; s_barrier_wait -1).const uint8_t* w_up_q8 = nullptrtogr_upmixsogr_upmix_kerneldequantizesw_up_q8(Q8_0) directly into shared memorysB, and wireprefill.cppto skiphc_bf16_view(*wu)whengr_upmixruns.hc_bf16_view(*wd)aftergr_norm_rsandhc_bf16_view(*wu)aftergr_siluinprefill.cpp.src/kernels/gr_parity.cpp,src/prefill/gr_upmix_parity.cu,CMakeLists.txt:fused_gr_dequant_q8_0andfused_gr_dequant_q8_0_pairacross finite, subnormal,±0,±Inf,qNaN,sNaNscales and aligned/unaligned pointers ingr_parity --selftest.gr_upmix_parityon HIP (gfx11/gfx12) and verify thatgr_upmix(..., d_wu_q8)is bitwise identical tofused_gr_dequant_q8_0 + gr_upmix(..., d_wu_deq)across all 21 token counts (T = 1 .. 32768).Evidence
1. Standalone Kernel Microbenchmarks & Parity (
gfx1201)A.
gr_dequant_q8_0(192matrices per chunk,1.93 GBtotal read+write traffic on Radeon AI PRO R9700gfx1201)65,536 × 256 = 16,777,216(fp16 d, int8 q)pairs (±0, subnormals, normals,±Inf,qNaN,sNaN): 0 bitwise mismatches vs scalargr_dequant_q8_0_kernel.gr_dequant_q8_0_kernel(scalar 1B loads, 2B store)3.204 ms601.5 GB/s1.00×gr_dequant_q8_0_kernel(vec8, 16Buint4store)1.701 ms1,132.7 GB/s1.88×gr_dequant_q8_0_pair_kernel(pair_vec8)1.646 ms1,170.7 GB/s1.95×(-1.56 ms)B.
gr_upmix_parity --benchongfx1201(STRATA_HIPBLASLT_TUNING=tools/hip/gfx1201-hipblaslt-100500.txt)T = 1, 7, 16, 31, 32, 33, 48, 63, 64, 65, 96, 126, 127, 128, 129, 600, 1000, 2048, 4099, 8192, 32768) pass (gr_upmix_parity: 0 failures):mixed max |diff| / max |value|:7.10e-08 .. 1.86e-07vshipBLASLt + gr_mix_r; sampled FP64 relative error5.60e-08 .. 1.92e-07(matchinghipBLASLt + gr_mix_r).Q8_0(gr_upmix(..., d_wu_q8)) vsfused_gr_dequant_q8_0 + gr_upmix(..., d_wu_deq): 0 bitwise diffs across all 21 token counts (mixedFP32,mixed16BF16, andmixed_hFP16 images).ThipBLASLtup GEMM +gr_mix_r)gr_upmix_kernel(gfx1201)T = 20480.508 ms(0.122 + 0.384 ms)0.325 ms1.57×444 GB/s,41 TFLOPST = 81921.942 ms(0.518 + 1.422 ms)1.208 ms1.61×461 GB/s,44 TFLOPST = 327687.820 ms(2.255 + 5.567 ms)4.826 ms1.62×458 GB/s,45 TFLOPS2. End-to-End
--servePrefill A/B Benchmark (gfx1201)--servewithGEN 1 <tokens>(Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ3_S,--prefill 4096 --spec 4 --kv k8v4 --prompt-cache 0 --expert-cache auto --expert-cache-per-layer),STRATA_HC_Q8=1,STRATA_HIPBLASLT_TUNING=tools/hip/gfx1201-hipblaslt-100500.txt, and RDNA4 defaults.T=4096plus 1 request at each lengthT=128, 256, 512, 1024, 2048, 4096) so expert cache andhipBLASLtplan cache are warm, followed byN = 5measured trials per length.STRATA_HC_UPMIX=1 STRATA_HC_UPMIX_CHECK=4atT=4095,mixed rel RMS = 4.480e-08 .. 8.184e-08(max |diff| = 1.088e-06 .. 1.967e-06), and all 30 measured requests acrossT=128..4096produce identical output token IDs across all three arms.A. Phase Breakdown (
STRATA_PREFILL_TIMING=1, warm runs atT = 2047andT = 4095)Thc read(ms)combine(ms)gdn out proj(ms)origin/main(477ac13c,STRATA_HC_Q8=1)204780, 8042, 4245, 45167.0980, 988984, 992STRATA_HC_Q8=1, STRATA_HC_UPMIX=0— P1a only)204783, 8338, 3843, 42163.5(-3.5 ms)972(warm)976(warm)STRATA_HC_Q8=1, STRATA_HC_UPMIX=1— P1a + P1b)204766, 6638, 3842, 42146.0(-21.0 ms,-12.6%)958, 958(-26 ms)961, 962(-26.5 ms)origin/main(477ac13c,STRATA_HC_Q8=1)4095149, 14983, 8377, 77309.01648, 16631655, 1671STRATA_HC_Q8=1, STRATA_HC_UPMIX=0— P1a only)4095153, 15379, 7974, 74306.0(-3.0 ms)1642, 1646(-11.5 ms)1649, 1653(-12.0 ms)STRATA_HC_Q8=1, STRATA_HC_UPMIX=1— P1a + P1b)4095123, 12379, 8074, 74276.5(-32.5 ms,-10.5%)1616, 1619(-38 ms)1624, 1627(-37.5 ms)B. End-to-End
--serveWall Latency (timing=0,N = 5trials per length)origin/main(477ac13c) median ms (range) [trials]STRATA_HC_UPMIX=0median ms (range) [trials]STRATA_HC_UPMIX=1median ms (range) [trials]UPMIX=1vsUPMIX=0)origin/main(Arm 3vsArm 1)T=128229.10 ms(223.4..235.9)[229.1, 235.9, 230.9, 223.4, 228.8]228.80 ms(222.0..235.2)[228.8, 235.2, 230.7, 222.0, 227.4]228.40 ms(220.4..231.3)[226.2, 231.3, 229.4, 220.4, 228.4]-0.40 ms(+0.18%)-0.70 ms(+0.31%)T=256330.80 ms(327.2..335.8)[328.3, 333.2, 327.2, 335.8, 330.8]329.40 ms(326.6..332.7)[327.4, 332.1, 326.6, 332.7, 329.4]326.50 ms(322.8..334.6)[326.5, 334.5, 322.8, 334.6, 324.9]-2.90 ms(+0.89%)-4.30 ms(+1.32%)T=512461.50 ms(453.1..480.8)[461.5, 480.8, 455.0, 470.8, 453.1]460.90 ms(452.6..479.7)[460.9, 479.7, 455.2, 470.7, 452.6]464.60 ms(452.0..476.6)[465.2, 476.6, 452.5, 464.6, 452.0]+3.70 ms(overlapping)+3.10 ms(overlapping)T=1024668.80 ms(662.1..678.5)[670.9, 678.5, 668.8, 668.8, 662.1]669.60 ms(663.5..679.5)[673.7, 679.5, 669.6, 669.2, 663.5]664.00 ms(661.8..846.7*)[846.7, 673.2, 664.0, 662.9, 661.8]-5.60 ms(+0.84%)-4.80 ms(+0.72%)T=2048995.20 ms(992.3..1104.6*)[1104.6, 993.7, 995.2, 992.3, 995.2]995.20 ms(994.0..995.9)[995.9, 995.2, 995.9, 994.2, 994.0]981.20 ms(977.7..981.8)[981.6, 981.2, 981.8, 977.7, 980.6]-14.00 ms(+1.43%, non-overlapping)-14.00 ms(+1.43%, non-overlapping)T=40961680.80 ms(1677.0..11750.5*)[1680.8, 1679.4, 1682.8, 11750.5, 1677.0]1687.60 ms(1686.0..1692.1)[1686.0, 1688.4, 1687.6, 1686.6, 1692.1]1659.00 ms(1654.2..11653.8*)[1665.9, 11653.8, 1654.2, 1654.5, 1659.0]-28.60 ms(+1.72%, non-overlapping warm)-21.80 ms(+1.31%, non-overlapping warm)*Note: Occasional single-trial outliers (
1104.6 ms,11750.5 ms,11653.8 ms) are background SSD/OS page-cache stalls on this host; all steady-state warm trials atT=2048(977.7..981.8 msvs992.3..995.9 ms) andT=4096(1654.2..1665.9 msvs1677.0..1692.1 ms) have zero range overlap.What this does NOT fix
hc_down(wd) in prefill still dequantizesQ8_0 -> BF16into slot 0 scratch (6.55 MBper half) and runshipBLASLt: FusingQ8_0wddirectly intopf_hcdownongfx12is left for a separate change.STRATA_HC_UPMIX=1remains opt-in on HIP: Becausegr_upmix_kernelaccumulatesK = 320in 16-element WMMA steps whilehipBLASLtsolutions110428/110462sum in a slightly different FP32 reduction order (mixed max |diff| / max |value| ~ 1.1e-7..1.9e-7),STRATA_HC_UPMIX=1stays behind its existing opt-in switch (hc_upmix()).gfx11and CUDAsm_80+: Both HIP (gfx1200,gfx1201) and CUDA (sm_80) backends were compiled and linked cleanly; runtime execution was validated ongfx1201(Radeon AI PRO R9700), whilegfx11and CUDA hardware runtime checks remain unrun on this machine.Test plan
gr_parity --selftestpasses ongfx1201(fused GR Q8_0 dequant (vec8 + pair), finite/Inf/NaN scales: pass,0 failures)gr_upmix_parity --benchpasses all 21 token counts (T = 1 .. 32768) ongfx1201(0 failures, including bitwise parity between directQ8_0w_up_q8and pre-dequantizedBF16w_up)STRATA_HC_UPMIX=1 STRATA_HC_UPMIX_CHECK=4end-to-end prefill check ongfx1201(mixed rel RMS 4.480e-08 .. 8.184e-08)sm_80) compile & link check (strata_kernels,strata_prefill,gr_upmix_parity)Reproduction
Environment: AMD Radeon AI PRO R9700 (
gfx1201, 32 GB), Linux7.2.9-2-cachyos-server, ROCm10.2.0a20260914,Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ3_S(v0.1.45base477ac13c).🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.