Skip to content

Bring Karim's MTP verify and prefill kernels to prism; measured winners on by default (Hopper prefill, Metal PTQ1_0 verify) - #452

Merged
bri-prism merged 16 commits into
prismfrom
mtp/verify-prefill-kernels
Nov 24, 2026
Merged

bri-prism merged 16 commits into
prismfrom
mtp/verify-prefill-kernels

Conversation

@bri-prism

@bri-prism bri-prism commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

What

All 14 changes here are @Karim-Mattar's work: he designed, wrote, measured and validated each kernel and setting, including the bit-exactness and lossless checks quoted in the commit messages. This PR only moves them from our private repo to prism, as a cherry-pick of his seven branches:

Area Change Switch
CUDA prefill PTQ1_0 prefill on the Hopper J=128 MMQ kernel GGML_CUDA_PTQ1_J128
CUDA prefill Bit-exact chunk-staged Gated DeltaNet prefill kernel GGML_CUDA_GDN_L8P
CUDA prefill The prefill rotation quantizes its output once for its MMQ consumers GGML_CUDA_FWHT_MMQ, GGML_CUDA_FWHT_MMQ_GLU
CUDA decode add_rms_norm issues every row load before the first store GGML_CUDA_ARN_PRE
MTP Lean prefill for the draft-mtp head: the head's prompt catch-up runs one batch late and overlaps the target's next batch LLAMA_MTP_PREFILL_LEAN
Metal Bit-exact verify paths for PTQ1_0, PQ2_0 and Q2_0 at 2 to 8 rows GGML_METAL_PTQ1_MULTICOL=2, GGML_METAL_PTQ1_COEF, GGML_METAL_PQ2_COEF, GGML_METAL_Q2_COEF
Metal 64x64 mul_mm output tile, and one-thread-per-row SSM_CONV for 1 to 8 tokens GGML_METAL_MM_W64, GGML_METAL_SSM_CONV_ROWS
Metal Per-op GPU profiler GGML_METAL_OP_PROFILE=<file>
SYCL 2 to 8-row PTQ1_0 batches run on GemmMT, bit-exact; per-batch-width graph timing GGML_SYCL_T2_VGEMM_MIN, GGML_SYCL_OP_TIMING=3

Why

Speculative decoding with the MTP head needs these kernels and settings to reach its speedups. They have only been in the private repo, so the public fork could not show them.

How

  • Every commit keeps @Karim-Mattar as its author. Commit messages were edited only to describe the benchmark model generically and to drop references to commits outside this repo.
  • One conflict in tests/test-backend-ops.cpp: the CUDA and Metal branches both added cases in the same place, and both are kept.
  • The last commit groups the four prefill paths that measured faster with identical output under one switch, GGML_CUDA_PREFILL_OPT: GGML_CUDA_GDN_L8P, GGML_CUDA_PTQ1_J128, and GGML_CUDA_FWHT_MMQ with GGML_CUDA_FWHT_MMQ_GLU.
    • Unset, they are on for NVIDIA Hopper (cc 900 to 999) and off elsewhere. This is the rule GGML_CUDA_PQ2_J128 already uses.
    • =0 turns all four off, and =1 turns them on wherever each path supports the GPU.
    • Each per-path switch still overrides it when set.
    • Every other switch in this PR stays opt-in. That includes GGML_CUDA_ARN_PRE, which measured neutral here and 6% slower on PTQ1_0 prefill in an earlier H200 run.
  • LLAMA_MTP_PREFILL_LEAN_SABOTAGE is a negative-control hook. It feeds the draft head the wrong hidden state on purpose, to show that the acceptance check catches a bad draft. Output stays the same, because drafts are verified. It can be dropped if we'd rather not ship it.

Defaults on Hopper

Measured on H200 with a 27B ternary model. llama-bench ran 3 repetitions in each of 2 passes with the order swapped. Figures are in tok/s.

GGML_CUDA_PREFILL_OPT=0 New default Change
PTQ1_0 pp512 / pp2048 2975 / 2971 3816 / 3810 +28% / +28%
PQ2_0 pp512 / pp2048 3426 / 3447 4078 / 4104 +19% / +19%
tg128 (both formats) within 0.4%

Each path's contribution, measured alone against all four off:

Path PTQ1_0 prefill PQ2_0 prefill
GDN_L8P +12% +15%
PTQ1_J128 +9% 0 (PTQ1_0 only)
FWHT_MMQ + GLU +3% +3%

Greedy output (127 tokens after a 3,067-token prompt) is byte-identical across these settings on both formats:

  • the new default
  • GGML_CUDA_PREFILL_OPT=0
  • each path alone
  • each per-path override

