mlx-m5-kernels

by liuweid95-hash · Integration · Text & code

Adds hand-tuned Metal GPU kernels for MLX that shrink the attention memory cache and speed up long-context text generation on Apple silicon Macs.

Latest · Nov 28Open-source repo of three Metal kernels for MLX found by evolutionary search on an M5 MacBook Air, with one-command benchmark reproduction

AccessOpen codeLicenseMIT

Evidence

  1. Nov 28
    Fused 4-bit KV-cache FlashDecoding on Llama-3-8B (32:8 GQA, 32K): KV memory 128 MB to 36 MB (-71.9%), 1.171 ms, 8e-6 max error
    Hands-on testliuweid95-hash
  2. Nov 28
    4-bit fused KV-cache kernel ran 2.405x faster than mx.dequantize plus SDPA at 32K context on Llama-3-8B shapes
    Hands-on testliuweid95-hash
  3. Nov 28
    4-bit fused KV-cache kernel ran 1.055x faster than uncompressed FP16 SDPA at 32K context while using 71.9% less KV memory
    Hands-on testliuweid95-hash
  4. Nov 28
    INT8 fused KV-cache kernel cut KV memory 46.9% and ran 2.334x faster than dequantize plus SDPA (1.239 ms), matching FP16 speed, 5.7e-5 error
    Hands-on testliuweid95-hash
  5. Nov 28
    FP16/BF16 GQA FlashDecoding kernel: 1.105x vs mx.fast.scaled_dot_product_attention at 4K (0.324 vs 0.358 ms), 1.005x at 16K (~129 GB/s)
    Hands-on testliuweid95-hash
Show all 6
  1. Nov 28
    Fused residual add + RMSNorm reached 131.6 GB/s (0.510 ms/layer on 2048x4096) with 2x better fp16 precision than unfused add + mx.fast.rms_norm
    Hands-on testliuweid95-hash