Skip to content
المشاريع

حول

Local LLM inference engine for Apple Silicon in C++20 and Metal, no runtime dependencies. Neural Engine and GPU share one copy of the weights; speculative decoding; OpenAI- and Anthropic-compatible server.

الذكاء الاصطناعي وتعلّم الآلة

كيف تُقاس هذه الأرقام

34 نجمة5 التفرعات +1 خلال 24 ساعة

ما يمكنك فعله

  • 32-token slab-indexed Copy-on-Write Radix KV pool (0.84 GiB peak phys_fp at 256–512 tok to 5.35 GiB at 128k, zero upfront 128k allocation, 0.00 MiB swap through 128k).
  • Optional opt-in Quest query-aware block-sparse decode (--sparse-kv-budget 8192 / 12288, 59.3 tok/s at 128k with 7/7 non-sink needles recalled and KL=0.0070 at 32k Quest-12k).
  • 6-slot in-RAM GDN/KV/DFlash snapshot ring (0.8–2.9 ms rollback/fork across agent turns) + 16-byte-aligned zero-copy mmap checksummed MICACHE3 disk prefix caching (--cache-dir, backward-compatible with MICACHE1/2, 142 ms restore for 18.2k tokens / 721 MiB, configurable --max-disk-cache-gb).
  • Zero External Runtime Dependencies & Regression Suite: Pure C++20 + Objective-C++ + Apple frameworks (Metal, Foundation, CoreML, ImageIO). No Python, no MLX, no external JSON/HTTP libraries. Verified by a CPU/GPU ctest regression and numerical-parity suite: 2 targets, 42 named checks (ctest --test-dir build --output-on-failure).

النمو المقاس للنجوم

خلال 24 ساعة
+١
خلال 7 أيام
جارٍ جمع السجل
خلال 15 يومًا
جارٍ جمع السجل
خلال شهر
جارٍ جمع السجل

تغيّر صافي النجوم خلال فترات متداخلة، وليس مجموعًا يوميًا. تبقى القياسات المفقودة فارغة. النجوم تقيس الاهتمام لا جودة البرمجيات.

README.md

الأصل

mometal

A local LLM inference engine for Apple Silicon (M1–M5), written in C++20, Objective-C++ and Metal with no runtime dependencies. It serves hybrid linear/full-attention models (Qwen3.5/3.8 with vision, Inkling 2 Mini) through an OpenAI- and Anthropic-compatible HTTP server. The Neural Engine reads the prompt, the GPU generates, and in single-copy mode both read the same physical weight pages, so Neural Engine prefill adds almost no memory.

Write-up of the design and the measurements: starmo.ai/reports/mometal-ane-share.

Measured against mlx-lm and Splash

One method for every engine: the same public weights, the same prompts, a fresh server per configuration, timing on the client, median of 3. The method and the launch commands are in benchmarks/README.md. Everything between the two rules below is generated by benchmarks/h2h.py report from benchmarks/results/2026-12-02/raw.jsonl; nothing in it is typed by hand.


Apple M4 Max, 48 GiB, macOS 27.0, SIP off, AC power, commit b22ff8b, 2026-12-02. Weights mlx-community/Qwen3.8-27B-4bit, drafter incoai/Qwen3.8-27B-DFlash2.

Tier Configuration Decode tok/s 4k prefill tok/s 32k prefill tok/s 32k time to first token Peak RAM over idle Start
Same math mometal: GPU only, no drafter, 16-bit KV 29.6 266 231 138 s 17.2 GiB 0.7 s
Same math mometal: GPU only, no drafter, 8-bit KV 29.1 n/a n/a n/a n/a 0.4 s
Same math mlx-lm 0.32.2 (mlx 0.32.5): defaults 27.2 236 211 151 s 37.8 GiB 2.5 s
Same math mlx-lm 0.31.3 (mlx 0.32.1): defaults 26.9 246 215 148 s 36.6 GiB 5.2 s
As shipped mometal: single-copy ANE-Share + DFlash 2, 8-bit KV (needs SIP off) 101.2 498 402 80 s 18.8 GiB 0.9 s
As shipped mometal: single-copy ANE-Share, --ane-rows 256 100.4 734 544 59 s 19.9 GiB 1.2 s
As shipped mometal: CoreML ANE prefill + DFlash 2, 8-bit KV (works with SIP on) 103.0 455 407 79 s 26.4 GiB 0.7 s
As shipped mometal: CoreML ANE prefill, --ane-rows 256 102.2 734 526 61 s 26.9 GiB 0.9 s
As shipped mometal: GPU only + DFlash 2, 8-bit KV 103.0 262 234 137 s 18.2 GiB 0.7 s
As shipped Splash 1.7.3: defaults (ANE + DFlash 2, int8 KV) 111.5 507 405 79 s 21.8 GiB 17.6 s

Decode by task, tok/s (median of 3, minimum to maximum):