Default on Metal

The last commit makes the bit-exact PTQ1_0 verify path the default at 2 to 8 rows, behind GGML_METAL_VERIFY_OPT. This is GGML_METAL_PTQ1_MULTICOL=2 with GGML_METAL_PTQ1_COEF=1. Unset means on, =0 means off, and either per-path switch overrides it. The non-exact GGML_METAL_PTQ1_MULTICOL=1 stays opt-in.

M5 Pro, 4B ternary PTQ1_0, default against GGML_METAL_VERIFY_OPT=0 (tok/s):

Rows Off Default Change
2 67.7 152.0 +125%
4 122.2 129.7 +6%
8 135.8 192.2 +41%
decode 120.3 120.6 same

Perplexity is identical per chunk at -ub 2, 4 and 8.

The other Metal switches stay opt-in:

  • GGML_METAL_PQ2_COEF is slower on M5 Pro at 4 and 8 rows (-22%, -46%). It is not bit-identical there, because the stock path on that GPU uses the tensor units.
  • GGML_METAL_MM_W64 and GGML_METAL_Q2_COEF only apply to GPUs without tensor units. They need an M1 to M4 to measure.

Testing

test-backend-ops, with the new switches unset and then all set:

  • Metal (M5 Pro): the PTQ1_0, PQ2_0, Q2_0 and Q4_0 MUL_MAT cases, including the new ones, pass 813/813 in both runs. The PTQ1_0 cases also pass 255/255 with the new default on.
  • CUDA (H200, sm_90): MUL_MAT, GATED_DELTA_NET, ROT_MATVEC, ROT_OUTPUT_VIEW and RMS_NORM.
    • With the new defaults active: 6723/6723 pass.
    • With every switch set: 6723/6723 pass.
    • Unset: one q4_1 case at m=16, n=1, k=256 failed with error 5.4e-4 against a 5e-4 limit. It passed 30 of 30 isolated reruns on this branch and on prism without this change. This PR does not touch that code path.
  • SYCL: not run here. It needs an Intel GPU.
  • Build: the library, common and the server context compile on macOS.

Separately, on unmodified prism, Metal's test-backend-ops -o MUL_MAT aborts on type 145 (PQ2_0M, the KV-only type from #428). Metal reports MUL_MAT as supported for it but has no kernel. This PR leaves that as is; it needs its own fix.

…Q kernel

Switch: GGML_CUDA_PTQ1_J128=1 (default off: stock path unchanged).
Hardware class: Hopper (cc 9.x); the J128 kernel is Hopper-only without
GGML_CUDA_J128_ADA.

The fork's bit-exact PQ2_0 Hopper kernel (mmq-pq2-j128.cuh, 113 registers,
2 blocks per SM) was never used for PTQ1_0, whose generic MMQ kernel uses
232 registers (1 block per SM). This adds a PTQ1_0 weight loader for the J128
kernel with the same trit codec and lane split as
ggml_cuda_mmq_load_tiles_ptq1_0, so the shared int8 tile and scales are
identical to the stock tile.

Benchmark (H100 SXM, 27B ternary PTQ1_0, llama-bench, 5 alternating runs,
spread 0.8%): pp512 3,033 -> 3,251 tok/s (+7.2%); pp4096 2,992 -> 3,195
(+6.8%). Every Bonsai 2 model +6-7% (CUDA-PREFILL2.md).

Gates: perplexity over 16 chunks identical (5.1349 both); test-backend-ops
MUL_MAT passes; nsys shows mul_mat_q_pq2_j128 running for PTQ1_0.
Tests: test-backend-ops MUL_MAT gains PTQ1_0 and PQ2_0 cases with 128-aligned rows (m=256/1024,
k=1024/5120, n=64/200/512). Rebased onto fork prism d12ca13, H100, nsys: the earlier MUL_MAT cases
launch the J=128 kernel for PTQ1_0 0 times with the switch on (only PQ2_0); with the new cases, 2 times.
MUL_MAT with the switch on: 2954/2955; the one failure is a random-input tolerance case of another type
(q4_1 m=16, NMSE 6.0e-4 > 5e-4) of the kind that also fails in switch-off runs.

Source: bonsai-mtp-heads docs/kernels/CUDA-PREFILL.md, patch 0002.
… kernel

Switch: GGML_CUDA_GDN_L8P=1 (default off; 1 = auto config, other values pick
a config from the table in launch_gated_delta_net). Hardware class: all CUDA
(not HIP/MUSA). Engages at 17+ tokens.
Debug switches: GGML_CUDA_GDN_XCHECK=1 runs the stock kernel too and counts
bitwise differences in output and state; GGML_CUDA_GDN_L8P_SAB=1 breaks the
reduction order on purpose (proves the cross-check and gates can fail).

