Repository navigation
cuda: TQ3_1S MMQ prefill on CDNA, RDNA2, RDNA3 and RDNA4, with a byte-permute unpack, on by default (stacked on #403) - #413
Conversation
TheTom
left a comment
There was a problem hiding this comment.
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.
| | `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 | |
There was a problem hiding this comment.
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.
| // 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 |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
| // 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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
|
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. |
|
Thanks. Done in b525884:
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? Run it once at this head and once with the |
…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]>
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]>
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]>
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, "fallback" is the default path with Qwen3.8-27B-TQ3_1S (dense), RX 9060 XT (gfx1200, RDNA4)
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 RDNA4The permute commit on gfx1200, with
The same two sweeps with 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. |
Follow-up: the RDNA4 measurement this was waiting forI see the branch now keeps the table-load form on RDNA4 ( 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:
The correctness gates in the comment above were run on
One thing worth flagging while you are in there: |
|
Thanks @superbudvar, that's the run this was waiting for. The guard is gone in 6ff5cb0, so RDNA4 now runs the loader you measured ( @TheTom, the MMQ default for TQ3_1S is in this PR after all, as its own commit (bf47d54) so it can be reverted alone. |
…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]>
|
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 ( The rebased branch is on my fork, so it can be pushed as-is: head: 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
|
…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]>
|
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 ( 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. The comment wording stays as it is on this branch, not #403's. This PR makes the TQ3_1S MMQ loader the default, so Rerun on the new head on MI210, 7900 XTX and V620: |
|
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. Left before I merge:
|
|
#403 has merged, so this branch's base moved again (the fork tip is now Two things worth noting:
I have rebased all four onto head: The only thing I had to resolve by hand in each case was the |
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]>
e43ec1b to
83753c8
Compare
|
Thanks @TheTom. Rebased onto @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. |
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.
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:
v_perm_b32(prmton NVIDIA). So the fourkvalues_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-fnsgets 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 againstkvalues_tq3for 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 istest-backend-ops -o MUL_MAT|MUL_MAT_ID -p type_a=tq3_1s.test-backend-ops perf, m=4096 n=512 k=14336).GGML_TQ_MMQrow updated.GGML_TQ_MMQ. Unset now means on for TQ3_1S and off for TQ4_1S, which still needsGGML_TQ_NATIVE=1.GGML_TQ_MMQ=0turns it off for both, and1on for both. On the 4096 x 14336, n = 512test-backend-ops perfcase 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 againstGGML_TQ_MMQ=0; all op tests above pass with the variable unset,0and1. TheGGML_TQ_MMQrows in AGENTS.md anddocs/KV-cache-quantization.mddescribe 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 nowGGML_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)
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:
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, andtest-quantize-fns -vprintsokfor all three checks.Qwen3.6-35B-A3B-TQ3_1S (MoE, MUL_MAT_ID)
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:
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 printok.test-backend-ops -o MUL_MAT|MUL_MAT_ID -p type_a=tq3_1s, both loaders, withGGML_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 withGGML_TQ_MMQunset,0and1(the count went up with the new base's cases).test-backend-ops -b ROCm0on commit 1 withGGML_TQ_MMQ=1 GGML_TQ_NATIVE=1: MI210 19239/19244, 7900 XTX 19242/19244, V620 19241/19244. Every failure is one of two kinds: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 fromGGML_TQ_NATIVE=1, not from this branch. On base (6439568) and on this branch it passes 8/8 by default and fails 7/8 withGGML_TQ_NATIVE=1, with or withoutGGML_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
__byte_permwith 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.🤖 Generated with Claude Code