e5rtstack

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: 12 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

e5rtstack

Runs a stack of transformer blocks on the Neural Engine through the private e5rt direct-dispatch route, and checks the result against a float64 CPU reference of the whole stack.

e5rtblock runs one block and closes its second residual on the host. This runs many, and closes everything on the device: the host writes the input once, calls execute, and reads the output once, whatever the depth.

go run ./examples/ane/e5rtstack
go run ./examples/ane/e5rtstack -layers 16
go run ./examples/ane/e5rtstack -layers 6 -dim 128 -heads 8 -seq 32 -hidden 256
stack: 4 layers, dim 64, heads 4, seq 16, hidden 128
  worst attention score spread over 4 layers: 1.836
  the last layer moves the result by 0.410 against a tolerance of 0.045
  backend for attention: the compiler emitted [ane]
  backend for feed forward: the compiler emitted [ane]
  backend for residual add: the compiler emitted [ane]
  compiled 12 programs in 828ms
  all 12 functions took an operation, so this run never reached the limit; it is usually 15
  12 functions fit within the 12 this process can hold, so the whole stack is one stream and one execute
  first dispatch 53.208ms for 4 layers, including the encode
  residual peak by layer: 1:0.80 2:1.05 3:1.08 4:1.16
  stack output matches the float64 reference over 1024 values: worst |diff| 0.0016, tolerance 0.0449, reference peak |1.1650|
  second dispatch 1.652ms, re-executing the encoded stream (413µs per layer)
  perturbed stack output matches the float64 reference over 1024 values: worst |diff| 0.0022, tolerance 0.0455, reference peak |1.1827|
  control: perturbing one input element moved 1020 of 1024 outputs, and the new output still matches its reference

Three programs per layer

GenSDPAForward ends in add(x, projection), so it carries the first residual itself. GenFFNForwardRMSReLU2 carries neither, so the second residual is a program of its own — a two-input elementwise add, generated by genAdd — whose output buffer is the next layer's input buffer. Doing that add on the host instead would put a read and a write between every pair of layers.

Nothing is copied between layers. One buffer object is bound to the attention program's output port and to the feed forward program's input port, and the host never sees an intermediate activation.

What this found: fifteen live functions

A process can hold a live precompiled compute operation on at most fifteen distinct compiled functions at once. The first operation on a sixteenth function fails with status 13.

An earlier version of this document said fifteen operations, and that is wrong. The distinction was not visible in the original measurements because every one of them used a fresh function for each operation, so the two readings predicted the same number. Varying them independently separates them:

operations distinct functions result
512 1 all succeed
512 15 all succeed
16 16 fails on the first operation of the 16th function
0 40 (compiled and retained) all succeed

The 512 is where the probe stopped counting, not where the engine objected. The limited call is e5rt_execution_stream_operation_create_precompiled_compute_operation_with_options, and what it is counting is the functions currently in use, not the operations and not the weights — a trivial weightless add program hits the same wall as an attention block, and it is a different limit from the weight-file compile ceiling.

Two further measurements pin down when a slot is returned:

action result
create, release, repeat over 40 functions all 40 succeed
execute and release the stream every 12 all 40 succeed

So the slot belongs to a function that has a live operation, and releasing that operation returns it — unless the operation has been encoded, in which case the stream holds the slot until the stream itself is released. That last point was briefly marked unverified here, because it had been established while the ceiling was still thought to count operations and a probe that skipped binding could not re-establish it (encode fails status 2 with unbound ports). It has since been settled directly, in espressoe5rtstack's probeEncodedStreamRetention:

step result
encode operations on 15 functions, then release the operations —
take an operation on function 16 refused, status 13
release the stream, retry the same function accepted

Both polarities are asserted, the refusal is checked for status 13 specifically rather than any error, and a run with no function to spare reports UNMEASURED instead of a verdict. That is what makes the phase boundary a stream release rather than an operation release.

The pool is shared between processes rather than reserved per process. Two copies of this program running concurrently reproducibly get 15 and 7, so the program measures its own budget at startup (probeFunctionBudget) rather than trusting the 15, and narrows its phases further if a phase will not encode. A stack with fewer than fifteen functions cannot observe the ceiling at all, and the program says so rather than reporting the constant as though it had been confirmed.

Going deeper than five layers

Each layer uses three functions, so fifteen caps a single stream at five layers. Deeper stacks run in phases: encode what fits, execute, release the stream, encode the next group. Buffers are allocated once and stay bound across the boundary, so phases hand activations to each other in place. A phase boundary costs an encode, not a copy and not a host round trip. Sixteen layers run this way, matching the reference to a worst difference of 0.0051 against a peak of 2.21.

The cost of the boundary is visible in the timings: a stack that fits in one phase keeps its stream encoded and re-executes in 367µs per layer, while an eight-layer stack that must re-encode both phases on every dispatch spends 10.7ms per layer.

Encode order is execution order

The stream runs operations in the order they were encoded and does not reorder them to satisfy a dependency. Encoding a layer's residual add before the feed forward it reads produces a stale result rather than an error; see mutation M2 below.

Why the weight file ceiling does not bite here

