r/MacLocalLLM • u/DankpawsDev • 6d ago
DankAttn: From-scratch Metal 4 / MPP inference engine for Hy4-flash (52 tok/s base, 117–357 tok/s speculative, SSD-streamed Engram)
I've been building a from-scratch inference engine in Objective-C++ and Metal 4 for Apple Silicon. There is no PyTorch, MLX, or llama.cpp in the inference path. I've been using this for both Qwen Flash Next and, more recently, Hy4-Flash. I just pushed the code and my quantized weight pack for tencent/Hy4-flash.
Engine: https://github.com/Dankpaws/DankAttn
Pre-packed weights (W4g64 GPTQ experts, W8 dense/attn, FP8 Engram, DFly2 drafter): https://huggingface.co/Dankpaws/Hy4-flash-DankAttn-g64-gptq-v3
Quick numbers (M5 Ultra, 12k context, kv8 on sparse layers):
- Prefill (12,272 tok): 1,700 tok/s GPU (1,694 wall)
- Single-lane decode: 52.4 tok/s (19.08 ms/tok)
- Speculative decode (DFly2 + 3-gram context lookup): 117–128 tok/s wall on general/code prompts, 357 tok/s on code edits (400/400 exact greedy match)
- Multi-lane: 376 tok/s at 16 lanes (btest), 200 tok/s on 4-lane joint speculation
- Quality vs bf16: 16k-calibrated GPTQ cut held-out KL by 28.8% vs RTN (wikitext-2 512-tok cold chunks: KL 0.287 / 81.9% top-1; kv8 vs bf16 KV: 97.4–98.4% top-1)
A few fun technical details:
- Uses Metal 4 MetalPerformancePrimitives (mpp::tensor_ops cooperative matmul2d) with register-direct W4/W6/W8 unpacking and indirect-dispatch grouped MoE prefill.
- Compiles kernels with MTLMathModeSafe + precise FP and uses counter-hashed Gumbel-max sampling, so 1-row decode, 16-row verify, and multi-lane speculative batching stay bit-identical.
- The 63.4 GB FP8 Engram n-gram table streams on demand from local NVMe instead of sitting in RAM, and slot 0 has an async SSD prefix cache.
- If you like perf deep-dives, hy4/ARCHITECTURE_AND_PERF.md has per-op GPU timestamp breakdowns and a log of optimizations I tried and rejected.
Caveats / requirements:
Needs macOS + Apple Silicon with Metal 4 / MPP support, ~151 GB NVMe, and ideally 128 GB+ unified memory (~87 GB weights + ~6.5 GB wired GPU state for 4 lanes; 96 GB works with a smaller cap / single lane).
I've only benchmarked it on an M5 Ultra so far, so if anyone tries it on other Metal 4 hardware I'd love to hear how it behaves!
1
u/aboeing 6d ago
Sounds cool, happy to try it - but don't have the ram required. Any chance of supporting a smaller model?
(I've been building a toy implementation for bitnet - happy to contribute that if your interested; makes it easy for others to test with a tiny model. I would just use an AI to 'translate' it into your package)
1
u/agapes1270 2d ago
Man you did wonderful job , not sure why your repo not got viral yet, the fastest and the best engine for hy4 on mac
1
u/agapes1270 2d ago
Should we hope to get faster speed with DankAttn on Hy4? The current one is amazing im getting 80tps on Mac Ultra 96gb but like to get more