surfaceffn

command
v0.7.0-beta.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

surfaceffn — the Neural Engine computes over pages another process wrote

The ANE arm of the surfacecompute family (examples/iosurface/tensorshare, examples/metal/surfacecompute). Two Go processes, zero tensor copies:

  • The consumer compiles a dynamic matmul (x/ane/mil) on the ANE. The compile allocates IOSurfaces in exactly the strided layout the NPU DMAs from — so instead of receiving a surface, the consumer sends its kernel's own input and output surfaces to the producer as mach port rights, then evaluates on command. It never reads or writes one element.
  • The producer writes activations and weights straight into the NPU's input pages (x/ane.WrapIOSurfaceFloat32WithLayout), triggers an eval, and reads the product from the NPU's output pages.

Proofs, mirroring surfacecompute:

  1. The result read from the ANE's output surface matches the producer's float64 CPU reference (fp16-scale tolerance 2e-2 — the ANE accumulates in reduced precision and the demo says so).
  2. The producer mutates one activation in place; the consumer re-evaluates with no new writes on its side, and the recomputed product appears — the kernel is bound to live pages, not a snapshot.
go run ./examples/ane/surfaceffn
go run ./examples/ane/surfaceffn -in 1024 -out 1024 -batch 128

Measured 2026-08-11 (M-series, macOS 26.x): 512×512×128 — 124µs wall per eval including the pipe round trip (543 GFLOPS), 81µs hardware-reported on-device (828 GFLOPS). 1024×1024×128 — 204µs wall (1318 GFLOPS), 196µs on-device (1370 GFLOPS).

The ANE compile path is private-framework territory (_ANEClient) and is presence-checked: where unavailable the consumer reports unavail and the demo exits cleanly.

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:

  1. 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).
  2. 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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL