Repository navigation
cuda: PQ2_0 J=128 prefill kernel numerically identical to MMQ, +5-16% prefill on H200, default on for Hopper - #374
Merged
Merged
Conversation
…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.
4 of 5 tasks
Collaborator
Author
|
Machine-checked evidence (Z3/Bitwuzla plus exhaustive enumeration), with a correction to the "bit-exact" wording. Integer side, proved for all inputs:
Float side:
Not SMT proofs:
One gap: |
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
ggml/src/ggml-cuda/mmq-pq2-j128.cuh: a J=128 PQ2_0 prefill mat-mul for NVIDIA sm_90+ that replacesmul_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_qtakes 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).GGML_CUDA_PQ2_J128=0turns it off;=1turns 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.cuhcarries 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.(b, q)runs every K segment that blockbof the old kernel runs: the samekbcarithmetic, with segment bounds aligned to the old 256-value K iteration. It covers row halfq(64 rows) of each 128×128 tile. The grid rule is the onelaunch_mul_mat_quses on NVIDIA. Stream-k tiles go through the unchangedmul_mat_q_stream_k_fixup.sum = fma(RN(idot*dA), dB, sum). The mma accumulator starts at 1.5·2^23, so a 2-FFMA epilogue gives exactlyRN(idot*dA)(the comment block at the top of the file has the argument).<<<>>>launches (no programmatic stream serialization prologue);__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 inlaunch_mul_mat_qchecks NVIDIA, cc ≥ 9.0, a compiled arch ≥ 9.0, warp size 32, configI == 128with stream-k, andggml_cuda_mmq_pq2_j128_supported(2D,nrows % 128,ncols % 256, 16-byte-aligned y, 32-bit offsets, and the old path'sntiles*nkb < 2^30bound 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(currentprism) with and without this branch, Slurm single-GPU jobs. "off" =GGML_CUDA_PQ2_J128=0or the env unset before the default flip (same code path as tip).Exactness
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.=1and with=0. The J=128 kernel ran in the on runs (log line).-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.llama-server. 6 short prompts × 128 tokens plus a 3000- and a 32000-token prompt × 64 tokens:MUL_MAT1630/1630 andMUL_MAT_ID1035/1035 on tip, off and on, with identical (empty) failure sets.MUL_MAT1630/1630 (the J=128 log line is present) andMUL_MAT_ID1035/1035 (it never takes this path).MUL_MAT1630/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.llama-bench -p 512,4096:mul_mat_q_pq2_j128has 3077 + 272 launches and 50.5% of GPU time, and nomul_mat_q<PQ2_0, 128>remains.-lv 4,llama-perplexitylogs the kernel for 2B/4B/9B/27B PQ2_0 and not for PTQ1_0 or Q2_0;llama-serverlogs 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)-ub 2048, 9B: pp2048 8480 → 9432 (1.112), pp4096 8799 → 9820 (1.116).llama-mtmd-cli(decoded (batch)times summed):Build
ggml-cudafor75;86;89;120aat this head: rc=0, so the sm_90+ guards hold. Repeated at 343bd08: rc=0.Not verified