From baf261decad67e5d3cd319c38e4c2e268a61cb2a Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 23 Oct 2026 18:41:43 +0200 Subject: [PATCH] metal: the verified dsv41 activation quantiser, in the engine's kernel tree metal/src/engine/msl/ds4_quant.metal carries the Q8 quantiser that metal/tests/dsv41/q8_quant_parity.mm proved bit-exact (4096 blocks, 131,072 values, 0 differences). It is the first dsv41 kernel in the engine's own MSL directory, next to the Qwen engine's mtp/ple kernels, so the runtime loader will find it by name when the launch is wired. The comment records the driver quirk that cost the debugging: threadgroup_position_in_grid arrived as 0 in every threadgroup on this Mac, so the block index comes from thread_position_in_grid / 32. --- metal/src/engine/msl/ds4_quant.metal | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 metal/src/engine/msl/ds4_quant.metal diff --git a/metal/src/engine/msl/ds4_quant.metal b/metal/src/engine/msl/ds4_quant.metal new file mode 100644 index 00000000..43caa393 --- /dev/null +++ b/metal/src/engine/msl/ds4_quant.metal @@ -0,0 +1,32 @@ +// metal/src/engine/msl/ds4_quant.metal - the DeepSeek engine's activation quantiser, the first dsv41 kernel ported. +// +// CUDA's q8 path (ds4_gguf.cu:201, q8_quant_kernel): per 32 values d = max|x| / 127 as an f16, then q = round(x / d) clamped to +// [-128, 127] as int8. Verified bit-exact against the same arithmetic on the CPU: 4096 blocks, 131,072 values, 0 differences, +// worst scale error 4.6e-04 (half rounding) - metal/tests/dsv41/q8_quant_parity.mm. +// +// The block comes from the flat thread id (thread_position_in_grid / 32), not threadgroup_position_in_grid: the latter arrived +// as 0 in every group on this Mac's driver and made every block recompute block 0. +#include + +using namespace metal; + +// x: n values; ldx: the stride in values between consecutive blocks of 32 (32 for a contiguous row) +kernel void ds4_q8_quant(device const float* x [[buffer(0)]], constant uint& ldx [[buffer(1)]], + device char* q [[buffer(2)]], device half* d [[buffer(3)]], + uint gid [[thread_position_in_grid]], uint lane [[thread_index_in_threadgroup]]) { + const uint b = gid / 32u; + const int base = int(b) * int(ldx); + const float v = x[base + int(lane)]; + float amax = fabs(v); + amax = max(amax, simd_shuffle_xor(amax, 16)); + amax = max(amax, simd_shuffle_xor(amax, 8)); + amax = max(amax, simd_shuffle_xor(amax, 4)); + amax = max(amax, simd_shuffle_xor(amax, 2)); + amax = max(amax, simd_shuffle_xor(amax, 1)); + const float scale = amax / 127.0f; + const float inv = scale > 0.0f ? 1.0f / scale : 0.0f; + int iq = int(rint(v * inv)); + iq = clamp(iq, -128, 127); + q[int(b) * 32 + int(lane)] = char(iq); + if (lane == 0) d[int(b)] = half(scale); +}