Configuration C++ ring buffer Python LRU cache C++ refactor Technical prose Mean
mometal: GPU only, no drafter, 16-bit KV 29.5 (29.5 to 29.6) 29.6 (29.5 to 29.7) 29.6 (29.5 to 29.7) 29.6 (29.6 to 29.7) 29.6
mometal: GPU only, no drafter, 8-bit KV 28.3 (28.3 to 29.5) 28.8 (28.5 to 29.6) 29.5 (28.8 to 29.6) 29.5 (28.4 to 29.5) 29.0
mlx-lm 0.32.2 (mlx 0.32.5): defaults 27.3 (26.4 to 27.3) 27.3 (26.3 to 27.3) 27.1 (26.1 to 27.2) 27.1 (26.0 to 27.2) 27.2
mlx-lm 0.31.3 (mlx 0.32.1): defaults 27.0 (26.7 to 27.3) 27.0 (26.8 to 27.1) 26.9 (26.7 to 27.0) 26.9 (26.8 to 27.0) 26.9
mometal: single-copy ANE-Share + DFlash 2, 8-bit KV (needs SIP off) 85.6 (85.4 to 85.6) 116.8 (116.8 to 117.0) 123.6 (123.1 to 123.9) 74.9 (74.9 to 75.0) 100.2
mometal: single-copy ANE-Share, --ane-rows 256 87.3 (87.3 to 87.3) 113.5 (113.2 to 113.6) 118.0 (117.7 to 118.3) 72.8 (72.7 to 72.8) 97.9
mometal: CoreML ANE prefill + DFlash 2, 8-bit KV (works with SIP on) 90.1 (89.6 to 90.3) 115.9 (115.9 to 116.2) 115.9 (115.8 to 116.2) 69.1 (69.1 to 69.3) 97.8
mometal: CoreML ANE prefill, --ane-rows 256 89.6 (88.4 to 89.6) 114.8 (114.8 to 115.1) 115.2 (115.0 to 115.3) 68.5 (68.5 to 68.6) 97.0
mometal: GPU only + DFlash 2, 8-bit KV 90.4 (89.3 to 90.4) 115.7 (115.6 to 116.0) 125.6 (125.3 to 125.8) 69.0 (69.0 to 69.2) 100.2
Splash 1.7.3: defaults (ANE + DFlash 2, int8 KV) 96.7 (96.2 to 97.0) 129.9 (129.8 to 130.0) 126.3 (125.1 to 126.7) 71.0 (70.9 to 71.2) 106.0

Comparisons (mometal relative to the other engine):

  • Same math against mlx-lm 0.32.2 (mlx 0.32.5): decode +8.6%, 4k prefill +12.6%, 32k prefill +9.4%, peak RAM 17.2 GiB against 37.8 GiB.
  • Same math against mlx-lm 0.31.3 (mlx 0.32.1): decode +9.7%, 4k prefill +7.8%, 32k prefill +7.2%, peak RAM 17.2 GiB against 36.6 GiB.
  • As shipped, single-copy mode against mlx-lm 0.32.2 (mlx 0.32.5): 3.7 times the decode speed, 1.9 times the 32k prefill speed (2.6 times with --ane-rows 256), 0.50 times the peak RAM.
  • As shipped, single-copy mode against mlx-lm 0.31.3 (mlx 0.32.1): 3.8 times the decode speed, 1.9 times the 32k prefill speed (2.5 times with --ane-rows 256), 0.51 times the peak RAM.
  • As shipped, single-copy mode against Splash 1.7.3 at defaults: decode -9.3% (mean of tasks -5.4%; Splash 1.7.3 faster on 3 of 4 tasks), 4k prefill -1.8%, 32k prefill -0.7%, peak RAM 3.1 GiB lower, start 0.9 s against 17.6 s.
  • As shipped, CoreML mode against Splash 1.7.3 at defaults: decode -7.6% (mean of tasks -7.7%; Splash 1.7.3 faster on 4 of 4 tasks), 4k prefill -10.2%, 32k prefill +0.4%, peak RAM 4.6 GiB higher, start 0.7 s against 17.6 s.
  • As shipped, single-copy mode with --ane-rows 256 against Splash 1.7.3 at defaults: decode -10.0% (mean of tasks -7.6%; Splash 1.7.3 faster on 3 of 4 tasks), 4k prefill +44.7%, 32k prefill +34.4%, peak RAM 1.9 GiB lower, start 1.2 s against 17.6 s.
  • As shipped, CoreML mode with --ane-rows 256 against Splash 1.7.3 at defaults: decode -8.3% (mean of tasks -8.4%; Splash 1.7.3 faster on 4 of 4 tasks), 4k prefill +44.9%, 32k prefill +29.9%, peak RAM 5.1 GiB higher, start 0.9 s against 17.6 s.
  • Adding Neural Engine prefill to GPU + DFlash 2 raises peak RAM by 0.6 GiB in single-copy mode and by 8.2 GiB through CoreML.

