Deploying a Transformer to the AURIX PPU

From emmtrix Wiki
Jump to navigation Jump to search

TINY 2.1 is a question-answering transformer of 367,000 parameters, written in Turbo C for DOS and meant for a 486. [1] [2] Compiled from ONNX to C by the emmtrix Edge AI Compiler, it now answers on the Parallel Processing Unit of an AURIX™ TC4x microcontroller at about 1,929 letters per second on a TC4D9, which is 233,295 PPU cycles per letter at the board template's 450 MHz.

The order of events matters for reading the rest. The model existed first and was written for none of this: it is someone else's DOS program, published as a curiosity and picked up because it is a real transformer small enough to fit a microcontroller whole. [1] This article is about what happened when our own compiler was pointed at it.

The companion article, State in a Stateless Graph: LLMs and Transformer Models in ONNX, asked how a transformer's KV cache can be expressed in a graph format with no notion of memory that outlives a call, and closed by stating that none of it had run on hardware. This is that part, and two of its results were not what the first article expected.

The first is that a vector unit given scalar code loses to the scalar core beside it. The same hand-written C needs more than twice the cycles on the PPU that it needs on a TriCore core, so a hand port of working code to this part is not a step forward but a step back. The second is that the deployment was decided in the memory map rather than in the arithmetic: across the twenty-four-fold improvement from the first working run to the figure above, not one step changed what is computed.

Both are results about the compiler. The figure above is what the Edge AI Compiler's output does against a hand-written C port of the same network as its baseline, on the same board, with the hand-written version given the benefit of a scalar core it suits: 8.5 times the answers per second. It reaches that while computing about seven times as much attention arithmetic, for a reason given below.

What this article does not claim is a general result, or a benchmark of anything against anything. One network, one board, one toolchain, a context of 128 letters, and a single question measured end to end. The closing section says which conclusions travel and which do not.

A vector unit is not a faster core

Five builds of the same network. The two cores do not run at the same clock — the PPU here is at the board template's 450 MHz and the TriCore cores at a nominal 500 MHz — and both columns are given because they answer different questions. Letters per second is the comparison that counts: it is what each part delivers at the clock it actually runs at, which is what a product has to live with. Cycles per letter is the same comparison with the clock taken out, which is what isolates the code from the part. Neither clock was measured, which is the caveat on the first column and the reason the second one is kept.

Code Target Letters/s Cycles per letter
hand-written C, 8-bit weights and cache one TriCore core at 500 MHz 227.7 2,195,472
the same hand-written C PPU at 450 MHz 92.7 4,854,333
plain generated C, no target-specific work one TriCore core at 500 MHz 65.5 7,638,575
the same generated C PPU, in simulation only 35.4 12,727,784
generated C through the emmtrix Edge AI Compiler PPU at 450 MHz 1,928.9 233,295

The first two rows are the uncomfortable ones. The PPU's arithmetic lives in a SIMD vector DSP with a vector memory of its own, and data reaches that memory through explicit transfers by the Streaming Transfer Unit. [3] Code that vectorizes nothing and transfers nothing runs on the private scalar core beside the DSP, and that core is not what the part is for. Retargeting a working C program at the PPU cost 2.2 times the cycles of leaving it on a TriCore core, and at the two clocks 2.5 times the wall-clock time per letter.

The third row looks worse than it is. The generated code needs 7.6 million cycles against the hand-written 2.2 million, but it is not doing the same work: the ONNX step graph computes the attention scores over all 128 context positions and masks the ones that are not allowed, where the hand-written C loops only up to the current position. Over the 33 steps of an answer the hand-written code touches an average of about 17 positions of 128. The generated code never stops doing the full 128, in this row and in the last one alike.

Which makes the last row the real comparison. The Edge AI Compiler's build does roughly seven times the attention work of the hand-written original and still answers 8.5 times faster than it does on a TriCore core — 9.4 times fewer cycles, the difference between the two being the slower clock the PPU is set to here. Against the same hand-written C on the PPU it is 20.8 times faster, and against the unoptimized generated code in simulation, 54.5 times.

The fourth row never ran on the board. Plain generated code wants about 2 MB of data, 1.6 MB of static temporaries and a 393 KB cache, against the 512 KB the board layout reserves for it, so that figure comes from the instruction set simulator. It is in the table because it is where an unprepared ONNX-to-C port of a transformer starts on this part: too large to link, and slower in simulation than the scalar core.

What produced it

The last row of that table is the emmtrix Edge AI Compiler, and everything below this point is a description of its output.

