Skip to content

Commit 02682bc

Browse files
authored
Arm backend: Simple example application for image classification (#16297)
The goal is to create a minimalistic application that can explain the ExecuTorch APIs to a person who is not familiar with ExecuTorch. The key is to make something simple and easy to understand.
1 parent db895e9 commit 02682bc

File tree

4 files changed

+492
-0
lines changed

4 files changed

+492
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# Copyright 2025 Arm Limited and/or its affiliates.
2+
#
3+
# This source code is licensed under the BSD-style license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
cmake_minimum_required(VERSION 3.20)
7+
8+
project(image_classification_minimal_application)
9+
10+
# Example ExecuTorch demo for bare metal Cortex-M based systems
11+
set(ET_DIR_PATH
12+
"${CMAKE_CURRENT_SOURCE_DIR}/../../../.."
13+
CACHE PATH "Path to ExecuTorch dir"
14+
)
15+
16+
set(ET_BUILD_DIR_PATH
17+
"${ET_DIR_PATH}/cmake-out-arm"
18+
CACHE PATH "Path to ExecuTorch build/install dir"
19+
)
20+
set(ET_INCLUDE_PATH
21+
"${ET_DIR_PATH}/.."
22+
CACHE PATH "Path to ExecuTorch headers"
23+
)
24+
25+
set(ET_PTE_FILE_PATH
26+
""
27+
CACHE PATH "Path to ExecuTorch model pte"
28+
)
29+
30+
set(IMAGE_PATH
31+
""
32+
CACHE
33+
PATH
34+
"Path to an RGB image to use for the application(e.g. a jpg image of a cat or a dog)"
35+
)
36+
37+
set(ETHOS_SDK_PATH
38+
"${ET_DIR_PATH}/examples/arm/ethos-u-scratch/ethos-u"
39+
CACHE PATH "Path to Ethos-U bare metal driver/env"
40+
)
41+
42+
set(PYTHON_EXECUTABLE
43+
"python"
44+
CACHE PATH "Define to override python executable used"
45+
)
46+
47+
if(NOT EXISTS "${IMAGE_PATH}")
48+
message(
49+
FATAL_ERROR
50+
"Image not provided. Please provide path to an image for the image classification application and retry."
51+
)
52+
endif()
53+
if(NOT EXISTS "${ET_PTE_FILE_PATH}")
54+
message(
55+
FATAL_ERROR
56+
"pte file not provided. Please provide pte file for the application and retry."
57+
)
58+
endif()
59+
if(NOT EXISTS "${ETHOS_SDK_PATH}")
60+
message(
61+
FATAL_ERROR
62+
"The ${ETHOS_SDK_PATH} directory does not exist. Please run examples/arm/setup.sh script and retry."
63+
)
64+
endif()
65+
66+
find_package(
67+
executorch REQUIRED HINTS "${ET_BUILD_DIR_PATH}/lib/cmake/ExecuTorch"
68+
)
69+
70+
# The core_platform project defines the corstone-320 target and contains the
71+
# start-up code for the Cortex-M
72+
add_subdirectory(${ETHOS_SDK_PATH}/core_platform/targets/corstone-320 target)
73+
74+
add_executable(img_class_example main.cpp)
75+
76+
target_sources(
77+
img_class_example
78+
PRIVATE main.cpp ../executor_runner/arm_memory_allocator.cpp
79+
../executor_runner/arm_perf_monitor.cpp
80+
)
81+
82+
target_link_libraries(
83+
img_class_example
84+
PUBLIC executorch
85+
ethosu_target_init
86+
extension_runner_util
87+
quantized_ops_lib
88+
portable_kernels
89+
cortex_m_kernels
90+
cortex_m_ops_lib
91+
)
92+
93+
# We need to include whole archive for the EthosUBackend
94+
target_link_libraries(
95+
img_class_example PUBLIC "-Wl,--whole-archive" executorch_delegate_ethos_u
96+
"-Wl,--no-whole-archive"
97+
)
98+
99+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
100+
set(LINK_FILE_EXT ld)
101+
set(LINK_FILE_OPTION "-T")
102+
set(COMPILER_PREPROCESSOR_OPTIONS -E -x c -P)
103+
endif()
104+
105+
set(LINK_FILE_OUT_BASE "platform_linker_script")
106+
set(LINK_FILE_IN "${CMAKE_SOURCE_DIR}/../../executor_runner/Corstone-320.ld")
107+
set(LINK_FILE_OUT
108+
${CMAKE_CURRENT_BINARY_DIR}/${LINK_FILE_OUT_BASE}.${LINK_FILE_EXT}
109+
)
110+
# The ETHOSU_ARENA symbol is defined in the Corstone-320 linker script and it
111+
# controls the placement of the intermediate tensors in the application memory
112+
# map. Here, because in the compile spec in the AoT flow, we generate pte for
113+
# Shared_Sram, in the application, we set ETHOSU_ARENA to 0 so that the
114+
# intermediate tensors are placed in the SRAM. If you generate a pte for a
115+
# different memory mode, you need to change the placement in the linker script.
116+
# Read
117+
# https://docs.pytorch.org/executorch/stable/backends-arm-ethos-u.html#ethos-u-memory-modes
118+
# for more information.
119+
set(ETHOSU_ARENA "0")
120+
# Generate linker script - we have a few if/else statements in
121+
# Corstone-320.ld/Corstone-300.ld that are compiled into a final linker script.
122+
execute_process(
123+
COMMAND ${CMAKE_C_COMPILER} ${COMPILER_PREPROCESSOR_OPTIONS} -DETHOSU_ARENA=0
124+
-o ${LINK_FILE_OUT} ${LINK_FILE_IN}
125+
)
126+
target_link_options(img_class_example PRIVATE "-T" "${LINK_FILE_OUT}")
127+
128+
# Run the pte_to_header.py script to convert the .pte file to an array in a .h
129+
# file
130+
execute_process(
131+
COMMAND
132+
${PYTHON_EXECUTABLE}
133+
${CMAKE_SOURCE_DIR}/../../executor_runner/pte_to_header.py --pte
134+
${ET_PTE_FILE_PATH} --outdir ${CMAKE_CURRENT_BINARY_DIR}
135+
)
136+
137+
# Convert an RGB image to an array in a .h file
138+
execute_process(
139+
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/rgb_to_array.py --image
140+
${IMAGE_PATH} --output ${CMAKE_CURRENT_BINARY_DIR}/image.h
141+
)
142+
143+
target_include_directories(
144+
img_class_example
145+
PRIVATE ${ET_INCLUDE_PATH} ${ET_DIR_PATH}/runtime/core/portable_type/c10
146+
${CMAKE_SOURCE_DIR}/../../executor_runner ${CMAKE_CURRENT_BINARY_DIR}
147+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
1. Make sure you have setup the Ethos-U ExecuTorch dependencies by running the examples/arm/setup.sh script. See the [readme](../../README.md) for instructions on how to do the setup.
2+
3+
2. Build executorch from the `examples/arm` folder, cross compiled for a Cortex-M device.
4+
```$ cmake --preset arm-baremetal \
5+
-DCMAKE_BUILD_TYPE=Release \
6+
-B../../cmake-out-arm ../..
7+
cmake --build ../../cmake-out-arm --target install -j$(nproc) ````
8+
9+
3. Set up the build system. You need to provide path to the DEiT-Tiny pte generated in the
10+
`examples/arm/image_classification_example/export` folder. You also need to provide an image of a dog, you can download such
11+
image from the [HuggingFace Oxford iiit pet dataset](https://huggingface.co/datasets/timm/oxford-iiit-pet).
12+
```
13+
$ cmake -DCMAKE_TOOLCHAIN_FILE=$(pwd)/ethos-u-setup/arm-none-eabi-gcc.cmake -DET_PTE_FILE_PATH=<path to DEiT-Tiny compiled for Ethos-U85-256> -DIMAGE_PATH=<path to a JPG image> -Bsimple_app_deit_tiny image_classification_example/runtime
14+
```
15+
16+
4. Compile the application.
17+
```
18+
$ cmake --build simple_app_deit_tiny -j$(nproc) -- img_class_example
19+
```
20+
21+
5. Deploy the application on the Corstone-320 Fixed Virtual Platform. Assuming you have the Corstone-320 installed on your path, do the following command to deploy the application.
22+
```
23+
$ FVP_Corstone_SSE-320 -C mps4_board.subsystem.ethosu.num_macs=256 -C mps4_board.visualisation.disable-visualisation=1 -C vis_hdlcd.disable_visualisation=1 -C mps4_board.telnetterminal0.start_telnet=0 -C mps4_board.uart0.out_file='-' -C mps4_board.uart0.shutdown_on_eot=1 -a simple_app_deit_tiny/img_class_example -C mps4_board.subsystem.ethosu.extra_args="--fast"
24+
```

0 commit comments

Comments
 (0)