Skip to content

HIP: RDNA4 float mat-vec over 256 to 1024 rows uses 64-thread blocks where it picked 256 - #507

Open
danielhanchen wants to merge 3 commits into
base/upstream-9a811bab3from
pin-mmvf-rdna4-bs
Open

danielhanchen wants to merge 3 commits into
base/upstream-9a811bab3from
pin-mmvf-rdna4-bs

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

Summary

The unslothai/llama.cpp prebuilt is an aged upstream b#### tag plus the pins listed in scripts/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 only launch_mul_mat_vec_f_cuda in ggml/src/ggml-cuda/mmvf.cu (+11 lines). Once upstream has an equivalent change, the pin is dropped.

The problem

launch_mul_mat_vec_f_cuda runs 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):

rows K 1024 K 2048 K 4608 64 threads vs the old choice
128 2.8 3.1 3.9 1.3 to 2.3x slower
256 15.7 16.0 15.8 0.24 to 0.68x
384 3.8 4.6 6.7 1.0 to 1.3x slower
512 17.3 18.4 21.4 0.22 to 0.48x
1024 19.1 21.2 26.4 0.60 to 0.95x
4096 16.5 24.2 121.3 1.6x slower to 0.96x

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

  • RDNA4 (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.
  • F16 weights at default precision accumulate in half2 and keep the old choice: fewer threads lengthen each half2 sum, which can overflow.
  • Kill switch: GGML_CUDA_MMVF_RDNA4_BS=0 restores the old choice exactly. Another multiple of 32 below 256 sets the replacement block size.
  • Every other device takes the unchanged code: the new condition is false there.
  • Numerics: on RDNA4 the order of a row's f32 partial sums changes for the shapes in the window, so the output is not bitwise. It is KLD-gated, below.

Evidence

Unless noted, the tree is the prebuilt source b12966-mix-e49c75f (the aged base plus the current pins) plus this pin at 211140ef353. On the R9700 (gfx1201, ROCm, one card), def is this pin and off is the same binary with GGML_CUDA_MMVF_RDNA4_BS=0, which runs the base code path exactly.

Check Result
Pin set (llama_pinset.py sim --tags window, master pr-set plus every open candidate pin plus this pin) b13023 and b13024: this pin merges cleanly and its lines are intact in the merged tree (pin_contract for 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 touches mmvf.cu
HIP build, gfx1201 exit 0, no diagnostics in mmvf.cu
R9700, test-backend-ops -o MUL_MAT_ID 1293 / 1293
R9700, per-shape sweep, test-backend-ops perf -o MUL_MAT, 388 F32 / BF16 shapes, cap off / 64 / 128, two passes see the table in "The problem"; inside the window every shape is 0.22 to 0.95x of the old time, outside it the code is unchanged
R9700, greedy 128 tokens, def vs off, plus KLD at decode width (-b 1 -ub 1, 3 chunks of 512) Qwen4-35B-A3B UD-Q4_K_XL: text changes, KLD 0.0201, same top-1 94.9%. gemma-3-1b-it BF16, Inkling-2-Mini UD-Q2_K_XL, Qwen3-30B-A3B UD-Q2_K_XL, Qwen3-0.6B BF16, granite-4.0-h-tiny Q4_K_M: identical text, KLD 0 (at most 1e-6), top-1 100%
R9700, decode tok/s, def vs off (llama-bench tg128, 11 pairs over three runs for Qwen3-0.6B, gemma and Qwen3-30B, 6 to 11 for the rest) Qwen3-0.6B BF16: median 182.5 vs 215.9 (+18%, no overlap; its 1024-row BF16 projections are in the window). gemma-3-1b-it BF16 (194.3 vs 195.1), Qwen3-30B-A3B (148.9 vs 146.0), granite-4.0-h-tiny (148.7 vs 146.0), Inkling-2-Mini (64.8 vs 64.7): equal within the run noise, which is 3 to 5% between rounds of the same arm on this host. None of their float weights is in the window, so both arms launch the same kernels
R9700, Qwen4-35B-A3B UD-Q4_K_XL decode tok/s (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 runs median 65.50 off vs 66.64 def (+1.7%); def is 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
Reference: the already pinned RDNA4 VDR4 MMVQ change, on vs off, Qwen4-35B-A3B, same KLD method mean KLD 0.0236, same top-1 95.2%
B200 (sm_100a), prebuilt source + this pin vs prebuilt source, greedy 64 tokens, --temp 0 identical text on 9 models: Qwen3-0.6B BF16, Llama-3.2-1B F16, gemma-3-1b-it BF16, granite-4.0-h-tiny Q4_K_M, Qwen3-30B-A3B UD-Q2_K_XL, Inkling-2-Mini UD-IQ1_S, Qwen3.8-27B UD-IQ2_XXS, Qwen4-35B-A3B UD-Q2_K_XL (experts on CPU), Qwen3.8-Flash-Next UD-IQ1_S (experts on CPU). Built from the first version of this pin; the CUDA code path is the same at every version
B200, test-backend-ops -o MUL_MAT / -o MUL_MAT_ID 3091 / 3091 and 1293 / 1293
AMD CI gfx1151 (Strix Halo, RDNA3.5, not RDNA4) Linux, first version of this pin build exit 0. MUL_MAT_ID 1293 / 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_S
AMD CI gfx1151 Windows (TheRock), 211140ef353 build exit 0; MUL_MAT_ID 0 FAIL; greedy 128-token md5 off / def / off identical on Qwen3-0.6B BF16 and Llama-3.2-1B F16

Not proven

  • Not proven neutral on Qwen4-35B-A3B. The pin is default-on for RDNA4 because its KLD there (0.020 to 0.025, top-1 93.7 to 94.9% over three runs) sits on the same floor as the already pinned, default-on RDNA4 VDR4 change measured the same way (0.0236 / 95.2%). This routing-sensitive model reacts at that scale to any change of f32 summation order. GGML_CUDA_MMVF_RDNA4_BS=0 turns the pin off.
  • Speed on models whose float weights fall outside the window is equal by construction (same kernels), and the measured differences are within the run noise of the shared R9700 host (3 to 5% between rounds of the same arm).
  • gemma-3-1b and other models with float projections of 4096 or more rows (where 64 threads were up to 30% faster in the sweep) are not sped up: the window is kept to the shapes where 64 threads never lost.
  • test-backend-ops -o MUL_MAT on 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.
  • Qwen3.8-Flash-Next was not run on the R9700: its smallest quant (66 GB) does not fit the card. Llama-3.2-1B F16 and Qwen3.8-27B were run on the R9700 only with the first version of this pin (identical text, equal speed).
  • RDNA4 parts other than gfx1201 (for example gfx1200) were not run.

…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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 6, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-06T08:33:37.665223Z 06d5824 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread ggml/src/ggml-cuda/mmvf.cu Outdated
Comment on lines +514 to +516
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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 06d5824: the cap now applies only to F32 accumulation, so F16 weights at default precision (half2 accumulators) keep the base block size.

@danielhanchen danielhanchen self-assigned this Dec 6, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 06d5824668

ℹ️ 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".

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.
@danielhanchen danielhanchen changed the title HIP: RDNA4 float mat-vec of 64 rows or more runs at most 64 threads per row HIP: RDNA4 float mat-vec over 256 to 1024 rows uses 64-thread blocks where it picked 256 Dec 6, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant