AppleNeuralEngine.jl
Run Julia code on the Apple Neural Engine (ANE), the fp16 neural-network accelerator in every Apple Silicon Mac.
using AppleNeuralEngine
W = randn(Float32, 512, 2048) ./ 23
layer(x) = max.(x * W, 0) # ordinary Julia
x = randn(Float16, 256, 512)
program = compile(layer, x) # traced, compiled for the ANE
y = program(x) # runs on the Neural EngineThe package drives Apple's private AppleNeuralEngine.framework directly from Julia through ObjectiveC.jl. There is no native code to build and no Core ML model to export. It offers three levels of control:
| level | you write | see |
|---|---|---|
| tracing | plain Julia on matrices: broadcasting, *, sum, ... | Tracing Julia functions |
| graph builder | MIL operations with inferred types, e.g. MIL.conv(x, W) | The MIL graph builder |
| raw runtime | MIL program text and buffers | Raw MIL and the runtime |
Installation
The package is not registered yet, so install it from GitHub:
using Pkg
Pkg.add(url="https://github.com/pitsianis/AppleNeuralEngine.jl")To run the examples, the benchmark or the op probe, clone the repository instead (see the README).
Requirements: an Apple Silicon Mac and Julia 1.10 or later. Nothing else needs to be installed. So far it has been tested on a Neural Engine only with Julia 1.13 on an M2 Max.
The ANE has no public programming interface; Apple exposes it only through Core ML. This package uses undocumented framework classes that may change with any macOS release. initialize!() checks that the classes it needs exist. Do not rely on it for production systems.
What the Neural Engine is good at
- fp16 matrix-matrix work. On an M2 Max a transformer feed-forward block (512 → 1536 → 512, 256 tokens) runs in about 0.26 ms, 10× faster than Float32 with Accelerate on the CPU. A 2048³ matrix product reaches about 10 TFLOP/s, 4× Accelerate (
benchmark/run.jl). - Fixed graphs with constant weights. Weights are compiled into the program.
- Activations, normalizations, reductions, convolutions, attention. See Op support.
It is less suited to small, latency-bound work. Every call costs about 0.1–0.2 ms on top of the compute, and the hardware computes only in fp16 (see Hardware notes).
Acknowledgements
- maderix/ANE reverse-engineered the private
_ANEInMemoryModelworkflow that this package uses: compiling MIL in memory, the model directory layout, and IOSurface-backed requests. - ObjectiveC.jl makes the Objective-C calls possible, and Metal.jl showed how to build an Apple accelerator package on it.
- Apple's coremltools documents the MIL op set that the graph builder follows.
- Orion catalogs ANE constraints; our Hardware notes compare against them.
- Much of the package was written together with Claude, Anthropic's AI assistant.