The workflow has two stages, kept deliberately apart. The front end, the open-source emx-onnx-cgen, lowers the model to portable C that computes what the graph computes and makes no attempt at speed: explicit loop nests over the tensor indices, buffers sized at compile time, no vector intrinsics and no placement, because those decisions belong to the stage that knows the part. [4] The third and fourth rows of the table are that stage's output on its own — correct, readable, and on this target either slower than a scalar core or too large to link.

The back end then rewrites that C for the processor and emits C again, leaving the vendor's compiler as the one that produces machine code. Where the product's own description puts the weight of its work is loop restructuring and fusion, memory layout and buffer reuse, reduction of temporary memory, and automatic vectorization including a dedicated vector unit where the part has one. [4] The rest of this article is that list turned into one concrete build: the memory map below is the second item, the twenty-four-fold progression further down is the first, third and fourth.

That product page gives no speedup figure, on the stated ground that no published number holds across models, targets and toolchains. [4] This article does not supply one either. It supplies one case — one network, one board, measured end to end — and is explicit about which parts of it are properties of the case.

Where everything is

The final build is spread across three memories, and the reasoning behind that layout is most of the engineering. It is also the item the back end's description lists second, in a form that is easier to see here than to describe in general.

Three horizontal bars drawn to one scale for the cluster shared memory, the LMU and the vector memory, divided into segments for what each holds, with two further bars below comparing how long one 4.6 kilobyte block takes to arrive in the vector memory from each of the first two.
The two 512 KB memories have room to spare. The 128 KB one does not, and that is the constraint the whole layout answers.
Memory Size In use Holds
CSM, cluster shared memory 512 KB 74,238 B code, 372,191 B data, 32 KB stack the step function and the 8-bit weights
LMU, on-chip SRAM across the interconnect 512 KB reserved 120,610 B the whole KV cache
VMEM, the vector memory 128 KB about 91 KB the buffer pool, small constant tables, the outputs
Program flash   20,961 B the startup image of those constant tables

The PPU executes from CSM, and the step is a single function of 64 KB of code. Beside it in CSM are 365,664 bytes of 8-bit weights as ordinary constant data — the four attention matrices at 9,216 bytes each per layer, the two feed-forward matrices at 24,576 each, the embeddings — and 6 KB of scale tables. The KV cache, 98,304 bytes of int8 plus 16,384 bytes of float row scales, sits in the LMU. Nothing of the model is in flash except the startup image of the tables that live in the vector memory.

The vector memory is where the work happens, and it is the only one of the three that is full.

The vector memory is an overlay, not an allocation

Of its 128 KB, 60,672 bytes are the compiler's buffer pool: 187 buffers at fixed offsets, in which buffers whose lifetimes do not overlap share the same bytes, and the same offsets are reused layer after layer. This is the back end's buffer reuse arriving as a number: 187 live buffers in 60 KB of a 128 KB memory, where allocating each of them its own bytes would not have fit. It holds the activations — the residual stream, the normalized vectors, the queries, keys and values, the scores, the softmax, the feed-forward hidden layer — and the landing buffers for the transfers, a weight block being 4,608 bytes for an attention matrix and 12,288 for the larger feed-forward one. Most landing buffers are doubled, so the next block is being fetched while the current one is being multiplied.

The rest of the vector memory is 20,961 bytes of small constant tables kept there whole rather than streamed — the normalization gains, the per-matrix scales, the unembedding weight — about 1 KB of outputs the step writes directly there, and an 8 KB vector stack.

The cache is streamed and never present whole

The keys arrive one head per block, 3,072 bytes, doubled; the values two heads per block, 6,144 bytes; the scales 1,024 bytes per block. One transfer from the LMU per block per layer, and at no moment does a complete copy of the cache exist in the vector memory. The step writes the new row for each layer into the vector memory, and the driver copies it into the LMU cache after the call — 24 bytes per head and layer plus its scale, a cost inside the measured cycles rather than beside them.

This is the shape the first article arrived at from the other side. Its step graph took the cache in and handed it back out, 384 KB each way, and its closing section was about making a graph input and a graph output share one buffer so that no copy happens. The deployed model does something simpler: the cache is an input only, the step returns just the row for this letter, and the caller owns the write. No aliasing of inputs to outputs is used in this build at all. Where the cache cannot be held in fast memory to begin with, who performs the write matters more than whether two tensors are the same buffer.

Why the weights are in the cluster memory and the cache is not

Measured on the board for one 4.6 KB block:

