Documentation
¶
Overview ¶
Command surfaceffn runs an Apple Neural Engine matmul over IOSurface pages that another Go process wrote — the ANE arm of the surfacecompute family (examples/iosurface/tensorshare, examples/metal/surfacecompute).
The direction of the handoff is inverted from surfacecompute, because the ANE compiler owns its tensors: compiling a model allocates IOSurfaces with the exact strided layout the NPU DMAs from. So the consumer compiles the kernel (x/ane/mil dynamic matmul: y = xᵀ·W in fp16, fp32 at the edges) and passes its input and output surfaces' mach ports BACK to the producer (IOSurfaceCreateMachPort → x/mach Send with MoveSend → IOSurfaceLookupFromMachPort). The producer writes activations and weights straight into the NPU's input pages and reads results from its output pages; the consumer only pulls the trigger. Neither process copies a tensor, and the consumer never sees one element of the data it computes on.
Two proofs, mirroring surfacecompute:
- The producer's float64 CPU reference matches what it reads out of the ANE's output surface after the consumer evaluates (fp16-scale tolerance — the ANE accumulates in reduced precision and the demo says so rather than pretending fp32 accuracy).
- The producer mutates one activation element in place; the consumer re-evaluates without any new writes on its side, and the producer sees the recomputed result. A kernel bound to a snapshot copy would return the stale product.
It then reports per-eval wall time from the producer's side and the hardware-reported execution time from the ANE's own telemetry.
The ANE compile path is private-framework territory (_ANEClient) and is presence-checked: where unavailable the demo reports itself unavailable and exits cleanly.
go run ./examples/ane/surfaceffn go run ./examples/ane/surfaceffn -in 1024 -out 1024 -batch 128