Skip to content

Commit 262c08a

Browse files
committed
Divided Environment Setup into 3 main parts
1 parent 5c8da85 commit 262c08a

File tree

5 files changed

+190
-161
lines changed

5 files changed

+190
-161
lines changed

content/learning-paths/microcontrollers/introduction-to-tinyml-on-arm/_index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ prerequisites:
1919
- Basic knowledge of machine learning concepts.
2020
- Understanding of IoT and embedded systems (helpful but not required).
2121
- 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, preferably Cortex-M boards but you can use Cortex-A7 boards as well.
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+
2324

2425
author_primary: Dominica Abena O. Amanfo
2526

@@ -36,7 +37,6 @@ operatingsystems:
3637
tools_software_languages:
3738
- Corstone 300 FVP
3839
- Grove - Vision AI Module V2
39-
- Arm Virtual Hardware
4040
- Python
4141
- PyTorch v2.x
4242
- Executorch
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: 11 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
# User change
3-
title: "Environment Setup"
3+
title: "Environment Setup on Host Machine"
44

55
weight: 6 # 1 is first, 2 is second, etc.
66

@@ -16,39 +16,25 @@ These instructions have been tested on:
1616

1717
The host machine is where you will perform most of your development work, especially cross-compiling code for the target Arm devices.
1818

