Skip to content

Commit 72dd830

Browse files
Merge pull request #1293 from dominica-of/main
TinyMl on Arm using PyTorch v2.x and ExecuTorch Intro
2 parents be8050c + 61487db commit 72dd830

File tree

14 files changed

+462
-0
lines changed

14 files changed

+462
-0
lines changed
2.28 MB
Loading
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Overview
3+
weight: 2
4+
5+
### FIXED, DO NOT MODIFY
6+
layout: learningpathall
7+
---
8+
9+
## Module Overview
10+
This session delves into TinyML, which applies machine learning to devices with limited resources like microcontrollers. This module serves as a starting point for learning how cutting-edge AI technologies may be put on even the smallest of devices, making Edge AI more accessible and efficient.
11+
12+
Additionally, we'll cover the necessary setup on your host machine and target device to facilitate cross-compilation and ensure smooth integration across all devices.
13+
14+
## Introduction to TinyML
15+
TinyML represents a significant shift in how we approach machine learning deployment. Unlike traditional machine learning, which typically depends on cloud-based servers or high-powered hardware, TinyML is tailored to function on devices with limited resources, such as constrained memory, power, and processing capabilities. TinyML has quickly gained popularity because it enables AI applications to operate in real-time, directly on the device, with minimal latency, enhanced privacy, and the ability to work offline. This shift opens up new possibilities for creating smarter and more efficient embedded systems.
21.3 KB
Loading
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
title: Introduction to TinyML on Arm using PyTorch v2.x and Executorch
3+
4+
minutes_to_complete: 40
5+
6+
who_is_this_for: This learning module is tailored for developers, engineers, and data scientists who are new to TinyML and interested in exploring its potential for edge AI. If you have an interest in deploying machine learning models on low-power, resource-constrained devices, this course will help you get started using PyTorch v2.x and Executorch on Arm-based platforms.
7+
8+
learning_objectives:
9+
- Identify TinyML and how it's different from the AI you might be used to.
10+
- Understand the benefits of deploying AI models on Arm-based edge devices.
11+
- Select Arm-based devices for TinyML.
12+
- Identify real-world use cases demonstrating the impact of TinyML in various industries.
13+
- Install and configure a TinyML development environment.
14+
- Set up a cross-compilation environment on your host machine.
15+
- Perform best practices for ensuring optimal performance on constrained edge devices.
16+
17+
18+
prerequisites:
19+
- Basic knowledge of machine learning concepts.
20+
- Understanding of IoT and embedded systems (helpful but not required).
21+
- A Linux host machine or VM running Ubuntu 20.04 or higher, or an AWS account to use [Arm Virtual Hardware](https://www.arm.com/products/development-tools/simulation/virtual-hardware)
22+
- Target device, phyisical or using the or Corstone-300 FVP, preferably Cortex-M boards but you can use Cortex-A7 boards as well.
23+
24+
25+
author_primary: Dominica Abena O. Amanfo
26+
27+
### Tags
28+
skilllevels: Introductory
29+
subjects: ML
30+
armips:
31+
- Cortex-A
32+
- Cortex-M
33+
34+
operatingsystems:
35+
- Linux
36+
37+
tools_software_languages:
38+
- Corstone 300 FVP
39+
- Grove - Vision AI Module V2
40+
- Python
41+
- PyTorch v2.x
42+
- Executorch
43+
- Arm Compute Library
44+
- GCC
45+
- Edge Impulse
46+
- Nodejs
47+
48+
### FIXED, DO NOT MODIFY
49+
# ================================================================================
50+
weight: 1 # _index.md always has weight of 1 to order correctly
51+
layout: "learningpathall" # All files under learning paths have this same wrapper
52+
learning_path_main_page: "yes" # This should be surfaced when looking for related content. Only set for _index.md of learning path content.
53+
---
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
next_step_guidance: Research which lightweight ML models can be used for TinyML, and what device is best for a specific project.
3+
4+
recommended_path: /learning-paths/microcontrollers/intro/
5+
6+
7+
further_reading:
8+
- resource:
9+
title: TinyML Brings AI to Smallest Arm Devices
10+
link: https://newsroom.arm.com/blog/tinyml
11+
type: blog
12+
13+
14+
15+
# ================================================================================
16+
# FIXED, DO NOT MODIFY
17+
# ================================================================================
18+
weight: 21 # set to always be larger than the content in this path, and one more than 'review'
19+
title: "Next Steps" # Always the same
20+
layout: "learningpathall" # All files under learning paths have this same wrapper
21+
---
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
review:
3+
- questions:
4+
question: >
5+
1. What is TinyML?
6+
answers:
7+
- "Machine learning models designed to run on large, cloud-based servers."
8+
- "Machine learning models designed to run on resource-constrained devices like microcontrollers and edge devices."
9+
- A cloud service for deep learning model deployment.
10+
- A special type of machine learning for virtual reality applications
11+
correct_answer: 2
12+
explanation: >
13+
TinyML is specifically designed to operate on devices with limited computational resources.
14+
15+
- questions:
16+
question: >
17+
2. Which of the following is NOT a benefit of deploying TinyML on Arm devices?
18+
answers:
19+
- "Enhanced data privacy"
20+
- "Low latency"
21+
- High power consumption
22+
- Cost-effectiveness
23+
correct_answer: 3
24+
explanation: >
25+
High power consumption is not a benefit, one of the key advantages of TinyML on Arm devices is their ability to perform tasks with very low power usage.
26+
27+
- questions:
28+
question: >
29+
3. Which of the following is an example of TinyML in healthcare?
30+
answers:
31+
- Smart sensors for soil moisture monitoring.
32+
- Wearable devices monitoring vital signs and detecting heart arrhythmias.
33+
- Predictive maintenance for factory machines.
34+
- Object detection for smart home cameras.
35+
correct_answer: 2
36+
explanation: >
37+
Wearable devices that monitor vital signs and detect heart arrhythmias show TinyML's ability to perform complex analyses in real-time on resource-constrained device.
38+
39+
40+
41+
# ================================================================================
42+
# FIXED, DO NOT MODIFY
43+
# ================================================================================
44+
title: "Review" # Always the same title
45+
weight: 20 # Set to always be larger than the content in this path
46+
layout: "learningpathall" # All files under learning paths have this same wrapper
47+
---
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: Real-World Applications of TinyML with Examples of Arm-Based Solutions
3+
weight: 5
4+
5+
### FIXED, DO NOT MODIFY
6+
layout: learningpathall
7+
---
8+
9+
TinyML is being deployed across various industries, enhancing everyday experiences and enabling groundbreaking solutions. Here are a few examples:
10+
11+
## Healthcare - Wearable Heart Rate Monitors
12+
- Arm-based microcontrollers like those in Fitbit devices run TinyML models to monitor vital signs such as heart rate, detect arrhythmias, and provide real-time feedback.
13+
14+
- **Example**: Fitbit Charge 5 smart band uses the Arm 32-bit Cortex-M4 processor.
15+
16+
## Agriculture - Smart Irrigation Systems
17+
- Arm-powered microcontrollers in smart sensors help monitor soil moisture and control water usage. TinyML models process environmental data locally to optimize water distribution.
18+
- **Example**: OpenAg uses Arm Cortex-M processors to run machine learning models on edge devices, optimizing irrigation based on real-time data.
19+
20+
## Home Automation - Smart Cameras
21+
- Arm-based processors in smart cameras can detect objects and people, triggering alerts or actions without needing to send data to the cloud, saving bandwidth and improving privacy.
22+
- **Example**: Arlo smart cameras, powered by Arm Cortex processors, perform object detection at the edge, enhancing performance and energy efficiency.
23+
24+
## Industrial IoT - Predictive Maintenance in Factories (e.g., Siemens Predictive Maintenance)
25+
- Arm-powered industrial sensors analyze vibration patterns in machinery, running TinyML models to predict when maintenance is needed and prevent breakdowns.
26+
- **Example**: Siemens utilizes Arm Cortex-A processors in industrial sensors for real-time data analysis, detecting faults before they cause significant downtime. They rely on Arm-based processors for their Industrial Edge computing solutions.
27+
28+
## Wildlife Conservation - Smart Camera Traps (e.g., Conservation X Labs)
29+
- Arm-based smart camera traps can identify animal movements or detect poachers using TinyML models. These energy-efficient devices can operate in remote areas without relying on external power sources.
30+
- **Example**: Conservation X Labs uses Arm Cortex-M microcontrollers to power camera traps, helping monitor endangered species in the wild.
31+
32+
33+
34+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Benefits of TinyML for Edge Computing on Arm Devices
3+
weight: 4
4+
5+
### FIXED, DO NOT MODIFY
6+
layout: learningpathall
7+
---
8+
9+
The advantages of TinyML for edge computing on Arm devices are vast, particularly when paired with Arm's architecture, which is widely used in IoT, mobile devices, and edge AI deployments. Here are some key benefits:
10+
11+
- Power Efficiency: TinyML models are designed to be extremely power-efficient, making them ideal for battery-operated devices like sensors, wearables, and drones.
12+
- Low Latency: Because the AI processing happens on-device, there's no need to send data to the cloud, reducing latency and enabling real-time decision-making.
13+
- Data Privacy: With on-device computation, sensitive data remains local, providing enhanced privacy and security. This is particularly crucial in healthcare and personal devices.
14+
- Cost-Effective: Arm devices, which are cost-effective and scalable, can now handle sophisticated machine learning tasks, reducing the need for expensive hardware or cloud services.
15+
- Scalability: With billions of Arm devices in the market, TinyML is well-suited for scaling across industries, enabling widespread adoption of AI at the edge.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
# User change
3+
title: "Build a Simple PyTorch Model"
4+
5+
weight: 9 # 1 is first, 2 is second, etc.
6+
7+
# Do not modify these elements
8+
layout: "learningpathall"
9+
---
10+
11+
With our Environment ready, we will create a simple program to test our setup. This example will define a simple feedforward neural network for a classification task. The model consists of 2 linear layers with ReLU activation in between. Create a file called simple_nn.py with the following code:
12+
13+
```python
14+
import torch
15+
from torch.export import export
16+
from executorch.exir import to_edge
17+
18+
# Define a simple Feedforward Neural Network
19+
class SimpleNN(torch.nn.Module):
20+
def __init__(self, input_size, hidden_size, output_size):
21+
super(SimpleNN, self).__init__()
22+
self.fc1 = torch.nn.Linear(input_size, hidden_size)
23+
self.relu = torch.nn.ReLU()
24+
self.fc2 = torch.nn.Linear(hidden_size, output_size)
25+
26+
def forward(self, x):
27+
out = self.fc1(x)
28+
out = self.relu(out)
29+
out = self.fc2(out)
30+
return out
31+
32+
# Create the model instance
33+
input_size = 10 # example input features size
34+
hidden_size = 5 # hidden layer size
35+
output_size = 2 # number of output classes
36+
37+
model = SimpleNN(input_size, hidden_size, output_size)
38+
39+
# Example input tensor (batch size 1, input size 10)
40+
x = torch.randn(1, input_size)
41+
42+
# torch.export: Defines the program with the ATen operator set for SimpleNN.
43+
aten_dialect = export(model, (x,))
44+
45+
# to_edge: Make optimizations for edge devices. This ensures the model runs efficiently on constrained hardware.
46+
edge_program = to_edge(aten_dialect)
47+
48+
# to_executorch: Convert the graph to an ExecuTorch program
49+
executorch_program = edge_program.to_executorch()
50+
51+
# Save the compiled .pte program
52+
with open("simple_nn.pte", "wb") as file:
53+
file.write(executorch_program.buffer)
54+
55+
print("Model successfully exported to simple_nn.pte")
56+
```
57+
58+
Run it from your terminal:
59+
60+
```console
61+
python3 simple_nn.py
62+
```
63+
64+
If everything runs successfully, the output will be:
65+
```bash { output_lines = "1" }
66+
Model successfully exported to simple_nn.pte
67+
```
68+
Finally, the model is saved as a .pte file, which is the format used by ExecuTorch for deploying models to the edge.
69+
70+
Now, we will run the ExecuTorch version, first run:
71+
72+
```console
73+
# Clean and configure the build system
74+
rm -rf cmake-out && mkdir cmake-out && cd cmake-out && cmake ..
75+
76+
# Build the executor_runner target
77+
cmake --build cmake-out --target executor_runner -j9
78+
```
79+
80+
You should see an output similar to:
81+
```bash { output_lines = "1" }
82+
[100%] Built target executor_runner
83+
```
84+
85+
Now, run the executor_runner with the Model:
86+
```console
87+
./cmake-out/executor_runner --model_path simple_nn.pte
88+
```
89+
90+
Expected Output: Since the model is a simple feedforward model, you can expect a tensor of shape [1, 2]
91+
92+
```bash { output_lines = "1-3" }
93+
Input tensor shape: [1, 10]
94+
Output tensor shape: [1, 2]
95+
Inference output: tensor([[0.5432, -0.3145]]) #will vary due to random initialization
96+
```
97+
98+
If the model execution completes successfully, you’ll see confirmation messages similar to those above, indicating successful loading, inference, and output tensor shapes.
99+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
# User change
3+
title: "Environment Setup on Host Machine"
4+
5+
weight: 6 # 1 is first, 2 is second, etc.
6+
7+
# Do not modify these elements
8+
layout: "learningpathall"
9+
---
10+
## Before you begin
11+
12+
These instructions have been tested on:
13+
- A GCP Arm-based Tau T2A Virtual Machine instance Running Ubuntu 22.04 LTS.
14+
- Host machine with Ubuntu 24.04 on x86_64 architecture.
15+
- Windows Subsystem for Linux (WSL): Windows x86_64
16+
17+
The host machine is where you will perform most of your development work, especially cross-compiling code for the target Arm devices.
18+
19+
- The Ubuntu version should be `20.04 or higher`.
20+
- If you do not have the board, the `x86_64` architecture must be used because the Corstone-300 FVP is not currently available for the Arm architecture.
21+
- Though Executorch supports Windows via WSL, it is limited in resource.
22+
23+
24+
## Install Executorch
25+
26+
1. Follow the [Setting Up ExecuTorch guide](https://pytorch.org/executorch/stable/getting-started-setup.html ) to install it.
27+
28+
2. Activate the `executorch` virtual environment from the installation guide to ensure it is ready for use:
29+
30+
```console
31+
conda activate executorch
32+
```
33+
34+
## Install PyTorch
35+
The latest version needs Python 3.8 or later
36+
37+
```console
38+
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
39+
40+
```
41+
42+
## Install Edge Impulse CLI
43+
1. Create an [Edge Impulse Account](https://studio.edgeimpulse.com/signup) if you do not have one
44+
45+
2. Install the CLI tools in your terminal
46+
47+
Ensure you have Nodejs installed
48+
49+
```console
50+
node -v
51+
```
52+
Install the Edge Impulse CLI
53+
```console
54+
npm install -g edge-impulse-cli
55+
```
56+
3. Install Edge Impulse Screen
57+
```console
58+
sudo apt install screen
59+
```
60+
61+
## Next Steps
62+
1. If you don't have access to the physical board: Go to [Environment Setup Corstone-300 FVP](/learning-paths/microcontrollers/introduction-to-tinyml-on-arm/env-setup-6-fvp/)
63+
2. If you have access to the board: Go to [Setup on Grove - Vision AI Module V2](/learning-paths/microcontrollers/introduction-to-tinyml-on-arm/setup-7-grove/)

0 commit comments

Comments
 (0)