statecache

command
v0.6.20 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 15 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

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. A decoder is not: 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. Lib.OperationRetainInoutPort is the interface that avoids it. The package's own probes proved it works; nothing had used it.

go run ./examples/ane/statecache
go run ./examples/ane/statecache -layout
go run ./examples/ane/statecache -chan 8 -dim 32 -steps 8
retained state: chan=4 dim=4 (16 values) steps=6

  state buffer: 256 bytes, 4 channels of 64 bytes — a packed 32 would be too small by 224
  a freshly allocated buffer object read back as all zeros (observed, not relied on)

  a host write to the bound state buffer is what the engine reads back

  6 updates match a float64 recurrence exactly, checked after every step
  the engine's writes are visible through the state buffer's own host pointer

  a separately compiled reader on the same buffer returns the accumulated state
  the same reader program bound to a different buffer returns zeros, as it must

  updating 8 values in the upper half left the lower half untouched

  two states advanced in alternation each followed their own recurrence

  refused, as it must be: an inout port the program does not declare
    e5rt: e5rt_execution_stream_operation_retain_inout_port: status 11
  and the declared port "kv" still resolves on the same operation

OK

The state buffer is not packed

The most useful thing this example produced is a fact about buffer layout that nothing in the MIL text tells you.

Each channel of a [1, channels, 1, dim] fp16 state starts on a 64-byte boundary. The buffer a caller must allocate is channels * round_up(dim*2, 64) bytes, not channels * dim * 2.

shape packed size required size
[1, 4, 1, 4] 32 256
[1, 4, 1, 32] 256 256
[1, 4, 1, 40] 320 512
[1, 2, 1, 64] 256 256
[1, 8, 1, 2] 32 512

Shapes whose row already fills whole 64-byte lines are packed by coincidence, which is a good way to write code that works on dim=32 and corrupts memory on dim=4.

Allocating the packed size produces no error. The engine reads and writes the padded offsets anyway, so for an accumulate program it writes past the end of the allocation — 224 bytes past, at dim=4. The first version of this example did exactly that; the symptom was that channel 0 round-tripped and every other channel came back zero.

e5rt.StateLayout owns this arithmetic now, and Lib.BindStatePort is a bind that asks the runtime how large a buffer is — via e5rt_buffer_object_get_size, which the library exports and which had no binding — and refuses one too small for the shape. The raw retain-and-bind pair accepts an undersized buffer without complaint; the package test asserts that as a mutation control, so if the raw path ever started checking, the new one would be flagged as redundant rather than quietly duplicated.

-layout measures it instead of citing it. It binds an oversized state buffer, fills every 2-byte slot with its own index, and prints the slot each tensor element was read from:

  state shape [1, 4, 1, 4] over a 1024-byte buffer
  each value below is the host 2-byte slot the engine read that element from:
    channel 0: [0 1 2 3]
    channel 1: [32 33 34 35]
    channel 2: [64 65 66 67]
    channel 3: [96 97 98 99]
  the state tensor is NOT host-packed; a caller must write through the layout above

The ordinary value and y ports are packed — the layout probe reads its 16-element result out of a 32-byte output buffer, which would be impossible if outputs carried the same padding. Only the state has it.

What each check would catch

check what it would catch
a float64 recurrence, compared after every step a dropped, doubled, or misordered update; a correct total arrived at wrongly
a host-written pattern read back through the engine, before anything depends on it a wrong buffer layout, and a host pointer the engine does not observe — without which the zeroing between arms is not a reset
a separately compiled reader on the same buffer an update program whose tensor output merely echoes what it just computed rather than reflecting stored state
the same reader program bound to a different buffer, required to return zeros the reader obtaining the right answer by any route other than the shared buffer
two states advanced in alternation, each against its own recurrence a single process-wide state behind the inout port, which every single-state check would pass
disjoint halves updated in separate steps an update that replaces rather than accumulates, or that lands at the wrong offset
an inout port name the program does not declare a retain call that returns non-nil without resolving anything

The refusal has a positive polarity in the same place: after no_such_state is refused, kv is retained again on the same operation and must still succeed, so the refusal is evidence about the name rather than about the call having stopped working.

Why the comparison is exact

The state is fp16 and the update is an fp16 add, so an accumulation would normally drift and need a tolerance. A tolerance is a weaker check, and here it is avoidable: every value is a multiple of 0.5 with magnitude at most 2, and at most 8 steps are allowed, so no partial sum passes 16. Multiples of 0.5 in that range are exactly representable in binary16, and a correctly rounded add of two exactly representable values with a representable sum is exact.

So the whole recurrence is exact and any disagreement at all is a defect. -steps outside 2..8 is refused rather than silently switching to a weaker comparison.

A control that fired

recurrenceArm refuses to continue if the accumulated state comes out all zeros, because the fresh-buffer control immediately below it requires a reader on an untouched buffer to return zeros — which would pass against a completely broken implementation if the real answer were zeros too.

It fired on the first run. The step generator was (7t + 3i) mod 9 - 4, whose values sum to exactly zero over six steps for every i. The generator is now (3t + 5i) mod 7 - 2, which is asymmetric about zero.

Limits

This is not a KV cache. A cache is position-indexed: token t writes row t and leaves rows 0..t-1 alone. Nothing here indexes by position. The disjoint-halves control shows that updates to separate regions do not disturb each other, which is necessary for a cache and not sufficient. A per-layer, position-indexed cache proved against a full-prefix oracle is separate, larger work.

Rebinding an inout port to a different buffer object after the operation is encoded is UNMEASURED. Each program here binds its state once at open, and sharing is arranged by binding several programs to the same buffer rather than by rebinding one.

Whether the 64-byte channel stride is a property of this hardware generation, this macOS version, or the E5RT ABI is UNMEASURED. It was observed on one machine across five shapes. Callers should compute it with the rule above rather than hard-coding a size, and -layout is there to re-check it.

The device mask is a permission, not a placement, so the main_ane directory the compiler leaves is the only local evidence the state lives on the engine. Every program here fails to open if that directory is absent.

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.

Jump to

Keyboard shortcuts

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