Documentation
¶
Overview ¶
Command statecache keeps mutable state resident on the Neural Engine across executions, and across two separately compiled programs, through one retained E5RT inout port.
Every other ANE example here is a pure function: the host writes an input, the engine computes, the host reads an output. A decoder is not that. It carries state between steps, and if that state has to be staged back to the host after every token then the state, not the arithmetic, sets the pace. e5rt.Lib.OperationRetainInoutPort is the interface that avoids it: one caller-owned buffer object serves as both the read and the write side of a MIL state parameter, and it stays bound across executions.
That capability was proven by the package's own probes. Nothing had used it.
The state buffer is not packed ¶
The first thing this example had to discover is that a bound state buffer is not laid out the way the MIL text reads. Each channel of a [1, channels, 1, dim] fp16 state starts on a 64-byte boundary, so the buffer a caller must allocate is channels*round_up(dim*2, 64) bytes, not channels*dim*2. At dim=4 that is 256 bytes where the packed size is 32.
A caller who allocates the packed size does not get an error. The engine reads and writes the padded offsets regardless, which for the state of an accumulate program means writing 224 bytes past the end of the allocation. The first version of this example did exactly that, and the symptom was that one channel round-tripped and the rest came back zero.
Run with -layout to measure it rather than take it from this comment: that mode binds an oversized state buffer, fills every 2-byte slot with its own index, and prints the slot each tensor element was read from.
e5rt.StateLayout now owns the arithmetic, and e5rt.Lib.BindStatePort is a bind that asks the runtime how large the buffer is and refuses one too small for the shape. This example uses both, so it doubles as their exercise: the raw retain-and-bind pair accepts an undersized buffer without complaint, which is why the checked path exists.
The ordinary input and output ports are packed. Only the state carries the padding, and the -layout mode plus the exact recurrence below are what establish that asymmetry.
What this is, and what it is not ¶
This is one resident mutable tensor that a program reads, modifies, and writes back in place. It is the mechanism a KV cache needs, exercised on its own.
It is NOT a KV cache. A cache is position-indexed — token t writes row t and leaves rows 0..t-1 alone — and nothing here indexes by position. The disjoint control below shows that updates to separate regions do not disturb each other, which is a necessary property of a cache and not a sufficient one. Writing an actual per-layer, position-indexed cache and proving it against a full-prefix oracle is a separate, larger piece of work.
What each check would catch ¶
A float64 recurrence, checked at every step. The values are chosen so the whole recurrence is exact in fp16, so this requires exact agreement rather than a tolerance. A program that dropped an update, applied one twice, or accumulated in the wrong order fails here.
A host-visibility probe, run before anything depends on it. This program zeroes the state between arms by writing through the host pointer, which is only a reset if the engine actually observes host writes to a bound inout buffer. That is checked by writing a distinctive pattern and reading it back through the engine, not assumed. It is also what catches a wrong buffer layout, which is how the padding above was found.
A separately compiled reader, bound to the same buffer object. The update program returns the new state as an ordinary tensor output, so its own output cannot distinguish stored state from an echo of what was just computed. A different program that only reads can.
The same reader program bound to a different buffer, required to return zeros. Without this arm, the reader agreeing with the reference would be consistent with the reader seeing the right answer by some route other than the shared buffer. This is the negative polarity of the check above, and the two together are what make the sharing claim load-bearing.
Two states advanced in an interleaved order with different values, each required to follow its own recurrence. A single global state behind the inout port would pass every single-state check and fail this one.
Disjoint regions updated in separate steps. An update that replaced the state rather than accumulating into it, or that wrote at the wrong offset, leaves a detectable hole.
A state name the program does not declare, required to be refused, so that "the inout port was retained" is evidence of resolution rather than evidence that the call returns non-nil.
Why exact agreement is the right bar ¶
The state is fp16 and the update is an fp16 add, so an accumulation would normally drift away from a float64 reference and need a tolerance. A tolerance is a weaker check, and here it is avoidable: every value is a multiple of 0.5 and every partial sum stays well under 1024, and multiples of 0.5 in that range are exactly representable in binary16. A correctly rounded add of two exactly representable values whose exact sum is also representable is exact. So the entire recurrence is exact, and any disagreement at all is a defect rather than rounding. The flag guards below keep the inputs inside that range; step counts outside it are refused rather than silently switching to a weaker comparison.