Stock arithmetic, new scheduling: 8 lanes share a state column (3 shuffles
instead of 5) and tokens are staged into shared memory in chunks. The fork's
GGML_CUDA_GDN_MMA computes the chunked form and is not bit-exact; this is.

Benchmark: GDN per 512-token call 586 -> ~150 us (27B ternary, H100).
With FWHT_MMQ on top, H100 prefill pp512 vs stock: 2.5 27B +26%, 27B +26%,
9B +37%, 4B +47%, 2B +64%; RTX 4090 +9.6% to +44%.

Gates: XCHECK 0 bitwise differences on every call (the SAB variant differs on
every call); perplexity identical in all 16 chunks; greedy 24/24 plain and
MTP n3p1; KLD at the same-binary floor; five models, both GPUs.
Source: bonsai-mtp-heads docs/kernels/CUDA-PREFILL2.md, patch 0009.
…t once for its MMQ consumers

Switches: GGML_CUDA_FWHT_MMQ=1, GGML_CUDA_FWHT_MMQ_GLU=1 (both default off).
Hardware class: all CUDA.

In prefill the Hadamard rotation now writes block_q8_1_mmq (d4 scales, new
layout GGML_CUDA_Q8_1_MMQ_D4) once for every consuming MMQ mat-mul instead
of each consumer quantizing the same rows; with _GLU the FFN silu*up folds
into the same kernel. Same values as the stock quantizer: bit-exact.

Benchmark: +1.4% to +6.5% prefill over GDN_L8P (27B pp512 +3.3% with CUDA
graphs off; above 3% at pp2048+ on every model). Final H100 table with all
prefill switches: CUDA-PREFILL2.md.

Gates: perplexity identical in all 16 chunks, greedy 24/24 plain and MTP
n3p1, KLD at the same-binary floor, five models, both GPUs.
Rebase note (fork prism d12ca13): ggml_cuda_fwht_mmq_find_uses now also refuses an output-flagged
RESHAPE of the transform, the guard fork commit 5da2802 added to ggml_cuda_fwht_q8_find_uses.
Tests: ROT_MATVEC gains MMQ-width cases (M=64/512, PTQ1_0 and PQ2_0, swiglu on/off; the earlier 1-2 column
cases never reached the path) and a new ROT_OUTPUT_VIEW case (an output view of the rotation that feeds an
MMQ mat-mul must stay F32). H100: 183/183 with the switches on; nsys shows fwht_quantize_mmq_d4_block in all
20 new ROT_MATVEC cases.

Source: bonsai-mtp-heads docs/kernels/CUDA-PREFILL2.md, patch 0010.
…the first store

Switch: GGML_CUDA_ARN_PRE=1 (default off). Hardware class: H100 (Hopper);
inconsistent on the RTX 4090 (pinned: +0.1% plain), keep off there.

Multi-row add+RMS-norm (MTP verify, prefill): the stock loop waits for each
load because the store may alias it. This variant loads up to MAXI columns
per thread first; the column order of the sum of squares, the reduction and
the output expression are the same, so results are bit-identical.

Benchmark: 4-row verify step (pp4) H100 +4.3%, RTX 4090 +4.5%; H100 check40
MTP n3p6 +3.0-3.5% together with RPW and NFQ.

Gates: perplexity per chunk identical, greedy 24/24, five models.
Source: bonsai-mtp-heads docs/kernels/CUDA-DECODE.md, CUDA-4090-HEADROOM.md,
patch 0006.
Switch: LLAMA_MTP_PREFILL_LEAN=1 (default off). Hardware class: shared code,
every backend; helps CUDA and Metal, no change on SYCL.
Debug switch: LLAMA_MTP_PREFILL_LEAN_SABOTAGE=1 (wrong draft state; proves
the draft-count gate can fail).

With --spec-type draft-mtp the server ran the MTP head's prompt catch-up
after every prompt batch, after waiting for the target, on the host's
critical path (GPU idle doubled). With the switch the catch-up for a prompt
batch without outputs runs one batch later, right after the next target
batch is launched, so it overlaps the target. Supporting changes: nextn rows
double-buffered per decode, a K/V-only head pass, pooled ubatch data blocks.

Benchmark (RTX 4090, server prompt tok/s, MTP on / MTP off): stock 0.65-0.78
at 2K and 0.72-0.88 at 8K; lean 0.88-0.96 at 2K and 0.93-0.97 at 8K.

Gates: greedy tokens 30/30 and draft counts 30/30 on five models; 27B KLD 0
over 1,537 positions; perplexity identical; sabotage fails the draft gate.
Source: bonsai-mtp-heads docs/release/MTP-PREFILL-FIX.md, patch 0013.
…ETAL_PTQ1_MULTICOL=2, default off)

