Skip to content

cuda: PQ2_0 J=128 prefill kernel numerically identical to MMQ, +5-16% prefill on H200, default on for Hopper - #374

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

bri-prism merged 2 commits into
prismfrom
cuda/pq2-j128-prefill

Conversation

@bri-prism

@bri-prism bri-prism commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

What

  • Add ggml/src/ggml-cuda/mmq-pq2-j128.cuh: a J=128 PQ2_0 prefill mat-mul for NVIDIA sm_90+ that replaces mul_mat_q<GGML_TYPE_PQ2_0, 128, false> and its stream-k fixup. It is numerically identical to the MMQ path; bit-identical except the sign of zero at extreme underflow (proof in comment).
  • launch_mul_mat_q takes it for plain 2D PQ2_0 mat-muls (no ids, channels or samples) when the J=128 config is the stream-k one. Everything else is unchanged, including all pre-sm_90 devices, MUL_MAT_ID, every other type and every batch under J=128 (decode, MTP/DFlash verify).
  • Default on for compute capability 9.x (Hopper). GGML_CUDA_PQ2_J128=0 turns it off; =1 turns it on for any NVIDIA sm_90+ device (Blackwell/GB10 are opt-in because they are untested here).

Why

PQ2_0 prefill on H200 runs the generic MMQ kernel at 8 warps per SM. The Yukon cuda.fast Bonsai engine (MIT, licenses/yukon-cudafast) has a J=128 PQ2_0 kernel that keeps the old kernel's work partition and FP32 accumulation order but runs 16 warps per SM. On H200 it gives +5 to +16% prefill on every PQ2_0 size, with identical outputs. It also makes image-token prefill on the VLM trunk faster (27B, 4096 image tokens: 1437 ms to 1288 ms).

Outputs are numerically identical to the MMQ path; bit-identical except the sign of zero at extreme underflow (proof in comment). They were also checked bitwise on 25 shapes, by KLD = 0 and by greedy tokens on 4 sizes, so there is no accuracy trade-off. That is why it is on by default where it was measured. It is not on by default on Blackwell/GB10: there is no hardware here to time it.

