Skip to content

cuda: compute every src1 column in the TQ fused and cuBLAS paths (multi-sequence ubatches on hybrid models) - #414

Merged
TheTom merged 1 commit into
TheTom:feature/turboquant-kv-cachefrom
jasstrong:pr/tq-fused-batched-src1
Nov 11, 2026
Merged

TheTom merged 1 commit into
TheTom:feature/turboquant-kv-cachefrom
jasstrong:pr/tq-fused-batched-src1

Conversation

@jasstrong

Copy link
Copy Markdown

What

On hybrid GDN models (qwen35, qwen35moe) with TQ weights, every sequence but the first in a multi-sequence ubatch gets garbage output.

The fused TQ decode path (ggml_cuda_mul_mat_tq) and the TQ4_1S cuBLAS path take their column count from src1->ne[1] only, and the dispatch checks only ne11 <= MMVQ_MAX_BATCH_SIZE. A hybrid model's GDN output projection (ssm_out) multiplies a 2D weight by [K, n_seq_tokens, n_seqs]. With several sequences in one ubatch, only the first sequence's columns are computed, and the rest of dst keeps whatever the buffer held before.

Several sequences per ubatch is what llama-perplexity does by default (n_batch / n_ctx sequences per batch). It's also what a server with parallel slots does. Instrumenting the dispatch on Qwen3.8-27B at -b 2048 -ub 1 shows the shape directly:

blk.N.ssm_out.weight   src1 ne=[6144,1,2,1]  dst ne=[5120,1,2,1]   <- 1 column computed, 2 needed
blk.N.ffn_up.weight    src1 ne=[5120,2,1,1]  dst ne=[17408,2,1,1]  <- fine

For contiguous src1 and dst against a 2D weight, the batch dims fold into the column count exactly. So this PR counts every src1 row (ggml_nrows) in both paths and in the dispatch. A batched (3D/4D) weight is not one matrix, so it now takes the stride-aware cuBLAS fallback instead of the TQ fast paths.

Results

llama-perplexity -c 512 --chunks 3, per-chunk values. -b 2048 puts 4 sequences in each batch; -b 512 puts one.

model, card build -b 2048 -ub 1 -b 2048 -ub 8 -b 512 -ub 8 (reference)
Qwen3.8-27B-TQ3_1S, MI210 base 4.54 / 1780.6 / 256.0 4.54 / 1780.6 / 11021.3 4.54 / 6.34 / 5.97
this PR 4.54 / 6.34 / 5.97 4.54 / 6.33 / 5.97 4.54 / 6.34 / 5.97
Qwen3.8-27B-TQ3_1S, RX 7900 XTX base 4.55 / 1780.8 / 256.0 4.55 / 1780.9 / 11023.0 4.54 / 6.34 / 5.97
this PR 4.55 / 6.34 / 5.97 4.55 / 6.34 / 5.97 4.54 / 6.34 / 5.97
Qwen3.6-35B-A3B-TQ3_1S, PRO V620 base 4.53 / 2110.8 / 275.3 4.52 / 2108.6 / 15677.7 4.51 / 6.58 / 5.88
this PR 4.53 / 6.59 / 5.88 4.52 / 6.58 / 5.87 4.51 / 6.58 / 5.88

The same with TQ4_1S weights (Qwen3.8-27B-TQ4_1S, GGML_TQ_NATIVE=1), where the fused path and the TQ4_1S cuBLAS path both apply:

card build -b 2048 -ub 1 -b 2048 -ub 8 -b 512 -ub 8 (reference)
MI210 base 4.34 / 1723.2 / 257.5 4.35 / 1724.1 / 10633.3 4.34 / 6.14 / 6.02
this PR 4.35 / 6.14 / 6.01 4.35 / 6.15 / 6.02 4.34 / 6.14 / 6.02
RX 7900 XTX base 4.35 / 1724.3 / 257.5 4.35 / 1724.1 / 10639.2 4.35 / 6.14 / 6.01
this PR 4.35 / 6.14 / 6.01 4.35 / 6.14 / 6.01 4.35 / 6.14 / 6.01

Tests

test-backend-ops gets TQ3_1S and TQ4_1S MUL_MAT cases with src1 batch dims (nr = {2,1}, {3,1}, {2,2}, n = 1, 3, 8), on both sides of the fused-decode limit.

On all three cards (MI210 gfx90a, RX 7900 XTX gfx1100, PRO V620 gfx1030; ROCm 7.2.3), -o MUL_MAT, with GGML_TQ_NATIVE=1 so TQ4_1S stays TQ4_1S on the device:

tq3_1s tq4_1s
this PR 165/165 156/156
the new tests against the base kernels 158/165 (7 of the new cases fail) 149/156 (7 of the new cases fail)

Limits of this testing

  • AMD only. The change is host-side dispatch plus two column counts, so it's the same on CUDA, but no NVIDIA build was run.
  • On NVIDIA the fused TQ3_1S path is off, so there it only affects TQ4_1S. The TQ4_1S cuBLAS path had the same ne[1]-only count and is fixed here too.

🤖 Generated with Claude Code

The fused TQ decode path (ggml_cuda_mul_mat_tq) and the TQ4_1S cuBLAS path took
their column count from src1->ne[1] alone, and the dispatch checked only ne11
against MMVQ_MAX_BATCH_SIZE. A hybrid model's GDN output projection (ssm_out in
qwen35 / qwen35moe) multiplies a 2D weight by [K, n_seq_tokens, n_seqs]. With
several sequences in one ubatch, which is what llama-perplexity does by default
and what a server with parallel slots does, only the first sequence's columns were
computed and the rest of dst was left as whatever the buffer held.

On Qwen3.8-27B-TQ3_1S, llama-perplexity -c 512 -b 2048 gave [1]4.54 [2]1778.7
[3]256.0 at -ub 1: the first sequence right, the others garbage. With -b 512 (one
sequence per batch) it was [1]4.54 [2]6.33 [3]5.97.

For contiguous src1 and dst against a 2D weight the batch dims fold into the
column count exactly, so count every src1 row (ggml_nrows) in both paths and in
the dispatch. A batched (3D/4D) weight is not a single matrix; it now takes the
stride-aware cuBLAS fallback instead of the TQ fast paths.

test-backend-ops gets TQ3_1S / TQ4_1S MUL_MAT cases with src1 batch dims
(nr = {2,1}, {3,1}, {2,2}) on both sides of the fused decode limit.

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

TheTom commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Thanks @jasstrong, merged. The per-chunk perplexity tables on three AMD cards made the bug obvious. The new MUL_MAT cases also fail on base kernels and pass with this PR on a GB10.

jasstrong pushed a commit to jasstrong/llama-cpp-turboquant that referenced this pull request Nov 11, 2026
… into the TQ3_4S stack

One conflict, in ggml_cuda_graph_check_compatibility: TheTom#412 moved is_tq_w
inside the MUL_MAT_ID branch so source-less nodes are not dereferenced, and
this branch adds TQ3_4S to that test. Resolved as TheTom#412's placement with
TQ3_4S added. TheTom#414's tq_ncols change to the fused TQ condition merged
cleanly with this branch's TQ3_4S additions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

2 participants