What: GGML_METAL_PTQ1_MULTICOL=2 runs PTQ1_0 mat-vecs at 2-8 rows on
kernel_mul_mv_ptq1_0_f32_extc, which is bit-identical to the stock path
there (mul_mv_ext): the same per-lane chunk order, the same weight values
((trit - 1)*d from ptq1_0_lut, as ptq1_0_elem), dot() per float4 and the same
simd_shuffle_down reduction, with nxpsg = 16 at 2 rows when K % 256 == 0 and 8
otherwise, exactly as mul_mv_ext picks it. Only the scheduling changes: each
lane owns 4 rows (GGML_METAL_PTQ1_EXTC_NR = 2, 4, 8) and up to 5 columns (6-8
rows: two tiles of 3-4), so each weight is read once at 2-5 rows and twice at
6-8; each row's four weights are decoded once per chunk for all columns, from
one aligned 32-bit word, with the trit table in threadgroup memory.
The multi-column kernel of GGML_METAL_PTQ1_MULTICOL=1 (2-4 columns, base-3
collapse coefficients, already in the tree) is a different float evaluation of
the dot product and is not bit-identical; =2 does not use it. At 9+ rows the
stock path (mul_mm, half precision) is kept.

Why: on GPUs without tensor units, 2-8 PTQ1_0 rows run on mul_mv_ext, which
re-reads the weights per group of <= 5 columns and decodes every weight per
group; a speculative-decoding verify step pays that on every pass.

Measured on M4 Pro, 27B ternary PTQ1_0, llama-bench -p 2,3,4,8 -n 0 -r 5
-ngl 99 -fa 1, 3 alternated off/on pairs, ms per step (with
GGML_METAL_PTQ1_COEF=1 also set; both switches use this kernel at 2-8 rows):
n=2 233.5 -> 118.0, n=3 270.6 -> 146.0, n=4 322.6 -> 200.0, n=8 532.3 -> 373.1.
The earlier, not bit-identical versions of the two switches measured
63.2 / 111.9 / 114.3 / 222.1 ms: exactness costs that much, because
mul_mv_ext's arithmetic needs a table decode per weight where the collapse
form needs about two float ops.

Lossless: llama-perplexity -c 512 -b 512 --chunks 8 -fa on on 27B ternary
PTQ1_0: the logits of every chunk hash bit-identical to the switches unset at
-ub 2, 3, 4, 6, 8 and 512 (=2 with GGML_METAL_PTQ1_COEF=1; =2 alone at -ub 3).
A one-sign flip in the kernel changes the hashes at -ub 4.

Unset = the stock path.

Tests: test-backend-ops gains PTQ1_0 eval cases at 9-16 rows (m = 67, so a
partial row group and partial column tiles) and the PTQ1_0 perf sweep at the
27B shapes. test-backend-ops -b MTL0 -o MUL_MAT -p type_a=ptq1_0: 225/225
unset, =2, and =2 with GGML_METAL_PTQ1_COEF=1.
What: GGML_METAL_PTQ1_COEF=1 (or =2) runs PTQ1_0 mat-vecs at 2-8 rows on
kernel_mul_mv_ptq1_0_f32_extc, the bit-identical mul_mv_ext-arithmetic kernel
added with GGML_METAL_PTQ1_MULTICOL=2; the two switches are independent and
share it. 1 row stays on the stock decode kernel.

A coefficient pre-pass (each column's base-3 collapse coefficients staged once
and read by a multi-column mat-vec) was tried under this switch and dropped: it
is not bit-identical to mul_mv_ext at 2-8 rows, and at 1 row its logit hashes
differ from the stock decode kernel's at -ub 1 although the source arithmetic
is the same (Metal fast math).

Why: a speculative-decoding verify step runs the target on 1 + n drafted
tokens; on GPUs without tensor units 2-8 PTQ1_0 rows run on mul_mv_ext.

Measured on M4 Pro, 27B ternary PTQ1_0, with GGML_METAL_PTQ1_MULTICOL=2 also
set, llama-bench -p 2,3,4,8 -n 0 -r 5 -ngl 99 -fa 1, 3 alternated pairs, ms
per step: n=2 233.5 -> 118.0, n=3 270.6 -> 146.0, n=4 322.6 -> 200.0,
n=8 532.3 -> 373.1 (the pre-pass version, not bit-identical: 63.2 / 111.9 /
114.3 / 222.1).

Unset = the stock path.

