Skip to content

cuda: PQ2_0 multi-column mat-vec on Hopper + its missing PDL wait (+10-14% MTP, +14-16% DFlash2 decode on H200) - #381

Merged
bri-prism merged 2 commits into
prismfrom
cuda/pq2-mc-hopper
Oct 24, 2026
Merged

bri-prism merged 2 commits into
prismfrom
cuda/pq2-mc-hopper

Conversation

@bri-prism

Copy link
Copy Markdown
Collaborator

Overview

mul_mat_vec_pq2_0_mc (mmvq-pq2_0.cuh) is the dedicated PQ2_0 mat-vec for 3–8 columns in plain 2D. Until now it was gated to Ada (cc == 890). On Hopper those widths, which cover MTP verify and DFlash verify at depth 7, fell back to the generic mmvq at a fraction of DRAM bandwidth. Under MTP depth 3, nsys on an H200 put the 4-column PQ2_0 mat-vec at 32% of GPU time. This PR enables the kernel on Hopper (cc == 900).

Enabling it required a fix. The kernel is launched through ggml_cuda_kernel_launch, which uses PDL on sm_90+, but the kernel never waited for the kernel that writes its q8 activations. Ada has no PDL, so the bug never showed there. On Hopper it read stale activations:

  • the model produced garbage from the first token;
  • test-backend-ops still passed (small graphs);
  • llama-eval-callback, which synchronizes after every node, was correct, which is what pointed to the race.

The kernel now waits before reading vy and triggers launch completion after the K loop, the same as mul_mat_vec_q. The restrict qualifiers move to locals, also as in mul_mat_vec_q.

Validation

All runs are on an H200 with CUDA 12.8 and a Release sm_90 build, at prism 73bfe6cc plus this patch.

Kernel (test-backend-ops perf):

shape n generic mmvq this kernel change
12288×4096 (ffn) 4 19.3 µs 10.2 µs −47%
4096×12288 (ffn) 4 16.7 µs 11.8 µs −29%
248320×4096 (vocab head) 4 332 µs 167 µs −50%

Across n = 3–8 the kernel is 24–50% faster. n = 1, 2, 12 and 16 are unchanged.

End to end: Ternary-Bonsai-2-9B PQ2_0-MTP-Q8_0 on llama-server, 4 prompts × 128 greedy tokens, 2 rounds. Decode tok/s, stock prism → this PR:

prompt MTP depth 3 DFlash2 depth 7
gen-lru 265.8 → 302.4 (+13.8%) 193.5 → 223.6 (+15.5%)
edit-cpp 361.5 → 403.7 (+11.7%) 394.3 → 448.0 (+13.6%)
explain-cpp 236.0 → 265.5 (+12.5%) 172.6 → 197.8 (+14.6%)
public-cuda 237.9 → 260.9 (+9.7%) 213.6 → 247.1 (+15.7%)

No speculation and DFlash2 depth 15 (16-column verify) are flat to slightly positive.

Combined with #380 (thin F32 mat-muls) and #379 (batched snapshot copies), MTP depth 3 reaches +17.6% to +22.8% and DFlash2 depth 7 +22% to +26% over stock prism.

Correctness

  • New eval cases at the 9B shapes (m = 4096 / 12288 / 248320, k = 4096 / 12288, n = 3 / 4 / 8) all pass, both with this kernel and with the generic path.
  • PQ2_0 MUL_MAT: 240/240.
  • Not bit-exact vs the generic kernel; it sums in a different order, as on Ada, where it is already the default. Greedy output is deterministic across runs and diverges late on 2 of 4 prompts (tokens 56–71).

Blackwell (sm_100/120) and GB10 are left as they are because I didn't measure them.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. Claude Code profiled the hot spot, found the missing PDL wait, wrote the change and tests, and ran the benchmarks; reviewed by me.

🤖 Generated with Claude Code

https://claude.ai/code/session_01PRQc3TQpn72qBxrUn5vKqo

@bri-prism

Copy link
Copy Markdown
Collaborator Author

Final check on this branch as pushed (H200, job 29566): test-backend-ops -b CUDA0 -o MUL_MAT 1645/1645 tests passed, 0 FAIL.

…PDL wait

mul_mat_vec_pq2_0_mc (3-8 columns, plain 2D) was Ada-only. On Hopper the
generic mmvq handles those widths at a fraction of DRAM bandwidth; the
dedicated kernel is 24-50% faster there (H200, 9B shapes: 12288x4096 n=4
19.3 -> 10.2 us, vocab head 248320x4096 n=4 332 -> 167 us).

The kernel is launched through ggml_cuda_kernel_launch, which uses PDL on
sm_90+, but never waited for the kernel that wrote its activations. On Ada
there is no PDL so this never showed; on Hopper it read stale q8 data and the
model produced garbage from the first token while test-backend-ops (small
graphs) passed. Wait before reading vy and trigger launch completion after the
K loop, as mul_mat_vec_q does; restrict-qualified locals instead of
parameters, as in mul_mat_vec_q.

H200, Ternary-Bonsai-2-9B PQ2_0-MTP, llama-server, 4 prompts x 128 greedy:
MTP depth 3 +9.5..+13.2% decode, DFlash2 depth 7 +17..+20%, depth 15
-1..+5%, no speculation +-0. Not bit-exact vs the generic kernel (same as on
Ada); greedy output is deterministic and diverges late on 2/4 prompts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PRQc3TQpn72qBxrUn5vKqo
# Conflicts:
#	tests/test-backend-ops.cpp
@bri-prism
bri-prism merged commit 0d74df5 into prism Oct 24, 2026
7 checks passed
bri-prism added a commit that referenced this pull request Oct 25, 2026
… bias and gate+GLU fusion

The 3-8 column PQ2_0 kernel (#381) needs no per-weight decode: the activation
layout is permuted so the raw 2-bit codes pair with it. On H200 it is also the faster
1-column kernel for the larger projections (2.3-2.9 TB/s against 1.7-2.2 for the generic
tall-row path). Plain decode calls the 1-column mat-vec with fused bias (residual) and
fused gate+up+GLU, so the kernel gets the same epilogue as mul_mat_vec_q: x_bias added
to the result, and with a gate a second weight matrix dotted against the same
activations, gate_bias added, then the GLU op. Hopper takes 1-8 columns; elsewhere it
stays at 3-8. GGML_CUDA_PQ2_MC_MIN overrides the lower bound.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant