From 1bea4d8d3150a1132e9ccc7093314e14b704078e Mon Sep 17 00:00:00 2001 From: leoperleo <203133183+leoperleo@users.noreply.github.com> Date: Sat, 28 Nov 2026 19:52:37 +0100 Subject: [PATCH] metal : PTQ1_0 decode mat-vec through fp16 subnormals 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 --- ggml/src/ggml-metal/ggml-metal-device.cpp | 17 ++++ ggml/src/ggml-metal/ggml-metal-device.h | 1 + ggml/src/ggml-metal/ggml-metal-ops.cpp | 36 +++++++ ggml/src/ggml-metal/kernels/mul_mv.metal | 117 ++++++++++++++++++++++ tests/test-backend-ops.cpp | 5 + 5 files changed, 176 insertions(+) diff --git a/ggml/src/ggml-metal/ggml-metal-device.cpp b/ggml/src/ggml-metal/ggml-metal-device.cpp index d55b152bf0b9..289044663c01 100644 --- a/ggml/src/ggml-metal/ggml-metal-device.cpp +++ b/ggml/src/ggml-metal/ggml-metal-device.cpp @@ -496,6 +496,23 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv_q2_extc(g } // GGML_METAL_PQ2_COEF at 4..8 rows: mul_mv_ext's arithmetic over all columns at once (kernel_mul_mv_pq2_0_f32_extc) +// PTQ1_0 decode, 1 row (kernel_mul_mv_ptq1_0_f32_sn): the trit table, then the per-simdgroup sums +ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv_ptq1_sn(ggml_metal_library_t lib, int nr0, int nsg) { + char base[256]; + char name[256]; + snprintf(base, 256, "kernel_mul_mv_ptq1_0_f32_sn_r%d", nr0); + snprintf(name, 256, "%s", base); + ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); + if (!res.pipeline) { + res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); + } + res.nr0 = nr0; + res.nr1 = 1; + res.nsg = nsg; + res.smem = 256*sizeof(uint16_t) + (size_t) nr0*nsg*sizeof(float); + return res; +} + ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv_pq2_extc(ggml_metal_library_t lib, int nr1, int nrl, int nsg) { char base[256]; char name[256]; diff --git a/ggml/src/ggml-metal/ggml-metal-device.h b/ggml/src/ggml-metal/ggml-metal-device.h index 57a6347bf6e3..5566b0fc1fd8 100644 --- a/ggml/src/ggml-metal/ggml-metal-device.h +++ b/ggml/src/ggml-metal/ggml-metal-device.h @@ -137,6 +137,7 @@ struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv_pt struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_coef_prepass (ggml_metal_library_t lib, enum ggml_type t); struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv_pq2_coef (ggml_metal_library_t lib, int nr0, int nr1, int nsg); struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv_pq2_extc (ggml_metal_library_t lib, int nr1, int nrl, int nsg); +struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv_ptq1_sn (ggml_metal_library_t lib, int nr0, int nsg); struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv_q2_extc (ggml_metal_library_t lib, int nx, int nr1, int nrl, int nsg); struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_cumsum_add (ggml_metal_library_t lib, const struct ggml_tensor * op); struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_tri (ggml_metal_library_t lib, const struct ggml_tensor * op); diff --git a/ggml/src/ggml-metal/ggml-metal-ops.cpp b/ggml/src/ggml-metal/ggml-metal-ops.cpp index fe9bb4383174..ab7c11eecc10 100644 --- a/ggml/src/ggml-metal/ggml-metal-ops.cpp +++ b/ggml/src/ggml-metal/ggml-metal-ops.cpp @@ -3031,6 +3031,42 @@ static int ggml_metal_op_mul_mat_mv(ggml_metal_op_t ctx, int idx) { ggml_metal_library_t lib = ctx->lib; ggml_metal_encoder_t enc = ctx->enc; + // PTQ1_0 decode, 1 row, trits decoded as fp16 subnormals (GGML_METAL_PTQ1_SN=0 to disable, _NR0 / _NSG to tune) + { + static const bool sn_on = !getenv("GGML_METAL_PTQ1_SN") || atoi(getenv("GGML_METAL_PTQ1_SN")) != 0; + static const int sn_nr0 = getenv("GGML_METAL_PTQ1_SN_NR0") ? atoi(getenv("GGML_METAL_PTQ1_SN_NR0")) : 8; + static const int sn_nsg = getenv("GGML_METAL_PTQ1_SN_NSG") ? atoi(getenv("GGML_METAL_PTQ1_SN_NSG")) : 0; + const ggml_tensor * s0 = op->src[0]; + const ggml_tensor * s1 = op->src[1]; + if (sn_on && s0->type == GGML_TYPE_PTQ1_0 && s1->type == GGML_TYPE_F32 && op->type == GGML_TYPE_F32 && + s1->ne[1] == 1 && s0->ne[2] == 1 && s0->ne[3] == 1 && s1->ne[2] == 1 && s1->ne[3] == 1 && + s0->ne[0] % 128 == 0 && s1->nb[0] == sizeof(float) && s0->nb[1] % 4 == 0 && ggml_is_contiguous(op)) { + const int nr0 = sn_nr0 == 4 ? 4 : 8; + // enough threadgroups without splitting K from 10240 rows (measured M2 Pro) + const int nsg = (sn_nsg >= 1 && sn_nsg <= 8) ? sn_nsg : (s0->ne[1] >= 10240 ? 1 : 2); + + auto pipeline = ggml_metal_library_get_pipeline_mul_mv_ptq1_sn(lib, nr0, nsg); + + ggml_metal_kargs_mul_mv args = {}; + args.ne00 = (int32_t) s0->ne[0]; + args.ne01 = (int32_t) s0->ne[1]; + args.nb01 = s0->nb[1]; + args.ne0 = (int32_t) op->ne[0]; + args.ne1 = (int32_t) op->ne[1]; + args.nr0 = nr0; + + ggml_metal_encoder_set_pipeline(enc, pipeline); + ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); + ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(s0), 1); + ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(s1), 2); + ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3); + ggml_metal_encoder_set_threadgroup_memory_size(enc, pipeline.smem, 0); + ggml_metal_encoder_dispatch_threadgroups(enc, (int) ((s0->ne[1] + nr0 - 1)/nr0), 1, 1, 32, nsg, 1); + + return 1; + } + } + GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); diff --git a/ggml/src/ggml-metal/kernels/mul_mv.metal b/ggml/src/ggml-metal/kernels/mul_mv.metal index faad89b7b690..b1f8229ba8d4 100644 --- a/ggml/src/ggml-metal/kernels/mul_mv.metal +++ b/ggml/src/ggml-metal/kernels/mul_mv.metal @@ -1423,6 +1423,123 @@ kernel void kernel_mul_mv_pq2_0_f32( kernel_mul_mv_pq2_0_f32_impl(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg); } +// PTQ1_0 decode, 1 row. ptq1_0_lut turns a byte into its five trits as 2-bit fields at bits 0, 2, 4, 6, 8, and a +// field in the low bits of a half is the exact subnormal t*2^(2n-24): one and decodes a trit, two of them when two +// bytes share a half2, and the fma converts it to fp32 for free. The floor chain of ptq1_0_dot_reg spent a multiply +// and a floor per trit before the fma. The activations are scaled by 2^(24-2n) once per block and reused by every row. +// The table is copied to threadgroup memory once per threadgroup. Thread it of the 8 that cover a block owns the +// same bytes as in kernel_mul_mv_ptq1_0_f32_impl: qs[2*it], qs[2*it+1], qs[16+it] and trit it>>1 of qh[it&1]. +// It reads them with three loads and no byte loads (the half-word that holds qs[16+it], and the word at byte 24 +// that holds qh[0], qh[1] and d): byte loads made the weight reads slower than the math. +// Threadgroups of nr0 rows split K over nsg simdgroups and add the partial sums in threadgroup memory. +template +kernel void kernel_mul_mv_ptq1_0_f32_sn( + constant ggml_metal_kargs_mul_mv & args, + device const char * src0, + device const char * src1, + device char * dst, + threadgroup char * shmem [[threadgroup(0)]], + uint3 tgpig[[threadgroup_position_in_grid]], + ushort tiitg[[thread_index_in_threadgroup]], + ushort tiisg[[thread_index_in_simdgroup]], + ushort sgitg[[simdgroup_index_in_threadgroup]], + ushort nsg [[simdgroups_per_threadgroup]]) { + threadgroup ushort * lut = (threadgroup ushort *) shmem; + threadgroup float * sums = (threadgroup float *) (shmem + 256*sizeof(ushort)); + + for (short i = tiitg; i < 256; i += 32*nsg) { + lut[i] = ptq1_0_lut[i]; + } + threadgroup_barrier(mem_flags::mem_threadgroup); + + const int nb = args.ne00/QK_PTQ1_0; + const int first_row = tgpig.x*nr0; + + // 32-bit row offsets use fewer registers than nr0 pointers. Tail rows read the last row and write nothing. + const uint rowb = (uint) args.nb01; + device const char * base = src0 + (uint64_t) first_row*args.nb01; + const int nrow_valid = args.ne01 - first_row; + + device const float * y = (device const float *) src1; + + const short ix = tiisg/8; + const short it = tiisg%8; + + // this thread's qh trit: field it>>1 of byte qh[it&1], element 120 + it + const short nq = it >> 1; + const ushort mq = (ushort) (3u << (2*nq)); + const float sq = (float) (1u << (24 - 2*nq)); + + // c[2n+k]: element n*16 + 2*it + k (qs[2*it+k]); c[10+n]: element 80 + n*8 + it (qs[16+it]); c[15]: element 120 + it + float c[16]; + float sumf[nr0] = { 0.f }; + + for (int ib = ix + 4*sgitg; ib < nb; ib += 4*nsg) { + device const float * yb = y + ib*QK_PTQ1_0; + float sumy = 0.f; + + FOR_UNROLL (short n = 0; n < 5; n++) { + const float s = (float) (1u << (24 - 2*n)); + const float v0 = yb[n*16 + 2*it + 0]; + const float v1 = yb[n*16 + 2*it + 1]; + const float v2 = yb[80 + n*8 + it]; + sumy += (v0 + v1) + v2; + c[2*n + 0] = v0*s; + c[2*n + 1] = v1*s; + c[10 + n] = v2*s; + } + { + const float v = yb[120 + it]; + sumy += v; + c[15] = v*sq; + } + + const uint ob = (uint) ib*sizeof(block_ptq1_0); + FOR_UNROLL (short row = 0; row < nr0; row++) { + device const char * pb = base + ((uint) min((int) row, nrow_valid - 1)*rowb + ob); + + const ushort b01 = *((device const ushort *) (pb + 2*it)); + const ushort b2p = *((device const ushort *) (pb + 16 + 2*(it >> 1))); + const uint qx = *((device const uint *) (pb + 24)); // qh[0], qh[1], d + const uint w01 = (uint) lut[b01 & 0xFF] | ((uint) lut[b01 >> 8] << 16); + const ushort w2 = lut[(b2p >> (8*(it & 1))) & 0xFF]; + const ushort wq = lut[(qx >> (8*(it & 1))) & 0xFF] & mq; + + float acc = 0.f; + FOR_UNROLL (short n = 0; n < 5; n++) { + const half2 h01 = as_type(w01 & (0x00030003u << (2*n))); + const half h2 = as_type((ushort) (w2 & (3u << (2*n)))); + acc = fma((float) h01[0], c[2*n + 0], acc); + acc = fma((float) h01[1], c[2*n + 1], acc); + acc = fma((float) h2, c[10 + n], acc); + } + acc = fma((float) as_type(wq), c[15], acc); + + sumf[row] += (float) as_type((ushort) (qx >> 16)) * (acc - sumy); + } + } + + FOR_UNROLL (short row = 0; row < nr0; row++) { + const float t = simd_sum(sumf[row]); + if (tiisg == 0) { + sums[sgitg*nr0 + row] = t; + } + } + threadgroup_barrier(mem_flags::mem_threadgroup); + + if (sgitg == 0 && tiisg < nr0 && first_row + tiisg < args.ne01) { + float t = 0.f; + for (short s = 0; s < nsg; s++) { + t += sums[s*nr0 + tiisg]; + } + ((device float *) dst)[first_row + tiisg] = t; + } +} + +typedef decltype(kernel_mul_mv_ptq1_0_f32_sn<8>) mul_mv_ptq1_0_sn_t; +template [[host_name("kernel_mul_mv_ptq1_0_f32_sn_r4")]] kernel mul_mv_ptq1_0_sn_t kernel_mul_mv_ptq1_0_f32_sn<4>; +template [[host_name("kernel_mul_mv_ptq1_0_f32_sn_r8")]] kernel mul_mv_ptq1_0_sn_t kernel_mul_mv_ptq1_0_f32_sn<8>; + // 2-bit coefficient pre-pass (GGML_METAL_PQ2_COEF, 1..3 rows): the activations are staged once per column as base-4 // collapse coefficients, the same float arithmetic as the stock PQ2_0 kernels at 1..3 rows. // Layout per column and block: [it*16 + j] the 16 base-4 collapse coefficients of thread it (il = 16*it), diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index a3e83de2dddc..3a7b845b712a 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -12528,6 +12528,11 @@ static std::vector> make_test_cases_eval() { test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_PTQ1_0, GGML_TYPE_F32, 4, 2, false, 70, n, 2048)); test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_PQ2_0, GGML_TYPE_F32, 4, 2, false, 70, n, 2048)); } + // PTQ1_0 decode at the real Ternary-Bonsai-2-27B shapes (m = output rows, k = input width): ffn_up/gate, ffn_down, + // DeltaNet qkv, z, and the 248320-row output head. The Metal decode kernel takes these. + for (auto mk : std::vector>{{10240, 5120}, {17408, 5120}, {5120, 17408}, {6144, 5120}, {5120, 6144}, {248320, 5120}}) { + test_cases.emplace_back(new test_mul_mat(GGML_TYPE_PTQ1_0, GGML_TYPE_F32, mk[0], 1, mk[1], {1, 1}, {1, 1})); + } // PTQ1_0 1..8-row batches on shapes the SYCL T2 path takes (N % 16 == 0, K % 128 == 0): with GGML_SYCL_PTQ1_T2=all // they run on the GEMV, and with GGML_SYCL_T2_VGEMM_MIN=k the k..8-row ones on GemmMT in the GEMV's K-slice order // (N <= 8192: 4 slices, N > 8192: 2)