Lossless: logit hashes per chunk identical to the switches unset at -ub 1, 2,
4 and 8 (and 3, 6, 512 before the pre-pass code was removed; see the
PTQ1_MULTICOL commit).

Tests: test-backend-ops gains PTQ1_0 cases with m = 132, n = 1..8, K = 1024,
5120, 17408. test-backend-ops -b MTL0 -o MUL_MAT -p type_a=ptq1_0: 225/225
unset, =1, and =1 with GGML_METAL_PTQ1_MULTICOL=2.
…default off)

What: GGML_METAL_PQ2_COEF=1 runs PQ2_0 mat-vecs at 1..GGML_METAL_PQ2_COEF_MAX
rows (default 8) on paths that are bit-identical to the stock Metal path,
with different scheduling:
- 1..3 rows: kernel_pq2_0_coef writes each src1 column's 16 base-4 collapse
  coefficients per thread and its block sums once (same layout and stride as
  the PTQ1_0 pre-pass), then kernel_mul_mv_pq2_0_f32_coef_c<nr1>_r<nr0> reads
  them. This is the same float arithmetic, in the same order, as the stock
  kernels at these widths (kernel_mul_mv_pq2_0_f32, _nr1_2/_nr1_3).
- 4..8 rows: the stock path is mul_mv_ext (nxpsg = 8, r1ptg <= 5), a different
  float evaluation of the dot product, so the pre-pass cannot match it.
  kernel_mul_mv_pq2_0_f32_extc_c<nr1>_r<nrl> keeps mul_mv_ext's arithmetic
  exactly (per-lane chunk order, dequantize_pq2_0_t4 values, dot(), the
  shuffle-down 4/2/1 reduction) but gives each lane 4 rows and up to 5
  columns, so each weight is read once at 4..5 rows and twice at 6..8.
=2 keeps 1 row (decode) on the stock kernel. The coefficient scratch is
carved out of the padding the allocator adds behind dst (sized in
ggml_backend_metal_buffer_type_get_alloc_size through the same predicate the
encoder uses, as the Q1_0 bit-plane path does). The 1..3-row kernels are
templates on the block type (kernel_q2x_coef, kernel_mul_mv_q2x_coefmv).
Tuning knobs: GGML_METAL_PQ2_COEF_MAX (rows, default 8), _NR1 (column tiles,
default 2), _KMAX, _NR0 (2, 4, 8), _NSG, _NSG1 (simdgroups at 1 row), _ENR
(rows per lane at 4..8 rows: 2, 4, 8), _ENSG, _EMAX (columns per threadgroup
at 4..8 rows, default 5).

Why: at 2-8 rows PQ2_0 runs the stock 2-3 column mat-vec or mul_mv_ext,
which re-stage the activations per threadgroup or per column, and
mul_mv_ext re-reads the weights per group of <= 5 columns.

Measured on M4 Pro, 27B ternary PQ2_0, llama-bench -p 2,3,4,8 -n 0 -r 5
-ngl 99 -fa 1, 3 alternated off/on pairs, ms per step:
n=2 70.4 -> 58.3, n=3 129.7 -> 99.2, n=4 222.9 -> 97.8, n=8 393.9 -> 170.4.
An earlier version of this switch (coefficient pre-pass at every width)
was not bit-identical at 4..8 rows and measured 102.9 / 198.4 ms at n=4 / 8.
On GPUs with tensor units the switch takes precedence over the PQ2_0
few-row tensor tile (not measured there; leave it unset on those).

Unset = the stock path.

Lossless: llama-perplexity -c 512 -b 512 --chunks 8 -fa on on 27B ternary
PQ2_0: the logits of every chunk hash bit-identical to the switch unset at
-ub 2, 3, 4, 6, 8 and 512. A one-sign flip in either kernel changes
perplexity at -ub 4 and -ub 2.

Tests: test-backend-ops gains PQ2_0 cases with m = 132, n = 1..8, K = 1024,
5120, 17408. test-backend-ops -b MTL0 -o MUL_MAT -p type_a=pq2_0, : 284/284
unset, =1 and =2.
…xt (GGML_METAL_Q2_COEF, default off)

