mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #3259] [Engine] perf(hip): optimize EXPDEC routed-expert kernels and enable by default on gfx12 #4533
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#4533
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/3259
Author: @bsorensen110
Created: 10/19/2026
Status: 🔄 Open
Base:
main← Head:new/pr3024📝 Commits (2)
6cb4538perf(hip): optimize EXPDEC routed-expert kernels and enable by default on gfx12c0f2cf3docs(kernels): document gfx12 EXPDEC defaults and resolve expdec_bench params per device📊 Changes
7 files changed (+169 additions, -80 deletions)
View changed files
📝
CMakeLists.txt(+5 -7)📝
docs/EXPERTDEC.md(+10 -6)📝
include/strata/hip_compat/cuda_runtime.h(+2 -0)📝
include/strata/kernels/iq_kernels.hpp(+5 -4)📝
src/kernels/cuda/iq_kernels.cu(+88 -40)📝
src/kernels/expdec_bench.cpp(+40 -18)📝
src/kernels/iq_multi_parity.cpp(+19 -5)📄 Description
Problem and result
Supersedes #3024 (closed during the
0.2.0-betatracker reset), rebased onto0.2.0-beta(e88ee39d).On RDNA4 (
gfx1200/gfx1201), grouped routed-expert decode/verify kernels insrc/kernels/cuda/iq_kernels.cupreviously:EXPDECkernels (expdec_gu_kernelforIQ2_XXS/IQ2_XS/IQ3_XXS/IQ3_S/IQ2_Sgate/up andexpdec_down_kernelforQ2_0down) off by default ongfx12.__syncthreads()(s_wait_loadcnt_dscnt 0x0; s_barrier; global_inv scope:SCOPE_SEongfx12) instage_iq_grid,native_down_multi_kernel,expdec_gu_kernel, andexpdec_down_kernel, including an unconditional top-of-loop barrier on the very first group/entry pass (g == blockIdx.y && e == e0) where shared memory had not yet been read.Q8_1activations intos_uvia scalar 32-bit stores with integer division by 9 (w / 9,w - k * 9) ined_stage_entry, and interpreted__launch_bounds__(256, MINB)on HIP as waves-per-EU (MINB=4= 1,024 threads/SIMD, over-constraining register allocation).This PR adds LDS-only
iq_sync_lds(), skips the redundant first-iteration pre-stage barrier, vectorizesed_stage_entrywithint416-byte stores (no/ 9division), scalesexpdec_gu_kernellaunch bounds on HIP (MINB * 2waves/SIMD), and enablesEXPDECgate/up andQ2_0down by default per device ongfx12while preserving first-use environment evaluation (apply_nv_defaultson CUDA) and per-devicearch[64]resolution on mixed-GPU systems. Ongfx1201,expdec_benchlatency drops-17.9%to-22.9%(96.2 -> 74.2 usatT=2) with0bitwise differences.Related discussion
Supersedes #3024 (includes the per-device
arch[64]caching, first-useSTRATA_EXPDEC/STRATA_EXPDEC_DOWNevaluation forapply_nv_defaults, andnative_expert_expdec_params()effectivep.downfixes from review on #3024).Approach
iq_sync_lds()(s_wait_dscnt 0x0; s_barrier_signal -1; s_barrier_wait -1ongfx12,s_waitcnt lgkmcnt(0); s_barrieron other HIP targets,__syncthreads()on CUDA) and replace LDS staging barriers instage_iq_grid,native_down_multi_kernel,expdec_gu_kernel, andexpdec_down_kernel.ed_stage_entryusing 2 threads perQ8_1block (int4stores tos_u).expdec_mode()andexpdec_down_on()at call time wheng_expdec < 0/g_expdec_down < 0: checkSTRATA_EXPDEC/STRATA_EXPDEC_DOWNfirst (soapply_nv_defaultson CUDA is respected), then fall back to per-deviceexpdec_default_gfx12()without overwriting the-1automatic sentinel.expdec_benchon HIP inCMakeLists.txtand extendsrc/kernels/iq_multi_parity.cpp(check_grouped) to verify bothEXPDEC=0andEXPDEC=1(down=1, down_iq4_nl=1) bit-for-bit against the reference kernels.Validation
Passed:
cmake --build build-copilot-hip --target strata iq_multi_parity expdec_bench arch_defaults_test -j16: revision6cb45386on0.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: revision6cb45386(0.2.0-beta),arch_defaults_test: OK../build-copilot-hip/iq_multi_parity: revision6cb45386(0.2.0-beta),gfx1201, all 26iq_mmvqcases and 29native_expert_groupedformat pairs (bothEXPDEC=0andEXPDEC=1) pass (iq_multi_parity: 0 failures, 0 bitwise differences)../build-copilot-hip/expdec_bench 150 0 20 18/42 def,dec,dec:w0,dec:w1 real: revision6cb45386(0.2.0-beta),gfx1201,expdec_bench: ok (0 choice comparisons differ)(T=1:54.2 -> 44.5 us,T=2:96.2 -> 74.2 us,T=3:101.4 -> 80.3 us,T=4:106.8 -> 85.7 us).Failed:
Not run:
gfx11and CUDA hardware (nogfx11or NVIDIA GPU in this host).iq_kernels.cuis CUDA/HIP only).Limits
IQ4_NLdown (STRATA_EXPDEC_D20) remains off by default (0) ongfx12becausenative_down_multi_kernel<20, true>withiq_sync_lds()is slightly faster thanexpdec_down_kernel<20>ongfx1201.Summary
Optimize the grouped native routed-expert decode kernels (
src/kernels/cuda/iq_kernels.cu) for RDNA4 (gfx1200/gfx1201), enableEXPDEC(expdec_gu_kernelandexpdec_down_kernelonQ2_0) by default ongfx12, and enableexpdec_benchon HIP builds.Changes
Fix HIP
__launch_bounds__(256, MINB)wave-occupancy mapping onexpdec_gu_kernel(src/kernels/cuda/iq_kernels.cu):__launch_bounds__(256, MINB)specifiesMINBblocks per SM (4blocks =\le 64registers). On HIP (amd_hip_runtime.h), the second argument of__launch_bounds__(max_threads, min_blocks)maps directly toamdgpu_waves_per_eu(min_blocks)(waves per SIMD, not blocks per WGP). Since a 256-thread CTA has 8 waves = 2 waves/SIMD on RDNA3/RDNA4,MINB = 4requested only 4 waves/SIMD (allowing up to 256 VGPRs), soexpdec_gu_kernel<16..18, 1, 4>compiled to222–223 VGPRsongfx1201(capping occupancy at 6 waves/SIMD = 3 CTAs/WGP instead of 8 waves/SIMD = 4 CTAs/WGP).__launch_bounds__argument toMINB * 2on HIP (8waves/SIMD forMINB = 4), droppinggfx1201register usage to171–187 VGPRs(0spills,\le 192VGPRs = 4 CTAs/WGP acrossIQ2_XXS,IQ2_XS,IQ3_XXS, andIQ3_S).Vectorize
ed_stage_entryinexpdec_gu_kernelandexpdec_down_kernel:ed_stage_entry<XB, HALVES>looped overw = tid; w < XB * 9; w += 256, executing 3 integer divisions by 9 (w / 9), 3 intra-warp divergentif (j == 0)branches, and 3 scattered 4-byteds_store_b32stores per thread per staged entry.XB <= 80 <= 128(ED_XB = 80,ED_HB = 20), map the 256 threads directly as 128 thread-pairs(k = tid >> 1, h = tid & 1)where each active pair loads one 16-byte half-block (int4) and writes a single 16-byteds_store_b128tos_u, whiletid < XBwritess_ds[c * XB + tid]with zero loops, zero/ 9divisions, and zero intra-warp branch divergence.LDS-only barriers (
iq_sync_lds()) and elided first-pass pre-staging barriers:iq_sync_lds()(s_wait_dscnt 0x0; s_barrier_signal -1; s_barrier_wait -1ongfx1200/gfx1201,s_waitcnt lgkmcnt(0); s_barrieron other HIP targets,__syncthreads()on CUDA) instage_iq_grid,native_down_multi_kernel,expdec_gu_kernel, andexpdec_down_kernel, avoidingglobal_inv scope:SCOPE_SEL0 vector-cache invalidations and global load-queue drains (s_wait_loadcnt_dscnt 0x0).for (int e = e0; e < e1; ...)withif (g > (int) blockIdx.y || e > e0) iq_sync_lds();so the first pass (g == blockIdx.y && e == e0, which covers allT <= 4decode groups whengridDim.y == cap_groups) skips a redundant barrier.Enable
EXPDEC(STRATA_EXPDEC=1,STRATA_EXPDEC_DOWN=1) by default ongfx12and enableexpdec_benchon HIP:expdec_on()andexpdec_down_on()to1ongfx1200/gfx1201(expdec_default_gfx12()) whenSTRATA_EXPDEC/STRATA_EXPDEC_DOWNare unset (STRATA_EXPDEC=0orSTRATA_EXPDEC_DOWN=0turns them off; non-gfx12architectures remain unchanged).expdec_notestderr logging on explicitSTRATA_EXPDEC=1.expdec_benchtarget on HIP builds inCMakeLists.txtand mapcudaDevAttrMemoryClockRate/cudaDevAttrGlobalMemoryBusWidthininclude/strata/hip_compat/cuda_runtime.h.iq_multi_parity(check_grouped) to verify bitwise equality of bothmulti(EXPDEC=0) andexpdec(EXPDEC=1, EXPDEC_DOWN=1, EXPDEC_D20=1) against the reference per-entry kernels across all format pairs.Validation & Benchmarks (
gfx1201, AMD Radeon AI PRO R9700)1. Bitwise Parity & Multi-Arch Compilation
./build-copilot-hip/iq_multi_parity:0 failuresacross all 18iq_mmvqchecks, 24native_expert_groupedchecks (testing bothmultiandexpdecagainstold), and 3q8_1 finitechecks../build-copilot-hip/expdec_bench 150 0 20 18/20,21/20,22/20,18/42,21/42,22/42 def,dec,dec:w1 real:identical(0 choice comparisons differ) across all 6 format pairs andT = 1..4.gfx1100,gfx1151,gfx1200,gfx1201) and NVIDIA CUDA (sm_75,sm_89,sm_100).2. Cold Routed-Expert Microbenchmark (
./build-copilot-hip/expdec_bench 150 0 20 ..., 150 reps\times5 rounds over 121–186 MB cold expert pools)gu/down)Tdef(all / gu / down, us)gfx12DefaultEXPDEC(all / gu / down, us)18/20(IQ3_XXS / IQ4_NL, 14 layers)158.7 / 34.6 / 18.954.7 / 31.0 / 18.9-6.8%(1.073x)gu:-10.4%18/20(IQ3_XXS / IQ4_NL, 14 layers)2103.0 / 62.0 / 35.594.1 / 53.4 / 35.6-8.6%(1.095x)gu:-13.9%18/20(IQ3_XXS / IQ4_NL, 14 layers)3107.6 / 65.0 / 37.298.7 / 56.3 / 37.1-8.3%(1.090x)gu:-13.4%18/20(IQ3_XXS / IQ4_NL, 14 layers)4111.6 / 68.0 / 38.6102.8 / 59.4 / 38.3-7.9%(1.086x)gu:-12.6%21/20(IQ3_S / IQ4_NL, 9 layers)162.6 / 37.8 / 18.958.8 / 35.5 / 19.5-6.1%(1.065x)gu:-6.1%21/20(IQ3_S / IQ4_NL, 9 layers)2108.8 / 68.5 / 35.7101.2 / 60.6 / 35.8-7.0%(1.075x)gu:-11.5%21/20(IQ3_S / IQ4_NL, 9 layers)3112.7 / 71.0 / 37.2103.5 / 61.6 / 37.5-8.2%(1.089x)gu:-13.2%21/20(IQ3_S / IQ4_NL, 9 layers)4116.0 / 72.9 / 38.8107.6 / 64.8 / 39.0-7.2%(1.078x)gu:-11.1%22/20(IQ2_S / IQ4_NL, 15 layers)2101.5 / 59.4 / 35.796.4 / 54.7 / 36.2-5.0%(1.053x)gu:-7.9%22/20(IQ2_S / IQ4_NL, 15 layers)4111.5 / 67.4 / 38.8106.7 / 63.0 / 39.0-4.3%(1.045x)gu:-6.5%18/42(IQ3_XXS / Q2_0, 3 layers)297.7 / 62.9 / 31.379.3 / 54.0 / 22.7-18.8%(1.232x)gu:-14.1%,down:-27.5%18/42(IQ3_XXS / Q2_0, 3 layers)4107.9 / 68.7 / 35.388.9 / 60.1 / 26.3-17.6%(1.214x)gu:-12.5%,down:-25.5%21/42(IQ3_S / Q2_0, 1 layer)2104.0 / 68.2 / 32.186.5 / 60.7 / 23.2-16.8%(1.202x)gu:-11.0%,down:-27.7%21/42(IQ3_S / Q2_0, 1 layer)4112.2 / 72.5 / 36.393.8 / 64.9 / 26.6-16.4%(1.196x)gu:-10.5%,down:-26.7%22/42(IQ2_S / Q2_0, 5 layers)296.9 / 60.8 / 31.981.6 / 54.9 / 22.8-15.8%(1.188x)gu:-9.7%,down:-28.5%22/42(IQ2_S / Q2_0, 5 layers)4108.6 / 68.6 / 35.792.7 / 64.5 / 26.4-14.6%(1.172x)gu:-6.0%,down:-26.1%3. End-to-End 397B
--serveDecode A/B (Swift-Qwen3.8-Flash-Next-IQ3_S,--spec 4 --kv k8v4, 256 tokens, 5 matched deterministic prompts)STRATA_EXPDEC=0(legacynative_gu_multi_kernel+native_down_multi_kernel):256tokens each, identical draft acceptance on all 5 prompts):2766.0 ms(92.60 tok/s),2696.0 ms(95.00 tok/s),2731.0 ms(93.80 tok/s),1989.0 ms(128.70 tok/s),2118.0 ms(120.90 tok/s)1,280generated tokens):12,300.0 ms(104.07 tok/s)gfx12Default (STRATA_EXPDEC=1 STRATA_EXPDEC_DOWN=1):256tokens each, winning5/5prompts by-21.0to-25.0 ms):2741.0 ms(93.40 tok/s),2671.0 ms(95.90 tok/s),2706.0 ms(94.60 tok/s),1965.0 ms(130.20 tok/s),2097.0 ms(122.10 tok/s)1,280generated tokens):12,180.0 ms(105.09 tok/s,-120.0 ms/+0.98%)--adapt-async 15-trial median (256tokens):103.40 -> 106.20 tok/s(2475.0 -> 2412.0 ms,+2.71%/-63.0 ms).🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.