Embedded ML Lessons · Lesson 1

Weights, Activations and Memory.

Why a more precise number can cost an embedded processor time, not just memory. A real voice-command model on an ESP32-S3 shows where the memory goes, where the time goes, and what a failed experiment teaches.

About 15 minutes · No ML background needed · Measured values are marked as measured

Audio and speech: a voice-interface wearable with a sound-wave graphic

What you will learn

1. Start with one spoken word

Say the word for mute to a voice-command device. Here is what happens, in order:

you speak  →  microphone  →  audio samples
           →  audio features (1,950 numbers)
           →  neural network  →  13 scores, one per command
           →  highest score wins  →  "mute" event sent to the product

A neural network is a long chain of multiply-and-add steps. Two kinds of numbers flow through it, and they are easy to mix up.

TermWhat it isAnalogyWhen it changes
WeightA number the model learned during training. It says how much each input matters.Years of experience: what a sound engineer knows about voices.Never, while the device runs.
ActivationA number the model computes from the current input, and passes to the next step.What the engineer hears right now, and how they interpret it.Every time you speak.

The weights are fixed. The activations change with every sound. So weights are stored knowledge, and activations are work in progress.

You speak“mute”Microphoneaudio samplesAudio features1,950 numbersNeural network251,264 weights13 scoreshighest winsMute eventsent to productShaded stages hold the model’s weights and activations.
Figure 1. One spoken word, end to end. The model is one stage in a longer chain; its weights and activations are the two shaded stages.
Weightslearned in trainingfixed while runningstored in flash251,264 valuesActivationscomputed per soundchange when you speakheld in RAM bufferreused each inferenceInferenceweights × activations+ multiply-and-add= 13 scores
Figure 2. Weights are stored knowledge; activations are the work in progress. Inference combines the two.

2. A tiny example you can check by hand

Imagine a neuron that looks at only four audio features. Its score is the sum of activation times weight for each feature. Here the weights are illustrative, not taken from our model:

FeatureActivation (sound "mute")WeightProduct
10.80.90.72
20.70.80.56
30.2−0.5−0.10
40.90.70.63
Score1.81

Now say up instead. The weights stay the same, but the activations change: 0.2, 0.3, 0.9, 0.1. The score becomes 0.18 + 0.24 − 0.45 + 0.07 = 0.04. Same weights, different inputs, different answer. That is inference.

Each multiply-and-add is a MAC (multiply-accumulate). Counting MACs is the simplest way to estimate how much work a model does.

3. Our real model in numbers

The voice-command model has three layers:

LayerValuesWeights into this layer
Input (audio features)1,950–
Hidden layer, 128 neurons1281,950 × 128 = 249,600
Output, 13 command scores13128 × 13 = 1,664
Total251,264 weights (plus 141 biases)

Every hidden neuron looks at all 1,950 inputs. That single fact drives most of the story below.

input A1input A2input A3input A4neuron N1neuron N2neuron N3A1 feeds allthree neurons:stored once,used three timesOur model: 1,950 inputs and 128 neurons, so 249,600 multiply-adds per inference.
Figure 3. Every hidden neuron reads every input. The input is stored once but used once per neuron. This is why the arithmetic, not the storage, dominates. Four inputs and three neurons shown for clarity.

4. Where the memory goes: flash versus RAM

The weights are stored in flash, the device's program memory, built into the firmware. The activations are held in RAM, in a working buffer that the runtime reuses on every inference.

Weights: storage grows with precision

Weight formatBytes per weightWeight storage
8-bit integer (INT8)1251,264 B
16-bit integer (INT16)2502,528 B
32-bit float (FP32)41,005,056 B

Activations: the working set is small

The input buffer holds 1,950 values. Its size depends on the number format:

Input formatInput buffer (RAM)
8-bit (INT8)1,950 B
16-bit (INT16)3,900 B
32-bit float (FP32)7,800 B
FLASH (weights)8-bit weights251,264 B16-bit weights502,528 BRAM (input buffer)8-bit input1,950 B16-bit input3,900 BSame scale for all four bars. The 16-bit input adds about 2 KB of RAM; 16-bit weights would add about 251 KB of flash.
Figure 4. The same scale for every bar. Weights dominate flash; the input buffer is small. Logical activation counts are not the peak RAM of the runtime.
The key comparison. Moving our input from 8 to 16 bits adds about 2 KB of RAM. Moving every weight from 8 to 16 bits would add about 251 KB of flash. Activations are a small part of this model's memory; weights are the big part.

