Guide to AI and Data Courses · Lecture Notes
Chapter 36: Embedded AI
A smartwatch has to notice within a few milliseconds that its wearer has fallen, and it has to do so without draining the battery; sending the data to the cloud and waiting for an answer is neither fast enough nor cheap enough. Yet when we train models on a server, memory and energy hardly cross our minds. Embedded AI is the course that bridges the two worlds: fitting a trained model onto a device with a few hundred kilobytes of memory, running it fast, and measuring how much energy it uses. These notes introduce the course and work through its three core ideas with small examples you can compute by hand.
Learning outcomes
- Resource budget
- Compute by hand the parameter, operation and activation-memory cost of a convolution layer.
- Efficient architecture
- Compare the cost of standard and depthwise separable convolution and explain the saving.
- Quantization
- Convert weights to 8-bit integers with a scale factor, find the rounding error and show the effect of an outlier.
- Pruning and distillation
- Apply magnitude pruning and compute how temperature softens a teacher's output.
- Place of the course
- State what separates embedded AI from deep learning, GPU programming, MLOps and robotics.
36.1What question does the course ask?
How do AI models run on small, low-power devices?
Embedded AI studies running trained models on devices with limited memory, compute and energy, such as phones, cameras or microcontrollers. The course covers shrinking a model by quantization, pruning and distillation, moving it onto the device, and measuring latency and energy; the extreme form is called TinyML.
In a deep learning course, success is usually measured by accuracy, and the model grows on the assumption of a fast server or GPU. In embedded AI that assumption disappears. The device's memory, compute and battery are fixed, and the model has to fit inside them. The question is: how far can we shrink a model while losing very little accuracy, and how fast and how frugally does the shrunken model run on the device?
That is why the performance table of this course does not list accuracy alone. Model size (flash), peak memory (SRAM), latency and energy go in the same row. A model with high accuracy is useless if it does not fit in memory or can only run once a second.
36.1.1Training on the server, inference on the device
Many misconceptions in this course are cleared up by one distinction. Training is mostly done on a server in 32-bit floating point; what goes to the device is a trained, frozen model, and the device only performs inference. The shrinking steps (quantization, pruning, distillation) sit between these two stages. The three core sections of these notes show in turn how to measure whether a model fits a device and how to shrink it.
36.1.2At a glance
| Criterion | Embedded AI |
|---|---|
| Approach | resource-constrained inference |
| Input | trained model, device constraints |
| Output | small, fast model |
| Success measure | latency, energy, memory |
| Math load | medium |
| Data needs | training data already exists |
36.2A short history
| Year | Milestone |
|---|---|
| 2015 | Hinton, Vinyals and Dean introduce knowledge distillation: a large model's knowledge passes to a small one. |
| 2016 | Han et al. combine pruning and quantization in Deep Compression, shrinking networks by tens of times. |
| 2017 | MobileNets is published and TensorFlow Lite announced: deep networks on phones become routine. |
| 2018 | Google announces the Edge TPU accelerator for edge devices. |
| 2019 | The first tinyML Summit is held; Warden and Situnayake publish the book TinyML. |
| 2020 | MCUNet runs ImageNet classification on a microcontroller with over 70% accuracy. |
36.3Core concepts
The rest of the course is built on the concepts below. The following sections work through the most important ones with examples.
| Concept | Meaning |
|---|---|
| Quantization | 8-bit or smaller integers instead of 32-bit floats. |
| Pruning | Removing unimportant weights, channels or layers. |
| Knowledge distillation | A small student model mimics a large teacher. |
| Efficient architectures | MobileNet, depthwise separable convolution. |
| Inference engine | The thin software layer that runs the model on the device. |
| Accelerators | NPU, DSP, Edge TPU: hardware built for neural networks. |
| Memory budget | A few hundred KB of SRAM and flash for weights and activations. |
| Latency and energy | Two performance metrics as important as accuracy. |
36.4Memory and compute budget: efficient convolution
A microcontroller has two separate memories. Weights are stored in flash, like the program; the intermediate results (activations) are held in SRAM during inference, and SRAM is often only a few hundred kilobytes. So for each layer we count three things: the number of parameters (flash), the number of multiply-accumulates (compute, tied to latency and energy) and the activation memory (SRAM).
With Cin input channels, Cout output channels and a k × k kernel, the number of parameters is k2 · Cin · Cout. If the output map is H × H, the number of multiply-accumulates is H2 times the number of parameters, because the same weights are reused at every position.
The trick of the MobileNet family is the depthwise separable convolution. A standard convolution mixes the spatial neighborhood and the channels at the same time. This method splits the job in two: first each channel is filtered on its own with a k × k kernel (depthwise), then a 1 × 1 convolution mixes the channels (pointwise).
Input 32 × 32 × 16, output 32 × 32 × 32, kernel 3 × 3, same padding (the output map is also 32 × 32).
- Standard convolution. Parameters: 3 · 3 · 16 · 32 = 4608. Multiply-accumulates: 4608 · 1024 = 4,718,592.
- Depthwise part. One 3 × 3 kernel per input channel: 3 · 3 · 16 = 144 parameters.
- Pointwise part. 1 · 1 · 16 · 32 = 512 parameters.
- Total. 144 + 512 = 656 parameters; multiply-accumulates 656 · 1024 = 671,744.
- Ratio. 4608 / 656 ≈ 7.0: both parameters and operations drop about sevenfold.
- Memory (8-bit integers). Weights: 656 bytes. Activations: input 32 · 32 · 16 = 16,384 bytes, output 32 · 32 · 32 = 32,768 bytes; while the layer runs both sit in SRAM, so peak memory is 49,152 bytes (48 KB).
The parameter count fell from 4608 to 656, but the SRAM need did not change: peak memory comes from the size of the activations.
As a rule of thumb, as the number of output channels Cout and the kernel size k grow, the operation ratio approaches 1/Cout + 1/k2; with a 3 × 3 kernel that is about an 8-9 fold reduction. This is why efficient architectures are the standard choice on small devices.
Assuming that fewer parameters means a proportionally faster model. Depthwise convolution does little work per memory access and on much hardware it does not speed up as much as the parameter count suggests; speed has to be measured on the hardware. Also, as the example shows, peak SRAM use does not shrink with the parameter count.
36.5Quantization
Quantization represents 32-bit floating-point numbers with 8-bit integers. Memory drops fourfold, integer arithmetic is faster and more energy-efficient than floating point on most hardware, and many small processors have no floating-point unit at all. The price is rounding error.
Scale factor s = max|w| / 127. The quantized value is q = round(w / s) (between −127 and 127), and the recovered value is ŵ = s · q. The rounding error is at most s / 2.
Let the weights be w = (0.80, −1.27, 0.35, 0.004, −0.503) and the input x = (1, 2, −1, 3, 0.5).
- Scale. max|w| = 1.27, s = 1.27 / 127 = 0.01.
- Quantize. q = (80, −127, 35, 0, −50); 0.004/0.01 = 0.4 rounds to 0, and −0.503/0.01 = −50.3 rounds to −50.
- Error. ŵ = (0.80, −1.27, 0.35, 0, −0.50); the errors are 0, 0, 0, 0.004, 0.003, all below s/2 = 0.005.
- Output. The true dot product is w · x = 0.8 − 2.54 − 0.35 + 0.012 − 0.2515 = −2.3295. The quantized one is ŵ · x = 0.8 − 2.54 − 0.35 + 0 − 0.25 = −2.34. The difference is about 0.01: a relative error below 0.5%.
In memory terms, the 5 weights went from 20 bytes (32-bit) to 5 bytes.
36.5.1An outlier spoils the scale
Because the largest absolute value sets the scale factor, a single outlier lowers the resolution of all the weights. If we add a sixth weight of 12.7 to the same weights, then s = 12.7 / 127 = 0.1. Now q = (8, −13, 4, 0, −5, 127); recovering gives −1.3 for −1.27 and 0.4 for 0.35 (errors 0.03 and 0.05; the largest error before was 0.004). That is why per-channel scales, clipping outliers and quantization-aware training (imitating the rounding during training) are common remedies in practice.
Assuming that quantization always ruins accuracy, or never does. 8-bit post-training quantization costs very little accuracy in most models, but the loss can grow in layers with outliers or with narrower formats such as 4 bits. We always re-measure the quantized model on real validation data.
36.6Pruning and knowledge distillation
Quantization shrinks each number; pruning reduces the number of numbers. Most weights in a network contribute very little to the output. The simplest method is magnitude pruning: set the weights with the smallest absolute values to zero, then retrain the model briefly with the remaining weights.
A neuron has eight weights w = (0.9, −0.05, 0.4, 0.02, −0.7, 0.1, −0.03, 0.3) and input x = (1, 2, 0, −1, 1, 3, 2, 1).
- Full output. 0.9 − 0.1 + 0 − 0.02 − 0.7 + 0.3 − 0.06 + 0.3 = 0.62.
- 50% pruning. The four weights with the largest absolute value stay: 0.9, 0.4, −0.7, 0.3 (the 1st, 3rd, 5th and 8th weights); the other four are set to zero.
- Pruned output. 0.9·1 + 0.4·0 − 0.7·1 + 0.3·1 = 0.5.
- Interpretation. Half the weights are gone and the output moved from 0.62 to 0.5. If the remaining weights are retrained, this gap usually closes.
Assuming that when half the weights are zero the model is half the size and twice as fast. If the zeros are scattered (unstructured pruning), dense hardware still multiplies all of them and the file stays the same size; a gain needs a sparse format and hardware that supports it. Structured pruning, which removes whole channels or filters, creates fewer zeros but gives a real speedup.
The third route is knowledge distillation: a small student model is trained to imitate the outputs of a large teacher model. What matters is that the teacher provides not just the correct class but the whole probability distribution over classes; that distribution carries how similar the classes are to one another. To make this information visible, a temperature T is added to the softmax: pi = exp(zi/T) / Σ exp(zj/T).
The teacher's raw outputs (logits) for three classes are z = (5, 2, 1).
- T = 1. exp(5), exp(2), exp(1) ≈ 148.4, 7.39, 2.72, sum 158.5. The probabilities are ≈ (0.936, 0.047, 0.017): almost one class, and no information about the other two.
- T = 4. The logits are z/T = (1.25, 0.5, 0.25); their exponentials are ≈ 3.49, 1.65, 1.28, sum 6.42. The probabilities are ≈ (0.543, 0.257, 0.200).
- Interpretation. At high temperature the teacher reveals that the second class is closer to the truth than the third. A student trained on these soft targets learns not only the correct label but this relationship too.
In practice the methods are combined: an efficient architecture is chosen, trained by distillation from the teacher, then pruned and quantized to 8 bits. After each step accuracy, size and latency are measured together.
36.7How is the course taught?
36.7.1A typical workflow
A small keyword-spotting network is trained on a computer with 32-bit floats, quantized to 8-bit integers and loaded onto a microcontroller with a few hundred kilobytes of memory. The device listens all the time, never sends audio to the cloud and uses very little power.
36.7.2Topics over the semester
- Embedded hardware: MCUs, memory and energy budgets
- Sensor data collection and preprocessing
- Small neural networks and efficient architectures
- Post-training quantization
- Quantization-aware training
- Pruning and sparse networks
- Knowledge distillation
- Model conversion: TensorFlow Lite and ONNX
- Inference on microcontrollers: TFLite Micro, CMSIS-NN
- Accelerators: NPUs, DSPs and the Edge TPU
- Measuring latency, memory, energy; MLPerf Tiny
- On-device learning and over-the-air model updates
This is a typical semester; the order and coverage vary between universities.
36.7.3Skills you will gain
- converting models with TensorFlow Lite
- applying 8-bit quantization
- deploying a model to Arduino
- an end-to-end project in Edge Impulse
- measuring latency, memory and energy
36.7.4Try it yourself
The Try It: Fit the Model on a Microcontroller slide in the presentation lets you solve a typical problem of this course yourself: A vibration sensor on a motor must recognize faults by itself, but its processor has 512 KB of memory. Choose how many bits to store each weight in; size, speed and accuracy change together.
36.8Are you ready for this course?
36.8.1Prerequisites
| Course | Why it is needed |
|---|---|
| Deep Learning | quantization, pruning, small models |
| Microprocessors | memory, registers, interrupts and peripherals: understanding device limits |
| Programming with C | the language of embedded software; pointers and static memory management |
36.8.2Ask yourself
For every "no", the right-hand column shows where to start. If you can say "yes" to most of them, you are ready.
| Question | If not, start with |
|---|---|
| Can you train a convolutional network and report its validation accuracy? | Deep Learning |
| Can you write C code with pointers and without dynamic memory allocation? | A C programming course |
| Can you flash a program to a microcontroller and read data from a sensor? | A microprocessors course and an Arduino starter kit |
| Can you explain how a number is represented as an 8-bit integer? | Binary numbers and fixed point from digital design |
36.9Neighboring courses
36.9.1Where it overlaps
| Relation | Course | Shared ground |
|---|---|---|
| application | This course applies the methods of Deep Learning | quantization, pruning, small models |
| shares methods | Shares methods with Robotics and Robot Vision | real-time on-device inference |
| shares methods | Shares methods with Machine Learning Systems and MLOps | deploying and updating models on devices |
| shares methods | Shares methods with GPU Programming and Parallel Computing | hardware accelerators |
36.9.2Where it differs
- Deep Learning: Deep learning grows the model for accuracy; this course shrinks the same model while losing little accuracy.
- GPU Programming and Parallel Computing: GPU programming gets peak performance from large accelerators; this course runs inference on devices drawing a few milliwatts.
- Machine Learning Systems and MLOps: MLOps deploys and monitors models on servers; this course embeds the model in devices that may have no connection.
- Robotics and Robot Vision: Robotics treats perception, localization and motion as a whole; this course makes the robot's model run in real time.
36.10In practice
36.10.1Applications
| Area | What is done |
|---|---|
| Wake words | An always-listening model in speakers and phones. |
| Smart cameras | Object detection without sending images to the cloud. |
| Wearables | Fall, step and heart rhythm analysis on a watch. |
| Predictive maintenance | Catching fault signs in motor vibration. |
| On-phone AI | Photo enhancement, offline translation. |
| Farming and environment | Sound and pest detection on battery sensors. |
36.10.2Risks and ethics
| Risk | Explanation |
|---|---|
| The hidden cost of shrinking | Quantization and pruning barely move average accuracy, yet rare classes and minority groups can lose far more. |
| Devices that cannot be updated | A faulty or vulnerable model spread over thousands of devices in the field is hard to recall. |
| Unnoticed surveillance | Always-listening microphones and cameras can monitor bystanders unawares, even when data stays on the device. |
| Model theft | The model file on a device can be read and copied, and reverse engineering opens it to adversarial attacks. |
These issues are discussed in depth in Explainable AI and AI Ethics.
36.10.3Tools and datasets
Tools: TensorFlow Lite, ONNX Runtime, ExecuTorch, Edge Impulse, CMSIS-NN, Arduino, NVIDIA Jetson.
Well-known datasets: Speech Commands, Visual Wake Words, MNIST, CIFAR-10, ImageNet, MLPerf Tiny.
Success is not measured by accuracy alone: model size, peak memory, latency and energy go in the same table.
36.11Common misconceptions
Models are trained on the small device.
Training is mostly done on a server; only inference runs on the device.
Quantization always ruins accuracy.
8-bit quantization costs most models very little accuracy; quantization-aware training reduces the loss further.
Fewer parameters means a faster model.
Speed depends on memory access, operation types and hardware support; parameter count is not enough.
Embedded AI is only needed without internet.
For privacy, latency and energy, on-device processing is preferred even when a connection exists.
36.12Summary
- In embedded AI the model has to fit the device's memory, compute and energy; success is measured by size, peak memory, latency and energy together with accuracy.
- Training happens on a server and only inference runs on the device; the shrinking steps sit between the two stages.
- Depthwise separable convolution cuts parameters and operations by about seven to eight times with a 3 × 3 kernel, but peak SRAM use depends on the size of the activations.
- 8-bit quantization cuts memory to a quarter; the rounding error is at most half the scale factor, and a single outlier can enlarge the scale and increase the error.
- Pruning reduces the number of weights and distillation reduces model size; unstructured zeros do not speed up hardware automatically.
36.13Test Yourself
Compute the parameter count, in standard and depthwise separable form, of a convolution layer with input 16 × 16 × 8, output 16 × 16 × 16 and a 3 × 3 kernel.
Answer
Standard: 3·3·8·16 = 1152. Separable: depthwise 3·3·8 = 72, pointwise 1·1·8·16 = 128, total 200. Ratio 1152 / 200 = 5.76.
For the standard form of the layer in question 1, how many multiply-accumulates are needed? With 8-bit integers, how many bytes of SRAM do the input and output activations need together?
Answer
The output map has 16 × 16 = 256 positions: 1152 · 256 = 294,912 multiply-accumulates. Input 16·16·8 = 2048 bytes, output 16·16·16 = 4096 bytes; total 6144 bytes (6 KB).
The weights are (0.6, −0.2, 0.9). Find the scale factor and the quantized values for symmetric 8-bit quantization.
Answer
s = 0.9 / 127 ≈ 0.00709. 0.6/0.00709 ≈ 84.7 → 85; −0.2/0.00709 ≈ −28.2 → −28; 0.9/0.00709 = 127. So q = (85, −28, 127).
The weights are (1.27, 0.46, −0.234, 0.007). (a) Quantize with s = 0.01 and find the errors. (b) If a weight of 12.7 is added to the same weights, the scale becomes 0.1; find the new quantized values and the errors.
Answer
(a) q = (127, 46, −23, 1); recovered (1.27, 0.46, −0.23, 0.01), errors 0, 0, 0.004, 0.003. (b) With s = 0.1, q = (13, 5, −2, 0); recovered (1.3, 0.5, −0.2, 0), errors 0.03, 0.04, 0.034, −0.007. A single outlier made the error about ten times larger.
Apply 50% pruning (zero the four weights with the smallest absolute value) to w = (0.5, −0.1, 0.3, 0.05, −0.4, 0.2, −0.02, 0.6) and x = (1, 1, 1, 1, 1, 1, 1, 1); find the full and the pruned output.
Answer
Full output: 0.5 − 0.1 + 0.3 + 0.05 − 0.4 + 0.2 − 0.02 + 0.6 = 1.13. The four smallest absolute values are 0.02, 0.05, 0.1, 0.2; the rest are 0.5, 0.3, −0.4, 0.6. Pruned output 0.5 + 0.3 − 0.4 + 0.6 = 1.0.
A teacher's logits are z = (4, 0). Find the teacher's probabilities for T = 1 and T = 2. Which gives the student more information?
Answer
T = 1: e4 ≈ 54.6, sum 55.6; p ≈ (0.982, 0.018). T = 2: logits (2, 0), e2 ≈ 7.39, sum 8.39; p ≈ (0.881, 0.119). T = 2 is softer; the probability of the second class rises from 0.018 to 0.119, so the distribution carries more information to the student. With two classes the effect is limited; the real benefit appears in many-class problems.
36.14After this course
Embedded AI connects deep learning to hardware: from here you move on to robotics, autonomous vehicles and Internet of Things applications. For more depth in hardware, accelerator design and compilers come next; on the software side, MLOps and updating fleets of devices.
Related careers: Embedded software engineer, Edge AI engineer, Machine learning engineer, Hardware-software co-designer, IoT solutions architect.
36.15Further reading
- P. Warden and D. Situnayake, TinyML (2019): walks through keyword-spotting and image-classification projects on a microcontroller from start to finish.
- V. Sze, Y.-H. Chen, T.-J. Yang and J. Emer, Efficient Processing of Deep Neural Networks (2020): a systematic account of how memory, dataflow and hardware accelerators shape deep networks.
- S. Han, H. Mao and W. Dally, "Deep Compression" (ICLR 2016): the paper that shrinks networks by tens of times by combining pruning, quantization and coding.
36.16References
- Warden and Situnayake, TinyML, O'Reilly, 2019.
- Sze, Chen, Yang and Emer, Efficient Processing of Deep Neural Networks, Morgan & Claypool, 2020.
- Han, Mao and Dally, "Deep Compression: Compressing Deep Neural Networks with Pruning, Trained Quantization and Huffman Coding", ICLR, 2016.
- Howard et al., "MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications", arXiv:1704.04861, 2017.
- Hinton, Vinyals and Dean, "Distilling the Knowledge in a Neural Network", arXiv:1503.02531, 2015.
Related pages: Slides · Slides (PDF) · Course page