Skip to content

Commit 687f69c

Browse files
TheTomclaude
andcommitted
experiment: inline block WORSE on M2 (-10-15%), reverted to 4-mag
Inline dequant in FA loop hurt instruction cache on M2 Pro. Also: ternary-based approaches consistently worse than constant LUT. The 4-mag LUT (4 divergent reads, 0 branches) is the hardware limit for dequant-level optimizations on Apple8/M2. Tested approaches total: 10 4-mag LUT: 15.1 at 8K (BEST) Inline block: 13.5 at 8K (-10.6%) Named-reg ternary: 10.3 at 8K (-31.8%) M2 Pro constraint confirmed: Branches > constant cache divergence > ALU cost Cannot trade constant reads for branches on Apple8. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-Authored-By: tturney@psyguard.ai
1 parent 18738e5 commit 687f69c

2 files changed

Lines changed: 1 addition & 54 deletions

File tree

ggml/src/ggml-metal/ggml-metal-device.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,7 @@ ggml_metal_library_t ggml_metal_library_init(ggml_metal_device_t dev) {
229229
// M5+ (has tensor API): 8-entry full LUT (best decode speed)
230230
if (!ggml_metal_device_get_props(dev)->has_tensor) {
231231
[prep setObject:@"1" forKey:@"TURBO_USE_4MAG"];
232-
[prep setObject:@"1" forKey:@"TURBO_INLINE_BLOCK"];
233-
GGML_LOG_INFO("%s: turbo3 using 4-mag + inline block (pre-M5)\n", __func__);
232+
GGML_LOG_INFO("%s: turbo3 using 4-mag LUT (pre-M5 hardware)\n", __func__);
234233
}
235234

236235
// TurboQuant profiling: set TURBO_PROFILE_MODE env var (0-4)

ggml/src/ggml-metal/ggml-metal.metal

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7339,57 +7339,6 @@ kernel void kernel_flash_attn_ext_vec(
73397339
mqk[cc] += dot((float4) pk4[cc*NE*NS10/4 + ii*NL], (float4) pq4[ii*NL]);
73407340
}
73417341
} else {
7342-
#if TURBO_USE_4MAG && TURBO_INLINE_BLOCK
7343-
// INLINE BLOCK PROCESSING: bypass template dequant entirely.
7344-
// Read all bytes for this thread's slice of the block once,
7345-
// then process 4 elements with 4-mag LUT inline.
7346-
// The compiler sees the full computation and can optimize
7347-
// across the entire loop without function call boundaries.
7348-
{
7349-
device const char * kp = k + ((ic + NE*cc + ty)*args.nb11);
7350-
7351-
FOR_UNROLL (short ii = 0; ii < DK4/NL; ++ii) {
7352-
const short i = ii*NL + tx;
7353-
const short blk_idx = i / nl_k;
7354-
const short il = i % nl_k;
7355-
7356-
// Read block data (same as dequant but inline)
7357-
device const block_turbo3_0 * xb = ((device const block_turbo3_0 *)kp) + blk_idx;
7358-
const float norm = float(xb->norm);
7359-
const uint8_t qb = xb->qs[il];
7360-
const uint8_t sb = xb->signs[il >> 1];
7361-
const int sshift = (il & 1) << 2;
7362-
7363-
const uint8_t q0 = (qb ) & 0x03;
7364-
const uint8_t q1 = (qb >> 2) & 0x03;
7365-
const uint8_t q2 = (qb >> 4) & 0x03;
7366-
const uint8_t q3 = (qb >> 6);
7367-
const uint8_t s0 = (sb >> (sshift )) & 1;
7368-
const uint8_t s1 = (sb >> (sshift + 1)) & 1;
7369-
const uint8_t s2 = (sb >> (sshift + 2)) & 1;
7370-
const uint8_t s3 = (sb >> (sshift + 3)) & 1;
7371-
7372-
const uint8_t mi0 = q0 ^ (s0 ? 0u : 0x3u);
7373-
const uint8_t mi1 = q1 ^ (s1 ? 0u : 0x3u);
7374-
const uint8_t mi2 = q2 ^ (s2 ? 0u : 0x3u);
7375-
const uint8_t mi3 = q3 ^ (s3 ? 0u : 0x3u);
7376-
7377-
const float v0 = float(turbo_mag_3bit_h[mi0]) * norm;
7378-
const float v1 = float(turbo_mag_3bit_h[mi1]) * norm;
7379-
const float v2 = float(turbo_mag_3bit_h[mi2]) * norm;
7380-
const float v3 = float(turbo_mag_3bit_h[mi3]) * norm;
7381-
7382-
const float4 mk = float4(
7383-
s0 ? v0 : -v0,
7384-
s1 ? v1 : -v1,
7385-
s2 ? v2 : -v2,
7386-
s3 ? v3 : -v3
7387-
);
7388-
7389-
mqk[cc] += dot(mk, (float4) sq4[i]);
7390-
}
7391-
}
7392-
#else
73937342
device const kd4_t * pk = (device const kd4_t *) (k + ((ic + NE*cc + ty)*args.nb11));
73947343

73957344
k4_t mk;
@@ -7401,7 +7350,6 @@ kernel void kernel_flash_attn_ext_vec(
74017350

74027351
mqk[cc] += dot((float4) mk, (float4) sq4[i]);
74037352
}
7404-
#endif
74057353
}
74067354

74077355
if (NE == 1) {

0 commit comments

Comments
 (0)