Skip to content

HIP: RDNA4 short-row single-column mat-vec launches (stacked on #491) - #505

Open
danielhanchen wants to merge 3 commits into
pin-mmvq-wprfrom
pin-mmvq-rdna4
Open

danielhanchen wants to merge 3 commits into
pin-mmvq-wprfrom
pin-mmvq-rdna4

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

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_k launch 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:

  • 1 pass: one warp per row, 4 warps per block. This uses the c_wpr kernel mode from CUDA: give each warp its own rows in short-K single-column mat-vec launches #491.
  • 2 passes: 2 warps per block.
  • 3 or 4 passes: 4 warps per block. With 3 passes, warp 3 adds +0.0 as before.
  • Rows per warp (1 pass) or per block: 1 by default. GGML_CUDA_MMVQ_IDS_WPR_ROWS=2 or 4 changes it. At 2 rows, dense (non MUL_MAT_ID) rows of 2 passes keep the 8-warp launch unless GGML_CUDA_MMVQ_RDNA4_DENSE2=1.

Dense MUL_MAT and MUL_MAT_ID both 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=0 returns before any new launch, which restores the previous launches.

Exactness

The output is bitwise the same as the 8-warp launch:

  • Lane t of warp w still takes K slot 32*w + t, in a single pass, exactly as in the 8-warp launch.
  • The partials of warps 1..3 are added to warp 0's in the same order, followed by the same warp reduction.
  • The only additions removed are the +0.0 partials of idle warps. A partial starts at +0.0, so it is never -0.0, and x + 0.0 == x.
  • A row's sum does not depend on the other rows of its block, so the rows-per-block setting does not change any output either.

Measurements

Prebuilt-equivalent tree on one Radeon AI PRO R9700 (gfx1201, Linux):

arm no drafts (tok/s, median [min-max]) MTP 2 (tok/s) text sha
off 67.25 [66.66-67.61] 88.28 [85.80-88.55] same
off, repeat (A/A) 66.63 [66.44-67.57] 88.13 [86.67-88.46] same
this change (default, 1 row) 77.55 [77.35-78.80] 89.01 [87.59-89.21] same
2 rows per block 74.46 [74.02-74.81] 89.04 [87.66-89.37] same
  • Without drafts: +15.9% over the off A/A mean, which has a spread of 0.9%. 1 row per block beats 2 rows by 4.1%.
  • With MTP 2 (multi-column verify batches, which these single-column launches do not cover): +0.9%, acceptance 0.460 in every arm.
  • Text: in each mode, the greedy text of every arm has the same sha.
  • KLD of the logits vs off (wikitext, -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):

case 8-warp 2 rows per block 1 row per block
dense Q8_0 2048 x K 512, 1 column 6.6 17.0 3.9
dense Q4_K 2048 x K 2048, 1 column 7.3 17.7 4.9
Q8_0 8192 x K 256, 1 column (warp per row) 19.2 at 2 rows 19.2 5.5
MUL_MAT_ID Q5_K / Q6_K 2048 x K 512 x 8 experts 41 9.2

Testing

Not tested

  • Qwen3.8-Flash-Next (smallest quant 66 GB) was not run on the R9700. It was run on the B200 only, where this code is not compiled.
  • gfx1200 (RX 9060 XT) is RDNA4 and takes the same launches. It was not run.
  • RDNA3 / RDNA3.5 / CDNA / Vulkan are not affected.
  • Windows HIP was not built.

Pin

The unslothai/llama.cpp prebuilt is an aged upstream b#### tag plus the pins in scripts/unsloth/pr-set.json, merged in order. This branch is stacked on #491 (pin-mmvq-wpr, b12966 9a811bab3c4 base) because it uses #491's c_nwarps / c_rpb / c_wpr kernel parameters. Its diff over #491 only adds lines to ggml/src/ggml-cuda/mmvq.cu, so it must sit after #491 in pr-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.

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.
@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:38:51.655466Z 6fbee1a 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: 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".

Comment thread ggml/src/ggml-cuda/mmvq.cu Outdated
Comment on lines +1314 to +1315
// 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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

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 6fbee1a: comment is one line now.

Comment thread ggml/src/ggml-cuda/mmvq.cu Outdated
Comment on lines +1406 to +1408
static bool logged = false;
if (!logged) {
logged = true;

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

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 6fbee1a: the once-per-type log now runs from a function-local static initializer, which C++11 makes thread-safe.

@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. 🎉

Reviewed commit: 6fbee1af38

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

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