Skip to content

metal : PTQ1_0 decode mat-vec through fp16 subnormals - #504

Open
leoperleo wants to merge 1 commit into
PrismML-Eng:prismfrom
Loomstar-app:pr/metal-ptq1-decode-sn
Open

leoperleo wants to merge 1 commit into
PrismML-Eng:prismfrom
Loomstar-app:pr/metal-ptq1-decode-sn

Conversation

@leoperleo

Copy link
Copy Markdown

Overview

At decode the PTQ1_0 mat-vec is slower than PQ2_0 on M2 Pro even though it reads 18% fewer bytes: at 248320x5120 the stock kernel takes 3166 us (88 GB/s). Two things hold it back. ptq1_0_dot_reg spends a multiply and a floor per trit before the fma, and each thread reads its bytes with four loads per row, two of them single bytes, which made the weight reads alone slower than the math.

kernel_mul_mv_ptq1_0_f32_sn takes the five trits of a byte from ptq1_0_lut (copied to threadgroup memory once per threadgroup) as 2-bit fields at bits 0, 2, 4, 6, 8. A field in the low bits of a half is the exact subnormal t*2^(2n-24), so one and decodes a trit, or the same trit of two bytes that share a half2, and the fma converts it to fp32 for free. The activations are scaled by 2^(24-2n) once per block and reused by every row. The thread-to-byte mapping is the one of kernel_mul_mv_ptq1_0_f32, read with three loads and no byte loads. Threadgroups of 8 rows; K is split over 2 simdgroups below 10240 rows.

It is used for 1-row, 2D PTQ1_0 x f32 mat-vecs. GGML_METAL_PTQ1_SN=0 keeps the stock kernel.

Additional information

M2 Pro 16-core GPU, Ternary-Bonsai-2.5-27B PTQ1_0, same binary, same session, llama-bench -fa 1 -r 5:

prism this PR
tg128 12.58 16.70
pp512 69.86 69.85

test-backend-ops perf, n=1: 248320x5120 3166 -> 2061 us, 17408x5120 228 -> 154 us, 5120x17408 227 -> 155 us, 10240x5120 134 -> 91 us.

Not bit-identical to kernel_mul_mv_ptq1_0_f32 (different summation order). Greedy 256 tokens identical; perplexity 6.0949 vs 6.0949 at -ub 1 (decode path, 6x256). New test-backend-ops cases for PTQ1_0 n=1 at the model shapes (10240x5120, 17408x5120, 5120x17408, 6144x5120, 5120x6144, 248320x5120); MUL_MAT 3206/3207 on Metal, MUL_MAT_ID for PTQ1_0 85/85. The one failure is pq2_0 m=1, n=1, k=1024, which also fails intermittently with GGML_METAL_PTQ1_SN=0 (1 of 3 runs) and does not go through this kernel.

Independent of #492-#495. Together with them PTQ1_0 decode reaches 17.9 t/s on the same machine (PQ2_0: 19.6).

Only tested on M2 Pro. Numbers on M3/M4/M5 would tell if it should be the default there too.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. The code, tests, benchmarks and this description were written with Claude Opus 5.5. I reviewed the changes, ran the tests and benchmarks on my M2 Pro, and I am responsible for them.

ptq1_0_lut gives a byte's five trits as 2-bit fields at bits 0, 2, 4, 6, 8. A
field in the low bits of a half is the exact subnormal t*2^(2n-24), so one and
decodes a trit (two when two bytes share a half2) and the fma converts it to
fp32. The activations are scaled by 2^(24-2n) once per block and reused by
every row. The floor chain of ptq1_0_dot_reg spent a multiply and a floor per
trit before the fma.

Same thread-to-byte mapping as kernel_mul_mv_ptq1_0_f32, read with three loads
and no byte loads. Threadgroups of 8 rows; K is split over 2 simdgroups below
10240 rows. Used for 1-row, 2D PTQ1_0 x f32 mat-vecs; GGML_METAL_PTQ1_SN=0 keeps
the stock kernel. New test-backend-ops cases for PTQ1_0 n=1 at the model shapes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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