Repository navigation
Conversation
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]>
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.
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_regspends 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_sntakes the five trits of a byte fromptq1_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 ofkernel_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=0keeps 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: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). Newtest-backend-opscases 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 ispq2_0m=1, n=1, k=1024, which also fails intermittently withGGML_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