TinyML inference
On this page
A smoke alarm does not telephone anyone to ask whether there is smoke. It decides by itself, at once, with a small circuit and a battery. TinyML inference gives the same independence to a model fitted to data: the finished model runs inside a microcontroller, the small computer on a sensor board, and gives its answer there with no network.
See it
A small model gives its answer in 0.8 ms on a microcontroller. The same model on a server answers in 250 ms, of which 200 ms is the network. The network becomes 4 times slower. What happens to the time on the microcontroller?
The two time bars share one scale. The figure starts from a model measured on a common board: 15 KB, 0.8 ms, 4 KB of RAM. It scales time and RAM in proportion to the size of the model, which is a fair first estimate and no more. The budgets are 500 KB of flash and 100 KB of RAM. A real model must be measured on its own board.
The idea
A model fitted to data has two lives. Training finds the numbers of the model from thousands of examples, and needs a large computer. Inference is the use of the finished model: one new input goes in, one answer comes out. Inference is far lighter than training, and for a small model a microcontroller can do it.
The finished model is stored in the flash of the microcontroller (the memory that keeps the program) as a table of numbers. While it runs, it needs working space in RAM (the memory for values that change). Both are counted in kilobytes, which is why the model must be small, and why it is first made smaller by quantization.
The answer comes in about a millisecond, with no network, and the raw readings never leave the board. The limit: a model too large for the two memories must stay on a server.
One worked number
A vibration monitor must switch a machine off within 20 ms of seeing a fault pattern. Its model takes 0.8 ms on the microcontroller. Sent to a server, the reading takes 100 ms to travel, 50 ms to process and 100 ms for the answer to return. Which path meets the limit?
Show the worked solution, 3 steps
- Add the server path
100 + 50 + 100 = 250 ms. The limit is 20 ms, so this path is 250 ÷ 20 = 12.5 times too slow.
- Check the microcontroller path
0.8 ms is 20 ÷ 0.8 = 25 times faster than the limit requires.
- Check what happens with no network
The server path gives no answer at all. The microcontroller still answers in 0.8 ms. The model must run on the board. The figure opens on this case.
The mistake to avoid
A second trap: people check only that the model file fits in flash. The working space in RAM is often what runs out first, as it does in the figure above 375 KB.
Check yourself
A reading takes 200 ms to reach a server, the server needs 50 ms, and the answer takes 200 ms to return. How long does the sensor wait for its answer?
A sensor with a model inside it has watched one motor for six months and has processed a million readings. Is its model better now than on the day it was installed?
A microcontroller has 1,000 KB of flash and 192 KB of RAM. The rest of its program uses 400 KB of flash and 100 KB of RAM. A model needs 300 KB of flash and 150 KB of RAM while it runs. Will it run?
Where you meet it
- A vibration monitor on a motorThe board decides on the spot whether the pattern is normal, and sends only the result.
- A pump station far from any townThe network is weak or missing, and the station must still recognise a fault and protect the pump.
- A sensor that runs on a batterySending every raw reading by radio costs far more energy than deciding on the board and sending one short message.
- Inference is the use of a finished model: one input in, one answer out.
- On a microcontroller the answer comes in about a millisecond and needs no network.
- The model must fit in flash, and its working space must fit in RAM.
- The board does not learn. A better model has to be trained elsewhere and loaded again.
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.