Skip to content

cuda: TQ3_1S MMQ prefill on CDNA, RDNA2, RDNA3 and RDNA4, with a byte-permute unpack, on by default (stacked on #403) - #413

Merged
TheTom merged 6 commits into
TheTom:feature/turboquant-kv-cachefrom
jasstrong:pr/tq3_1s-mmq-amd
Nov 22, 2026
Merged

TheTom merged 6 commits into
TheTom:feature/turboquant-kv-cachefrom
jasstrong:pr/tq3_1s-mmq-amd

Conversation

@jasstrong

@jasstrong jasstrong commented Nov 7, 2026 •

Copy link
Copy Markdown

Rebased onto 50b4aa28b, after #403 merged, so the diff is only the six commits below.

What

#403 adds a native int8 MMQ path for TQ3_1S, with a config entry for RDNA4 only, because that was the only card available. These commits take it to the other AMD architectures that have a TQ MMQ path:

  1. Config entries for CDNA, RDNA2 and RDNA3. Each table gets TQ3_1S entries copied from its own TQ4_1S entries. That's the same rule cuda: add a native int8 MMQ path for TQ3_1S and enable the TQ MMQ path on RDNA4 #403 used for RDNA4: both types are 32-value blocks with a per-16 scale pair. This makes all three loader branches reachable: MFMA on CDNA, WMMA on RDNA3, and on RDNA2 the dp4a tile layout, which cuda: add a native int8 MMQ path for TQ3_1S and enable the TQ MMQ path on RDNA4 #403 marked as unreached and needing a run first. It has had that run now, see below, and its comment is updated.
  2. One byte permute instead of four table loads. The 8-entry int8 centroid table is exactly 8 bytes, the source window of v_perm_b32 (prmt on NVIDIA). So the four kvalues_tq3[] loads per quad become one instruction once the 3-bit indices sit in selector fields. cuda: add a native int8 MMQ path for TQ3_1S and enable the TQ MMQ path on RDNA4 #403's loader comment already names this as "the obvious next lever". The index extraction is unchanged. test-quantize-fns gets a third check alongside cuda: add a native int8 MMQ path for TQ3_1S and enable the TQ MMQ path on RDNA4 #403's two: it emulates both instructions on the host and compares both selector packings against kvalues_tq3 for all 4096 index quads. Like cuda: add a native int8 MMQ path for TQ3_1S and enable the TQ MMQ path on RDNA4 #403's two, it compares typed-in copies, so it is a one-time derivation check. The gate for the loader is test-backend-ops -o MUL_MAT|MUL_MAT_ID -p type_a=tq3_1s.
  3. One shift, then fixed fields. The quad's four indices are extracted with a single variable shift instead of four. MI210 kernel time drops 1647 -> 1511 us (test-backend-ops perf, m=4096 n=512 k=14336).
  4. Review follow-up. Comments trimmed, GGML_TQ_MMQ row updated.
  5. RDNA4 uses the permute too. @superbudvar measured it on an RX 9060 XT (comment), see below, so every architecture now runs the same loader.
  6. MMQ is the default for TQ3_1S. TQ3_1S is never converted at load, so nothing else depends on GGML_TQ_MMQ. Unset now means on for TQ3_1S and off for TQ4_1S, which still needs GGML_TQ_NATIVE=1. GGML_TQ_MMQ=0 turns it off for both, and 1 on for both. On the 4096 x 14336, n = 512 test-backend-ops perf case the default now runs 1511 vs 2187 us on the MI210, 1404 vs 1981 on the 7900 XTX and 2516 vs 4188 on the V620 against GGML_TQ_MMQ=0; all op tests above pass with the variable unset, 0 and 1. The GGML_TQ_MMQ rows in AGENTS.md and docs/KV-cache-quantization.md describe the new default. The default also reaches CDNA1 (gfx908), CDNA3 (gfx942) and CDNA4 (gfx950) through the shared CDNA table, but only gfx90a was measured.

Results

GGML_TQ_MMQ=1 GGML_TQ_NATIVE=1, ROCm 7.2.3, llama-bench -fa 1 -r 3, tok/s. "fallback" is the dequant + BLAS path, which was the default before commit 6 and is now GGML_TQ_MMQ=0. "#403 loader" is commit 1 alone. "this PR" is b525884, rerun on all three cards on 2026-11-11; the loader these cards run has not changed since.

Qwen3.8-27B-TQ3_1S (dense)

card fallback #403 loader this PR
MI210 (gfx90a, CDNA2) pp512 487.1 453.9 683.9
pp2048 485.7 453.5 682.7
RX 7900 XTX (gfx1100, RDNA3) pp512 579.5 757.0 801.4
pp2048 572.5 752.4 794.4
PRO V620 (gfx1030, RDNA2) pp512 271.7 445.4 453.2
pp2048 265.6 439.6 446.9

RDNA4, measured by @superbudvar on an RX 9060 XT (gfx1200), ROCm 7.1, 5 reps. "this PR" is 0bb36b9, whose loader is the one RDNA4 runs now:

card fallback #403 loader this PR
RX 9060 XT (gfx1200, RDNA4) pp512 66.99 314.31 +/- 24.65 297.25 +/- 38.77
pp2048 71.51 448.60 +/- 3.35 493.74 +/- 9.40

pp512 is inside its own spread on this card. On gfx1200 the same op tests pass, 158/158 and 44/44, default and with GGML_TQ_MMQ=1, and test-quantize-fns -v prints ok for all three checks.

Qwen3.6-35B-A3B-TQ3_1S (MoE, MUL_MAT_ID)

card fallback #403 loader this PR
RX 7900 XTX pp512 962.0 - 2336.7
pp2048 947.4 - 2281.1
PRO V620 pp512 443.8 1788.2 1821.9
pp2048 438.7 1731.4 1751.0

On CDNA the table loads make the MMQ path slower than the fallback (-7%). With the permute and the single shift it is 40% faster. On RDNA the loader is a smaller share of the kernel, so commits 2 and 3 add 2-6% on top of #403's gain. Decode (tg64) is unchanged everywhere, as expected.

Perplexity, wikitext-2, 16 x 512 tokens:

model, card fallback #403 loader this PR
27B, MI210 7.6587 7.6605 7.6605
27B, 7900 XTX 7.6751 7.6677 7.6677
27B, V620 7.6879 7.6642 7.6642
35B-A3B, V620 7.7458 7.7236 7.7236
35B-A3B, 7900 XTX - - 7.7249

The current head matches the table loader to the last digit on every card, because it produces the same int8 words. MMQ against the fallback is int8 rounding, within the error bars (about +/-0.30), and it's no worse in any row.

Testing

On each of the three cards:

  • test-quantize-fns -v: cuda: add a native int8 MMQ path for TQ3_1S and enable the TQ MMQ path on RDNA4 #403's unpack and centroid-table checks, plus the new byte-permute check. All three print ok.
  • test-backend-ops -o MUL_MAT|MUL_MAT_ID -p type_a=tq3_1s, both loaders, with GGML_TQ_MMQ=1 GGML_TQ_NATIVE=1: 158/158 and 44/44. The same counts pass on the default path. On the rebased head: this branch alone on the MI210 passes 165/165 and 44/44 by default, and ggml: add TQ3_4S, 3.5 bpw TQ3 with 128-value super-blocks (stacked on #403, #413, #415) #416's head, which contains it, passes 183/183 and 44/44 on all three cards with GGML_TQ_MMQ unset, 0 and 1 (the count went up with the new base's cases).
  • TQ4_1S on commit 1: 149/149 and 44/44, default and MMQ.
  • Full test-backend-ops -b ROCm0 on commit 1 with GGML_TQ_MMQ=1 GGML_TQ_NATIVE=1: MI210 19239/19244, 7900 XTX 19242/19244, V620 19241/19244. Every failure is one of two kinds:
    • the intermittent ConvRot MUL_MAT(type_a=q5_cr|q6_cr|q8_cr, ..., cr_pattern=random) tolerance failures, also seen on base;
    • MUL_MAT_RESIDUAL_FUSION(type=tq4_1s, m=64, n=1, k=256, self_add=0): "fusion 'mul_mat_bias' did not fire". This one is pre-existing and comes from GGML_TQ_NATIVE=1, not from this branch. On base (6439568) and on this branch it passes 8/8 by default and fails 7/8 with GGML_TQ_NATIVE=1, with or without GGML_TQ_MMQ. The fusion test assumes TQ4_1S was converted to q8_0 at load. That's a gap in the test, left for a separate change.

Limits of this testing

  • AMD only. The NVIDIA side of commit 2 is a __byte_perm with a nybble selector, the same transform the TQ4 loader beside it already uses. The host emulation checks its selector packing, but no NVIDIA build was run, and the TQ MMQ path is AMD-only at runtime anyway.
  • Not measured here: TQ4_1S speed (commit 1 doesn't touch it), and MoE prefill on CDNA (no TQ3_1S MoE model on that host). The MUL_MAT_ID op tests cover correctness there.
  • CDNA other than CDNA2: gfx908, gfx942 and gfx950 get the default through the shared CDNA table without having been run.

🤖 Generated with Claude Code

@TheTom TheTom left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Built together with #415 on a GB10 (sm_121, CUDA 13.0.88): no compiler warnings and no new register spills in the TQ kernels. TQ3_1S and TQ4_1S MUL_MAT (158/158, 149/149) and MUL_MAT_ID (44/44) pass under 7 GGML_TQ_* combinations, and test-quantize-fns passes. TQ3_1S MMQ is AMD-gated, so none of that runs the new loader.

Comment thread AGENTS.md Outdated
| `TURBO_AUTO_ASYMMETRIC` | `1` | Auto-select asymmetric K/V types for large-GQA models |
| `TURBO_SPARSE_V` | `1` | Sparse-V dequant skip in flash attention |
| `GGML_TQ_NATIVE` | unset | `1` opts out of load-time TQ->q8_0 conversion, uses fused native TQ kernels (saves ~1.7x VRAM on decode-heavy workloads) |
| `GGML_TQ_MMQ` | unset | `1` opts into the native int8 MMQ path for TQ3_1S/TQ4_1S prefill instead of the dequant-to-f16 + cuBLAS fallback. AMD only: the TQ MMQ config exists for RDNA4 (TQ3_1S) and for CDNA/RDNA2/RDNA3/RDNA4 (TQ4_1S). Off by default. TQ4_1S also needs `GGML_TQ_NATIVE=1`, because otherwise the weights are converted to q8_0 at load and this path is not reached; TQ3_1S is never converted, so it needs only this knob |

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This row still says the TQ MMQ config exists for RDNA4 only (TQ3_1S). With the CDNA, RDNA2 and RDNA3 rows added here it is CDNA/RDNA2/RDNA3/RDNA4 for both types. Please update it in the config commit.

Comment thread ggml/src/ggml-cuda/mmq-load-tiles.cuh Outdated
// permute (v_perm_b32 on AMD, prmt on NVIDIA), so the four 3-bit indices only
// need to be placed in selector fields. The table is held as two constants,
// TQ3_KV_LO = kvalues_tq3[0..3] and TQ3_KV_HI = kvalues_tq3[4..7], one byte per
// entry, least significant first; tests/test-quantize-fns.cpp checks both

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

test-quantize-fns checks tq3_1s_kv_lo/tq3_1s_kv_hi, which are copies typed into the test, against tq3_1s_kvalues, another copy. Editing TQ3_KV_LO, TQ3_KV_HI or kvalues_tq3 will not fail it. Same problem as the unpack check in #403. Either put the two constants and the table in one header that both the loader and the test include, or word this as a one-time derivation check and name test-backend-ops -o MUL_MAT -p type_a=tq3_1s under GGML_TQ_MMQ=1 as the real gate.

// v_perm_b32: one selector byte per output byte; source bytes 0-3 are the second
// operand and 4-7 the first. Keep the builtin here: __byte_perm() with a runtime
// selector is emulated on HIP through a dynamically indexed byte array.
return (int) __builtin_amdgcn_perm(TQ3_KV_HI, TQ3_KV_LO, i0 | (i1 << 8) | (i2 << 16) | (i3 << 24));

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Commits 2 and 3 change the loader RDNA4 runs, and there is no RDNA4 run behind them. Identical words by construction is a compile-level argument. Needs GGML_TQ_MMQ=1 test-backend-ops -o MUL_MAT -p type_a=tq3_1s and -o MUL_MAT_ID -p type_a=tq3_1s plus one pp512 before/after on an RDNA4 card before merge.

const uint32_t packed = (uint32_t)qp[0] | ((uint32_t)qp[1] << 8) | ((uint32_t)qp[2] << 16);
// One variable shift, then fixed-offset fields: shifting by 3*sub + 3*j for each j
// measured 9% slower on CDNA2 (gfx90a) prefill.
const uint32_t q = packed >> (3*sub); // the quad's four 3-bit indices in bits 0-11

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The PR body says two new commits and its MI210 table stops at 637 t/s pp512. This third commit claims 633 -> 684. Please update the body and tables to the current head, and say whether RDNA2/RDNA3 and PPL were rerun with this commit.

Comment thread ggml/src/ggml-cuda/mmq-load-tiles.cuh Outdated
// it against the CPU codec exhaustively over all 2^24 group words. Keep the two
// in sync.
//
// The centroids are selected with one byte permute, not four table loads: the

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Seven lines of comment here plus the 9% measurement note below. The fork convention is 1-2 lines with no measurement history. Keep the operand-order facts (which operand supplies bytes 0-3 on each vendor) and move the rest to the commit message.

CASE(GGML_TYPE_TQ4_1S, 512, 1, 128, 48, GGML_CUDA_MMQ_SRAM_LAYOUT_Q3_K, MMQ_ITER_K, true, false);
CASE(GGML_TYPE_TQ4_1S, 512, 1, 128, 64, GGML_CUDA_MMQ_SRAM_LAYOUT_Q3_K, MMQ_ITER_K, true, false);

// TQ3_1S: per-16-scale layout like TQ4_1S, so the same tile geometry and SRAM layout.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

With these rows TQ3_1S MMQ beats the fallback on CDNA2, RDNA2 and RDNA3 at equal PPL, and by 4x on MoE, but it stays behind GGML_TQ_MMQ=1. TQ3_1S is never converted at load, so nothing else depends on the gate. Please make MMQ the default for TQ3_1S when ggml_cuda_mmq_has_config is true and keep GGML_TQ_MMQ=0 as the opt-out, here or in a follow-up PR right after this one. TQ4_1S can stay as is since it also depends on GGML_TQ_NATIVE.

@TheTom

TheTom commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Thanks @jasstrong for running the TQ3_1S MMQ on the MI210, 7900 XTX and V620, and for the byte-permute idea. Comments are inline. The main ones are the stale AGENTS.md row and an RDNA4 run.

@jasstrong

Copy link
Copy Markdown
Author

Thanks. Done in b525884:

  • RDNA4 keeps cuda: add a native int8 MMQ path for TQ3_1S and enable the TQ MMQ path on RDNA4 #403's table-load loader for now. Commits 2 and 3 apply to CDNA, RDNA2 and RDNA3 only, so RDNA4 runs exactly what it ran before this PR.
  • Comments: the loader comment is cut to the gate and the operand-order facts.
  • Test: the test-quantize-fns checks now say they are one-time derivation checks. The loader comment names GGML_TQ_MMQ=1 test-backend-ops -o MUL_MAT -p type_a=tq3_1s as the real gate.
  • AGENTS.md: the row now lists CDNA/RDNA2/RDNA3/RDNA4 for both types.
  • PR description: updated to the current head. All three cards were rerun at this head, prefill and PPL. The numbers match the earlier ones, and PPL is unchanged.

MMQ on by default for TQ3_1S: I'll do that as a follow-up PR right after this one, so it can be reverted on its own.

@apollo-mg, could you run this on your RX 9060 XT when you have time?

GGML_TQ_MMQ=1 test-backend-ops -o MUL_MAT -p type_a=tq3_1s
GGML_TQ_MMQ=1 test-backend-ops -o MUL_MAT_ID -p type_a=tq3_1s
GGML_TQ_MMQ=1 llama-bench -m <any TQ3_1S model> -fa 1 -p 512 -n 0

Run it once at this head and once with the #if defined(RDNA4) branch in tq3_1s_qs_to_int8x4 deleted (mmq-load-tiles.cuh), so RDNA4 takes the byte permute. If the permute passes and isn't slower there, I'll remove the guard.

jasstrong pushed a commit to jasstrong/llama-cpp-turboquant that referenced this pull request Nov 11, 2026
…d head)

TheTom#403 was rebased and gained two commits (the TQ3_1S MMQ test reads the real
kvalues_tq3 and CPU decoder; a stray blank line in the RDNA4 config is
gone), and TheTom#413 merged them in 628916c. This brings the same content into
the TQ3_4S stack; no force-push.

This branch already carries TheTom#403 and TheTom#413 as cherry-picks, so every
conflicted source file keeps this branch's version. The net change against
the previous head is TheTom#403's new test (as merged into TheTom#413, plus the TQ3_4S
thresholds), its include path in tests/CMakeLists.txt, and the blank line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
jasstrong pushed a commit to jasstrong/llama-cpp-turboquant that referenced this pull request Nov 11, 2026
TheTom#413 now has TheTom#403's reworded head (31f7d95) as an ancestor; this branch
already carries the same content, so this merge changes no files (strategy
ours) and only keeps the ancestry in step for when TheTom#403 and TheTom#413 land.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
jasstrong pushed a commit to jasstrong/llama-cpp-turboquant that referenced this pull request Nov 11, 2026
TheTom#403/TheTom#413 add the GGML_TQ_MMQ row directly after GGML_TQ_NATIVE, where
this row also sat, so whichever PR merged second would conflict on
AGENTS.md. Moving it a few rows down keeps the two independent.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@superbudvar

