r/SideProject • u/zouyee • 10h ago
dmlx: an open-source Zig + MLX + Metal + ANE local inference engine for Apple Silicon
I've been building dmlx (MIT licensed) — a Zig-controlled local LLM inference and serving runtime for Apple Silicon built on mlx-c, custom Metal kernels, and Apple Neural Engine (ANE) acceleration, with zero Python required at serve time.
Why another Apple Silicon runtime?
I wanted a single, inspectable local server on my 48 GB M4 Pro that could actually sit behind coding agents (Claude Code, Codex, aider, Cline) without eating all RAM or re-prefilling 8K prompts on every restart:
- Model-specific speculative decoding — dedicated MTP and single-command-buffer native Metal DFlash2 drafters with transactional rollback, prefix-cache restore, and custom verify QMM kernels.
- Memory-bounded execution — run models whose raw tables/experts exceed unified memory by streaming cold weights from SSD via
preadinstead of forcing everything into GPU RAM. - Heterogeneous ANE + GPU prefill — put the idle Apple Neural Engine to work in parallel with GPU MoE during long-context prefill.
- Agent-grade serving out of the box — OpenAI Chat Completions, Anthropic
/v1/messages, and Codex/v1/responsesAPIs with streaming tool calls, properthinking/no-thinking(reasoning_content) splitting, disk-persisted prefix snapshots, LoRA load-time merge, OOM memory guard, anddmlx connect.
Three Flagship Paths (Measured on Single M4 Pro 48 GB)
| Model Path | Peak Server RSS | Measured Speed | Key Mechanism |
|---|---|---|---|
| Qwen4-35B-A3B-4bit + DFlash2 + ANE | 8.42 GB | 119.99 tok/s math (107.20 e2e) · 104.78 tok/s code (96.25 e2e) · 883.3 / 870.9 tok/s 2K/8K prefill | 11.5 GB PLE tables streamed off-GPU via 12-thread CPU pread; 8-bit single-CB Metal DFlash2; ANE 40-layer Shared Expert + 30-layer GDN parallel with GPU MoE (+13.4% 8K prefill); warm 8K TTFT 0.237 s; optional 8-bit QSA KV (−46.88% KV RAM) |
| Qwen3.8-27B-4bit + DFlash2 / MTP | 17.30 GB | 44.50 tok/s 4-case GMean (52.48 2K greedy · 46.98 short greedy · 39.44 8K sampled) | Fused Metal GDN/RoPE/add+rmsNorm kernels, GPU-chained drafts, Vision (bf16 ViT + MRoPE), load-time PEFT LoRA merge |
| DeepSeek V4 Flash 4-bit (284B MoE) | ~20.3 GiB (+ SSD) | 1.24 tok/s effective completion | Native MLX-free path with packed expert files, pread, bounded SMELT hot-expert cache (NATIVE_SMELT_N=20), async cold loading (full preload would need ~147 GB) |
(Other registered architectures like Llama, Mistral, Qwen2/3, Gemma, GLM-4/5, Phi, and Inkling 2 Mini also exist as compatibility paths.)
Try It (macOS arm64, self-contained bundle — no Zig or Python needed)
|
# Serve Qwen4-35B-A3B + DFlash2 (ANE prefill on by default, DMLX_ANE=0 to disable)
DMLX_QWEN4_DFLASH2_DIR=/path/to/Qwen4-35B-A3B-Dflash2 \
# Or Qwen3.8-27B via built-in aliases + agent harness setup
&&
Honest Caveats & What's Still Rough
- Experimental project, single-machine numbers: all benchmarks above are from one 48 GB M4 Pro on fixed suites (
temp=0.0for the 6-scenario Qwen4 DFlash2 run; raw JSONs are inartifacts/). - Prose speculative acceptance: Qwen4 prose (
69.86 tok/s,66.07e2e) still trails math/code significantly due to lower draft acceptance. - Sampled speculation on Qwen4: Qwen4 DFlash2 currently uses
greedy_only = true(requests withtemperature > 0fall back cleanly to target decode rather than running slow rejection sampling). - macOS Apple Silicon only: relies on MLX, Metal, and Apple ANE (with automatic GPU fallback when ANE isn't available).
Repo & releases: https://github.com/zouyee/dmlx
Happy to answer questions about the Zig runtime, Metal kernels, off-GPU pread PLE / MoE streaming, or ANE + GPU overlap!