[PR #3230] [Engine] perf(hip): enable exact wave32 QSA decode kernels and B6 MMVQ fragment reuse on gfx12 #4510

Open
opened 2026-11-07 15:36:33 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/3230
Author: @bsorensen110
Created: 10/19/2026
Status: 🔄 Open

Base: main ← Head: perf/rdna4-d5-qsa-decode-mmvq


📝 Commits (1)

  • 67c4a31 perf(hip): enable exact wave32 QSA decode kernels and B6 MMVQ fragment reuse on gfx12

📊 Changes

5 files changed (+236 additions, -82 deletions)

View changed files

📝 CMakeLists.txt (+23 -27)
📝 include/strata/kernels/native_mmvq.hpp (+4 -4)
📝 src/kernels/b6_mmvq_bench.cpp (+5 -3)
📝 src/kernels/cuda/native_mmvq.cu (+164 -35)
📝 src/kernels/cuda/qsa_decode_attn.cu (+40 -13)

📄 Description

Problem and result

Supersedes #3102 (closed during the 0.2.0-beta tracker reset), rebased onto 0.2.0-beta (e88ee39d).

On RDNA4 (gfx1200/gfx1201), QSA decode attention (src/kernels/cuda/qsa_decode_attn.cu) and multi-column native MMVQ (src/kernels/cuda/native_mmvq.cu) left several 100% bitwise-identical wave32 optimizations disabled:

  1. qsa_decode_attn fell back to attn_chunk_kernel (12 warp-wide shuffle reductions per KV cell, 1 cell per warp step, scalar V loads) and attn_merge_kernel instead of the exact wave32 attn_chunk_kernel_pre75<M, 8, true> (NC=8 cells per warp step, DPP16 reduce12, 4-cell unrolled V loads, float4 broadcast weight folds) and attn_merge_v2_kernel<kMergeU>, and used full-workgroup __syncthreads() (s_wait_loadcnt_dscnt 0x0; s_barrier; global_inv scope:SCOPE_SE) for LDS-only barriers.
  2. In src/kernels/cuda/native_mmvq.cu, multi-column native_mmvq_multi_kernel reloaded each column's Q8_1 activation fragment for every output row in the block and recomputed Q4_K/Q5_K dot2 and Q6_K/Q4_K/Q5_K weight nibble shifts per column instead of reusing pre-decoded activation/weight fragments in registers across ROWS = 2 or 4 rows (b6_mmvq_rows_kernel), while iq4_table_lookup gated the 6-instruction v_perm_b32 lookup behind STRATA_HIP_GFX906 instead of __HIPCC__.

This PR enables attn_chunk_kernel_pre75<M, 8, true> and attn_merge_v2_kernel<kMergeU> by default per device on gfx12, adds LDS-only qda_sync_lds() and mmvq_sync_lds(), enables v_perm_b32 iq4_table_lookup on all HIP targets, and enables a per-format/shape b6_default_rows_gfx12 table (STRATA_B6_MMVQ_ROWS=-1 auto) on gfx12 while preserving first-use STRATA_B6_MMVQ_ROWS evaluation for apply_nv_defaults on CUDA. All outputs are 100% bitwise identical (0 differing bits across 1,172 QSA decode cases, 148,480 MMVQ multi-column outputs, and all 102 b6_mmvq_bench shape/column pairs).

Supersedes #3102. Rebased cleanly onto 0.2.0-beta (e88ee39d) preserving upstream kPdlPrefetch/launch_pdl, Il58Arch (ncols 5..8), kMergeU, and first-use b6_rows_mode() evaluation.

Approach

  • Enable pre75_attn() (attn_chunk_kernel_pre75<M, 8, true>) and attn_merge_v2() (attn_merge_v2_kernel<kMergeU>) by default on gfx12 (cached per device in cc[64] / g12[64]) and replace LDS barriers with qda_sync_lds() in src/kernels/cuda/qsa_decode_attn.cu.
  • Add mmvq_sync_lds(), enable v_perm_b32 iq4_table_lookup under #if defined(__HIPCC__), pre-decode B6X<Q6KTraits> / B6XK<Q4KTraits, 4> / B6XK<Q5KTraits, 5> fragments (vi, v0/v1, dot2), add B6X<SmallTraits<IQ4NLBlock, 4>> and B6X<SmallTraits<Q80Block, 8>>, and enable b6_default_rows_gfx12<F, NCOLS>(n_out) when b6_rows_mode() < 0 && dev_class() == 1 in src/kernels/cuda/native_mmvq.cu.
  • Enable qsa_merge_daily_parity and b6_mmvq_bench on HIP in CMakeLists.txt.

Validation

Passed:

  • cmake --build build-copilot-hip --target strata qsa_merge_daily_parity mmvq_multi_parity b6_mmvq_bench arch_defaults_test -j16: revision 67c4a310 on 0.2.0-beta (e88ee39d), AMD Radeon AI PRO R9700 (gfx1201, 32 GB, Linux 7.2.9-2-cachyos-server, ROCm 10.2.0a20260914), clean build & link.
  • ./build-copilot-hip/arch_defaults_test: revision 67c4a310 (0.2.0-beta), arch_defaults_test: OK.
  • ./build-copilot-hip/qsa_merge_daily_parity: revision 67c4a310 (0.2.0-beta), gfx1201, 1172 cases, 0 differ, 21336168 nonzero output values in all -> PASS (fp16 295, int8 291, q4_0 294, k8v4 292).
  • ./build-copilot-hip/mmvq_multi_parity: revision 67c4a310 (0.2.0-beta), gfx1201, multi_exact on: 148480 outputs compared, 0 differ bitwise, 0 non-finite, mmvq_multi_parity: ok.
  • ./build-copilot-hip/b6_mmvq_bench 80 0 -1: revision 67c4a310 (0.2.0-beta), gfx1201, all 102 shape/column pairs bitwise equal (B=A, A=1col, B=1col all ok; e.g., head Q6_K T=4: 1.4507 -> 1.0208 ms (-29.6%), MTP head Q6_K T=5: 0.6422 -> 0.5186 ms (-19.2%), gdn qkv Q4_K T=4: 0.0586 -> 0.0537 ms (-8.4%)).

Failed:

  • none observed

Not run:

  • Target-device runtime execution on gfx103x/gfx11 and CUDA hardware (no gfx103x/gfx11 or NVIDIA GPU in this host).
  • SYCL and Metal builds (qsa_decode_attn.cu and native_mmvq.cu are CUDA/HIP only).

Limits

  • b6_default_rows_gfx12 is active only when dev_class() == 1 (gfx12) and STRATA_B6_MMVQ_ROWS is unset or -1; CUDA and non-gfx12 HIP devices keep their existing defaults unless STRATA_B6_MMVQ_ROWS is explicitly set.

Problem and result
This PR combines two bitwise-exact RDNA4 (gfx12, wave32) decode/speculative-verification optimizations and enables their parity/benchmark harnesses under STRATA_ENABLE_HIP:

  1. D5 — Wave32 QSA decode attention: Replaces cross-warp __syncthreads() with qda_sync_lds() to avoid stalling on global KV VMEM loads, enables wave32 attn_chunk_kernel_pre75<M,8,true> by default on gfx12, enables bounded attn_merge_v2_kernel<16> by default on gfx12.
  2. D6 — Native MMVQ cross-row fragment reuse & LDS barriers: Replaces cross-warp partial LDS reduction barriers with mmvq_sync_lds() on gfx12/gfx103x, enables v_perm_b32 IQ4 lookup path across HIP targets, extends b6_mmvq_rows_kernel to SmallTraits (Q8_0, IQ4_NL) with preload of activation fragments, adds b6_default_rows_gfx12 for auto row count on gfx12.

Related discussion
none

Approach
On gfx12, the prior implementation suffered from shared-memory barriers draining in-flight global KV VMEM loads due to __syncthreads() emitting full system scope barriers. The workgroup-scoped qda_sync_lds() and mmvq_sync_lds() avoid L0 vector cache invalidation and global-load drain while preserving synchronization. For MMVQ, activation fragments and reusable weight-code transforms are preloaded once across output rows, reducing redundant global loads. The auto row count table (b6_default_rows_gfx12) selects measured optimal rows per shape on gfx12, falling back to off elsewhere.

Validation

Passed:

  • qsa_scores_parity: revision e88ee39d, GPU AMD Radeon AI PRO R9700 (gfx1201), observed PASS (zero bitwise differences)
  • qsa_topk_active_parity: revision e88ee39d, GPU AMD Radeon AI PRO R9700 (gfx1201), observed PASS (selected IDs bitwise identical)
  • mmvq_multi_parity: revision e88ee39d, GPU AMD Radeon AI PRO R9700 (gfx1201), observed PASS (0 bit-different with multi_exact on)
  • b6_mmvq_bench 80 0 -1: revision e88ee39d, GPU AMD Radeon AI PRO R9700 (gfx1201), observed PASS (0 failed comparisons)
  • qsa_merge_daily_parity: revision e88ee39d, GPU AMD Radeon AI PRO R9700 (gfx1201), observed PASS (0 bitwise differences)
  • arch_defaults_test: revision e88ee39d, GPU AMD Radeon AI PRO R9700 (gfx1201), observed PASS

Failed:

  • none observed

Not run:

  • CUDA builds (no CUDA toolchain available)
  • SYCL builds (no SYCL toolchain available)
  • Metal builds (no Metal support)

Limits

  • One-column MMVQ (ncols == 1) is unchanged: b6_launch is only invoked from launch_multi_n when ncols > 1 and multi_exact() is true. Evaluated STRATA_Q6_PACKED=1 for one-column Q6_K heads on gfx1201, but at the default 640-block grid it regressed the 106,299-row MTP head (339.9 → 408.8 us) and 248,320-row output head (794.1 → 960.4 us) while requiring ~213–497 MiB of additional persistent VRAM; both STRATA_Q8_PACKED and STRATA_Q6_PACKED remain off by default.
  • Target hardware validation: Runtime parity, microbenchmarks, and end-to-end 397B measurements were executed on gfx1201 (AMD Radeon AI PRO R9700). gfx906, gfx1030, gfx1100, gfx1200 (HIP) and sm_75, sm_89, sm_120 (CUDA nvcc) were verified by compilation only; non-gfx12 default dispatch paths are unchanged.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/3230 **Author:** [@bsorensen110](https://github.com/bsorensen110) **Created:** 10/19/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/rdna4-d5-qsa-decode-mmvq` --- ### 📝 Commits (1) - [`67c4a31`](https://github.com/Niko1221/Strata/commit/67c4a31081e7ec2a95c8e5f8c947d592b5fa34f3) perf(hip): enable exact wave32 QSA decode kernels and B6 MMVQ fragment reuse on gfx12 ### 📊 Changes **5 files changed** (+236 additions, -82 deletions) <details> <summary>View changed files</summary> 📝 `CMakeLists.txt` (+23 -27) 📝 `include/strata/kernels/native_mmvq.hpp` (+4 -4) 📝 `src/kernels/b6_mmvq_bench.cpp` (+5 -3) 📝 `src/kernels/cuda/native_mmvq.cu` (+164 -35) 📝 `src/kernels/cuda/qsa_decode_attn.cu` (+40 -13) </details> ### 📄 Description ## Problem and result Supersedes #3102 (closed during the `0.2.0-beta` tracker reset), rebased onto `0.2.0-beta` (`e88ee39d`). On RDNA4 (`gfx1200`/`gfx1201`), QSA decode attention (`src/kernels/cuda/qsa_decode_attn.cu`) and multi-column native MMVQ (`src/kernels/cuda/native_mmvq.cu`) left several **100% bitwise-identical** wave32 optimizations disabled: 1. `qsa_decode_attn` fell back to `attn_chunk_kernel` (12 warp-wide shuffle reductions per KV cell, 1 cell per warp step, scalar V loads) and `attn_merge_kernel` instead of the exact wave32 `attn_chunk_kernel_pre75<M, 8, true>` (`NC=8` cells per warp step, DPP16 `reduce12`, 4-cell unrolled V loads, `float4` broadcast weight folds) and `attn_merge_v2_kernel<kMergeU>`, and used full-workgroup `__syncthreads()` (`s_wait_loadcnt_dscnt 0x0; s_barrier; global_inv scope:SCOPE_SE`) for LDS-only barriers. 2. In `src/kernels/cuda/native_mmvq.cu`, multi-column `native_mmvq_multi_kernel` reloaded each column's `Q8_1` activation fragment for every output row in the block and recomputed `Q4_K`/`Q5_K` `dot2` and `Q6_K`/`Q4_K`/`Q5_K` weight nibble shifts per column instead of reusing pre-decoded activation/weight fragments in registers across `ROWS = 2` or `4` rows (`b6_mmvq_rows_kernel`), while `iq4_table_lookup` gated the 6-instruction `v_perm_b32` lookup behind `STRATA_HIP_GFX906` instead of `__HIPCC__`. This PR enables `attn_chunk_kernel_pre75<M, 8, true>` and `attn_merge_v2_kernel<kMergeU>` by default per device on `gfx12`, adds LDS-only `qda_sync_lds()` and `mmvq_sync_lds()`, enables `v_perm_b32` `iq4_table_lookup` on all HIP targets, and enables a per-format/shape `b6_default_rows_gfx12` table (`STRATA_B6_MMVQ_ROWS=-1` auto) on `gfx12` while preserving first-use `STRATA_B6_MMVQ_ROWS` evaluation for `apply_nv_defaults` on CUDA. All outputs are **100% bitwise identical** (`0` differing bits across `1,172` QSA decode cases, `148,480` MMVQ multi-column outputs, and all `102` `b6_mmvq_bench` shape/column pairs). ## Related discussion Supersedes #3102. Rebased cleanly onto `0.2.0-beta` (`e88ee39d`) preserving upstream `kPdlPrefetch`/`launch_pdl`, `Il58Arch` (`ncols 5..8`), `kMergeU`, and first-use `b6_rows_mode()` evaluation. ## Approach - Enable `pre75_attn()` (`attn_chunk_kernel_pre75<M, 8, true>`) and `attn_merge_v2()` (`attn_merge_v2_kernel<kMergeU>`) by default on `gfx12` (cached per device in `cc[64]` / `g12[64]`) and replace LDS barriers with `qda_sync_lds()` in `src/kernels/cuda/qsa_decode_attn.cu`. - Add `mmvq_sync_lds()`, enable `v_perm_b32` `iq4_table_lookup` under `#if defined(__HIPCC__)`, pre-decode `B6X<Q6KTraits>` / `B6XK<Q4KTraits, 4>` / `B6XK<Q5KTraits, 5>` fragments (`vi`, `v0`/`v1`, `dot2`), add `B6X<SmallTraits<IQ4NLBlock, 4>>` and `B6X<SmallTraits<Q80Block, 8>>`, and enable `b6_default_rows_gfx12<F, NCOLS>(n_out)` when `b6_rows_mode() < 0 && dev_class() == 1` in `src/kernels/cuda/native_mmvq.cu`. - Enable `qsa_merge_daily_parity` and `b6_mmvq_bench` on HIP in `CMakeLists.txt`. ## Validation **Passed:** - `cmake --build build-copilot-hip --target strata qsa_merge_daily_parity mmvq_multi_parity b6_mmvq_bench arch_defaults_test -j16`: revision `67c4a310` on `0.2.0-beta` (`e88ee39d`), AMD Radeon AI PRO R9700 (`gfx1201`, 32 GB, Linux `7.2.9-2-cachyos-server`, ROCm `10.2.0a20260914`), clean build & link. - `./build-copilot-hip/arch_defaults_test`: revision `67c4a310` (`0.2.0-beta`), `arch_defaults_test: OK`. - `./build-copilot-hip/qsa_merge_daily_parity`: revision `67c4a310` (`0.2.0-beta`), `gfx1201`, `1172 cases, 0 differ, 21336168 nonzero output values in all -> PASS` (`fp16` 295, `int8` 291, `q4_0` 294, `k8v4` 292). - `./build-copilot-hip/mmvq_multi_parity`: revision `67c4a310` (`0.2.0-beta`), `gfx1201`, `multi_exact on: 148480 outputs compared, 0 differ bitwise, 0 non-finite`, `mmvq_multi_parity: ok`. - `./build-copilot-hip/b6_mmvq_bench 80 0 -1`: revision `67c4a310` (`0.2.0-beta`), `gfx1201`, all 102 shape/column pairs bitwise equal (`B=A`, `A=1col`, `B=1col` all `ok`; e.g., `head Q6_K` `T=4`: `1.4507 -> 1.0208 ms` (`-29.6%`), `MTP head Q6_K` `T=5`: `0.6422 -> 0.5186 ms` (`-19.2%`), `gdn qkv Q4_K` `T=4`: `0.0586 -> 0.0537 ms` (`-8.4%`)). **Failed:** - none observed **Not run:** - Target-device runtime execution on `gfx103x`/`gfx11` and CUDA hardware (no `gfx103x`/`gfx11` or NVIDIA GPU in this host). - SYCL and Metal builds (`qsa_decode_attn.cu` and `native_mmvq.cu` are CUDA/HIP only). ## Limits - `b6_default_rows_gfx12` is active only when `dev_class() == 1` (`gfx12`) and `STRATA_B6_MMVQ_ROWS` is unset or `-1`; CUDA and non-`gfx12` HIP devices keep their existing defaults unless `STRATA_B6_MMVQ_ROWS` is explicitly set. --- **Problem and result** This PR combines two bitwise-exact RDNA4 (gfx12, wave32) decode/speculative-verification optimizations and enables their parity/benchmark harnesses under `STRATA_ENABLE_HIP`: 1. **D5 — Wave32 QSA decode attention**: Replaces cross-warp `__syncthreads()` with `qda_sync_lds()` to avoid stalling on global KV VMEM loads, enables wave32 `attn_chunk_kernel_pre75<M,8,true>` by default on gfx12, enables bounded `attn_merge_v2_kernel<16>` by default on gfx12. 2. **D6 — Native MMVQ cross-row fragment reuse & LDS barriers**: Replaces cross-warp partial LDS reduction barriers with `mmvq_sync_lds()` on gfx12/gfx103x, enables `v_perm_b32` IQ4 lookup path across HIP targets, extends `b6_mmvq_rows_kernel` to SmallTraits (Q8_0, IQ4_NL) with preload of activation fragments, adds `b6_default_rows_gfx12` for auto row count on gfx12. **Related discussion** none **Approach** On gfx12, the prior implementation suffered from shared-memory barriers draining in-flight global KV VMEM loads due to `__syncthreads()` emitting full system scope barriers. The workgroup-scoped `qda_sync_lds()` and `mmvq_sync_lds()` avoid L0 vector cache invalidation and global-load drain while preserving synchronization. For MMVQ, activation fragments and reusable weight-code transforms are preloaded once across output rows, reducing redundant global loads. The auto row count table (`b6_default_rows_gfx12`) selects measured optimal rows per shape on gfx12, falling back to off elsewhere. **Validation** **Passed:** - `qsa_scores_parity`: revision e88ee39d, GPU AMD Radeon AI PRO R9700 (gfx1201), observed PASS (zero bitwise differences) - `qsa_topk_active_parity`: revision e88ee39d, GPU AMD Radeon AI PRO R9700 (gfx1201), observed PASS (selected IDs bitwise identical) - `mmvq_multi_parity`: revision e88ee39d, GPU AMD Radeon AI PRO R9700 (gfx1201), observed PASS (0 bit-different with multi_exact on) - `b6_mmvq_bench 80 0 -1`: revision e88ee39d, GPU AMD Radeon AI PRO R9700 (gfx1201), observed PASS (0 failed comparisons) - `qsa_merge_daily_parity`: revision e88ee39d, GPU AMD Radeon AI PRO R9700 (gfx1201), observed PASS (0 bitwise differences) - `arch_defaults_test`: revision e88ee39d, GPU AMD Radeon AI PRO R9700 (gfx1201), observed PASS **Failed:** - none observed **Not run:** - CUDA builds (no CUDA toolchain available) - SYCL builds (no SYCL toolchain available) - Metal builds (no Metal support) **Limits** - One-column MMVQ (`ncols == 1`) is unchanged: `b6_launch` is only invoked from `launch_multi_n` when `ncols > 1` and `multi_exact()` is true. Evaluated `STRATA_Q6_PACKED=1` for one-column `Q6_K` heads on gfx1201, but at the default 640-block grid it regressed the 106,299-row MTP head (339.9 → 408.8 us) and 248,320-row output head (794.1 → 960.4 us) while requiring ~213–497 MiB of additional persistent VRAM; both `STRATA_Q8_PACKED` and `STRATA_Q6_PACKED` remain off by default. - Target hardware validation: Runtime parity, microbenchmarks, and end-to-end 397B measurements were executed on gfx1201 (AMD Radeon AI PRO R9700). gfx906, gfx1030, gfx1100, gfx1200 (HIP) and sm_75, sm_89, sm_120 (CUDA nvcc) were verified by compilation only; non-gfx12 default dispatch paths are unchanged. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#4510
No description provided.