Copy link
Copy Markdown

RDNA4 (gfx1200)

Ran the permute commit on an RX 9060 XT, which is the one architecture this PR says it could not cover. ROCm 7.1, wave32, llama-bench -fa 1, GGML_TQ_MMQ=1 GGML_TQ_NATIVE=1, Qwen3.8-27B-TQ3_1S (dense), 5 reps per case.

"fallback" is the default path with GGML_TQ_MMQ unset, "#403 loader" is a4d0bcb12 and "byte permute" is 0bb36b9c1. Both were built from the same base and measured back to back on the same machine.

Qwen3.8-27B-TQ3_1S (dense), RX 9060 XT (gfx1200, RDNA4)

fallback #403 loader byte permute
pp512 66.99 ± 0.93 314.31 ± 24.65 297.25 ± 38.77
pp2048 71.51 ± 0.11 448.60 ± 3.35 493.74 ± 9.40
tg64 9.75 ± 0.09 9.80 ± 0.04 9.49 ± 0.20

pp2048 is the stable case here and the permute is +10.1% over the #403 loader, with the two intervals not overlapping. pp512 and tg64 are inside their own spreads and carry no signal either way: pp512 is a 512-token window on a 27B model and its spread is ±8-13% on both loaders. Decode is unchanged, as expected, since the loader is not on the decode path.

