Skip to content

cuda: opt-in one-scale-per-128 activations for PQ2_0 prefill (GGML_CUDA_PQ2_D1), +4-24% on H200, not bit-exact - #375

Open
bri-prism wants to merge 2 commits into
prismfrom
cuda/pq2-j128-d1
Open

bri-prism wants to merge 2 commits into
prismfrom
cuda/pq2-j128-d1

Conversation

@bri-prism

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

Copy link
Copy Markdown
Collaborator

The bit-exact J=128 PQ2_0 kernel from #374 is in prism. This PR adds the opt-in D1 path.

What

Why

This is the remaining large prefill item in the Yukon cuda.fast Bonsai engine (MIT, licenses/yukon-cudafast), measured on H200 on top of the bit-exact J=128 port.

  • It cuts the per-block epilogue from four scale loads and four FFMA pairs to one. It also cuts the activation quantizer's per-32 reductions to a per-128 one.
  • It changes numerics, so it is a separate switch from the exact kernel.
  • It ships off by default, gated on the KLD result below, so that a deployment can opt in for prefill-heavy workloads (long prompts, image tokens) after its own check.

How

  • quantize.cu: quantize_mmq_q8_1 gets a d1 template flag (D4 layout only). It widens the amax shuffle from 8 lanes (32 values) to the whole warp (128 values). Rounding and the stored scale expression are unchanged, so the four D4 fields are equal. The plain, fused RMS-norm and fused SwiGLU entry points take a trailing d1 = false argument.

  • mmq.cu: d1 is set only when all of these hold:

    • the weight is PQ2_0, ne11 >= 128, NVIDIA sm_90+, and GGML_CUDA_PQ2_D1=1 with the J=128 kernel enabled;
    • this call quantizes y itself.

    It is passed to the quantizer and in mmq_args::d1_y. A consumer of a shared external_q8 keeps d1_y = false and reads all four (equal) scales, which is correct for both layouts. The same holds for any shape that ends up on the generic kernel.

  • mmq-pq2-j128.cuh: ggml_cuda_pq2_j128_d1_compute is the source's D1 consumer. It uses the accumulating mma.sync and reads only scale 0 of each column record. The two D1 call sites in the segment loop are restored, and the launcher picks mul_mat_q_pq2_j128<..., D1=true> when args.d1_y.

Quality gate

Threshold, set from existing anchors:

  • mean KLD < 8e-4 nats, which is our kld-measure noise floor for a lossless 8-bit change;
  • same top-1 within 0.5 pp of the engine's own FP-reordering noise. That noise is the same exact build with -ub 256 instead of -ub 512, which changes MMQ tile shapes and stream-k partitions.

Setup: llama-perplexity on wiki.test.raw, with base logits from the exact J=128 build with D1 off (bit-identical to prism, per #374). There are 24 chunks × 512 for 9B and 4B, and one 16384-token chunk for the 9B long-context spot check.

run mean KLD 99.9% KLD same top-1 PPL ratio
9B c512, D1 3.36e-4 0.0140 99.18% 1.00024
9B c512, anchor (exact, ub 256) 2.79e-4 0.0124 99.22% 0.99984
4B c512, D1 3.58e-4 0.0130 99.09% 1.00107
4B c512, anchor 2.76e-4 0.0063 99.22% 1.00069
9B c16384, D1 3.45e-4 0.0363 99.22% 1.00084
9B c16384, anchor 2.24e-4 0.0089 99.33% 0.99965

Verdict: passes.

  • Mean KLD is 2.2 to 2.4× below the 8e-4 floor and 1.2 to 1.5× the engine's own reordering noise.
  • Same top-1 is within 0.04 to 0.13 pp of the anchor.
  • The 99.9th-percentile KLD is 1.1 to 4× the anchor's, so the tail is where D1 shows. That is the reason it is opt-in.
  • Greedy tokens are not identical to the exact path, which is expected for a numerical change.

Speed

llama-bench -fa 1 -ub 512 -b 2048 -r 5. ABBA, 3 pairs per size, exact J=128 (A) vs J=128 + D1 (B), H200.

model test exact D1 D1/exact per-pair range
2B PQ2_0 pp128 13201 13659 1.035 1.029..1.041
2B PQ2_0 pp512 21340 22701 1.064 1.061..1.067
2B PQ2_0 pp4096 22719 (2 pairs) 24318 (2 pairs) 1.070 1.069..1.072
4B PQ2_0 pp128 8251 8814 1.068 1.043..1.088
4B PQ2_0 pp512 12447 13615 1.094 1.080..1.101
4B PQ2_0 pp4096 12919 14236 1.102 1.098..1.105
9B PQ2_0 pp128 6361 7123 1.120 1.114..1.131
9B PQ2_0 pp512 9258 10538 1.138 1.134..1.142
9B PQ2_0 pp4096 9717 11117 1.144 1.142..1.145
27B PQ2_0 pp128 2556 2966 1.161 1.138..1.194
27B PQ2_0 pp512 3276 3932 1.200 1.197..1.202
27B PQ2_0 pp4096 3230 3993 1.236 1.201..1.314
  • 2B pp4096: one D1 run was disturbed by co-located load on the shared node (samples 4021 to 24259 t/s), so the table uses the two clean pairs.
  • Decode (tg32): 0.997 to 1.005 per pair on every size.

Test plan

  • D1 path ran:
    • nsys, 9B pp512: mul_mat_q_pq2_j128<1,0,0,1,1> (D1) has 181 launches and 42.5% of GPU time;
    • quantize_mmq_q8_1<D4,0,0,0,1> (D1 quantizer) has 197 launches;
    • with -lv 4, the J=128 log line appears in the D1 perplexity run.
  • D1 off: with GGML_CUDA_PQ2_D1 unset, MUL_MAT outputs on 25 PQ2_0 shapes are bitwise identical to prism.
  • test-backend-ops: -o MUL_MAT passes 1630/1630 with GGML_CUDA_PQ2_D1=1, within the suite tolerance vs CPU.
  • Final head: the KLD rerun on this head gives the same 9B numbers (3.36e-4, 99.183%).
  • Not verified:
    • Blackwell/GB10 (no hardware here);
    • task-level evals beyond KLD/top-1;
    • MoE (MUL_MAT_ID never takes this path).

@bri-prism
bri-prism changed the base branch from cuda/pq2-j128-prefill to prism October 24, 2026 14:41
…prefill

GGML_CUDA_PQ2_D1=1 quantizes the activations of PQ2_0 mat-muls with at least
128 columns with one scale per 128 values instead of one per 32 (the four D4
scale fields of a block_q8_1_mmq hold the same value), and the J=128 PQ2_0
kernel then sums the four 32-value integer products of a block in int32 and
scales once. This is the D1 consumer of the Yukon cuda.fast Bonsai engine.

Not bit-exact and off by default. It needs the J=128 kernel (it is ignored
when GGML_CUDA_PQ2_J128 is off) and only applies when the mat-mul quantizes
its own activations; a consumer of a shared quantized buffer still reads all
four scales, which is correct for both layouts. Decode and small verify
batches (fewer than 128 columns) are unchanged.
Resolve the mmq_args conflicts by keeping prism's J_best and fusion fields
and adding d1_y after them; the args initializer passes d1 after
fusion_device, and the type switch takes prism's prec_src1 argument.
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