Three-Engine Apple Silicon Speculative Decode
three-engine-specdec is a self-contained, zero-cgo (CGO_ENABLED=0, //go:build darwin) demonstration of composing all three Apple Silicon compute engines — AMX/CPU, Apple Neural Engine (ANE), and Metal 4 GPU — in a single speculative decoding loop with zero-copy IOSurface handoffs and unified monotonic timeline correlation.
Architecture
Each speculative decode round generates K draft tokens (default K=4) and verifies them in parallel across the three engines:
seed state [1, dim]
│
▼
┌────────────────────────────────────────────┐
│ 1. AMX Draft (x/bnnsgraph + x/coremlcompiler)
│ K autoregressive batch=1 residual MLP │
│ steps (~2.2 µs/tok, 0 allocs/step) │
└─────────────────────┬──────────────────────┘
│ direct write (zero-copy IOSurface inSlab)
┌────────────┴────────────┐
▼ (parallel) ▼ (parallel)
┌─────────────────────┐ ┌────────────────────────────────┐
│ 2A. ANE Verify FFN │ │ 2B. Metal 4 Draft Logits │
│ (x/ane Conv1x1) │ │ (x/metal4 AllocatorPool + │
│ [dim, 1, 16] │ │ x/metalcounters heap) │
└────────┬────────────┘ └────────────────┬───────────────┘
│ zero-copy IOSurface outSlab │
└────────────┬────────────────────┘
▼
┌──────────────────────────────────────────────────────────┐
│ 3. Metal 4 Verify + Target Logits (x/metal4) │
│ verify_rmsnorm(inSlab + outSlab) -> DispatchBarrier │
│ -> target_logits -> argmax -> greedy prefix accept │
└──────────────────────────────────────────────────────────┘
│
▼
┌──────────────────────────────────────────────────────────┐
│ 4. Unified Timeline (x/unifiedtrace + x/metalcounters) │
│ Correlates Go runtime/trace, x/signpost, and Metal 4 │
│ MTL4CounterHeap GPU timestamps on one Mach clock │
└──────────────────────────────────────────────────────────┘
Packages Composed
x/bnnsgraph + x/coremlcompiler: compiles a pure-Go batch=1 residual MLP draft block (h = relu(linear(x, w1, b1)), y = linear(h, w2, b2) + x) and runs it on AMX/CPU with a preallocated workspace (0.00 allocs/step).
x/ane + x/coremlcompiler/nnproto + x/zerocopy.SurfacePool: compiles a pure-Go K-token parallel Conv1x1 FFN onto ANE and wraps its input/output IOSurfaces as zero-copy MTLBuffers, proving physical page aliasing via SurfaceSlab.Verify().
x/metal4: executes concurrent draft logits scoring and zero-copy target residual+RMSNorm + LM-head verification using CommandContext, a reusable AllocatorPool ring, MTLResidencySet, pre-populated MTL4ArgumentTables, and metal4.DispatchBarrier.
x/metalcounters + x/unifiedtrace: captures hardware GPU timestamps via MTL4CounterHeap (NewHeapSampler), calibrates them onto the monotonic Mach clock (mach_absolute_time), computes cross-engine ANE || Metal 4 overlap via unifiedtrace.IntervalOverlapNS, and optionally exports Perfetto / Chrome Trace Event JSON (-trace-out).
Running
# Vet and test (includes numerical parity, zero-copy proof, and Perfetto JSON export check)
CGO_ENABLED=0 go vet ./examples/compute/three-engine-specdec
CGO_ENABLED=0 go test -v -count=1 -timeout 60s ./examples/compute/three-engine-specdec
# Live run
CGO_ENABLED=0 go run ./examples/compute/three-engine-specdec
# Export a Perfetto / Chrome Trace Event JSON timeline (open at https://ui.perfetto.dev)
CGO_ENABLED=0 go run ./examples/compute/three-engine-specdec -k 4 -steps 32 -trace-out specdec.json
Flags
| Flag |
Default |
Description |
-dim |
64 |
Model hidden dimension |
-hidden |
256 |
AMX draft MLP intermediate dimension |
-vocab |
128 |
LM-head vocabulary size |
-k |
4 |
Speculative draft tokens per round (K) |
-steps |
24 |
Measured speculative decode rounds |
-warmup |
6 |
Warmup rounds before measurement |
-draft-delta |
0.012 |
Draft vs. target LM-head weight perturbation |
-trace-out |
"" |
Optional path to write Perfetto / Chrome Trace Event JSON |
-gotrace-out |
"" |
Optional path to write raw Go runtime/trace binary |
Measured Results (Apple M4 Max, macOS 26.6.2)
Captured with CGO_ENABLED=0 go run ./examples/compute/three-engine-specdec (K=4, dim=64, hidden=256, vocab=128, rounds=24, warmup=6):
- Zero-Copy & Allocator Reuse:
x/zerocopy.SurfacePool page aliasing verified=true; x/bnnsgraph 0.00 allocs/step (1280 B workspace); MTL4CommandAllocator 1,746,768 B before and after (delta=0).
- Numerical Parity vs. float64 CPU Reference:
- AMX Draft (
x/bnnsgraph, fp32): max_abs_err = 1.192e-07
- ANE FFN (
x/ane, fp16): max_abs_err = 7.874e-03
- Metal 4 RMSNorm (zero-copy, fp32):
max_abs_err = 2.384e-07
- Metal 4 Target Logits (zero-copy, fp32):
max_abs_err = 5.960e-07
- End-to-End Logits (incl. ANE fp16):
max_abs_err = 4.663e-03
- Token Parity:
draft=96/96, target=96/96, exact_acceptance=true
- Speculative Acceptance:
59/96 drafted tokens accepted (61.5%), 83 total tokens emitted including bonus tokens (3.46 tokens/round).
| Stage |
Mean (µs) |
p50 (µs) |
p95 (µs) |
p99 (µs) |
| AMX Draft (per token, batch=1) |
4.40 |
2.17 |
15.42 |
30.29 |
AMX Draft (K=4 tokens + surface) |
26.03 |
23.96 |
33.83 |
54.92 |
ANE Parallel Verify FFN (x/ane) |
458.75 |
224.50 |
806.75 |
4349.42 |
| Metal 4 Draft Score (GPU counter) |
40.58 |
28.67 |
102.96 |
140.75 |
| Metal 4 Verify + Logits (GPU counter) |
30.67 |
20.42 |
82.00 |
124.88 |
- Unified Timeline (
x/unifiedtrace):
- Clock calibration (
9 samples): slope=1.000000000, drift=+0.00 ppm, rms_res=0.0 ns, monotonic=(cpu:true gpu:true)
- Correlated events:
120 signposts, 48 Metal 4 GPU events, 96 Go regions, 359 Chrome/Perfetto trace events
ANE || Metal 4 overlap: 106.1 µs GPU counter overlap (10.9% of Metal 4 draft GPU time), 5474.2 µs concurrent host/ANE overlap (49.7% of ANE time)
- Go STW GC overlap with GPU:
0.0 µs