So on RDNA4 the permute is worth about 10% of prefill. That is above the 2-5% you measured on RDNA2 and RDNA3 and below the 31% you measured on CDNA, which fits the pattern of the loader being a smaller share of the kernel on a consumer card.

Correctness on RDNA4

The permute commit on gfx1200, with GGML_TQ_MMQ=1 GGML_TQ_NATIVE=1:

  • test-quantize-fns -v: all three checks ok - cuda: add a native int8 MMQ path for TQ3_1S and enable the TQ MMQ path on RDNA4 #403's index-extraction and centroid-table checks plus your byte-permute check.
  • test-backend-ops -o MUL_MAT -p type_a=tq3_1s: 158/158 on ROCm0. The 120 cases the sweep reports as "not supported" are the type_b=f16 ones, the same 120 the shipped TQ4_1S skips.
  • test-backend-ops -o MUL_MAT_ID -p type_a=tq3_1s: 44/44 on ROCm0, none skipped.

The same two sweeps with GGML_TQ_MMQ unset, so the default path, also pass 158/158 and 44/44.

So RDNA4 now has the permute covered by the same gates as the three cards you ran, on the arch where the loader is the one #403 wrote.

@superbudvar

Copy link
Copy Markdown

Follow-up: the RDNA4 measurement this was waiting for