What: GGML_METAL_Q2_COEF=1 (or =2) runs Q2_0 mat-vecs on kernel_mul_mv_q2x_extc
(the PQ2_0 extc kernel, now a template on the block type, the decoder and the
lane count) wherever the stock path is mul_mv_ext: at 2 rows, and at 2-8 rows
on GPUs without the few-row MMA kernels. The kernel keeps mul_mv_ext's
arithmetic exactly (nxpsg = 16 at 2 rows when K % 256 == 0, 8 when K % 128 == 0,
else 4; the same chunk order, dequantize_q2_0_t4 values, dot() and
shuffle-down reduction) with 4 rows (GGML_METAL_Q2_COEF_ENR = 2, 4, 8) and up
to 5 columns per lane. 1 row (the decode kernel's bit-select dot) and the MMA
widths (3-16 rows on GPUs with simdgroup MMA) stay on the stock kernels:
no other arithmetic matches them bit for bit. The earlier version of this
switch (the PQ2_0 coefficient pre-pass at 1-8 rows) matched none of the stock
Q2_0 kernels and is dropped.

Why: on GPUs without tensor units a 2-row verify step runs Q2_0 on mul_mv_ext.

Measured on M4 Pro, Ternary Bonsai 2 4B Q2_0 (the 27B Q2_0 file is not on the
Mac), llama-bench -p 2,3,4,8 -n 0 -r 5 -ngl 99 -fa 1, 3 alternated pairs, ms
per step: n=2 20.1 -> 16.3; n=3, 4, 8 unchanged (stock MMA: 18.3 / 18.5 / 18.7).
The earlier version measured n=2 13.4 but n=3/4/8 19.4 / 19.4 / 32.0 (slower
than stock) and was not bit-identical (logit hashes differ at -ub 2 and 4).

Unset = the stock path.

Lossless: llama-perplexity -c 512 -b 512 --chunks 8 -fa on on that 4B file:
logit hashes per chunk identical to the switch unset at -ub 2, 3, 4, 6, 8, 512;
a one-sign flip in the kernel changes them at -ub 2. PQ2_0 (same template)
stays identical at -ub 4 and 8.

Tests: test-backend-ops gains Q2_0 cases with m = 132, n = 1..8 and K = 576 (a
multiple of 64, not of 128), 2560, 9728 and the 27B row lengths 5120, 6144,
17408. test-backend-ops -b MTL0 -o MUL_MAT -p type_a=q2_0, : 130/130 unset and
=1; PQ2_0 284/284 with GGML_METAL_PQ2_COEF=1.
…TAL_MM_W64=1, default off)

What: GGML_METAL_MM_W64=1 routes the non-tensor mul_mm for PTQ1_0, PQ2_0 and
Q4_0 weights (F32 src1, ne00 % 32 == 0) to kernel_mul_mm_w64: the same A tile
and dequantize as kernel_mul_mm (64 weight rows x 32 K), but 64 src1 columns
per threadgroup instead of 32, so each dequantized weight feeds twice as many
multiply-accumulates. Each of the 4 simdgroups owns 32 x 32 outputs; the
per-output accumulation order (8-wide K chunks, in order) is unchanged, so the
result matches kernel_mul_mm. GPUs with tensor units keep the tensor path
(the kernel is not built there).

Why: on GPUs without tensor units prompt reading is dominated by the weight
mat-mul, and for the Bonsai formats the dequantize is a large part of it;
sharing each dequantized tile across 64 columns halves that cost per output.

Measured on the earlier fork base b3ebc47 (M4 Pro), from the original commit
("64x64 mul_mm tile (GGML_METAL_MM_W64=1, PTQ1_0 prefill +9.3%)"):
27B ternary PTQ1_0 pp512 109.32 -> 119.52 tok/s, pp4096 107.58 -> 117.39;
PQ2_0 +1.6% / +1.4% (under the 2% bar). Q4_0 instance built, not measured. Not
re-measured on this base. The closed variants of that commit (=2 branch-free
dequantize, =3 64 x 128 tile) are not carried.

Unset = the stock path.

Tests: existing cases reach the kernel (PTQ1_0/PQ2_0 256 x {9, 64, 100} and
4096 x {9..512}, Q4_0 2880 x 32 and the generic Q4_0 sweeps, with and without
a partial output tile). test-backend-ops -b MTL0 -o MUL_MAT (all types except
pq2_0m, whose Metal mul_mat aborts on the stock base too): 2946/2946 with
the switch set; unset 2945/2946, the one failure being the stock 1 x 1 PQ2_0
mat-vec (m = 1, n = 1, error 0.016 against a 5e-4 bound), which this switch
does not touch and which passes in other runs. The per-op profiler shows
kernel_mul_mm_w64_{ptq1_0,pq2_0,q4_0}_f32 with both bounds-check variants.
…NV_ROWS=1, default off)

What: GGML_METAL_SSM_CONV_ROWS=1 runs SSM_CONV (and the fused SSM_CONV + SiLU)
at 1..8 tokens with nc % 4 == 0 on kernel_ssm_conv_f32_f32_4_rows: one thread
per channel row in 256-thread threadgroups, instead of one single-thread
threadgroup per row (1 token) or the token-batched kernel (2-8 tokens). The
arithmetic is that of kernel_ssm_conv_f32_f32_4 (float4 dot over nc/4, in
order, nc as a function constant), so decode and verify rows match the
decode kernel bit for bit.

