Skip to content

Commit 91d69be

Browse files
committed
second Learning Path: First commit
1 parent 8c4a6ef commit 91d69be

File tree

5 files changed

+263
-0
lines changed

5 files changed

+263
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: Deploying DistilBERT on Arm - Training and Inference with PyTorch and ExecuTorch
3+
4+
minutes_to_complete: 120
5+
6+
who_is_this_for: This topic is for machine learning engineers, embedded AI developers, and researchers interested in deploying TinyML models for NLP on Arm-based edge devices using PyTorch and ExecuTorch.
7+
8+
learning_objectives:
9+
- Fine-tune a DistilBERT model for sentiment analysis using PyTorch.
10+
- Optimize and convert the model using ExecuTorch for Arm-based edge devices.
11+
- Deploy and run inference on the Corstone-320 FVP and Raspberry Pi 5.
12+
13+
14+
prerequisites:
15+
- Basic knowledge of machine learning concepts.
16+
- It is advised to complete The Learning Path [Introduction to TinyML on Arm using PyTorch and ExecuTorch](/learning-paths/embedded-and-microcontrollers/introduction-to-tinyml-on-arm) before starting this learning path.
17+
- Familiarity with Python and PyTorch.
18+
- A Linux host machine or VM running Ubuntu 22.04 or higher.
19+
- (Optional) Raspberry Pi 5 or an Arm license to run the Corstone-320 Fixed Virtual Platform (FVP), for hands-on deployment.
20+
21+
22+
author: Dominica Abena O. Amanfo
23+
24+
### Tags
25+
skilllevels: Intermediate
26+
subjects: ML
27+
armips:
28+
- Cortex-A
29+
tools_software_languages:
30+
- tinyML
31+
- Transformers
32+
- PyTorch
33+
- ExecuTorch
34+
- Raspberry Pi
35+
36+
operatingsystems:
37+
- Linux
38+
- Raspberry Pi OS
39+
40+
41+
further_reading:
42+
- resource:
43+
title: Run Llama 3 on a Raspberry Pi 5 using ExecuTorch
44+
link: /learning-paths/embedded-and-microcontrollers/rpi-llama3
45+
type: website
46+
- resource:
47+
title: ExecuTorch Examples
48+
link: https://github.com/pytorch/executorch/blob/main/examples/README.md
49+
type: website
50+
51+
52+
53+
### FIXED, DO NOT MODIFY
54+
# ================================================================================
55+
weight: 1 # _index.md always has weight of 1 to order correctly
56+
layout: "learningpathall" # All files under learning paths have this same wrapper
57+
learning_path_main_page: "yes" # This should be surfaced when looking for related content. Only set for _index.md of learning path content.
58+
---
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
# ================================================================================
3+
# FIXED, DO NOT MODIFY THIS FILE
4+
# ================================================================================
5+
weight: 21 # Set to always be larger than the content in this path to be at the end of the navigation.
6+
title: "Next Steps" # Always the same, html page title.
7+
layout: "learningpathall" # All files under learning paths have this same wrapper for Hugo processing.
8+
---
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: Environment Setup
3+
weight: 2
4+
5+
### FIXED, DO NOT MODIFY
6+
layout: learningpathall
7+
---
8+
9+
## Overview
10+
#TODO: Add intro on Distil
11+
12+
In this course, you will learn how to train and run inference using DistilBERT. You'll deploy the model on the Arm Corstone-320 FVP and optionally on a Raspberry Pi 5 for sentiment analysis.
13+
14+
## Environment Setup
15+
Setup your development environment for TinyML by following the first 3 chapters of the [Introduction to TinyML on Arm using PyTorch and ExecuTorch](/learning-paths/embedded-and-microcontrollers/introduction-to-tinyml-on-arm) Learning Path (LP).
16+
17+
18+
If you just followed the LP above, you should already have your virtual environment activated. If not, activate it using:
19+
20+
```console
21+
source $HOME/executorch-venv/bin/activate
22+
```
23+
The prompt of your terminal now has `(executorch)` as a prefix to indicate the virtual environment is active.
24+
25+
Run the commands below to install the dependencies.
26+
27+
```bash
28+
pip install transformers datasets torch
29+
```
30+
You are now ready to fine-tune the model
31+
32+
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---
2+
title: Fine-Tune DistilBERT
3+
weight: 3
4+
5+
### FIXED, DO NOT MODIFY
6+
layout: learningpathall
7+
---
8+
9+
## Fine-Tune the Model
10+
11+
Using a file editor of your choice, create a file named distilbert-sentiment-analysis.py with the code shown below:
12+
13+
```python
14+
from transformers import DistilBERTTokenizerFast, DistilBertForSequenceClassification, Trainer, TrainingArguments
15+
from datasets import load_dataset
16+
17+
# Load dataset and tokenizer
18+
dataset = load_dataset("imdb")
19+
tokenizer = DistilBERTTokenizerFast.from_pretrained("distilbert-base-uncased")
20+
21+
def tokenize(batch):
22+
return tokenizer(batch["text"], padding=True, truncation=True)
23+
24+
# Tokenize data
25+
dataset = dataset.map(tokenize, batched=True)
26+
27+
# Load pretrained model
28+
model = DistilBertForSequenceClassification.from_pretrained("distilbert-base-uncased")
29+
30+
# Training arguments
31+
training_args = TrainingArguments(
32+
output_dir="./results",
33+
evaluation_strategy="epoch",
34+
per_device_train_batch_size=16,
35+
per_device_eval_batch_size=16,
36+
num_train_epochs=2,
37+
)
38+
39+
# Trainer
40+
trainer = Trainer(
41+
model=model,
42+
args=training_args,
43+
train_dataset=dataset["train"].shuffle().select(range(2000)),
44+
eval_dataset=dataset["test"].shuffle().select(range(500))
45+
)
46+
47+
trainer.train()
48+
49+
# Save model
50+
model.save_pretrained("distilbert_sentiment")
51+
```
52+
53+
#TODO: Talk about what the example does.
54+
55+
56+
Run the model using:
57+
```bash
58+
python distilbert-sentiment-analysis.py
59+
```
60+
61+
The output should look like:
62+
```bash
63+
#TODO add output
64+
```
65+
You are now ready to optimize and convert the model using ExecuTorch.
66+
67+
68+
## Compile and build the executable
69+
70+
Start by setting some environment variables that are used by ExecuTorch.
71+
72+
```bash
73+
export ET_HOME=$HOME/executorch
74+
export executorch_DIR=$ET_HOME/build
75+
```
76+
77+
Then, generate a `.pte` file using the Arm examples. The Ahead-of-Time (AoT) Arm compiler will enable optimizations for devices like the Raspberry Pi and the Corstone-320 FVP. Run it from the ExecuTorch root directory.
78+
79+
Navigate to the root directory using:
80+
81+
```bash
82+
cd ../../../
83+
```
84+
You are now in $HOME/executorch and ready to create the model file for ExecuTorch.
85+
86+
87+
```bash
88+
cd $ET_HOME
89+
python -m examples.arm.aot_arm_compiler --model_name=examples/arm/distilbert-sentiment-analysis.py \
90+
--delegate --quantize --target=ethos-u85-256 \
91+
--so_library=cmake-out-aot-lib/kernels/quantized/libquantized_ops_aot_lib.so \
92+
--system_config=Ethos_U85_SYS_DRAM_Mid --memory_mode=Sram_Only
93+
```
94+
95+
From the Arm Examples directory, you build an embedded Arm runner with the `.pte` included. This allows you to get the most performance out of your model, and ensures compatibility with the CPU kernels on the FVP. Finally, generate the executable `arm_executor_runner`.
96+
97+
```bash
98+
cd $HOME/executorch/examples/arm/executor_runner
99+
100+
101+
cmake -DCMAKE_BUILD_TYPE=Release \
102+
-DCMAKE_TOOLCHAIN_FILE=$ET_HOME/examples/arm/ethos-u-setup/arm-none-eabi-gcc.cmake \
103+
-DTARGET_CPU=cortex-m85 \
104+
-DET_DIR_PATH:PATH=$ET_HOME/ \
105+
-DET_BUILD_DIR_PATH:PATH=$ET_HOME/cmake-out \
106+
-DET_PTE_FILE_PATH:PATH=$ET_HOME/simple_nn_arm_delegate_ethos-u85-256.pte \
107+
-DETHOS_SDK_PATH:PATH=$ET_HOME/examples/arm/ethos-u-scratch/ethos-u \
108+
-DETHOSU_TARGET_NPU_CONFIG=ethos-u85-256 \
109+
-DPYTHON_EXECUTABLE=$HOME/executorch-venv/bin/python3 \
110+
-DSYSTEM_CONFIG=Ethos_U85_SYS_DRAM_Mid \
111+
-B $ET_HOME/examples/arm/executor_runner/cmake-out
112+
113+
cmake --build $ET_HOME/examples/arm/executor_runner/cmake-out --parallel -- arm_executor_runner
114+
115+
```
116+
117+
Now, you can run the model on the Corstone-320 FVP.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
title: Deploy the model on Corstone-320 FVP
3+
weight: 4
4+
5+
### FIXED, DO NOT MODIFY
6+
layout: learningpathall
7+
---
8+
9+
Now run the model on the Corstone-320 with the following command:
10+
11+
```bash
12+
FVP_Corstone_SSE-320 \
13+
-C mps4_board.subsystem.ethosu.num_macs=256 \
14+
-C mps4_board.visualisation.disable-visualisation=1 \
15+
-C vis_hdlcd.disable_visualisation=1 \
16+
-C mps4_board.telnetterminal0.start_telnet=0 \
17+
-C mps4_board.uart0.out_file='-' \
18+
-C mps4_board.uart0.shutdown_on_eot=1 \
19+
-a "$ET_HOME/examples/arm/executor_runner/cmake-out/arm_executor_runner"
20+
```
21+
22+
{{% notice Note %}}
23+
24+
The argument `mps4_board.visualisation.disable-visualisation=1` disables the FVP GUI. This can speed up launch time for the FVP.
25+
26+
{{% /notice %}}
27+
28+
29+
#todo: VERIFY
30+
31+
Observe that the FVP loads the model file.
32+
```output
33+
telnetterminal0: Listening for serial connection on port 5000
34+
telnetterminal1: Listening for serial connection on port 5001
35+
telnetterminal2: Listening for serial connection on port 5002
36+
telnetterminal5: Listening for serial connection on port 5003
37+
I [executorch:arm_executor_runner.cpp:412] Model in 0x70000000 $
38+
I [executorch:arm_executor_runner.cpp:414] Model PTE file loaded. Size: 3360 bytes.
39+
```
40+
41+
You've now ......
42+
43+
44+
45+
46+
47+
IGNORE anything BELOW:
48+

0 commit comments

Comments
 (0)