I see the branch now keeps the table-load form on RDNA4 (#if defined(RDNA4)) until the permute form has been measured there. The numbers in the comment above are that measurement: on an RX 9060 XT the permute form is +10.1% on pp2048 (448.60 ± 3.35 -> 493.74 ± 9.40 tok/s), and neutral on pp512 and tg64.

So the guard has nothing to protect against on RDNA4. Dropping it gains about 10% of prefill there, the same direction as the 2-5% you measured on RDNA2 and RDNA3.

For the record, which head ran which loader:

head RDNA4 loader
"#403 loader" a4d0bcb12 four table loads
"byte permute" 0bb36b9c1 one v_perm_b32 over TQ3_KV_LO/HI

The correctness gates in the comment above were run on 0bb36b9c1, so they cover the loader RDNA4 would run with the guard dropped:

  • test-backend-ops -o MUL_MAT -p type_a=tq3_1s: 158/158 on ROCm0
  • test-backend-ops -o MUL_MAT_ID -p type_a=tq3_1s: 44/44 on ROCm0
  • test-quantize-fns -v: all three checks ok

One thing worth flagging while you are in there: RDNA4 is defined in ggml/src/ggml-cuda/vendors/hip.h as #if defined(__GFX12__), so the guard is live on gfx1200 and the permute form is what the #else branch would have used. With the guard dropped there is nothing arch-specific left in the loader.

@jasstrong jasstrong changed the title cuda: TQ3_1S MMQ prefill on CDNA, RDNA2 and RDNA3, with a byte-permute unpack (stacked on #403) cuda: TQ3_1S MMQ prefill on CDNA, RDNA2, RDNA3 and RDNA4, with a byte-permute unpack, on by default (stacked on #403) Nov 12, 2026
@jasstrong

Copy link
Copy Markdown
Author

Thanks @superbudvar, that's the run this was waiting for. The guard is gone in 6ff5cb0, so RDNA4 now runs the loader you measured (0bb36b9c1's code, unchanged), the same one as every other architecture. I've added your gfx1200 numbers to the description.

@TheTom, the MMQ default for TQ3_1S is in this PR after all, as its own commit (bf47d54) so it can be reverted alone. GGML_TQ_MMQ unset is on for TQ3_1S and off for TQ4_1S; 0 is off for both.

jasstrong pushed a commit to jasstrong/llama-cpp-turboquant that referenced this pull request Nov 12, 2026
…kernels

Conflicts: the GGML_TQ_MMQ AGENTS.md row and the two TQ MMQ dispatch sites
in ggml-cuda.cu. TQ3_4S keeps its dispatch, and stays opt-in: with
GGML_TQ_MMQ unset only TQ3_1S takes the MMQ path.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@superbudvar

Copy link
Copy Markdown

This branch sits on the old #403 head, so the diff shows #403's changes again inside it.

I rebased the six commits of this PR onto the new #403 head (d8c874dc4) and the result is content-identical to the current head: the diff over the non-comment lines is empty. The only difference is comment wording, where the new #403's text is kept.

The rebased branch is on my fork, so it can be pushed as-is:

git fetch https://github.com/superbudvar/llama-cpp-turboquant.git pr/tq3_1s-mmq-amd
git push origin FETCH_HEAD:pr/tq3_1s-mmq-amd --force-with-lease

head: b413cf020

Once #403 merges, this branch's diff collapses to its own changes with no further action.

The rebase picks up #414 (src1 column count in the TQ fused and cuBLAS paths), #412 (null src[0] in the graph compat check) and #406 (conditional subgroup size for the TurboQuant SET_ROWS on wave64-only GPUs), plus turbo5/turbo6 KV types and the Spark2_5 model.

test-quantize-fns -v passes with 0 failures on the rebased head, including the byte-permute check.

superbudvar added a commit to superbudvar/llama-cpp-turboquant that referenced this pull request Nov 21, 2026
jasstrong pushed a commit to jasstrong/llama-cpp-turboquant that referenced this pull request Nov 21, 2026
…8c874d)