Checks:

  • 128k context, single-copy mode, 1 run: 126,970 prompt tokens at 244.5 tok/s, first token after 519 s, passkey recalled 1/1, peak RAM 19.3 GiB over idle, swap growth over the whole arm 0 MB.
  • Passkey recalled in 54 of 54 prefill runs at 4k and 32k. No engine reported cached prompt tokens or emitted reasoning tokens.
  • Swap grew during: mlx-lm 0.32.2 (mlx 0.32.5) (+1.2 GiB), mlx-lm 0.31.3 (mlx 0.32.1) (+4.0 GiB). No mometal or Splash arm grew swap.
  • mometal with the drafter on produced the same text as with the drafter off in 5 of 12 runs (GPU only, 8-bit KV, temperature 0).
  • Same text on all 3 fresh runs of every task: 7 of 10 configurations. Not stable: mometal: CoreML ANE prefill + DFlash 2, 8-bit KV (works with SIP on); mometal: CoreML ANE prefill, --ane-rows 256; mometal: GPU only + DFlash 2, 8-bit KV.
  • Text identical, mometal: GPU only, no drafter, 16-bit KV against Splash 1.7.3: defaults (ANE + DFlash 2, int8 KV): 2 of 4 tasks.
  • Text identical, mometal: GPU only, no drafter, 8-bit KV against Splash 1.7.3: defaults (ANE + DFlash 2, int8 KV): 3 of 4 tasks.
  • Text identical, mometal: GPU only, no drafter, 16-bit KV against mlx-lm 0.32.2 (mlx 0.32.5): defaults: 1 of 4 tasks.
  • Text identical, mometal: GPU only, no drafter, 16-bit KV against mometal: single-copy ANE-Share + DFlash 2, 8-bit KV (needs SIP off): 0 of 4 tasks.
  • Same server, same task three times (not the headline; shows reuse of earlier output), median tok/s on repeat 1, 2, 3: mometal: GPU only, no drafter, 16-bit KV: 29.73, 29.56, 29.45; mometal: GPU only, no drafter, 8-bit KV: 29.09, 29.12, 29.11; mlx-lm 0.31.3 (mlx 0.32.1): defaults: 27.07, 27.12, 27.09; mometal: single-copy ANE-Share + DFlash 2, 8-bit KV (needs SIP off): 100.81, 106.51, 116.56; mometal: CoreML ANE prefill + DFlash 2, 8-bit KV (works with SIP on): 96.87, 106.09, 109.83; mometal: GPU only + DFlash 2, 8-bit KV: 95.93, 169.85, 165.56; Splash 1.7.3: defaults (ANE + DFlash 2, int8 KV): 97.64, 101.21, 98.72.
  • WikiText-2 (2048 scored tokens), exact weights, 16-bit KV (reference): perplexity 7.851.
  • WikiText-2 (2048 scored tokens), single-copy Neural Engine weights, 16-bit KV: perplexity 8.639, mean KL to the reference 0.0812, same top token as the reference 88.3%.
  • WikiText-2 (2048 scored tokens), single-copy Neural Engine weights, 8-bit KV: perplexity 8.646, mean KL to the reference 0.0797, same top token as the reference 87.9%.
  • Earlier README prefill figures (commit 4a4b3e5, single-copy mode, another checkpoint and prompt), tok/s: 4k with --ane-rows 256 746.6 then, 733.5 now; 32k with --ane-rows 256 550.1 then, 543.9 now; 4k at default rows 531.7 then, 497.7 now; 128k at default rows 239.7 then, 244.5 now.

How to read it

  • Same math rows run the checkpoint's exact 4-bit weights on the GPU alone, with no drafter and a 16-bit KV cache. They compare kernels and memory layout and nothing else.
  • As shipped rows turn on each engine's own accelerations: Neural Engine prefill, a DFlash 2 speculative drafter, an 8-bit KV cache. These options change the arithmetic. The rows are not kernel speed.
  • The decode gain over mlx-lm in the as-shipped rows comes from the drafter, a separate public model (incoai/Qwen3.8-27B-DFlash2). Splash uses the same drafter. mlx-lm ran at its defaults, without one.
  • Decode is first-exposure speed: every request gets a freshly started server. On a warm server mometal drafts from its own earlier output, so a repeated task runs faster than the table says. The "Same server" check line shows that effect; it is not in the headline.
  • --ane-rows 256 is an option, not the default. It raises long-prompt prefill and is compared against Splash at Splash's defaults.

