Skip to content
You Start with one site
Amounts in Start with one site
Machine Learning · Concept

TinyML inference

One idea Advanced 5 min read
Found this useful?

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.

On a serverAny size of model. The data travel there and the answer travels back.About 250 ms, needs a network
On the microcontrollerA small model only. Nothing travels.About 1 ms, works with no network
What is the price?The model must fit in kilobytes of memory.Small models only

See it

Predict first

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?

See it: one model, on the board or sent to a server

Answer on the microcontroller
Answer from the server
Fits in 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

The question

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
  1. 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.

  2. Check the microcontroller path

    0.8 ms is 20 ÷ 0.8 = 25 times faster than the limit requires.

  3. 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

The microcontroller does not learn. It runs a model that was trained somewhere else and then fixed. After a year in service its numbers are exactly those of the first day. It can only become worse: when the machine, the sensor or its mounting changes, the readings move away from the examples it was trained on. To improve it, someone must collect new data, train again on a large computer and load the new model into the board.

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

Question 1

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?

Question 2

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?

Question 3

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.
Key points
  • 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.
Found this useful?

Discussion

    Comments show your name. Be kind, stay on the topic.