Documentation
¶
Overview ¶
Command modelchain runs a transformer block on the Neural Engine through the public x/ane API, chaining two compiled models by sharing an IOSurface, and checks the result against a float64 CPU reference.
The other ANE examples all drive the private e5rt route directly. This one uses what an outside caller of the package has: ane.Probe for the device, ane.Open and ane.Client.Compile to build models from MIL text and weight blobs, ane.ShareSurface to hand one model's output to the next without a copy, and ane.Model.Eval to run them.
What sharing a surface means here ¶
Compile gives every model its own input and output IOSurfaces. ShareSurface replaces the second model's input surface with the first model's output surface, so the feed forward network reads the attention result exactly where the attention model left it. Nothing is copied and the host does not touch the intermediate activation.
That is worth a control of its own, because a chain that carried the activation some other way would produce the same numbers. So the block is run twice: once before the two models are chained, where the feed forward network reads its own untouched input and the result must not match the reference, and once after, where it must. Checking only that the surfaces are now the same object would show that the call assigned something, not that the assignment is what carries the data.
The residual the generator does not add ¶
GenFFNForwardRMSReLU2 computes W2(relu(W1(rms_norm(x)))^2) and stops. It has no residual add, so the block output is the attention result plus the feed forward result, and this program does that last add on the host. The e5rt examples close it with a third program on the device; through the public API there are two models and two Eval calls, so the host is already in the loop between them.
What is checked ¶
The reference recomputes the whole block in float64 from the same weights. Everything on the engine is fp16, so the comparison is to an fp16-scale tolerance, printed next to the magnitude of the reference rather than asserted as agreement. The weights are decorrelated so that the softmax selects rather than averages: an attention that averages over positions is reproduced exactly by a wrong head split and by a missing causal mask, which is how an earlier version of the e5rtblock example passed while verifying nothing. One input element is then perturbed and the block re-evaluated, which must move the output and must still match its own reference.
go run ./examples/ane/modelchain go run ./examples/ane/modelchain -dim 128 -heads 8 -seq 32 -hidden 256