Ported to this base's SSM_CONV changes: nc is now a function constant of the
stock kernels and SiLU is fused through the fusion table, so the row kernel
takes the same nc/SiLU constants and writes to the fused node's buffer.

Why: the 27B's conv has 10240 rows; at decode the stock kernel launches 10240
threadgroups of one thread each.

Measured on the earlier fork base b3ebc47 (M4 Pro): the original commit
("SSM_CONV one thread per row for 1-8 tokens
(GGML_METAL_SSM_CONV_ROWS=1); same float4 dot as the decode kernel; tbo
SSM_CONV 45/45; speed not yet measured") carries no number; the follow-up
A/B on that base gave +1.9% decode, under the 2% bar, so it was not in the
kept set. Kept here as an opt-in switch only.

Unset = the stock path.

Tests: existing cases reach it (d_conv = 4 at 1 and 5 tokens, 1 and 4
sequences, plain and fused with bias + SiLU). test-backend-ops -b MTL0:
SSM_CONV 60/60 and SSM_CONV_BIAS_SILU 120/120, unset and set. The per-op
profiler shows kernel_ssm_conv_f32_f32_4_rows for those shapes.
What: with GGML_METAL_OP_PROFILE=<file> set, ggml_metal_graph_compute encodes
every (fused) op into its own command buffer, commits it and waits, and adds
the buffer's GPU time (GPUEndTime - GPUStartTime) to a per-op table keyed by
graph batch width, op, src0 type, node name (layer index stripped), shape,
number of fused nodes and the names of the pipelines the op encoded. The
table is written to <file> at exit, one line per key: total us, count, key.

Why: a per-op GPU timeline is the cheapest way to see which kernels dominate
decode, verify and prefill steps, and the pipeline names prove which kernel a
given shape actually reached (test-backend-ops shapes can miss a new path).

Cost: unset = the stock path; one getenv'd static pointer check per graph.
When set, the run is slower (one synchronous command buffer per op), so use it
for attribution only, never for end-to-end speed.

Ported from the earlier fork base b3ebc47 (an earlier private commit, which also
carried the PTQ1_0 multi-column changes, split out separately). Adapted to the
current ggml_metal_op_init signature (fusion info instead of a use_fusion
flag); pipeline names are new in this port (ggml_metal_pipeline keeps its name;
ggml_metal_prof_kernels_enable/_take collect the names set on encoders).

Tests: no test-backend-ops case (tool only). Checked by running
test-backend-ops -b MTL0 with the variable set: op tests pass and the file
lists every op with its pipeline names.
GGML_SYCL_OP_TIMING=3 times graphs of 2000+ nodes (the target model) instead of
draft-sized ones and keeps one table per batch width: the largest weight
mat-mul's column count, 1 (decode), 2..8 (speculative verify), 9+ (prompt).
Mat-muls are labelled by weight name (ffn_down, attn_qkv, ...). Reports every
16 graphs (every 2 for prompts). Profiling only; off by default, and modes 1-2
are unchanged.
…T, bit-exact

What: with GGML_SYCL_PTQ1_T2 on, GGML_SYCL_T2_VGEMM_MIN=k (2..8, default 0 = off)
sends PTQ1_0/PQ2_0 mat-muls of k..8 rows (speculative-decoding verify steps) to the
int8 x int2 GemmMT kernel (tile {8,64,1,8}) instead of the SGM 4/8 GEMV. GemmMT gets
a KSPLIT mode that sums K in the GEMV's slices (ks = the GEMV's LS: 4 if N <= 8192,
else 2; for a same-input merge, from its largest member) and adds the slices in the
GEMV's order, so the outputs are the GEMV's floats bit for bit. Same-input merges keep
their single activation quantization and run one GemmMT per member.

Why: the GEMV re-loads the activations for every 16 output columns, so its cost grows
with every row (8 rows = 1.68x one row on an Xe3 iGPU), while GemmMT at 16 rows costs
about one GEMV row.

Benchmark (Core Ultra Xe3 iGPU, oneAPI 2025.3, 27B ternary PTQ1_0-mtp,
GGML_SYCL_PTQ1_T2=all, f16 KV, VGEMM_MIN=3 vs off, same binary, alternating):
  verify step ms (llama-bench, one ubatch, 3 x -r 5): n=3 111.8 -> 104.2,
  n=4 119.8 -> 106.2, n=6 137.0 -> 110.8, n=8 151.5 -> 115.1; n=1, 2, 16 unchanged
  check40 (no multilingual) MTP n8 p-min 0.6, 5 pairs: 22.86 [22.77-22.91] ->
  24.17 [24.11-24.23] tok/s (+5.7%), every prompt group faster, acceptance unchanged;
  plain decode 11.09 -> 11.08; prompt tok/s at 2K/8K unchanged (drafting on and off).

