bnns-amx-draft
Demonstrates compiling a batch=1 residual MLP draft block in pure Go with
x/coremlcompiler and executing it on Apple Silicon CPU/AMX via x/bnnsgraph
with a preallocated page-aligned workspace (0 Go heap allocations per step),
comparing numerical parity and per-step latency against a Metal GPU compute
shader and a float64 Go reference.
Model
A two-layer residual MLP (dim=256, hidden=1024, batch=1, fp32, ~1.05M FLOPs):
h = relu(x * W1^T + b1)
y = (h * W2^T + b2) + x
The model and weights are emitted directly to an .mlmodelc bundle in Go using
x/coremlcompiler.CompileProgram (no Python, no coremltools, no cgo).
Usage
CGO_ENABLED=0 go run ./examples/accelerate/bnns-amx-draft
CGO_ENABLED=0 go run ./examples/accelerate/bnns-amx-draft -dim 256 -hidden 1024 -iters 500
CGO_ENABLED=0 go test -v -count=1 ./examples/accelerate/bnns-amx-draft
Measured Results (Apple M4 Max, macOS 26.6.2, Go 1.27.0)
batch=1, dim=256, hidden=1024, iters=500, single_thread=true:
- Compiled BNNSGraph IR:
2,118,748 bytes
- Preallocated page-aligned workspace:
5,120 bytes
- Heap allocations per
Context.Execute: 0.00 (testing.AllocsPerRun)
| Backend |
Mean (µs) |
p50 (µs) |
p95 (µs) |
p99 (µs) |
Max abs err vs fp64 |
| BNNSGraph (AMX/CPU) |
12.75 |
7.83 |
20.00 |
88.67 |
1.192e-07 |
| Metal Compute (GPU) |
748.43 |
654.04 |
1277.12 |
2732.88 |
1.788e-07 |
| Go Reference (fp64) |
1054.17 |
1028.25 |
1108.75 |
1628.33 |
0 (oracle) |
- BNNSGraph vs Metal max abs diff:
1.788e-07
- Speedup (BNNSGraph p50 vs Metal p50):
83.50x (batch=1 avoids per-step GPU
command buffer submission and synchronization latency).