No content change: this branch already had 4185b90 and the RDNA4
config without the stray blank line. The merge records TheTom#403's rebased
head in this branch's history, so its diff drops TheTom#403's changes once
TheTom#403 merges.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jasstrong

Copy link
Copy Markdown
Author

Thanks @superbudvar, and thanks for checking the range-diffs. I keep these branches merge-only, so instead of force-pushing the rebased copies I merged your new #403 head (d8c874dc4) into this branch (1e0016c0d). That also brings it onto 4185b90a0, so once #403 merges this diff shrinks to its own changes, same as with the rebase. #416 has the same merge (86461ad26, no content change). #415 and #417 don't contain #403 and are mergeable as they are, so I left them.

Your rebase also showed that an earlier merge here had brought back the blank line in the RDNA4 Q3_K block that Tom asked you to drop. e43ec1b8a removes it again.

The comment wording stays as it is on this branch, not #403's. This PR makes the TQ3_1S MMQ loader the default, so test-backend-ops -o MUL_MAT -p type_a=tq3_1s covers it without GGML_TQ_MMQ=1. On #403 alone the env var is still needed, so your wording is right there.

Rerun on the new head on MI210, 7900 XTX and V620: test-quantize-fns ok, tq3_1s MUL_MAT 165/165 and MUL_MAT_ID 44/44 with GGML_TQ_MMQ unset, 0 and 1, tq4_1s native 156/156 and 44/44. The MUL_MAT counts are up from 158 and 149 because the new base has #414's cases.

