Repository navigation
HIP: RDNA4 short-row single-column mat-vec launches (stacked on #491) - #505
danielhanchen wants to merge 3 commits into
Conversation
On RDNA4 the mat-vec table runs 8 warps per single column block for Q4_0 / Q5_0 / Q8_0 / Q4_K / Q5_K / Q6_K / IQ4_NL / IQ4_XS and small-K rows are off on RDNA, so rows whose K fits 1-4 warp passes run 4-7 warps that only add +0.0. Such rows now take one warp per row (1 pass), 2 warps (2 passes) or 4 warps (3-4 passes), 1 row per warp or block by default, with the same K split and reduction order: bitwise the same output. GGML_CUDA_MMVQ_RDNA4=0 restores the old launches, GGML_CUDA_MMVQ_IDS_WPR_ROWS=2 / 4 sets the rows (dense rows of 2 passes keep the 8-warp launch at 2 rows unless GGML_CUDA_MMVQ_RDNA4_DENSE2=1). HIP builds only; CUDA code paths are unchanged.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a82d7b090f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // RDNA4: rows of 1-4 warp passes on 1 (one warp per row), 2 or 4 warps instead of 8; the same K split and order of sums, without the | ||
| // +0.0 partials of the idle warps, so the output is bitwise unchanged. |
There was a problem hiding this comment.
Remove the hard wrap from the new explanatory comment
The new comment splits a sentence across two lines at without the, contrary to the repository's explicit prohibition on splitting sentences and hard-wrapping comments. Keep the sentence on one line or shorten it without introducing a mid-sentence break.
AGENTS.md reference: AGENTS.md:L55-L59
Useful? React with 👍 / 👎.
| static bool logged = false; | ||
| if (!logged) { | ||
| logged = true; |
There was a problem hiding this comment.
Synchronize the shared first-launch logging flag
When separate inference contexts execute eligible short-row operations concurrently on RDNA4, their host threads access the same per-type logged flag without synchronization. Parallel inference contexts are explicitly supported and exercised in tests/test-thread-safety.cpp. Although initialization of the local static is thread-safe, the subsequent read and assignment are not, introducing a C++ data race and undefined behavior. Remove the diagnostic or protect its once-only execution with an atomic exchange or std::call_once.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 6fbee1a: the once-per-type log now runs from a function-local static initializer, which C++11 makes thread-safe.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
What
On RDNA4 (gfx120x, wave32) the mat-vec table gives Q4_0 / Q5_0 / Q8_0 / Q4_K / Q5_K / Q6_K / IQ4_NL / IQ4_XS single-column products 8 warps per block and one row per block. The
small_klaunch is off on RDNA. A row whose K fits 1 to 4 warp passes therefore runs 4 to 7 warps that only add +0.0 partials. Examples are the K 256 Q8_0 hyper-connection up projection and the K 512 expert down projections of Qwen4-35B-A3B. Such rows now take a launch that matches the work:c_wprkernel mode from CUDA: give each warp its own rows in short-K single-column mat-vec launches #491.GGML_CUDA_MMVQ_IDS_WPR_ROWS=2or4changes it. At 2 rows, dense (nonMUL_MAT_ID) rows of 2 passes keep the 8-warp launch unlessGGML_CUDA_MMVQ_RDNA4_DENSE2=1.Dense
MUL_MATandMUL_MAT_IDboth use these launches. Gate / bias fusion is supported, the shared-expert fusion is not. The new call sits behind#if defined(GGML_USE_HIP)and only runs when the device is RDNA4 with a 32-wide warp and the RDNA4 table.GGML_CUDA_MMVQ_RDNA4=0returns before any new launch, which restores the previous launches.Exactness
The output is bitwise the same as the 8-warp launch:
32*w + t, in a single pass, exactly as in the 8-warp launch.x + 0.0 == x.Measurements
Prebuilt-equivalent tree on one Radeon AI PRO R9700 (gfx1201, Linux):
pr-set.jsonpins + the open candidate pins (including CUDA: give each warp its own rows in short-K single-column mat-vec launches #491 and HIP: 32-element k-quant MMVQ dot products, default on for RDNA4 (gfx120x) #496) + this pin, HIP build for gfx1201 (GGML_HIP_MMVQ_VDR4defaults to ON from HIP: 32-element k-quant MMVQ dot products, default on for RDNA4 (gfx120x) #496).GGML_CUDA_MMVQ_RDNA4=0is the tree without this change; "2 rows" isGGML_CUDA_MMVQ_IDS_WPR_ROWS=2.-b 1 -ub 1): mean -1e-6 and max 5.1e-5, the same as the off vs off repeat; same top token 100%.Background, from our development fork with the same RDNA4 launch rules (same R9700 and model, no drafts): 75.2 tok/s with the launches off, 87.5 with 2 rows per block and dense 2-pass rows kept on 8 warps, 92.5 with 1 row per block. All text identical.
Op level (test-backend-ops perf, R9700, us per run):
Testing
mmvq.cucompiles with ROCm 7.0 clang for gfx1201 (with and withoutGGML_HIP_MMVQ_VDR4from HIP: 32-element k-quant MMVQ dot products, default on for RDNA4 (gfx120x) #496) and for gfx1151, with no warnings. The new kernel instantiations are present in the objects.mmvq.cuobject for sm_100a with this commit and with CUDA: give each warp its own rows in short-K single-column mat-vec launches #491 alone has identical SASS. The host code differs only in__LINE__immediates passed to the assert helpers.llama_pinset.py sim, master pr-set plus the open candidate pins cuda, vulkan: fuse the prescaled hc_post gate SIGMOID -> SCALE -> DSV4_HC_POST #485-llama : keep each MoE cache bank within one device allocation #487, fit : cap an integrated GPU at the host memory left after the resident input layer #489-common: cap the default thread count at the CPUs this process may use #498, CUDA: sparse FA decode over a q8_0 / q4_0 KV cache converts only the selected rows #501, kv-cache : write multi-stream KV caches per stream when a layer tensor exceeds 4 GiB #502, with this pin right after CUDA: give each warp its own rows in short-K single-column mat-vec launches #491), tags b13023 and b13024:src/llama-model-loader.cpp. It conflicts with fit : cap an integrated GPU at the host memory left after the resident input layer #489 / llama : upload mapped tensors through pinned staging buffers on ROCm when RAM is short #492, which touch the same file. On b13024, master pins common : add --moe-cache-mib auto to size the MoE expert cache from the fit #251 and Host-weight prefetch in the scheduler, faster MoE cache prompt and decode, cache seeding under llama-server #268 are refused by an upstream change. This pin only touchesggml/src/ggml-cuda/mmvq.cu, so it is not involved in any of these.pin_contractall intact,merge_checksclean.llama test-llama-archs test-backend-ops test-mtmd-impl) and the full CUDA build (sm_100a) both succeed.MUL_MAT3123/3123 andMUL_MAT_ID1298/1298, with and without this commit.--temp 0) on the merged tree, B200, with and without this commit: identical on all 9 models (Qwen3-0.6B Q8_0 / Q4_K_M, gemma-3-1b-it Q4_K_M, granite-4.0-h-tiny Q4_K_M, Qwen3.6-35B-A3B UD-Q4_K_XL, Qwen4-35B-A3B UD-Q2_K_XL, Qwen3.8-27B UD-IQ2_XXS, Qwen3.8-Flash-Next UD-IQ1_S, Inkling-2-Mini UD-IQ1_S), each run twice.MUL_MAT_ID: 1298/1298.MUL_MAT: 2621 cases OK, then an abort inmmvf.cu(GGML_ASSERT(stride_col_y % 2 == 0), an upstream assert) on an F32 x F32 case. F32 products never reachmul_mat_vec_q, so this abort is in the pin-set tree (traced to the test cases added by CUDA: few-channel MUL_MAT without a batched cuBLAS GEMM #460; fixed in CUDA: few-channel MUL_MAT without a batched cuBLAS GEMM #460 at 5c5d91b), not in this change.MUL_MAT2735/2735,MUL_MAT_ID1775/1775.GGML_CUDA_MMVQ_RDNA4=0: identical on all 8 models. The models are Qwen4-35B-A3B UD-Q4_K_XL, Qwen3.8-27B UD-Q4_K_XL, Inkling-2-Mini UD-Q2_K_XL, Qwen3.6-35B-A3B UD-Q4_K_XL, Qwen3-0.6B Q8_0 and Q4_K_M, gemma-3-1b-it Q4_K_M and granite-4.0-h-tiny Q4_K_M. The server logs of the speed arms show the launches engaged: Q8_0 dense K 256 warp per row, Q5_KMUL_MAT_ID2-warp, Q6_KMUL_MAT_IDwarp per row.Not tested
Pin
The unslothai/llama.cpp prebuilt is an aged upstream
b####tag plus the pins inscripts/unsloth/pr-set.json, merged in order. This branch is stacked on #491 (pin-mmvq-wpr, b129669a811bab3c4base) because it uses #491'sc_nwarps/c_rpb/c_wprkernel parameters. Its diff over #491 only adds lines toggml/src/ggml-cuda/mmvq.cu, so it must sit after #491 inpr-set.json. It does not touch the lines of #491 or #496. If #491 is dropped, this pin carries #491's change with it. Once upstream gives RDNA4 short rows their own launches in a base tag, the pin should be deleted.