Gates: test-backend-ops MUL_MAT (2781/2781) and ARCLAB_T2_MULTI (24/24) pass with
the switch off, at 2 and at 3; new PTQ1_0 cases (N 1024/5120/10240, 1..8 rows) reach
the path (GGML_SYCL_DEBUG=1 logs "t2 vgemm" for 2..8 rows, ks 2 and 4; none when
off). Perplexity per chunk (16 x 512) identical stock vs off vs on at -ub 512 and
-ub 4; greedy tokens 20/20 identical plain on vs off and MTP on vs off. Sabotage
(GGML_SYCL_PTQ1_T2_GEMV_MAX=2: the same rows on the one-pass GemmMT) fails both
(perplexity 3.9035 vs 3.9028, greedy 18/20).
…efault on Hopper

One switch for GGML_CUDA_GDN_L8P, GGML_CUDA_PTQ1_J128 and GGML_CUDA_FWHT_MMQ
with GGML_CUDA_FWHT_MMQ_GLU. Unset, they are on for NVIDIA Hopper (cc 900 to
999) and off elsewhere, the same rule GGML_CUDA_PQ2_J128 already uses.
GGML_CUDA_PREFILL_OPT=0 turns all four off and =1 turns them on wherever each
path supports the GPU. A per-path switch, when set, still overrides it for
that path. GGML_CUDA_ARN_PRE stays opt-in.

The four paths are Karim Mattar's (the earlier commits in this series); this
commit only groups them and changes their defaults.

Measured on H200 with a 27B ternary model, llama-bench, 3 reps in each of 2
order-swapped passes, against all four off:

  PTQ1_0 pp512 / pp2048: GDN_L8P +12.5% / +12.3%, PTQ1_J128 +8.7% / +8.7%,
    FWHT_MMQ + GLU +3.3% / +3.4%, all four +27.1% / +28.5%
  PQ2_0 pp512 / pp2048: GDN_L8P +14.7% / +14.8%, FWHT_MMQ + GLU +2.5% / +2.9%,
    all four +18.3% / +18.5%
  tg128 within 0.4% for every switch

Greedy output over 127 tokens after a 3,067-token prompt is byte-identical
with each switch alone and with all four, on PTQ1_0 and PQ2_0.
@bri-prism bri-prism changed the title Bring MTP verify and prefill kernels from the private repo (all switches off by default) Bring Karim's MTP verify and prefill kernels to prism; Hopper prefill paths on by default Nov 24, 2026
…on by default

Unset, PTQ1_0 mat-vecs at 2 to 8 rows run on the bit-exact verify kernel
(GGML_METAL_PTQ1_MULTICOL=2 with GGML_METAL_PTQ1_COEF=1). =0 turns it off.
Either per-path switch, when set, overrides it. The multi-column kernel of
GGML_METAL_PTQ1_MULTICOL=1, which is not bit-identical, stays opt-in.

The kernel is Karim Mattar's (the earlier metal commits in this series);
this commit only changes its default.

M5 Pro, 4B ternary PTQ1_0, llama-bench -p 2,4,8 -n 128 -r 3, two passes,
default vs GGML_METAL_VERIFY_OPT=0, tok/s:
  pp2 152.0 vs 67.7 (+125%), pp4 129.7 vs 122.2 (+6%), pp8 192.2 vs 135.8
  (+41%), tg128 120.6 vs 120.3
Per-chunk and final perplexity (-c 512, 4 chunks) are identical at -ub 2,
4 and 8. test-backend-ops -b MTL0 MUL_MAT PTQ1_0: 255/255. Karim measured
the same kernel on M4 Pro as bit-identical and faster at 2 to 8 rows.

The other Metal switches in this series stay opt-in. On M5 Pro,
GGML_METAL_PQ2_COEF is slower at 4 and 8 rows and not bit-identical to the
tensor-unit path, and GGML_METAL_MM_W64 and GGML_METAL_Q2_COEF do not apply
to GPUs with tensor units.
@bri-prism bri-prism changed the title Bring Karim's MTP verify and prefill kernels to prism; Hopper prefill paths on by default Bring Karim's MTP verify and prefill kernels to prism; measured winners on by default (Hopper prefill, Metal PTQ1_0 verify) Nov 24, 2026
@bri-prism
bri-prism merged commit 68abc91 into prism Nov 24, 2026
10 of 19 checks passed
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.

2 participants