Repository navigation
HIP: RDNA4 float mat-vec over 256 to 1024 rows uses 64-thread blocks where it picked 256 - #507
danielhanchen wants to merge 3 commits into
Conversation
…eads per row On gfx1201 one 256-thread block per row hits a slow grid shape: the Qwen4-35B-A3B router (F32 256 x K 2048) takes 16.6 us at 256 threads per row and 3.9 us at 64 (R9700, test-backend-ops perf); 512 x K 2560 takes 17.5 us at 128 threads and 5.6 at 64. 64 threads per row are fast on every measured shape of 64 rows or more; fewer rows keep the old block size (one row: 2.7 vs 3.3 us). The f32 order of a row's sums changes (not bitwise). GGML_CUDA_MMVF_RDNA4_BS=0: the block size of other devices.
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: dc957ee23c
ℹ️ 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".
| if (GGML_CUDA_CC_IS_RDNA4(ggml_cuda_info().devices[device].cc) && warp_size == 32 && rdna4_bs >= 32 && rdna4_bs % 32 == 0 && | ||
| nrows >= 64 && rdna4_bs < max_block_size) { | ||
| max_block_size = rdna4_bs; |
There was a problem hiding this comment.
Exclude half-precision accumulation from the block-size cap
On RDNA4, F16 weights with default precision reach mul_mat_vec_f<T, half>, which accumulates each thread's products in half2, not F32. Capping the block at 64 quadruples the accumulation length relative to 256 threads and can turn previously finite results into infinities. For example, a 64-row, K=4096 F16 matrix and a single F32 input vector, both filled with 64, previously produce 16777216 per row: each half accumulator holds 32768 before conversion to F32. With this cap, each half accumulator overflows before conversion. Restrict the cap to F32 accumulation, or use F32 accumulators for the affected F16 path.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 06d5824: the cap now applies only to F32 accumulation, so F16 weights at default precision (half2 accumulators) keep the base block size.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! 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". |
A per-shape sweep on the R9700 (F32 and BF16 weights, 64 to 6912 rows, K 1024 to 6912, 1 and 3 columns) shows the slow grid shape only where the old choice is a 256-thread block and the row count is a multiple of 256 up to 1024: about 16 to 21 us for any K, 0.22 to 0.95x of that at 64 threads. Below 256 rows, at K 1152 (192-thread blocks) and at 4096 rows, 64 threads are 1.1 to 2.8x slower, which slowed gemma-3-1b, Inkling-2-Mini and Qwen3-30B-A3B decode. The cap now replaces the chosen block only in that window. GGML_CUDA_MMVF_RDNA4_BS=0 still disables it.
Summary
The unslothai/llama.cpp prebuilt is an aged upstream
b####tag plus the pins listed inscripts/unsloth/pr-set.json, merged in order with plain git. This pin is three commits on top of upstream b12966 (9a811bab3c4), so it needs no other pin. It touches onlylaunch_mul_mat_vec_f_cudainggml/src/ggml-cuda/mmvf.cu(+11 lines). Once upstream has an equivalent change, the pin is dropped.The problem
launch_mul_mat_vec_f_cudaruns one block per row and picks the block size with the fewest K iterations, which is 256 threads for most K. On gfx1201 (Radeon AI PRO R9700), 256-thread blocks over 256, 512, 768 or 1024 rows hit a slow grid shape: the launch takes about 16 to 21 us whatever K is, while 384 or 1152 rows at K 1024 to 3072 take 4 to 11 us. A sweep of 388 shapes (test-backend-ops perf, F32 and BF16 weights, 64 to 6912 rows, K 1024 to 6912, 1 and 3 columns, two passes, pass-to-pass noise under 1%), us per run at one column, F32 (the last column covers every K in the sweep except 1152):Qwen4-35B-A3B's MoE router is F32 256 x K 2048, once per layer and token. Outside the window, 64 threads are often slower: rows below 256 (the 128-row routers of Qwen3-30B-A3B and Inkling-2-Mini), K 1152 (where the old choice is already 192 threads: gemma-3-1b's 1024 x 1152 projection is 2.8x slower at 64), 4096 rows at small K, and BF16 2048 x 1024.
The fix
GGML_CUDA_CC_IS_RDNA4, warp size 32), F32 accumulation (F32 and BF16 weights, and F16 at F32 precision): when the chosen block is 256 threads and the row count is 256, 512, 768 or 1024, the block becomes 64 threads. Every other shape keeps the old choice.GGML_CUDA_MMVF_RDNA4_BS=0restores the old choice exactly. Another multiple of 32 below 256 sets the replacement block size.Evidence
Unless noted, the tree is the prebuilt source
b12966-mix-e49c75f(the aged base plus the current pins) plus this pin at211140ef353. On the R9700 (gfx1201, ROCm, one card),defis this pin andoffis the same binary withGGML_CUDA_MMVF_RDNA4_BS=0, which runs the base code path exactly.llama_pinset.py sim --tags window, master pr-set plus every open candidate pin plus this pin)pin_contractfor this pin: intact). merge_checks: clean. Other candidates are refused or not intact there (#483, #495, and at b13024 also #251, #268, #479); none of them touchesmmvf.cummvf.cutest-backend-ops -o MUL_MAT_IDtest-backend-ops perf -o MUL_MAT, 388 F32 / BF16 shapes, cap off / 64 / 128, two passesdefvsoff, plus KLD at decode width (-b 1 -ub 1, 3 chunks of 512)defvsoff(llama-bench tg128, 11 pairs over three runs for Qwen3-0.6B, gemma and Qwen3-30B, 6 to 11 for the rest)llama-completion, 256 tokens, default--fit: the model does not fit the 32 GB card, so part of each token runs on the CPU), 11 off / def pairs over three runsdefis faster in 9 of 11 pairs. Single values range 60.9 to 68.3 on this shared host, so the gain is small against the noise; at kernel level the model's 256 x K 2048 F32 router launch drops to 0.31x of its old time--temp 0test-backend-ops -o MUL_MAT/-o MUL_MAT_IDMUL_MAT_ID1293 / 1293. Greedy md5 off / def / off identical on Qwen3-0.6B BF16, Llama-3.2-1B F16, Inkling-2-Mini UD-Q2_K_XL, Qwen3.8-27B UD-IQ2_XXS, Qwen4-35B-A3B UD-IQ2_XXS and Qwen3.8-Flash-Next UD-IQ1_S211140ef353MUL_MAT_ID0 FAIL; greedy 128-token md5 off / def / off identical on Qwen3-0.6B BF16 and Llama-3.2-1B F16Not proven
GGML_CUDA_MMVF_RDNA4_BS=0turns the pin off.test-backend-ops -o MUL_MATon HIP aborts in the prebuilt source with and without this pin (GGML_ASSERT(stride_col_y % 2 == 0)). It comes from pin CUDA: few-channel MUL_MAT without a batched cuBLAS GEMM #460's odd-stride src1 view tests reaching the general MMVF route on AMD; CUDA: few-channel MUL_MAT without a batched cuBLAS GEMM #460 has its own fix.