A word of caution: the sum of all activation values (2,104 logical values for this model) is not the peak RAM your runtime needs. Scratch buffers, alignment and the runtime's own bookkeeping all add to it. Always measure the arena.

5. Memory is capacity; computation is recurring work

Memory is a one-time reservation. Computation is paid again on every inference. Our model runs about ten times per second (a new window every 100 ms), and each run performs the same 251,264 MACs:

DurationMAC operations, if inference keeps upExtra input RAM from 16-bit input
One inference251,2641,950 B (fixed)
One second2,512,6401,950 B
One minute150,758,4001,950 B
One hour9,045,504,0001,950 B

The memory cost stays flat, while the work keeps adding up. A small change in how each number is stored can therefore change a very large number of operations.

Here is one way to see why. In the hidden layer, each of the 1,950 inputs is used by all 128 neurons. The input is stored once but used 128 times. The computation, not the storage, is where precision costs you.

6. Why a wider number can be slower

Think of a warehouse. An INT8 forklift can carry several small boxes in one trip. If the boxes are bigger, the forklift attachment does not fit, and the same boxes need more trips. The number of boxes has not changed. The time has.

Three things decide how fast the same arithmetic runs:

The accumulator example makes the last point concrete. Two extreme cases, each repeated 1,950 times:

FormatOne product (worst case)Sum of 1,950 productsFits in 32 bits?
8-bit input × 8-bit weight127 × 127 = 16,12931,451,550Yes (limit 2,147,483,647)
16-bit input × 8-bit weight32,767 × 127 = 4,161,4098,114,747,550No

A 16-bit path therefore needs a 64-bit accumulator. The extra cost is in the arithmetic, not in memory: one accumulator is reused for all 1,950 products of a neuron, so it adds only a few bytes of state.

A wider number is not automatically slower. A single 16-bit multiplication is no slower than an 8-bit one on paper. The risk is that a faster path is lost. In the TFLM version we used, the chip library provides an optimized kernel for our 8-bit fully connected layer, but the 16-bit-input path runs on a general kernel. We did not measure how much slower that is, because the 16-bit build did not produce correct output on the board (section 8).

7. Does more precision help? Find the error before you fix it

Our first idea was simple. The model's inputs are 1,950 numbers rounded to 8 bits. The words mute and up can produce similar numbers, and rounding may merge them. With a made-up scale of 0.01 for illustration, two features of 10.2 and 10.5 would become 1,020 and 1,050, and stay different. At 8 bits with our real input scale of about 0.8669, both round to the same value.

That idea is reasonable, but it is a hypothesis, so we measured each quantization step separately:

Step that is quantizedAccuracy cost on the test windows
Input features only0.03 points
Output scores (logits) only3.16 points

The input rounding costs almost nothing. The output scores hold most of the loss. A 16-bit candidate helped because it kept the output in higher precision, not because its input was wider. Our first explanation was wrong, and the measurement showed where the error really lived.

Input features only0.03 pointsOutput scores only3.16 pointsBars drawn to the same scale. Measured on the test windows.
Figure 5. Where the accuracy loss came from. Rounding the input cost almost nothing; rounding the output scores cost most of it.
Lesson: before you spend time or memory on a fix, measure which step causes the error. A reasonable-sounding cause can be the wrong one.

8. What happened on the board

The 16-bit candidate passed the checks we ran on the PC. On the ESP32-S3 it failed in a way that was easy to miss. For a silent input, all 13 output classes came back with the same probability, 0.07693 (one in thirteen). With every score equal, the code picks the first class, and that looked like a correct "silence" result.

Our boot self-test had the same blind spot. It checked only that the top class was silence, so the tie passed. We then changed the test to require a clear margin between silence and the next class, and the 16-bit build now fails the test as it should.

PC checkpassesBoardzero input13 scoresall 0.07693argmaxfirst class winsSelf-testneeds marginA tie passed the old self-test. The fix requires a clear margin between silence and the next class.
Figure 7. How a wrong output looked right. A tie resolves to the first class, which here was silence.
Lessons from the board:
  • A correct model file does not guarantee correct output. The runtime and its kernels decide what really runs.
  • A check that looks only at the winning label can be fooled by a tie. Check the margin, or check that the outputs are not all the same.
  • Do not time a build whose output is wrong. Its speed describes a different computation. The 16-bit timing from that build is not a valid comparison.

