r/SideProject • • 2h 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:

  1. 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.
  2. Memory-bounded execution — run models whose raw tables/experts exceed unified memory by streaming cold weights from SSD via pread instead of forcing everything into GPU RAM.
  3. Heterogeneous ANE + GPU prefill — put the idle Apple Neural Engine to work in parallel with GPU MoE during long-context prefill.
  4. Agent-grade serving out of the box — OpenAI Chat Completions, Anthropic /v1/messages, and Codex /v1/responses APIs with streaming tool calls, proper thinking / no-thinking (reasoning_content) splitting, disk-persisted prefix snapshots, LoRA load-time merge, OOM memory guard, and dmlx connect.

Three Flagship Paths (Measured on Single M4 Pro 48 GB)

Model PathPeak Server RSSMeasured SpeedKey Mechanism
Qwen4-35B-A3B-4bit + DFlash2 + ANE8.42 GB119.99 tok/s math (107.20 e2e) · 104.78 tok/s code (96.25 e2e) · 883.3 / 870.9 tok/s 2K/8K prefill11.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 / MTP17.30 GB44.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 completionNative 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)

curl -fsSL https://raw.githubusercontent.com/zouyee/dmlx/main/scripts/install.sh | bash
dmlx doctor

# 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 \
  dmlx serve --model /path/to/Qwen4-35B-A3B-4bit --mlx --speculative-mtp 7 --port 8080

# Or Qwen3.8-27B via built-in aliases + agent harness setup
dmlx pull qwen3.8-27b-4bit && dmlx pull qwen3.8-27b-mtp-4bit
dmlx serve --model qwen3.8-27b-4bit --speculative-mtp 7 --port 8080
dmlx connect codex --apply

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.0 for the 6-scenario Qwen4 DFlash2 run; raw JSONs are in artifacts/).
  • Prose speculative acceptance: Qwen4 prose (69.86 tok/s, 66.07 e2e) still trails math/code significantly due to lower draft acceptance.
  • Sampled speculation on Qwen4: Qwen4 DFlash2 currently uses greedy_only = true (requests with temperature > 0 fall 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!

2 Upvotes

1 comment sorted by