Repository navigation
I evolved custom Metal kernels for Apple M5 (applegpu_g17g) on MLX — Fused 4-bit KV-Cache GQA hits 2.40x over dequantize+SDPA (and beats uncompressed FP16 at 32K while saving 71.9% RAM) #5005
liuweid95-hash
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I spent today running a multi-island evolutionary Metal kernel search on a 16GB Apple M5 MacBook Air (
applegpu_g17g, MLX0.32.5), using an isolated subprocess verifier that enforces strictfloat32accuracy gates against official MLX baselines.Packaged the 3 winning kernels into a lightweight open-source repo (
mlx-m5-kernels):mx.quantize(..., group_size=64)):32:8GQA,S=32K), 4-bit cuts KV memory from 128 MB -> 36 MB (-71.9%), runs in 1.171 ms (2.405x faster thanmx.dequantize + SDPA, and 1.055x faster than uncompressed FP16 SDPA!), with8e-6max error vs exact float32 dequantized attention.dequantize + SDPA(1.239 ms), matching FP16 speed with5.7e-5error.mx.fast.scaled_dot_product_attentionat 4K (0.324 msvs0.358 ms) and 1.005x at 16K (~129 GB/s pure GPU KV bandwidth).Pack8<T>coalesced loads + zero-reloadfloat32register cache hitting 131.6 GB/s pure GPU bandwidth (0.510 ms/layeron2048x4096) and 2x better fp16 precision than unfusedadd + mx.fast.rms_norm.Most surprising M5 (
applegpu_g17g) microarchitecture takeaway:Contrary to textbook CUDA GQA advice, packing 4-7 query heads into one SIMD group to share KV loads in registers actually ran 2x slower on M5 due to register spilling (~80 regs/thread). Keeping each 32-thread SIMD group ultra-lean (~10 float registers, zero
threadgroup_barrier, no aggressive unrolling) maxed out occupancy and let M5's shared L2 cache coalesce the GQA KV reads automatically.Repo + 1-command benchmark reproduction (
bash run_reproduce.sh):https://github.com/liuweid95-hash/mlx-m5-kernels
Feedback and M1/M2/M3/M4/M5 Pro/Max benchmark comparisons welcome!
All reactions