Path Complete after Cost to the core
CSM to vector memory about 552 cycles, roughly 11 bytes per cycle about 151 cycles to issue, overlapping computation
LMU to vector memory 2,887 cycles  

Five times the latency across the interconnect is the entire reason for the split. The 366 KB that is read in full for every single letter sits in the memory inside the cluster; the 114 KB that is read in small blocks comes from outside it. Moving the weights to the LMU instead was tried and gained nothing.

Nobody wrote a linker script

The placement is not expressed with section attributes, placement pragmas or a model-specific linker script. The weights are plain constant arrays and therefore land in read-only data; the harness's own linker script maps code and read-only data to CSM and writable data to the LMU. The only placement the model's own code states is a memory qualifier on the vector memory, and that is emitted by the compiler's back end, for its pool and for the constant tables it decides to keep whole; the front end writes nothing target-specific by design. Which arrays go there, how they are blocked for transfer, and which are kept whole instead of streamed follows from the target's parameters — the size of this part's vector memory is one of them [4] — rather than from per-model annotation. There is no AURIX-shaped hand-written anything in this build.

The cache the original had all along

The size that makes this layout possible was not invented for the port. It was in the DOS program.

A decoder-only transformer keeps, for every letter already read, the keys and values each layer computed for it: here 4 layers, 4 heads, 128 positions and 24 values per head, twice over. The DOS program keeps that as bytes, four arrays of signed char with one float scale per row, and the comment above the declaration calls them "the only large working buffer and the reason the whole thing fits in half a megabyte". [5] It comes to 100 KB.

The ONNX export does not inherit that. A graph input is a typed tensor, the obvious type is float32, and the cache becomes 384 KB for exactly the same information. Putting it back is a quantization inside the graph, with one scale per head of every row: 112 KB, the 12 KB over the DOS form being the finer scales. [5]

Where Representation Size
the DOS original int8, one scale per row of 96 100 KB
the plain ONNX step export float32 384 KB
the export that was deployed int8, one scale per head 112 KB

Two details decide whether such a cache is expressible at all, and both concern where a scale may be applied. A score reads one cache row, so that row's scale is a constant factor of the dot product and may be applied to the finished score. The context sum runs over many rows with different scales, so there the scale has to enter the softmax weight before the sum. The two are not interchangeable.

On a host, over 60 questions, the 8-bit cache changes one answer in sixty, and that one is a rewording rather than a wrong fact. [5] Eight-bit weights cost nothing measurable at all, because the weights were 8-bit in the model file to begin with.

Twenty-four times, and almost none of it arithmetic

The progression on the board, from the first run that answered the question to the current one. Every row is a change in what the back end did with the same front-end C, not a change to the network: the model changed once along the way, from a float32 cache to the 8-bit one, and that is marked in the table.

Cycles per letter Letters/s What changed
5,655,254 79.6 the first run on the board
4,127,959 109.0 each head's cache fetched as one block instead of 128 short rows and 24 gathers of single values
1,200,011 375.0 the weight transfers arranged after vectorization instead of before, so they stopped preventing it
1,009,624 445.7 the invariant scale of a weight column moved out of the inner product: one multiply per output instead of one per weight
583,067 771.8 8-bit values widened to 32-bit lanes on load, instead of a masked byte load and a separate widening step
the 8-bit KV cache from here on
698,558 644.2 the 8-bit cache, before the code that quantizes each new row was vectorized
501,630 897.1 that code vectorized: the quantization of the new rows from 144,000 cycles per letter to 12,000
433,282 1,038.6 the scores over a cache row computed eight rows at a time, sharing the loads of the query
384,890 1,169.2 arrays below the transfer threshold left where they were: a 384-byte normalization gain had been fetched in twelve transfers of 32 bytes
256,804 1,752.3 transfers into the vector memory no longer invalidating the data cache, which does not hold that memory
233,295 1,928.9 the first transfer of a buffered loop started during the loop before it

Read down the third column: every entry is about moving data, or about letting the vectorizer see a loop it could not see before. The matrix products at 1,929 letters per second are the same matrix products as at 79.6.

Set against the four things the back end's own description says its gains come from, the column sorts without a remainder. Three rows are the shape or the scheduling of transfers, which is memory layout. Four are vectorization, or the removal of something that was preventing it. One is a loop-level removal of an invariant from an inner product. One is a cache maintenance operation that turned out not to be needed for this memory. One is the change of model. Nothing in the column is a cheaper multiplication, and nothing in it is a hand-written kernel.