@TheTom

TheTom commented Nov 22, 2026

Copy link
Copy Markdown
Owner

Thanks @jasstrong. I went through the six review threads against e43ec1b and all of them are done. #403 merged today.

GB10 (sm_121, CUDA 13), run at the #416 head, which contains this branch, merged onto the current base: no warnings from these files. MUL_MAT -p type_a=tq3_1s passes 183/183 under default, GGML_TQ_NATIVE=1, GGML_TQ_MMQ=0 and GGML_TQ_MMQ=1, and MUL_MAT_ID passes 44/44. The MMQ path is AMD only, so the default flip rests on your three cards and @superbudvar's gfx1200 run.

Left before I merge:

@superbudvar

Copy link
Copy Markdown

#403 has merged, so this branch's base moved again (the fork tip is now 50b4aa28b, which includes the merged #403).

Two things worth noting:

  1. The head here (e43ec1b8a) is code-identical to the rebased branch I put on my fork earlier (b413cf020) - I diffed the seven files and the non-comment lines are identical. So the two rebase routes (yours as a merge of cuda: add a native int8 MMQ path for TQ3_1S and enable the TQ MMQ path on RDNA4 #403's rebased head, mine as a linear replay) landed on the same code.
  2. Against the new base all four stacked PRs now conflict in AGENTS.md only, because docs: rewrite README around the fork, align AGENTS.md with cherry-pick policy #371 rewrote that file.

I have rebased all four onto 50b4aa28b; each merges cleanly into the base and is code-identical to the current head:

git fetch https://github.com/superbudvar/llama-cpp-turboquant.git pr/tq3_1s-mmq-amd
git push origin FETCH_HEAD:pr/tq3_1s-mmq-amd --force-with-lease

head: b42c60dd5 (this PR) - pr/tq-dp4a-decode 940d1cd2d (#415) - pr/tq3_4s-type 8240a50e4 (#418) - pr/tq3_4s f64e86ac84 (#416)

The only thing I had to resolve by hand in each case was the AGENTS.md knob table, where the new base's rewrite dropped the GGML_TQ_DP4A row and the TQ3_4S row, and where the GGML_TQ_MMQ wording needed the "for both types" and "Unset: on for TQ3_1S" clauses restored.

jas and others added 6 commits November 21, 2026 23:14
The native TQ3_1S MMQ path had a config entry only for RDNA4, so on CDNA,
RDNA2 and RDNA3 GGML_TQ_MMQ=1 still fell back to dequant + BLAS for TQ3_1S.
Give it the TQ4_1S entries of each table: both types are 32-value blocks with
a per-16 scale pair, so they share the Q3_K tile geometry and SRAM layout, as
the RDNA4 entries already assume.

This makes all three loader branches reachable: MFMA on CDNA, WMMA on RDNA3,
and the dp4a tile layout on RDNA2, which had no config entry before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The TQ3_1S MMQ tile loader expanded each quad of 3-bit indices with four
kvalues_tq3[] table loads. The 8-entry int8 table is exactly 8 bytes, which is
the source window of a byte permute (v_perm_b32 on AMD, prmt on NVIDIA), so the
four loads become one instruction once the indices are placed in selector
fields. The index extraction is unchanged; the table is held as two constants.

tests/test-quantize-fns.cpp checks both constants and both selector packings
against kvalues_tq3 for all 4096 quads of indices, by emulating the two
instructions on the host.

Prefill with GGML_TQ_MMQ=1, Qwen3.8-27B-TQ3_1S (dense), llama-bench pp512, -fa 1,
ROCm 7.2.3. "fallback" is the default dequant + BLAS path.

  card                    fallback   4 table loads   byte permute
  MI210 (gfx90a)             487.9           453.9          637.3
  RX 7900 XTX (gfx1100)      577.5           757.0          794.6
  PRO V620 (gfx1030)         271.4           445.4          458.4

On CDNA the table loads made the MMQ path slower than the fallback; with the
permute it is 31% faster. Perplexity is unchanged to the last digit, as the
int8 values are the same (wikitext-2, 16 x 512: 7.6605, 7.6677, 7.6642).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
tq3_1s_qs_to_int8x4 shifted the packed group by 3*sub + 3*j for each of the
four indices. Shifting once by 3*sub and taking the fields at fixed offsets
gives the same indices and is cheaper in the tile loader's inner loop.

MI210 (gfx90a), ROCm 7.2.3, test-backend-ops perf MUL_MAT tq3_1s m=4096 n=512
k=14336: 1647 -> 1511 us. Qwen3.8-27B-TQ3_1S pp512: 633 -> 684 t/s. The test
mirror of the index extraction in test-quantize-fns takes the same form.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review follow-up. The byte permute and the single shift change the loader
RDNA4 runs, and neither has been measured on an RDNA4 card, so RDNA4 keeps
the table-load form from TheTom#403 until it has been. The CDNA, RDNA2 and RDNA3
code is unchanged.

The loader comment now names test-backend-ops under GGML_TQ_MMQ=1 as the
gate: the test-quantize-fns checks compare typed-in copies, so they are
one-time derivation checks and say so. The AGENTS.md GGML_TQ_MMQ row lists
the TQ3_1S architectures added in this PR.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
superbudvar measured the permute form on an RX 9060 XT (gfx1200): pp2048
448.6 -> 493.7 t/s, pp512 and tg64 within noise, and test-backend-ops
MUL_MAT 158/158 and MUL_MAT_ID 44/44 with GGML_TQ_MMQ=1. Drop the RDNA4
table-load branch so every architecture runs the same loader.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
TQ3_1S is never converted at load, so the native MMQ path is reached
whenever the device has a TQ MMQ config (CDNA, RDNA2, RDNA3, RDNA4), and
there it beats the dequant + cuBLAS fallback at equal PPL. GGML_TQ_MMQ
unset now means on for TQ3_1S and off for TQ4_1S, which also needs
GGML_TQ_NATIVE; 0 turns it off for both and any other value on for both.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jasstrong

Copy link
Copy Markdown
Author

Thanks @TheTom. Rebased onto 50b4aa28b: six commits, no copies of #403, and the code is the same as e43ec1b. The GGML_TQ_MMQ rows in AGENTS.md and docs/KV-cache-quantization.md now describe the default (unset: on for TQ3_1S; 0 turns it off). The description no longer mentions 31f7d95, and it says the default also reaches CDNA1, CDNA3 and CDNA4 through the shared table, with only gfx90a measured.

@superbudvar, thanks for the rebased copies. I rebased these myself so the review changes could go in the same pass; apart from comment wording the code matches yours.

@TheTom
TheTom merged commit 43a4baf into TheTom:feature/turboquant-kv-cache Nov 22, 2026
TheTom added a commit to jasstrong/llama-cpp-turboquant that referenced this pull request Nov 23, 2026
No content change. The branch already holds TheTom#413, TheTom#415 and TheTom#418; this records their merge commits as parents so the pull request merges cleanly.
TheTom added a commit that referenced this pull request Nov 23, 2026
ggml: add TQ3_4S, 3.5 bpw TQ3 with 128-value super-blocks (stacked on #403, #413, #415)
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.

3 participants