Limits

  • Splash decodes faster on most tasks. The comparison lines and the decode-by-task table show by how much.
  • Single-copy mode needs SIP disabled and an ACL on the Neural Engine cache (see Caveats). With SIP on, mometal runs Neural Engine prefill through CoreML, which keeps a second copy of the MLP weights, or stays on the GPU.
  • Neural Engine modes are lossy. The Neural Engine pack re-quantizes the MLP weights into a second 4-bit format, and in single-copy mode the GPU decodes from that pack as well. The WikiText-2 check lines give the measured cost on 2,048 scored tokens, which is a small sample.
  • Speculative decoding does not reproduce the no-drafter text at this commit, and some drafter configurations were not stable from run to run at temperature 0. The check lines give the counts and name the configurations. The block-verification algorithm is tested to preserve the sampling distribution; the cause of the greedy differences is not isolated yet.
  • One model on one machine. Qwen3.8-27B 4-bit on an M4 Max with 48 GiB. Other chips and models are not measured here.
  • One request at a time. The server is single-lane.

Key Capabilities

  • Single-Copy ANE + Metal GPU Weight Sharing (--ane-share): Runs dense MLP prefill on Apple's Neural Engine (r64-c4d unified default; --ane-rows 256 r256-c4d peak-prefill) while Metal GPU decode reads the exact same physical pages from /Library/Caches/com.apple.aned/.../model.hwx via read-only MAP_SHARED — eliminating duplicate .safetensors MLP wiring. Features zero-copy IOSurface ↔ MTLBuffer bridging with async MTLSharedEvent GPU↔ANE pipelining, dynamic G=32 ↔ G=8 group-size probing across M1–M5, automatic existing-pack tag detection for explicit --ane-models <dir>, and a native zero-Python ARM64 NEON 4-bit .mlmodelc builder (mometal build-ane, ~7–8 s for 64 layers across hf4, lut4, and zero-runtime-cost per-linear Lloyd-Max nf4 LUTs that cut WT-2 KL by -38.3% vs hf4). When macOS SIP restricts /Library/Caches/com.apple.aned, automatically falls back to SIP-safe CoreML ANE prefill (--no-ane-share, r256-c4d, 743 tok/s 4k prefill on M4 Max — 2.7× GPU-only) on Macs with sufficient RAM headroom (>= 36 GiB for 27B), while safely staying GPU-only on 16–32 GiB Macs (or when --ane-models off is passed).
  • Zero-Disk-Copy GPU DFlash 2, Optimal-Transport Speculative Trees & Wide-DAG (--dflash, --mtp, --suffix-dir): On-the-fly ARM64 NEON BF16 → Q4_AFFINE_G64 loading with automatic hidden_size + vocab_size validation, K_sel=32 fused single-command-buffer GPU block-diffusion drafting (CB#1), fallback to built-in 1-layer MTP (BitScreen 1-bit sign pre-filter), Submodular PQ-DP tree verification (M ∈ {4, 8} context-aware target verify), Bayesian HPYP SuffixCache+DFlash fusion, multi-branch SuffixCache / Wide-DAG prompt & repository drafting (up to 387.5 tok/s at T=0 and 387.3 tok/s at T=0.7 on real codebase edits), and optimal-transport block verification that is tested to preserve the sampling distribution (KH-BTV / CanOT2-BTV + Philox Gumbel-Clock; greedy text is not byte-identical to no-drafter decode, see Limits).
  • 128k Long-Context Metal Kernels, Radix KV & Prefix Caching (KV8 / KV4 / FP16):
    • Single-pass BQ=48 FP16-MMA and M=1 register-resident FlashDecoding kernels, Chunkwise Extended-WY & Tree-WY Gated DeltaNet (GDN) kernels, Metal 4 mpp::tensor_ops::matmul2d backend for M5 (Apple10) Neural Accelerators (shaders/m4_matmul.metal, auto-enabled on M5 / macOS 26+ with SIP on and zero extra RAM; MOMETAL_DISABLE_METAL4=1 to A/B against Metal 3.0), persistent MTLBinaryArchive PSO cache (~1.2 ms warm reload), and MTLResidencySet resource residency across Metal 3.0/3.1/4.0 (M1–M5).
    • 32-token slab-indexed Copy-on-Write Radix KV pool (0.84 GiB peak phys_fp at 256–512 tok to 5.35 GiB at 128k, zero upfront 128k allocation, 0.00 MiB swap through 128k).
    • Optional opt-in Quest query-aware block-sparse decode (--sparse-kv-budget 8192 / 12288, 59.3 tok/s at 128k with 7/7 non-sink needles recalled and KL=0.0070 at 32k Quest-12k).
    • 6-slot in-RAM GDN/KV/DFlash snapshot ring (0.8–2.9 ms rollback/fork across agent turns) + 16-byte-aligned zero-copy mmap checksummed MICACHE3 disk prefix caching (--cache-dir, backward-compatible with MICACHE1/2, 142 ms restore for 18.2k tokens / 721 MiB, configurable --max-disk-cache-gb).
  • Built-In OpenAI & Anthropic HTTP Server (mometal serve) & Native Tokenizer: Native C++20 UTF-8 / UAX#15 NFC tokenizer (2.56 ms cold on 132k tokens; 0/8,728 mismatches vs HuggingFace across 4.1M tokens). Supports OpenAI (/v1/chat/completions, /v1/completions, /v1/models), native Anthropic (/v1/messages, /v1/messages/count_tokens in 0.4–0.5 ms, thinking, tool_use/tool_result, cache_control breakpoints, vision with cached ViT embeddings), preemptible shadow pre-prefill (/v1/messages/prewarm, /v1/prewarm, ~24 ms preemption), mid-prefill disconnect cancellation (TCP FIN & RST) with partial prefix salvage, coalesced RFC 3629 UTF-8 streaming SSE, and /health.
  • Zero External Runtime Dependencies & Regression Suite: Pure C++20 + Objective-C++ + Apple frameworks (Metal, Foundation, CoreML, ImageIO). No Python, no MLX, no external JSON/HTTP libraries. Verified by a CPU/GPU ctest regression and numerical-parity suite: 2 targets, 42 named checks (ctest --test-dir build --output-on-failure).

How Single-Copy ANE Weight Sharing Works

Offloading 27B dense MLP prefill to Apple's Neural Engine more than doubles prompt processing throughput on Apple Silicon, but naively pairing ANE prefill with Metal decode wires both the ~15.4 GiB .safetensors checkpoint and ~8.7 GiB of compiled CoreML model.hwx + I/O buffers.

mometal enforces a strict all-layer single-copy ANE-Share path (if any layer fails boot-time verification, ANE is unloaded and the engine falls back cleanly to GPU-only):

  1. mometal build-ane (also invoked automatically on first --ane-share run) quantizes dense MLP weights natively on CPU using ARM64 NEON into 4D conv1x1 .mlmodelc packs (r64-c4d unified default; --ane-rows 256 r256-c4d peak-prefill option).
  2. At boot, mometal parses Apple's Mach-O (0xbeefface) __KERN* 16-chunk 4-bit weight layout inside /Library/Caches/com.apple.aned/.../model.hwx, verifies the bytes bit-exact against weight.bin (cached in .hwx_manifest), and maps those files read-only via MAP_SHARED.
  3. Metal GPU decode kernels read directly from those shared physical pages (G=32 / G=8 probed dynamically across M1–M5), while prefill exchanges activations over zero-copy IOSurface ↔ MTLBuffer with MTLSharedEvent synchronization, leaving duplicate .safetensors MLP weights unmapped and unwired.

Earlier Measurements (commit 4a4b3e5, 2026-11-29, not re-run)

Everything in this section was measured on 2026-11-29 at commit 4a4b3e5 with mometal's own tooling and has not been re-run. It covers mometal only, on a different checkpoint from the comparison above (qwen38-pi-adapted-mlx4), and it reports single runs. Where the 2026-12-02 run covers the same cell, the newer number is the one to use; the last check line in the generated block shows how four of these figures reproduced. The memory columns here count wired pages only. The comparison above also counts compressed pages and reads about 2 to 3 GiB higher for the same configuration.

Apple M4 Max 48 GiB, AC power. Qwen3.8-27B 4-bit (qwen38-pi-adapted-mlx4) + DFlash 2 (incoai-dflash2-4bit), KV8 dense default, single-copy ANE-Share (64/64 verified), 0.00 MiB swap added across all runs (Release ThinLTO).

Dense KV8 Mid-Context Needle & Memory Sweep (256 → 128k, --ane-rows 256 r256-c4d, T=0)

Every row places a unique passkey mid-context (outside the 128-token sink and final 512 tokens for ctx ≥ 1024) inside a non-repetitive technical corpus, generating 64 tokens (--ane-share --ane-rows 256 --kv-bits 8 --temp 0).

Context (ptok) Needle ANE-Share Prefill TTFT Decode (ms/rnd) Peak phys_fp Wired Peak (+Δ) Swap Δ
256 1/1 64/64 386.6 tok/s 0.66 s 74.53 tok/s (47.0 ms) 0.84 GiB 19.05 GiB (+15.95) 0.00 MiB
512 1/1 64/64 726.9 tok/s 0.70 s 90.01 tok/s (46.7 ms) 0.84 GiB 19.09 GiB (+16.03) 0.00 MiB
1,024 1/1 64/64 748.9 tok/s 1.37 s 98.32 tok/s (49.3 ms) 0.87 GiB 19.09 GiB (+15.99) 0.00 MiB
2,048 1/1 64/64 752.9 tok/s 2.72 s 73.83 tok/s (50.2 ms) 0.94 GiB 19.16 GiB (+16.10) 0.00 MiB
4,096 1/1 64/64 746.6 tok/s 5.49 s 71.70 tok/s (48.8 ms) 1.08 GiB 19.15 GiB (+16.23) 0.00 MiB
8,192 1/1 64/64 717.5 tok/s 11.42 s 63.07 tok/s (50.0 ms) 1.34 GiB 19.56 GiB (+16.50) 0.00 MiB
16,384 1/1 64/64 655.5 tok/s 25.00 s 60.49 tok/s (54.8 ms) 1.87 GiB 20.09 GiB (+16.99) 0.00 MiB
32,767 1/1 64/64 550.1 tok/s 59.57 s 61.27 tok/s (57.1 ms) 2.41 GiB 20.62 GiB (+17.56) 0.00 MiB
65,536 1/1 64/64 419.2 tok/s 156.35 s 48.65 tok/s (61.7 ms) 3.47 GiB 21.58 GiB (+18.62) 0.00 MiB
128,001 1/1 64/64 287.4 tok/s 445.35 s 33.14 tok/s (82.7 ms) 5.35 GiB 23.37 GiB (+20.46) 0.00 MiB

Unified r64-c4d default (--ane-rows 64, 10/10 exact needles, 0.00 MiB swap): faster at 256 (493.9 tok/s, 0.52 s TTFT) and warm boot (~720 ms vs ~885 ms), reaching 514.8 tok/s (512), 524.3 tok/s (2k, 84.28 tok/s decode, 0.94 GiB), 531.7 tok/s (4k, 78.11 tok/s decode, 47.4 ms/rnd, 1.07 GiB), 333.2 tok/s (64k, 48.90 tok/s decode, 61.4 ms/rnd, 3.47 GiB), and 239.7 tok/s (128k, 34.41 tok/s decode, 5.35 GiB).

Real-World Workflows, Opt-In Quest & Wide-DAG (4a4b3e5)

Workload / Scenario Mode Prefill / Restore Decode Throughput Quality / Notes
Coding prompt (next_pow2, 64 tok) Dense KV8 + DFlash 2 (ANE-Share / Pure GPU / MTP) 242.6 / 214.0 tok/s 152.9 / 148.8 / 123.6 tok/s (T=0) Parity with no-drafter text was not reproduced on 2026-12-02, see Limits (M=1 = 29.4 tok/s)
4.2k prose (T=0 / T=0.7) Dense KV8 (r64-c4d) 473.9 / 474.5 tok/s 84.5 tok/s (T=0) · 66.2 tok/s (T=0.7) KH-BTV / CanOT2-BTV verification
Code-copy edit (--suffix-dir src/) Dense KV8 + Wide-DAG (T=0 / T=0.7) 29.7 ms index (442k tok) 387.5 tok/s (T=0) · 387.3 tok/s (T=0.7) Exact 95-line file reproduction + 1-line edit (93–94% spec)
Live pi coding agent (5-turn edit+test) mometal serve (r64-c4d + RAM ring) 0.8–2.9 ms RAM restore Up to 231.6 tok/s (78% spec) Parallel read/edit/bash tools; 0 tool errors
18.2k multi-turn + server restart mometal serve (--cache-dir, MICACHE3) 0.8 ms RAM fork · 142.4 ms disk (721 MiB) 72.7–100.9 tok/s (150 ms warm TTFT) Warm server restart 696.8 ms; count_tokens 0.4–0.5 ms
32k / 64k / 128k non-sink needles Opt-in Quest-8k (--sparse-kv-budget 8192) 423.8 / 339.6 / 250.3 tok/s 57.3 / 55.7 / 59.3 tok/s (54.4 ms/rnd) 7/7 non-sink needles (13.85 GiB total @ 128k)
32k WT-2 quality (eval_quest.py) Dense vs Quest-8k / Quest-12k (KV8) ~87 s (32,768 + 256 tok) — Dense 7.321; Quest-8k KL=0.0125; Quest-12k KL=0.0070, Top1=96.1%, Top5=100%

Notes:

  • Exact dense KV8 is always the default (--sparse-kv-budget 0).
  • In mometal serve, /v1/messages/prewarm yields to live requests in ~24 ms, mid-prefill client disconnect (TCP FIN or RST) cancels and salvages completed prefix chunks into the RAM ring (228–443 ms follow-up completion on 34k-token cancelled prefills), and SIGTERM exits cleanly (exit_code=0, ~133 ms).

Cross-Chip M1–M5 ANE Formats & 4-Bit Quality

WikiText-2 (2,048 scored tokens, KV8, 64-layer Qwen3.8-27B, checkpoint qwen38-pi-adapted-mlx4). On the public mlx-community checkpoint the 2026-12-02 run measured a higher loss for hf4 than this table shows; see the WikiText-2 check lines above.

Format Chips 64-Layer build-ane WT-2 PPL Mean KL vs exact_g64 Top-1 Top-5
exact_g64 (GPU ref) M1–M5 — 7.787 0.0000 100.00% 100.00%
--format nf4 M1–M3 (default), M4–M5 7.23 s 8.070 0.0347 92.82% 99.85%
--format hf4 M4/M5+ (default) 8.39 s 8.249 0.0550 89.75% 99.80%
--format lut4 M1–M5 ~7.5 s — — — —

Live eval-ppl parity checks on 4a4b3e5 (WikiText-2 512 prefix + 256 scored tokens, KV8):

  • Qwen3.8-27B (ANE-Share hf4 vs Pure GPU): PPL 3.9652 vs 3.8939 (+0.0713), mean_kl = 0.0302, Top-1 = 94.14%, Top-5 = 100.00%.
  • Per-linear Lloyd-Max nf4 vs hf4 (small-qwen, 32 layers built in 2.06 s): cuts KL vs Pure GPU (PPL 5.4433) by -38.3% (mean_kl 0.04216 → 0.02602, PPL 5.8388 → 5.6066, Top-1 91.80% → 94.53%, Top-5 = 100.00%) at zero decode overhead.

On M1–M3, ANECompiler promotes linear hf4 to 8-bit; lut4 and nf4 preserve true 0.503 B/wt across M1–M5 (and mometal probes and binds both G=8 and G=32 single-copy GPU dequantization kernels dynamically across M1–M5). Run python3 scripts/probe_m_chip.py to check local Metal 3.0, ANE architecture, and single-copy compatibility in ~30 seconds.

Build & Install

Requires macOS 15+ on Apple Silicon (M1–M5), Xcode Command Line Tools, and CMake >= 3.24.

# Build Release binary (ThinLTO, zero runtime dependencies)
cmake -B build -DCMAKE_BUILD_TYPE=Release && cmake --build build -j

# Run the CPU/GPU regression & numerical-parity suite (2 ctest targets, 42 named checks)
ctest --test-dir build --output-on-failure

# Optional: install binary and Metal shaders (resolved relative to the executable)
cmake --install build --prefix /usr/local

Quickstart

Download any compatible 4-bit MLX checkpoint (e.g., mlx-community/Qwen3.8-27B-4bit) and optional DFlash 2 drafter (e.g., incoai/Qwen3.8-27B-DFlash2):

# 1. Serve with single-copy ANE-Share + DFlash 2 (auto-builds r64-c4d ANE pack on first run)
./build/mometal serve \
  --model /path/to/Qwen3.8-27B-4bit \
  --dflash /path/to/Qwen3.8-27B-DFlash2 \
  --ane-share \
  --max-seq 131072 --kv-bits 8 \
  --cache-dir ~/.cache/mometal-prefix \
  --host 127.0.0.1 --port 8089

# 2. Pure GPU mode (works out of the box with SIP enabled, zero ANE wiring)
./build/mometal serve \
  --model /path/to/Qwen3.8-27B-4bit \
  --dflash /path/to/Qwen3.8-27B-DFlash2 \
  --ane-models off --no-ane-share \
  --max-seq 65536 --kv-bits 8 \
  --host 127.0.0.1 --port 8089

# 3. Universal M1-M5 per-linear Lloyd-Max NF4 quality mode
./build/mometal build-ane --model /path/to/Qwen3.8-27B-4bit --format nf4
./build/mometal serve \
  --model /path/to/Qwen3.8-27B-4bit \
  --dflash /path/to/Qwen3.8-27B-DFlash2 \
  --ane-share --ane-tag nf4 \
  --max-seq 131072 --kv-bits 8

# 4. Index a local repository into the suffix cache for faster coding edits
./build/mometal serve \
  --model /path/to/Qwen3.8-27B-4bit \
  --suffix-dir /path/to/your/repo \
  --max-seq 131072 --kv-bits 8

# 5. One-shot CLI prompt, interactive chat, or realistic benchmark
./build/mometal run   --model /path/to/Qwen3.8-27B-4bit --prompt "Explain radix KV caching."
./build/mometal chat  --model /path/to/Qwen3.8-27B-4bit --max-seq 32768
./build/mometal bench --model /path/to/Qwen3.8-27B-4bit --prompt-tokens 4096 --gen-tokens 128

Common CLI Flags

Flag Description
--model <dir> Path to model directory containing config.json and .safetensors
--model-name <id> Model ID returned by HTTP endpoints (default: basename of --model)
--max-seq <N> Maximum full-attention KV capacity (default: 8192; lazy-grown via Radix KV slabs)
--kv-bits 16|8|4 Full-attention KV cache precision (default: 8)
--sparse-kv-budget <N> Opt-in Quest block-sparse decode budget (0 = exact dense, e.g. 8192 or 12288)
--dflash [auto|off|dir] DFlash 2 drafter (--no-dflash falls back to built-in 1-layer MTP)
--mtp [auto|off|dir] Built-in MTP speculative decoding (--mtp off disables drafter completely)
--no-pld Disable SuffixCache & Wide-DAG prompt-lookup speculative drafting
--suffix-dir <dir> Index code/text files into SuffixCache for speculative drafting (repeatable)
--ane-share / --no-ane-share Enable/disable single-copy ANE-Share (--no-ane-share uses SIP-safe CoreML ANE prefill with auto-build; auto-enabled on >=36 GiB Macs when SIP is on; combine with --ane-models off for pure GPU)
--ane-models <dir|auto|off> ANE .mlmodelc directory (off disables ANE MLP prefill; explicit <dir> auto-detects existing nf4/lut4/hf4 tags when --ane-tag auto)
--ane-rows 64|256|32 ANE batch rows per dispatch (default: unified r64-c4d; 256 for peak long prefill)
--cache-dir <dir> Enable checksummed disk-backed MICACHE3 prefix cache (reads legacy MICACHE1/2)
--max-disk-cache-gb <N> Disk prefix cache cap in GiB (default: 6; 0 = unlimited)
--min-cache-tokens <N> Minimum token length before writing a .micache checkpoint to SSD when --cache-dir is set (default: 1024; prevents SSD wear from short one-off prompts while RAM snapshots still cache >=32-token turns)
--cors-origin <origin> CORS Access-Control-Allow-Origin header in serve mode (default: *; empty omits header)
--no-keep-warm Disable periodic idle GPU/ANE keep-warm pings in serve mode
--seed <N> Deterministic RNG seed for stochastic sampling (default: 42)
--synthetic Use legacy uniform random token IDs in mometal bench (default uses a realistic non-repetitive technical/coding prompt; --prompt sets a custom prompt)

HTTP API (mometal serve)

Endpoint Description
POST /v1/chat/completions OpenAI-compatible chat completions (streaming SSE + non-streaming, tools, reasoning, vision)
POST /v1/completions OpenAI-compatible text completions
GET /v1/models Lists the active model (--model-name or directory basename)
POST /v1/messages Native Anthropic Messages API (thinking, tool_use/tool_result, cache_control, vision, SSE)
POST /v1/messages/count_tokens Exact token count for Anthropic message payloads (text & multimodal, ~0.4–0.5 ms)
POST /v1/messages/prewarm · /v1/prewarm Preemptible background prefix pre-prefill into RAM/disk cache (yields in ~24 ms to live requests)
GET /health Health check and model readiness metadata

Environment Variables

  • MOMETAL_PROFILE=1: Emit per-stage Metal/ANE/speculative timing diagnostics to stderr.
  • MOMETAL_DISABLE_METAL4=1: Disable the Metal 4 mpp::tensor_ops::matmul2d M5 (Apple10) backend to A/B against Metal 3.0.
  • MOMETAL_DFLASH_CACHE_DIR=/path: Additional directory to check during --dflash auto discovery.
  • MI_ALLOW_LOCAL_IMAGE_PATHS=1: Opt in to loading local filesystem image paths in multimodal API requests (disabled by default for safety; data: base64 URIs always work).

Caveats & Operational Notes

  • Experimental Single-Copy ANE-Share & macOS SIP: Single-copy ANE-Share reads Apple's Mach-O model.hwx __KERN* layout inside /Library/Caches/com.apple.aned/.../ModelAssetsCache, which macOS protects with SIP (com.apple.rootless). Enabling --ane-share currently requires disabling SIP and granting read ACL access to your user:
    sudo chmod -R +a "user:$USER allow list,search,read,readattr,readextattr,readsecurity,file_inherit,directory_inherit" /Library/Caches/com.apple.aned
    # check: prints 0 when the ANE cache is readable
    head -c1 "$(find /Library/Caches/com.apple.aned -name model.hwx | head -1)" >/dev/null; echo $?
    
    list on a directory and read on a file are the same macOS right, so files inherit read access; read is spelled out only for clarity. Both conditions are needed: with SIP enabled the ACL alone is not enough. With SIP enabled, mometal detects EACCES immediately at startup and automatically enables SIP-safe CoreML ANE prefill (r256-c4d) on Macs with sufficient RAM headroom (>= 36 GiB for 27B), or stays GPU-only on 16–32 GiB Macs and M5 (Apple10 Metal 4); pass --ane-models off explicitly to force pure GPU without ANE wiring.
  • Single-Lane Interactive Serving: The built-in HTTP server serializes active inference through a single-lane mutex optimized for low-latency single-user workflows and coding agents (with preemptible /v1/messages/prewarm and mid-prefill TCP FIN/RST disconnect cancellation), rather than multi-tenant continuous batching.
  • Cross-Chip Verification Scope: lut4 / nf4 4-bit ANE layouts were verified bit-exact across h13g (M1) through h17g (M5) via ANECompiler.framework; live end-to-end throughput numbers above were measured on an Apple M4 Max (48 GiB).

License

Code is licensed under the MIT License.

استكشف مشاريع مشابهة ومرتبطة

جارٍ التحميل…