One attention program needs six weight files and one feed forward program needs three. Nine per layer, against a compiler that stops accepting weight files at sixteen — so a stack that put every layer's weights in one directory would fail to compile at two layers. It compiles here because each program is compiled separately from its own directory, which is what makes that ceiling a per-compile limit rather than a limit on model size. Packing many tensors into one file is the other way around it; see x/ane/mil's BlobWriter.

Controls

Depth is what this example adds over e5rtblock, so it checks that the depth is real. Every layer's attention scores must have enough spread that the softmax selects rather than averages — an attention that averages is reproduced exactly by a wrong head split and by a missing causal mask, which is how the first version of e5rtblock passed while verifying nothing. And the N-layer result must differ from the (N-1)-layer result by more than the comparison tolerance, or a stack that silently dropped its last layer would pass just as well.

The compiled bundle is read for which backend the compiler actually chose, since the device mask is a permission and not a placement and a stack that fell back to the CPU would return the same correct answer. One input element is then perturbed and the stack re-dispatched, which must move the output and must still match a reference recomputed from the perturbed input.

Residual peak is printed per layer. Everything on the engine is fp16, and a residual stream that grows geometrically with depth leaves its range; the weights here are damped so growth is sublinear (0.80 at layer 1 to 2.21 at layer 16), and a non-finite output is reported as such rather than compared.

Three deliberate mutations were each run against the real engine output and each was caught:

mutation worst |diff|
residual reads the layer input instead of the attention output 0.6813
residual encoded before the feed forward it reads 0.6389
last layer bound but never encoded 1.1650

The last is exactly the reference peak, because the output buffer is never written at all.

Documentation

Overview

Command e5rtstack runs a stack of transformer blocks on the Neural Engine through the private e5rt direct-dispatch route, in one dispatch, and checks what comes back against a float64 CPU reference of the whole stack.

e5rtblock runs one block and closes its second residual on the host. That is fine for one block and wrong for a stack: a host round trip between every layer is most of the cost of a small model, and it makes the example say nothing about whether a deep graph can be encoded at all. This program keeps everything on the engine. The host writes the input once, calls execute once, and reads the output once, whatever the depth.

Closing the residual on the device

Each layer is three programs. GenSDPAForward ends in add(x, projection), so it carries the first residual itself. GenFFNForwardRMSReLU2 carries neither, so the second residual is a third program here: a two-input elementwise add, generated by genAdd below, whose output buffer is the next layer's input buffer. Nothing is copied between layers and the host never sees an intermediate activation.

That makes 3N programs, all encoded into an execution stream in order. Order is what the stream runs them in; it does not reorder operations to satisfy a dependency, so encoding a layer's residual add before the feed forward it reads yields a stale result rather than an error.

Fifteen functions, and how to go deeper

A process can hold a live precompiled compute operation on at most fifteen distinct functions at once. The first operation on a sixteenth function fails with status 13, which caps this design at five layers, since each layer is three functions.

The limit counts functions, not operations and not weights. Five hundred operations on one function are accepted, and so are forty compiled functions that carry no operations; what is refused is the operation that would bring one more function into use. It is a different limit from the weight file ceiling. The program measures it on every run rather than assuming it, by taking an operation on each compiled function in turn until one is refused — see probeFunctionBudget.

Releasing an operation frees its function's slot, but releasing one that has already been encoded does not, because the stream holds it: with fifteen functions encoded and their operations released, a sixteenth is still refused, and is accepted as soon as the stream is released. Releasing the stream frees them all. The espressoe5rtstack example asserts both halves of that as a control. So a stack deeper than five layers runs in phases: encode what fits, execute, release the stream, encode the next group. Every buffer is allocated once and stays bound across the boundary, so the phases hand activations to each other in place — what a phase boundary costs is the encode, not a copy or a host round trip. Sixteen layers run this way here.

The pool is shared between processes rather than reserved per process, so a second copy of this program running at the same time gets whatever is left. That is why the budget is measured rather than assumed, and why execute narrows its phases further if a phase will not encode.

The weight file ceiling is per compile

One attention program needs six weight files and one feed forward program needs three. Nine per layer, and the compiler stops accepting weight files at sixteen — so a stack that put every layer's weights in one directory would fail to compile at two layers. It compiles here because each program is compiled separately from its own directory, which is what makes the ceiling a per-compile limit rather than a limit on model size. Packing many tensors into one file is the other way around it; see x/ane/mil's BlobWriter.

What is checked

The reference recomputes every layer in float64 from the same weights. Depth is the thing this program adds over e5rtblock, so it also checks that the depth is real: each layer's attention scores must have enough spread that the softmax selects rather than averages, and the N-layer result must differ from the (N-1)-layer result by more than the tolerance. Without that second check a stack whose later layers contributed nothing would pass exactly as well as one that worked.

Residual growth is reported per layer. Every value on the engine is fp16, and a residual stream that grows geometrically with depth runs out of range; the program prints the peak magnitude after each layer so a stack approaching that limit is visible rather than silently wrong.

go run ./examples/ane/e5rtstack
go run ./examples/ane/e5rtstack -layers 6 -dim 128 -heads 8 -seq 32 -hidden 256

Jump to

Keyboard shortcuts

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