19-
You can use your own Linux host machine or use [Arm Virtual Hardware (AVH)](https://www.arm.com/products/development-tools/simulation/virtual-hardware) for this Learning Path.
20-
21-
The Ubuntu version should be `20.04 or higher`. The `x86_64` architecture must be used because the Corstone-300 FVP is not currently available for the Arm architecture. You will need a Linux desktop to run the FVP because it opens graphical windows for input and output from the software applications. Also, though Executorch supports Windows via WSL, it is limited in resource.
22-
23-
If you want to use Arm Virtual Hardware the [Arm Virtual Hardware install guide](/install-guides/avh#corstone) provides setup instructions.
24-
25-
## Setup on Host Machine
26-
1. Setup if you don't have access to the physical board: We would use the Corstone-300 FVP, it is pre-configured.
27-
2. Setup if you have access to the board: Skip to **"Compilers"** Section
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+
- Also, though Executorch supports Windows via WSL, it is limited in resource.
2822

2923

3024
### Corstone-300 FVP Setup for ExecuTorch
31-
For Arm Virtual Hardware users, the Corstone-300 FVP is pre-installed.
3225

3326
To install and set up the Corstone-300 FVP and ExecuTorch on your machine, refer to [Building and Running ExecuTorch with ARM Ethos-U Backend](https://pytorch.org/executorch/stable/executorch-arm-delegate-tutorial.html). Follow this tutorial till the **"Install the TOSA reference model"** Section. It should be the last thing you do from this tutorial.
3427

35-
Since you already have the compiler installed from the above tutorial, skip to **"Install PyTorch"**.
3628

37-
### Compilers
3829

39-
The examples can be built with [Arm Compiler for Embedded](https://developer.arm.com/Tools%20and%20Software/Arm%20Compiler%20for%20Embedded) or [Arm GNU Toolchain](https://developer.arm.com/Tools%20and%20Software/GNU%20Toolchain).
30+
## Install Executorch
4031

41-
Both compilers are pre-installed in Arm Virtual Hardware.
32+
1. Follow the [Setting Up ExecuTorch guide](https://pytorch.org/executorch/stable/getting-started-setup.html ) to install it.
4233

43-
Alternatively, if you use Arch Linux or its derivatives, you can use Pacman to install GCC.
34+
2. Activate the `executorch` virtual environment from the installation guide to ensure it is ready for use:
4435

45-
Use the install guides to install the compilers on your computer:
46-
- [Arm Compiler for Embedded](/install-guides/armclang/)
47-
- [Arm GNU Toolchain](/install-guides/gcc/arm-gnu)
48-
- Using Pacman:
49-
5036
```console
51-
pacman -S aarch64-linux-gnu-gcc
37+
conda activate executorch
5238
```
5339

5440
## Install PyTorch
@@ -59,16 +45,6 @@ pip3 install torch torchvision torchaudio --index-url https://download.pytorch.o
5945

6046
```
6147

62-
## Install Executorch
63-
64-
1. Follow the [Setting Up ExecuTorch guide](https://pytorch.org/executorch/stable/getting-started-setup.html ) to install it.
65-
66-
2. Activate the `executorch` virtual environment from the installation guide to ensure it is ready for use:
67-
68-
```console
69-
conda activate executorch
70-
```
71-
7248
## Install Edge Impulse CLI
7349
1. Create an [Edge Impulse Account](https://studio.edgeimpulse.com/signup) if you do not have one
7450

@@ -88,130 +64,6 @@ npm install -g edge-impulse-cli
8864
sudo apt install screen
8965
```
9066

91-
4. Download and extract the latest Edge Impulse firmware
92-
Grove Vision V2 [Edge impulse Firmware](https://cdn.edgeimpulse.com/firmware/seeed-grove-vision-ai-module-v2.zip).
93-
94-
95-
## Setup on Grove - Vision AI Module V2
96-
**Kindly Note:** Only follow this part of the tutorial if you have the board.
97-
98-
Due to its constrained environment, we'll focus on lightweight, optimized tools and models (which will be introduced in the next learning path).
99-
100-
![Hardware Overview #center](Overview.png)
101-
102-
Hardware overview : [Image credits](https://wiki.seeedstudio.com/grove_vision_ai_v2/).
103-
104-
1. Connect the Grove - Vision AI Module V2 to your computer using the USB-C cable.
105-
106-
![Board connection](Connect.png)
107-
108-
109-
2. In the extracted Edge Impulse firmware, locate and run the installation scripts to flash your device.
110-
111-
```console
112-
./flash_linux.sh
113-
```
114-
115-
3. Configure Edge Impulse for the board
116-
in your terminal, run:
117-
118-
```console
119-
edge-impulse-daemon
120-
```
121-
Follow the prompts to log in.
122-
123-
4. Verify your board is connected
124-
125-
If successful, you should see your Grove - Vision AI Module V2 under 'Devices' in Edge Impulse.
126-
127-
128-
## Build a Simple PyTorch Model
129-
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:
130-
131-
```python
132-
import torch
133-
from torch.export import export
134-
from executorch.exir import to_edge
135-
136-
# Define a simple Feedforward Neural Network
137-
class SimpleNN(torch.nn.Module):
138-
def __init__(self, input_size, hidden_size, output_size):
139-
super(SimpleNN, self).__init__()
140-
self.fc1 = torch.nn.Linear(input_size, hidden_size)
141-
self.relu = torch.nn.ReLU()
142-
self.fc2 = torch.nn.Linear(hidden_size, output_size)
143-
144-
def forward(self, x):
145-
out = self.fc1(x)
146-
out = self.relu(out)
147-
out = self.fc2(out)
148-
return out
149-
150-
# Create the model instance
151-
input_size = 10 # example input features size
152-
hidden_size = 5 # hidden layer size
153-
output_size = 2 # number of output classes
154-
155-
model = SimpleNN(input_size, hidden_size, output_size)
156-
157-
# Example input tensor (batch size 1, input size 10)
158-
x = torch.randn(1, input_size)
159-
160-
# torch.export: Defines the program with the ATen operator set for SimpleNN.
161-
aten_dialect = export(model, (x,))
162-
163-
# to_edge: Make optimizations for edge devices. This ensures the model runs efficiently on constrained hardware.
164-
edge_program = to_edge(aten_dialect)
165-
166-
# to_executorch: Convert the graph to an ExecuTorch program
167-
executorch_program = edge_program.to_executorch()
168-
169-
# Save the compiled .pte program
170-
with open("simple_nn.pte", "wb") as file:
171-
file.write(executorch_program.buffer)
172-
173-
print("Model successfully exported to simple_nn.pte")
174-
```
175-
176-
Run it from your terminal:
177-
178-
```console
179-
python3 simple_nn.py
180-
```
181-
182-
If everything runs successfully, the output will be:
183-
```bash { output_lines = "1" }
184-
Model successfully exported to simple_nn.pte
185-
```
186-
Finally, the model is saved as a .pte file, which is the format used by ExecuTorch for deploying models to the edge.
187-
188-
Now, we will run the ExecuTorch version, first run:
189-
190-
```console
191-
# Clean and configure the build system
192-
rm -rf cmake-out && mkdir cmake-out && cd cmake-out && cmake ..
193-
194-
# Build the executor_runner target
195-
cmake --build cmake-out --target executor_runner -j9
196-
```
197-
198-
You should see an output similar to:
199-
```bash { output_lines = "1" }
200-
[100%] Built target executor_runner
201-
```
202-
203-
Now, run the executor_runner with the Model:
204-
```console
205-
./cmake-out/executor_runner --model_path simple_nn.pte
206-
```
207-
208-
Expected Output: Since the model is a simple feedforward model, you can expect a tensor of shape [1, 2]
209-
210-
```bash { output_lines = "1-3" }
211-
Input tensor shape: [1, 10]
212-
Output tensor shape: [1, 2]
213-
Inference output: tensor([[0.5432, -0.3145]]) #will vary due to random initialization
214-
```
215-
216-
If the model execution completes successfully, you’ll see confirmation messages similar to those above, indicating successful loading, inference, and output tensor shapes.
217-
67+
## Next Steps
68+
1. If you don't have access to the physical board: Skip to [Environment Setup Corstone-300 FVP](/learning-paths/microcontrollers/introduction-to-tinyml-on-arm/setup-6-FVP.md)
69+
2. If you have access to the board: Skip to [Setup on Grove - Vision AI Module V2](/learning-paths/microcontrollers/introduction-to-tinyml-on-arm/setup-6-Grove.md)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
# User change
3+
title: "Environment Setup Corstone-300 FVP"
4+
5+
weight: 7 # 1 is first, 2 is second, etc.
6+
7+
# Do not modify these elements
8+
layout: "learningpathall"
9+
---
10+
11+
### Corstone-300 FVP Setup for ExecuTorch
12+
13+
To install and set up the Corstone-300 FVP on your machine, refer to [Building and Running ExecuTorch with ARM Ethos-U Backend](https://pytorch.org/executorch/stable/executorch-arm-delegate-tutorial.html). Follow this tutorial till the **"Install the TOSA reference model"** Section. It should be the last thing you do from this tutorial.
14+
15+
16+
## Next Steps
17+
1. Go to [Build a Simple PyTorch Model](/learning-paths/microcontrollers/introduction-to-tinyml-on-arm/build-model-8.md) to test your environment setup.

0 commit comments

Comments
 (0)