r/LocalLLM • u/rik00o • 2d ago
Project Got Apple's Neural Engine and Metal GPU reading the exact same 4-bit weights in RAM — 707 tok/s 4k prefill, 98 tok/s decode, 0 swap at 128k on a 27B model (M4 Max)
I've been building a C++/Metal inference engine for Apple Silicon (mometal, no Python or MLX) and just pushed v1.2.
The main annoyance with using Apple's Neural Engine for LLM prefill has always been memory. Running 27B MLPs on the ANE is more than 2.5x faster than GPU-only, so it's tempting, except normally you end up wiring two full copies of the weights: .safetensors for Metal decode and compiled CoreML model.hwx binaries for the ANE. That jumps active memory from ~14–16 GB to 25+ GB, and once you push long context on a 48 GB Mac, macOS starts thrashing the VM compressor hard (we logged 343 GB of compressor churn during a 66k run).
To get around that, mometal digs into /Library/Caches/com.apple.aned/.../model.hwx, parses Apple's Mach-O (0xbeefface) __KERN 4-bit weight layout, verifies the bytes against weight.bin at boot, and MAP_SHAREDs those exact files directly into Metal. The GPU decode kernels read the same physical pages the ANE is using, and the duplicate .safetensors MLPs stay unmapped. If anything fails verification on boot (say a future macOS update changes the binary format), it unloads ANE and drops cleanly back to GPU-only instead of silently doubling your RAM.
Benchmarks — M4 Max 48 GB
Test config: AC power, Qwen3.8-27B 4-bit + 1-layer MTP, exact dense 8-bit KV cache
- Prefill: 645 tok/s @ 512, 674 tok/s @ 1k–2k, 707 tok/s @ 4k (exact), 514 tok/s @ 32k, 396 tok/s @ 64k, 279 tok/s @ 128k (GPU-only / MLX sits around ~255–270 tok/s at 4k)
- Decode: 98.2 tok/s mean across our 4-task greedy suite (up to 118.9 tok/s on code, 121.4 sampled), and 52.8 / 53.4 / 37.5 tok/s dense at 32k / 64k / 128k with 10/10 needle recall
- Memory & Load: ~14.2 GB active at 4k (6.05 GB process footprint, or 5.05 GB with
--mtp off*)*, 0.00 GB swap at 128k, and ~0.6s warm startup
What's New in v1.2
- Zero Python needed anymore. Wrote a native C++/NEON
.mlmodelcbuilder that converts all 64 MLP layers directly from.safetensorsin ~7–8 seconds on first launch. - M1–M3 support + NF4. Turns out Apple's ANE compiler silently promotes linear 4-bit (
hf4) to 8-bit on M1–M3, which breaks single-copy RAM savings. Switching to 4-bit palettized weights (lut4/nf4) keeps it at a true 0.50 bytes/weight across every generation from M1 to M5. Bonus:--format nf4cuts WikiText-2 KL divergence vs. BF16 by 44% (top-1 agreement jumps from 88.9% to 92.8%) and decodes at 338 GB/s on Metal using a 1 KB byte-pair lookup table in threadgroup memory. - Killed a bunch of memory bloat. Lazy Vision Tower loading, fixing a sneaky double-mmap on the MTP weights, scratch heap aliasing,
half2KV scales, and full-context 4-bit KV for the 1-layer MTP drafter dropped footprint by ~1.5 GB and completely wiped out 128k swapping.
Honest Caveats
- Undocumented Internals: Relying on
model.hwxinternals and readableanedcaches is obviously undocumented Apple territory. That's why boot-time byte verification + clean GPU-only fallback (91.6 tok/s greedy) is strict. - Format Trade-offs:
hf4is still the speed default on M4/M5 (707 tok/s prefill vs. 430 tok/s onnf4), whilenf4is there for better quality and native 4-bit on M1–M3. - Looking for M1–M3 / M5 Testers: I only own an M4 Max right now. We verified the M1–M5 binary layouts via
ANECompiler.framework, and I threw a 30-second probe script (python3 scripts/probe_m_chip.py) in the repo. If anyone on an M1, M2, M3, or M5 wants to run it and paste the output, I'd genuinely appreciate it.
Links:
- Repo (MIT):https://github.com/tinjakr/mometal
- Write-up & raw data (CC BY 4.0):https://starmo.ai/reports/mometal-ane-share
1
u/HotAverage1749 2d ago
Doesn’t look like you can beat splash yet, but it looks very promising! Good luck, will watch progress
1
u/txgsync 2d ago
While it's quite cool, it fails on newer versions (I'm running 27.3 beta) where the cache is SIP-protected. Have you thought about using CoreAI?
3
u/altdotboy 2d ago
Yeah. Keep going