The cause was not found. The runtime version we use does not let us read internal tensors, so the problem is still open. The 2× and 3× slowdown figures in the next sections are scenarios, not measurements.

9. The real-time budget: a 100 ms loop

A new window arrives every 100 ms. If one inference takes longer than that, windows wait in a queue, and when the queue is full, new windows are dropped. A late or missing window means a late or missing command.

On an earlier instrumented INT8 build, the median inference took 33.5 ms. Here is what that means for the budget:

100 ms: next window arrivesINT8 measured33.5 ms (66.5 ms free)2× scenario67 ms (33 ms free)3× scenario100.5 ms: overrun
Figure 6. The 100 ms budget. The INT8 median is measured on an earlier build; the 2× and 3× rows are scenarios, not measurements. Shares are of elapsed time, not CPU use.
ScenarioInference timeShare of the 100 ms windowStatus
INT8, measured median33.5 ms33.5%66.5 ms left for other work
Scenario: 2× slower67 ms67%33 ms left
Scenario: 3× slower100.5 ms100.5%0.5 ms overrun, queue starts to build

These percentages are elapsed time, not measured CPU use, because the real-time scheduler can interrupt inference to run other tasks. Latency, queues and CPU use all have to be measured on the device.

10. The other way to change a model: add neurons

Suppose the team doubles the hidden layer from 128 to 256 neurons to tell mute from up more clearly. That is a different kind of change: the architecture changes, so the work changes.

ResourceNow256 neurons16-bit activations
Weights251,264502,528251,264
Weight storage251,264 B502,528 B251,264 B
Input RAM1,950 B1,950 B3,900 B
Multiply-accumulates251,264502,528251,264

More neurons means more storage and more arithmetic. A wider number means a little more RAM and possibly a slower kernel, with the same arithmetic. Two different reasons for a slower model, and two different fixes.

11. The mental model to keep

QuestionWhat it controls
How much knowledge must I store?Weight count and weight precision (flash)
How much working memory do I need?Activation count and precision (RAM)
How much arithmetic must I do?Operation count (MACs), set by the architecture
How fast can my processor do that arithmetic?Kernel efficiency, packing and accumulator width

These four are related but not interchangeable. A model can barely change in flash size and still take much longer to run, because it changed its computation path. Measure all four.

Knowledge check

Try the questions first, then open the answer.

Why is the 16-bit-activation model only about 2 KB bigger than the INT8 one?

Because the 251,264 weights stay 8-bit. Only the input buffer and a few small items grow.

Does a 16-bit input use more RAM?

Yes. The input grows from 1,950 to 3,900 bytes. The total arena must still be measured, because scratch buffers and bookkeeping add to it.

Does a 16-bit input change the number of multiply-accumulates?

No, if the architecture stays the same. The count stays at 251,264. What can change is how much time each operation takes.

Our first guess was that 8-bit input rounding confused mute and up. What did the measurement say?

Input quantization cost only 0.03 accuracy points. Output score quantization cost 3.16 points. The output was the larger source of error.

Why could the 16-bit build's timing not be used?

Its output was wrong on the device: every class had the same score for silence. A fast wrong answer is not a valid timing.

Is a 2× slowdown guaranteed for 16-bit activations?

No. It is a scenario. The actual cost depends on the kernel, and it was not measured on a correct build.

Why does a wider accumulator not need much extra RAM?

One accumulator is reused for all products of a neuron. It is state, not a buffer for each product.

Glossary

TermPlain meaning
ActivationA number the network computes from the current input, layer by layer.
ArenaThe block of RAM the TensorFlow Lite Micro runtime reserves for activations and scratch space.
BiasA learned number added to a neuron's sum before the output is used.
CadenceHow often inference runs. Here, one window every 100 ms.
FlashNon-volatile program memory. Model weights live here in our firmware.
KernelThe code that performs one operation, such as a fully connected layer.
LogitA raw output score before it is turned into a probability.
MACMultiply-accumulate: one multiplication and one addition into a running total.
QuantizationStoring numbers with fewer bits, using a scale and a zero point to map them to integers.
WeightA learned number in the model. It stays fixed while the device runs.

Keep going

More lessons are planned in this series. If you want the full method behind building an embedded ML product, the book is the reference: The Embedded Engineer's Guide to Edge AI. For a guided path from reading to building, see Learning. Questions or corrections are welcome through Connect.

← All Embedded ML Lessons