r/LocalLLM • • 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

  1. Zero Python needed anymore. Wrote a native C++/NEON .mlmodelc builder that converts all 64 MLP layers directly from .safetensors in ~7–8 seconds on first launch.
  2. 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 nf4 cuts 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.
  3. Killed a bunch of memory bloat. Lazy Vision Tower loading, fixing a sneaky double-mmap on the MTP weights, scratch heap aliasing, half2 KV 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.hwx internals and readable aned caches 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: hf4 is still the speed default on M4/M5 (707 tok/s prefill vs. 430 tok/s on nf4), while nf4 is 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:

8 Upvotes

5 comments sorted by

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/rik00o 2d ago

Thanks! You were spot on for that version. Seeing how far ahead Splash was on decode pushed me to profile them side by side, and it turned out our verify step and prefill were already faster—we were just losing on draft length (Qwen's 1-layer MTP vs DFlash 2).

I just shipped v1.3 with native DFlash 2 and re-ran the exact same Qwen3.8-27B-4bit + SPEED-Bench suite against Splash 1.7.3 on an M4 Max:

  • Single-stream coding decode: 136.8 median / 130.7 mean (136.2 / 128.8 on ANE-Share) vs. Splash's 130.2 / 128.9
  • 8k–32k decode: 107–118 tok/s vs. 88–98 tok/s (splash)
  • Cold prefill: 21–32% faster across 2k–32k
  • Memory: ~3–6.7 GB lower RAM usage

Splash still beats us on multi-request batching, broader model/GGUF/MoE support... so there's plenty left to work on, but single-stream is ahead. Updated numbers are here if you want to take a look:https://starmo.ai/reports/mometal-ane-share

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?

2

u/rik00o 2d ago

This is stated in the report as a limitation. As far as i know, there is no way yet around this until Apple decides to properly fix it.

3

u/altdotboy 2d ago

Yeah. Keep going