That is the part of an embedded AI deployment most easily underestimated from the model side. The arithmetic of a small transformer is a handful of matrix-vector products and a softmax, and it was never the obstacle. The obstacle is that a vector unit with its own fast memory is fed by explicit transfers, and the distance between a naive schedule of those transfers and a considered one was, here, a factor of twenty-four.

For scale at the other end: the DOS program reports about 4.4 letters per second on the hardware it was written for. [1]

The better representation arrived slower

One row above deserves reading twice. The 8-bit cache entered at 698,558 cycles per letter, when the float32-cache build had already reached 583,067. The representation that is almost four times smaller, and that the rest of this article recommends, was 17 % slower on arrival.

The reason had nothing to do with the representation. Quantizing each new row is a small amount of work — about 12,000 cycles per letter once vectorized — but it was not being vectorized, and cost 144,000. A pointer copy introduced by inlining was enough to keep the vectorizer out of those loops.

This is a hazard rather than an anecdote. A change that reduces data movement usually adds arithmetic somewhere, and on a vector target that arithmetic either vectorizes or it does not. If it does not, the measurement reports that the better representation is worse — and it is telling the truth about the build in front of it and nothing about the idea. A representation change on this kind of target needs measuring twice: once as it lands, and once after the generated code has been read.

A related caution applies to the simulator. It charges far less than the board does for many small or strided transfers, which is exactly what an unoptimized schedule produces, and it also overstates the benefit of starting a transfer early. Every figure in this article is a board figure for that reason, and the one simulation figure is marked as such.

What this does not claim

The board result is one question. "What is the capital of peru", 33 network steps, answered "Lima." The board produces the same 33 characters as a host reference run of the same quantized model, and the largest logit of the final step agrees with it to about three parts in a thousand. That is a correctness check, not an accuracy evaluation.

The accuracy figures are host figures. The one-answer-in-sixty cost of the 8-bit cache was measured on a host against the float32 model. [5] Nothing on the board has been compared against the float32 model, and no multi-question evaluation has been run there. The two must not be added together.

There is no TriCore figure for the deployed model. The TriCore rows are the hand-written C and plain generated code. Measuring the 8-bit-cache export on a TriCore core is possible with what is already set up and has not been done, so the table compares what exists rather than best efforts on both sides.

It is this flow on this model, not a benchmark. One network of 367,000 parameters with a context of 128 letters on one TC4D9. A larger model, a longer context, a growing cache, paged attention or beam search would redistribute every cost here.

The factors are not a figure for the compiler. 8.5 times a hand-written C port, and twenty-four times its own first working run, are what happened to this network on this part against these baselines. A different model, a different target or a hand port written by someone determined to win would all move them, and the product page's refusal to publish a general number is the right one. What the sections above are for is the shape of where the factor came from, which is more portable than the factor.

The port is not a product. It answers questions on a development board over a serial console.

What travels is the shape of the result. The arithmetic was never the obstacle; the KV cache was, first for its size and then for how it moves. The size had already been solved by the program this model came from, and undone by the export. The movement was solved by the compiler, across a series of changes in which no multiplication was ever made cheaper. And the part that is easiest to forget: pointing a vector accelerator at working scalar code made it slower than not bothering.

See also

References

  1. ↑ 1.0 1.1 1.2 Not-So-Large Language Model Fits On A Floppy Disk — hackaday.com — https://hackaday.com/2026/11/18/not-so-large-language-model-fits-on-a-floppy-disk/
  2. ↑ TINY 2.1 — a transformer for DOS, the original project by WollyMonjac — github.com — https://github.com/WollyMonjac/Tiny-2.1
  3. ↑ Infineon AURIX TC4Dx User Manual v1.00, chapter 14 (Parallel Processing Unit); the memory sizes are Table 740, "Product specific parameters", section 14.14.1, the windows and the LMU banks are in the address map, and the vector memory appears there as PPU.VMEM.
  4. ↑ 4.0 4.1 4.2 4.3 The two-stage workflow, the list of where the optimization gains come from, the vector memory size as a platform parameter, and the deliberate absence of a published speedup figure are set out on emmtrix Edge AI Compiler, which cites the product page — emmtrix.com — https://www.emmtrix.com/tools/emmtrix-edge-ai-compiler
  5. ↑ 5.0 5.1 5.2 5.3 The ONNX models, the three cache interfaces and the accuracy of each, training/MODELS.md on the work branch of the fork; the embedded port's byte cache is in embedded/tiny.c on the same branch — github.com — https://github.com/strimo378/Tiny-2.1/blob/work/training/MODELS.md