Skip to content

cuda: opt-in fused FWHT + q8_1 quantize for PQ2_0 mat-vecs (GGML_CUDA_FWHT_FUSION_PQ2=1), +4-9% decode on H200 - #377

Merged
bri-prism merged 1 commit into
prismfrom
cuda/fwht-q8-pq2
Oct 24, 2026
Merged

bri-prism merged 1 commit into
prismfrom
cuda/fwht-q8-pq2

Conversation

@bri-prism

Copy link
Copy Markdown
Collaborator

What

GGML_CUDA_FWHT_FUSION_PQ2=1 (off by default) lets the existing fused Hadamard transform + q8_1 quantizer (ggml_cuda_try_fwht_q8) feed PQ2_0 mat-vecs, not only PTQ1_0 ones. The fused quantizer also gains the 4096 transform width. A new ROT_MATVEC test-backend-ops case covers the pattern for both weight types.

Why

On H200, batch-1 decode of our PQ2_0 hybrid (gated-delta-net + attention) models is bound by launch count, not bandwidth. With CUDA graphs and PDL on, an nsys trace (--cuda-graph-trace=node) of a decode step at 4k depth shows:

  • 70-89% of kernels run for under 5 us;
  • achieved DRAM bandwidth is 8-14% of peak.

Hazy Research's "No Bubbles" megakernel work reports 78% at batch 1. Every rotated activation on the PQ2_0 path still ran a standalone fwht_cuda_block and then one quantize_q8_1 per consuming mat-vec. PTQ1_0 had already folded the two into a single launch. This PR applies the same fusion to PQ2_0. It is a first, small "megakernel-lite" step: it removes launches instead of shortening them.

How

  • Consumer predicate. It now accepts PQ2_0 when the opt-in is set. All consumers of one transform must share one weight type, so they share one q8_1 layout.
  • Layout. It comes from the consumer type through ggml_cuda_q8_1_layout_host. Layouts the fused kernel does not write, such as the Ada PQ2_0 multi-column layout, fall through.
  • Pascal. PQ2_0 is skipped there because ggml_cuda_mul_mat_pq2_0_ordered quantizes src1 itself.
  • 4096 width. fwht_quantize_q8_1 gains N = 4096. Fusion at that width is also opt-in, so default behaviour does not change for any type.
  • Bit-exactness. The transform uses the shared ggml_cuda_fwht_block_butterfly and the same per-32 quantization as quantize_q8_1, so the q8_1 rows are identical to the unfused pair.

Results (H200, sm_90 build, CUDA graphs + PDL on)

Speed: llama-bench tg128 at depth 4096, -fa 1, r=5. Same binary, env off/on in ABBA order, 2 blocks per model.

model rotation width tok/s off -> on (median) ABBA block ratios on/off
PQ2_0 model A 512 634.3 -> 691.8 (+9.1%) 1.022*, 1.094
PQ2_0 model B 512 394.2 -> 426.1 (+8.1%) 1.080, 1.086
PQ2_0 model C 4096 278.2 -> 288.5 (+3.7%) 1.037, 1.041
PTQ1_0 model (control) 1024 142.1 -> 142.1 1.003, 1.000

* That block contains one noisy run (605.9 +- 104 tok/s); the other three "on" runs were 691-693.

Launch counts (nsys). Kernels per decode token fall by 20% (A) and 19% (C). Every standalone quantize_q8_1 and all but one fwht_cuda_block per token are gone. GPU busy time per token falls by 134 us (A) and 143 us (C).

Opt-in off vs base prism. ABBA ratios are 0.999-1.000 on all four models.

Correctness.

  • Greedy decode, 5 prompts x 192 tokens per model, all four models. Tokens and the top-3 logprobs are bit-identical vs base prism, with the opt-in both off and on.
  • test-backend-ops on CUDA0 vs base prism:
    • MUL_MAT: 1630/1630, off and on.
    • MUL_MAT_HADAMARD: 34/34.
    • ROT_MATVEC: 20/20, off and on. nsys confirms the fused fwht_quantize_q8_1 AoS kernels run at widths 512 and 4096.
    • ARCLAB_GLU_ROT and ARCLAB_ADD_NORM_ROT: the failing-case sets are identical to base (NaN on CUDA0 at K=17408 and others, 8/15 and 11/15 failing). This is pre-existing and not touched here.

Notes

  • The opt-in could become the default after review. It is off here so it can be tried without changing any shipped path.
  • The launch-overhead framing follows Hazy Research's megakernel posts ("Look Ma, No Bubbles!" and the tensor-parallel Llama-70B follow-up) and ThunderKittens.
  • The same traces show a bigger chain left to fuse: the gated-delta-net decode chain (conv state concat/copy, conv1d, L2 norm, gate projections, state update, gated norm), about 7-11 launches per GDN layer.

…_FWHT_FUSION_PQ2=1)

The fused Hadamard-transform + q8_1 quantizer (ggml_cuda_try_fwht_q8) only
accepted PTQ1_0 consumers, so PQ2_0 models still ran a standalone
fwht_cuda_block plus one quantize_q8_1 per consuming mat-vec at every
rotated activation. With GGML_CUDA_FWHT_FUSION_PQ2=1 the fusion also
accepts PQ2_0 consumers (AoS q8_1 layout; all consumers must share one
weight type) and the fused quantizer gains the 4096 transform width.

The transform uses the shared ggml_cuda_fwht_block_butterfly and the same
per-32 quantization as quantize_q8_1, so the q8_1 rows are bit-identical to
the unfused pair. Default behaviour is unchanged: the PQ2_0 consumers and
the 4096 width are only taken with the opt-in.

Adds ROT_MATVEC to test-backend-ops: [SWIGLU ->] MUL(signs) -> RESHAPE ->
Hadamard MUL_MAT -> PQ2_0/PTQ1_0 mat-vec at rotation widths 512 and 4096.
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