How

  • mmq-pq2-j128.cuh carries the load. It is adapted from the source kernel at 2c62b6c. I re-diffed it against the engine's latest commit (759629b). The only later kernel changes are a TMA-fed D1 consumer that reads a persistent plane copy of the weights, and an F16 GLU store. Neither is numerically identical to the MMQ path or applicable without persistent weight copies, so neither is taken.
    • Same work partition. CTA (b, q) runs every K segment that block b of the old kernel runs: the same kbc arithmetic, with segment bounds aligned to the old 256-value K iteration. It covers row half q (64 rows) of each 128×128 tile. The grid rule is the one launch_mul_mat_q uses on NVIDIA. Stream-k tiles go through the unchanged mul_mat_q_stream_k_fixup.
    • Same accumulation. Per output and 32-value sub-block, in increasing K order: sum = fma(RN(idot*dA), dB, sum). The mma accumulator starts at 1.5·2^23, so a 2-FFMA epilogue gives exactly RN(idot*dA) (the comment block at the top of the file has the argument).
    • What makes it faster. Weights are unpacked once per CTA into a shared int8 tile. Activations are double-buffered: cp.async for pure tiling, and for stream-k one TMA bulk copy per K block on an mbarrier, plus transposed D4 scales.
  • Port changes:
    • ordinary <<<>>> launches (no programmatic stream serialization prologue);
    • no D1 activations, fused GLU or plane-copy launches, so no persistent weight copies;
    • device code guarded to __CUDA_ARCH__ >= 900.
      The unused GLU/ADD/D1 template paths are kept so that the file stays diffable with the source; only <PURE, ADD=false, GLU=false, COMPACT> is instantiated.
  • mmq.cuh: the dispatch in launch_mul_mat_q checks NVIDIA, cc ≥ 9.0, a compiled arch ≥ 9.0, warp size 32, config I == 128 with stream-k, and ggml_cuda_mmq_pq2_j128_supported (2D, nrows % 128, ncols % 256, 16-byte-aligned y, 32-bit offsets, and the old path's ntiles*nkb < 2^30 bound on the stream-k index; shapes beyond it take the existing path and its assert). It logs once at trace level.

Test plan

All runs: H200, CUDA 12.8, Release sm_90, prism 73bfe6cc (current prism) with and without this branch, Slurm single-GPU jobs. "off" = GGML_CUDA_PQ2_J128=0 or the env unset before the default flip (same code path as tip).

Exactness

  • MUL_MAT outputs, bitwise. A standalone ggml program runs 25 PQ2_0 × F32 shapes and cmps the F32 outputs against tip. The shapes are model-like plus odd N, covering pure tiling and stream-k with fixup: M 128 to 17408, K 256 to 17408, N 128 to 8192.
    • Identical with the default (on), with =1 and with =0. The J=128 kernel ran in the on runs (log line).
    • Q2_0 and PTQ1_0 outputs are identical to tip too.
  • KLD vs tip. 9B, wiki.test.raw, -c 4096 -ub 4096: mean KLD 0.000000, same top-1 100.000%. The max of 5e-5 is the base file's uint16 logit storage; an exact rerun of the same binary gives the same max.
  • Greedy tokens vs tip, through llama-server. 6 short prompts × 128 tokens plus a 3000- and a 32000-token prompt × 64 tokens:
model cases identical tokens max top-1 logprob delta
2B PQ2_0 8/8 896 0
4B PQ2_0 8/8 896 0
9B PQ2_0 8/8 896 0
27B PQ2_0 8/8 896 0
9B PTQ1_0 (untouched type) 8/8 896 0
9B Q2_0 (untouched type) 8/8 896 0
  • test-backend-ops:
    • MUL_MAT 1630/1630 and MUL_MAT_ID 1035/1035 on tip, off and on, with identical (empty) failure sets.
    • On the final head with the env unset: MUL_MAT 1630/1630 (the J=128 log line is present) and MUL_MAT_ID 1035/1035 (it never takes this path).
    • After the stream-k index guard (343bd08), env unset: MUL_MAT 1630/1630, the same (empty) failure set as tip, J=128 log line present. 9B pp512/pp4096 with J=128 on vs the previous head, ABBA 4 pairs × 5 reps: median ratio 1.002 / 1.000.
  • The kernel ran.
    • nsys, 9B llama-bench -p 512,4096: mul_mat_q_pq2_j128 has 3077 + 272 launches and 50.5% of GPU time, and no mul_mat_q<PQ2_0, 128> remains.
    • With -lv 4, llama-perplexity logs the kernel for 2B/4B/9B/27B PQ2_0 and not for PTQ1_0 or Q2_0; llama-server logs it for 27B.

Speed (llama-bench -fa 1 -ub 512 -b 2048 -r 5, ABBA, 3 off/on pairs per size; t/s are the means of the 3 runs)

model test off on on/off per-pair range
2B-PQ2_0 pp128 12423.9 13087.9 1.053 1.035..1.063
2B-PQ2_0 pp256 17070.8 18070.0 1.059 1.055..1.061
2B-PQ2_0 pp512 19788.9 21283.6 1.076 1.070..1.079
2B-PQ2_0 pp1024 20154.6 21711.3 1.077 1.076..1.079
2B-PQ2_0 pp4096 21127.6 22830.6 1.081 1.078..1.083
4B-PQ2_0 pp128 7526.7 8287.8 1.101 1.098..1.105
4B-PQ2_0 pp256 10278.1 11142.7 1.084 1.083..1.086
4B-PQ2_0 pp512 11494.4 12479.5 1.086 1.083..1.089
4B-PQ2_0 pp1024 11588.8 12526.3 1.081 1.079..1.083
4B-PQ2_0 pp4096 11889.8 12947.1 1.089 1.087..1.092
9B-PQ2_0 pp128 5816.9 6459.7 1.111 1.107..1.115
9B-PQ2_0 pp256 7263.0 8212.0 1.131 1.129..1.133
9B-PQ2_0 pp512 8307.0 9273.7 1.116 1.115..1.119
9B-PQ2_0 pp1024 8458.2 9464.5 1.119 1.117..1.122
9B-PQ2_0 pp4096 8654.3 9695.2 1.120 1.119..1.121
27B-PQ2_0 pp128 2251.4 2575.4 1.144 1.132..1.151
27B-PQ2_0 pp256 2695.7 3090.3 1.146 1.144..1.148
27B-PQ2_0 pp512 2912.7 3293.7 1.131 1.130..1.131
27B-PQ2_0 pp1024 2923.3 3306.2 1.131 1.129..1.134
27B-PQ2_0 pp4096 2860.6 3314.4 1.159 1.130..1.221
  • -ub 2048, 9B: pp2048 8480 → 9432 (1.112), pp4096 8799 → 9820 (1.116).
  • VLM image-token prefill, 27B PQ2_0 trunk + Q8_0 mmproj, 2048² image (4096 image tokens), llama-mtmd-cli (decoded (batch) times summed):
    • off: 1429 / 1432 / 1450 ms;
    • on: 1276 / 1268 / 1319 ms;
    • mean −10.4%.
    • The 9B runs on the same job were disturbed by co-located load (the vision-encoder times varied 1.8 to 4.2 s), so I am not quoting them.
  • Decode is unchanged: tg128 per-pair minimum is 0.999 to 1.002 on every size. The larger means come from one disturbed off run on a shared node.
  • Untouched types: 9B PTQ1_0 and Q2_0 are 0.996 to 1.006 in undisturbed pairs. Two pairs were hit by co-located load: PTQ1_0 pp256/pp512 (1.4 to 2.5×) and Q2_0 pp4096/tg128 (0.86 / 0.92).
  • Fallback = tip: the same binary with the kernel off vs the tip binary, 9B, is 0.999 to 1.002 on all pp sizes.

Build

  • sm_90 Release.
  • Compile-only ggml-cuda for 75;86;89;120a at this head: rc=0, so the sm_90+ guards hold. Repeated at 343bd08: rc=0.

Not verified

  • Blackwell (sm_100/sm_120) and GB10: no hardware here. That is why the default stays off there.
  • Windows and HIP/MUSA: the code is excluded there.

…t on for Hopper

Add mmq-pq2-j128.cuh, adapted (MIT, see licenses/yukon-cudafast) from the
Yukon cuda.fast Bonsai engine's J=128 PQ2_0 prefill matmul. It replaces
mul_mat_q<PQ2_0, 128, false> and its stream-k fixup with the same work
partition and the same FP32 accumulation order, so results are bit-identical,
but runs two 64-row CTAs of 8 warps per SM, unpacks the weights once per CTA
into shared int8 tiles and feeds the stream-k activations through TMA bulk
copies.

The device code is compiled for sm_90+ only. launch_mul_mat_q takes it on
NVIDIA sm_90+ when the J=128 config is the stream-k one and the shape is a
plain 2D mat-mul (no ids, channels or samples); everything else, including all
pre-sm_90 devices, keeps the existing kernel. GGML_CUDA_PQ2_J128=0/1 overrides
the default, which is on for compute capability 9.x only.
@bri-prism

Copy link
Copy Markdown
Collaborator Author

Machine-checked evidence (Z3/Bitwuzla plus exhaustive enumeration), with a correction to the "bit-exact" wording.

Integer side, proved for all inputs:

  • The new weight-word extraction and the shared 2-bit codec reproduce the old loader's int8 codes exactly.
  • Each 32-value int32 dot product lies in [−8192, 8128], so there is no overflow.
  • The 0x4B400000 accumulator trick yields exactly 12582912 + idot.

Float side:

  • The per-sub-block product fma(D, dA, −12582912·dA) equals the old FMUL(I2F(idot), dA) for every finite fp16 dA. This was checked exhaustively over 1.04e9 (dA, idot) pairs. Results differ only in the sign of a zero, when dA ≤ 0 or idot = 0.
  • So the output always compares equal (==) to the old MMQ path.
  • It is bit-identical under the shipped FTZ build whenever every activation sub-block scale is 0 or ≥ 2^-79 in magnitude. This is proved by induction over the accumulation, and a mutation test shows the bound is tight.
  • Outside that range a −0/+0 difference is reachable, for example a 1e-33 activation scale followed by an all-zero-scale weight block. No real model reaches it, but "bit-exact" should read "numerically identical; bit-identical except the sign of zero at extreme underflow".

Not SMT proofs:

  • The stream-k partition matches the old kernel in 3.4M enumerated shape/grid cases.
  • Summation order and the FFMA contraction rest on code structure and SASS observation (CUDA 12.8, sm_90).

One gap: kbc = (int)(b*W/nb_old) can overflow int32 for supported-but-unrealistic shapes (e.g. K=256, 31.5M rows, 557k tokens), because the new path returns before the old ntiles*nkb < 2^30 assert (mmq.cuh). The same assert should be added to the J128 path; a follow-up commit will do that.

…ld overflow

The J=128 kernel computes kbc = b*ntiles*nkb/grid as int, but returned before
the ntiles_dst*blocks_per_ne00 < 2^30 assert in launch_mul_mat_q. Add the same
bound to ggml_cuda_mmq_pq2_j128_supported, so shapes beyond it take the
existing MMQ path (and its assert) instead of a wrong kbc.
@bri-prism bri-prism changed the title cuda: bit-exact PQ2_0 J=128 prefill kernel, +5-16% prefill on H200, default on for Hopper cuda: PQ2_0 J=128 prefill kernel numerically identical to MMQ, +5-16% prefill on H200, default on for Hopper Oct 23, 2026
@bri-prism
bri-prism merged commit 5388909 into prism Oct 24, 2026
6 checks passed
bri-prism added a commit that referenced this pull request Oct 24, 2026
…/MUSA build after #374) (#385)

The header uses inline PTX and the CUDA shared-memory carveout attribute.
Its only call site in launch_mul_mat_q is already guarded for HIP/MUSA.
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