Skip to content

Commit 391793b

Browse files
Arm backend: Generate kernel registration lib from .pte (#13220)
- Moves the arm_portable_op_lib target into the executor_runner CMakeLists.txt - Use the OPS_FROM_MODEL arg to generate the arm_portable_op_lib if possible - Adds possibility of setting EXECUTORCH_ENABLE_DTYPE_SELECTIVE_BUILD when building the executor_runner. - Makes cortex_m targets findeable using find_package to avoid rebuilding. + run cmake-format Signed-off-by: Adrian Lundell <[email protected]>
1 parent 29553c7 commit 391793b

File tree

10 files changed

+98
-209
lines changed

10 files changed

+98
-209
lines changed

backends/arm/scripts/build_executor_runner.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ output_folder_set=false
2525
output_folder="."
2626
et_build_root="${et_root_dir}/arm_test"
2727
ethosu_tools_dir=${et_root_dir}/examples/arm/ethos-u-scratch
28+
select_ops_list=""
2829

2930
build_bundleio_flags=" -DET_BUNDLE_IO=OFF "
3031
build_with_etdump_flags=" -DEXECUTORCH_ENABLE_EVENT_TRACER=OFF "
@@ -47,7 +48,10 @@ help() {
4748
echo " --et_build_root=<FOLDER> Build output root folder to use, defaults to ${et_build_root}"
4849
echo " --ethosu_tools_dir=<FOLDER> Path to your Ethos-U tools dir if you not using default: ${ethosu_tools_dir}"
4950
echo " --toolchain=<TOOLCHAIN> Toolchain can be specified (e.g. bare metal as arm-none-eabi-gcc or zephyr as arm-zephyr-eabi-gcc"
50-
exit 0
51+
echo " --select_ops_list=<OPS> Comma separated list of portable (non delagated) kernels to include Default: ${select_ops_list}"
52+
echo " NOTE: This is used when select_ops_model is not possible to use, e.g. for semihosting or bundleio."
53+
echo " See https://docs.pytorch.org/executorch/stable/kernel-library-selective-build.html for more information."
54+
exit 0
5155
}
5256

5357
for arg in "$@"; do
@@ -65,6 +69,7 @@ for arg in "$@"; do
6569
--et_build_root=*) et_build_root="${arg#*=}";;
6670
--ethosu_tools_dir=*) ethosu_tools_dir="${arg#*=}";;
6771
--toolchain=*) toolchain="${arg#*=}";;
72+
--select_ops_list=*) select_ops_list="${arg#*=}";;
6873
*)
6974
;;
7075
esac
@@ -157,6 +162,7 @@ cmake \
157162
-DPYTHON_EXECUTABLE=$(which python3) \
158163
-DSYSTEM_CONFIG=${system_config} \
159164
-DMEMORY_MODE=${memory_mode} \
165+
-DEXECUTORCH_SELECT_OPS_LIST="${select_ops_list}" \
160166
${extra_build_flags} \
161167
-B ${output_folder}/cmake-out
162168

backends/arm/scripts/build_portable_kernels.sh

Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -4,92 +4,4 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7-
# Optional parameter:
8-
# --build_type= "Release" | "Debug" | "RelWithDebInfo"
9-
# --etdump build with devtools-etdump support
10-
11-
set -eu
12-
13-
script_dir=$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd)
14-
et_root_dir=$(cd ${script_dir}/../../.. && pwd)
15-
et_root_dir=$(realpath ${et_root_dir})
16-
toolchain=arm-none-eabi-gcc
17-
setup_path_script=${et_root_dir}/examples/arm/ethos-u-scratch/setup_path.sh
18-
_setup_msg="please refer to ${et_root_dir}/examples/arm/setup.sh to properly install necessary tools."
19-
20-
21-
et_build_root="${et_root_dir}/arm_test"
22-
build_type="Release"
23-
portable_kernels="aten::_softmax.out"
24-
25-
help() {
26-
echo "Usage: $(basename $0) [options]"
27-
echo "Options:"
28-
echo " --et_build_root=<FOLDER> Build output root folder to use, defaults to ${et_build_root}"
29-
echo " --build_type=<TYPE> Build with Release, Debug or RelWithDebInfo, default is ${build_type}"
30-
echo " --portable_kernels=<OPS> Comma separated list of portable (non delagated) kernels to include Default: ${portable_kernels}"
31-
echo " --toolchain=<TOOLCHAIN> Toolchain can be specified (e.g. bare metal as arm-none-eabi-gcc or zephyr as arm-zephyr-eabi-gcc"
32-
exit 0
33-
}
34-
35-
for arg in "$@"; do
36-
case $arg in
37-
-h|--help) help ;;
38-
--et_build_root=*) et_build_root="${arg#*=}";;
39-
--build_type=*) build_type="${arg#*=}";;
40-
--portable_kernels=*) portable_kernels="${arg#*=}";;
41-
--toolchain=*) toolchain="${arg#*=}";;
42-
*)
43-
;;
44-
esac
45-
done
46-
47-
if [[ ${toolchain} == "arm-none-eabi-gcc" ]]; then
48-
toolchain_cmake=${et_root_dir}/examples/arm/ethos-u-setup/${toolchain}.cmake
49-
elif [[ ${toolchain} == "arm-zephyr-eabi-gcc" ]]; then
50-
toolchain_cmake=${et_root_dir}/examples/zephyr/x86_64-linux-arm-zephyr-eabi-gcc.cmake
51-
else
52-
echo "Error: Invalid toolchain selection, provided: ${tolchain}"
53-
echo " Valid options are {arm-none-eabi-gcc, arm-zephyr-eabi-gcc}"
54-
exit 1;
55-
fi
56-
toolchain_cmake=$(realpath ${toolchain_cmake})
57-
58-
# Source the tools
59-
# This should be prepared by the setup.sh
60-
[[ -f ${setup_path_script} ]] \
61-
|| { echo "Missing ${setup_path_script}. ${_setup_msg}"; exit 1; }
62-
63-
source ${setup_path_script}
64-
65-
et_build_dir=${et_build_root}/cmake-out
66-
67-
cd "${et_root_dir}"
68-
69-
echo "--------------------------------------------------------------------------------" ;
70-
echo "Build ExecuTorch Libraries ${build_type} portable kernels: ${portable_kernels} into '${et_build_dir}'" ;
71-
echo "--------------------------------------------------------------------------------"
72-
73-
if ! [[ $portable_kernels =~ ^((^|,)aten::[a-zA-Z0-9_]+\.[a-zA-Z0-9_]*out)*$ ]]; then
74-
echo " ERROR: specified argument --portable_kernels=${portable_kernels}"
75-
echo " is in the wrong format please use \"aten::<OP1>.out,aten::<OP2>.out,...\""
76-
echo " e.g. \"aten::_softmax.out,aten::add.out\""
77-
exit 1
78-
fi
79-
80-
set -x
81-
82-
cmake \
83-
-DCMAKE_INSTALL_PREFIX=${et_build_dir} \
84-
-DCMAKE_BUILD_TYPE=${build_type} \
85-
-DCMAKE_TOOLCHAIN_FILE="${toolchain_cmake}" \
86-
-DEXECUTORCH_SELECT_OPS_LIST=${portable_kernels} \
87-
-B"${et_build_dir}/examples/arm" \
88-
"${et_root_dir}/examples/arm"
89-
90-
cmake --build "${et_build_dir}/examples/arm" -j$(nproc) --config ${build_type} --
91-
92-
set +x
93-
94-
echo "[$(basename $0)] Generated static libraries for ExecuTorch:"
95-
find "${et_build_dir}/examples/arm" -name "*.a" -exec ls -al {} \;
7+
echo "DEPRECATED: build_portable_kernels.sh is deprecated and will be removed. The kernel registration library is now built directly with the arm_executor_runner."

backends/arm/test/test_arm_baremetal.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ test_pytest_ops_ethosu_fvp() { # Same as test_pytest but also sometime verify us
117117

118118
# Prepare Corstone-3x0 FVP for pytest
119119
backends/arm/scripts/build_executorch.sh
120-
backends/arm/scripts/build_portable_kernels.sh
121120
# Build semihosting version of the runner used by pytest testing. This builds:
122121
# arm_test/arm_semihosting_executor_runner_corstone-300
123122
# arm_test/arm_semihosting_executor_runner_corstone-320
@@ -133,7 +132,6 @@ test_pytest_models_ethosu_fvp() { # Same as test_pytest but also sometime verify
133132

134133
# Prepare Corstone-3x0 FVP for pytest
135134
backends/arm/scripts/build_executorch.sh
136-
backends/arm/scripts/build_portable_kernels.sh
137135
# Build semihosting version of the runner used by pytest testing. This builds:
138136
# arm_test/arm_semihosting_executor_runner_corstone-300
139137
# arm_test/arm_semihosting_executor_runner_corstone-320

backends/arm/test/test_model.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,6 @@ def build_libs(et_build_root: str, script_path: str):
110110
"--etdump",
111111
]
112112
)
113-
run_external_cmd(
114-
[
115-
"bash",
116-
os.path.join(script_path, "build_portable_kernels.sh"),
117-
f"--et_build_root={et_build_root}",
118-
"--build_type=Release",
119-
"--portable_kernels=aten::_softmax.out",
120-
]
121-
)
122113

123114

124115
def build_pte(

backends/cortex_m/CMakeLists.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
22
# All rights reserved.
3+
# Copyright 2025 Arm Limited and/or its affiliates.
34
#
45
# This source code is licensed under the BSD-style license found in the
56
# LICENSE file in the root directory of this source tree.
67

7-
# Kernel library for Cortex-M operators. Please keep this file formatted by running:
8+
# Kernel library for Cortex-M operators. Please keep this file formatted by
9+
# running:
810
# ~~~
911
# cmake-format -i CMakeLists.txt
1012
# ~~~
@@ -29,8 +31,8 @@ set(_cortex_m_kernels__srcs
2931
${CMAKE_CURRENT_SOURCE_DIR}/ops/op_dequantize_per_tensor.cpp
3032
)
3133

32-
# Generate C++ bindings to register kernels into Executorch (for runtime).
33-
# Here select all ops in operators.yaml
34+
# Generate C++ bindings to register kernels into Executorch (for runtime). Here
35+
# select all ops in operators.yaml
3436
set(_yaml_file ${CMAKE_CURRENT_LIST_DIR}/ops/operators.yaml)
3537
gen_selected_ops(LIB_NAME "cortex_m_ops_lib" OPS_SCHEMA_YAML "${_yaml_file}")
3638

@@ -52,6 +54,7 @@ gen_operators_lib(
5254

5355
install(
5456
TARGETS cortex_m_kernels cortex_m_ops_lib
57+
EXPORT ExecuTorchTargets
5558
DESTINATION lib
5659
PUBLIC_HEADER DESTINATION include/executorch/backends/cortex_m/ops/
5760
)

docs/source/tutorial-arm-ethos-u.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,17 +300,13 @@ To run a `.pte` file with the Arm backend delegate call instructions, you will n
300300

301301
- `libexecutorch_delegate_ethos_u.a`
302302

303-
These libraries are generated by the `backends/arm/scripts/build_executorch.sh` and `backends/arm/scripts/build_portable_kernels.sh` scripts called from the `run.sh` script.
304-
305-
The `--portable_kernels` flag can be used to set the build flag `EXECUTORCH_SELECT_OPS_LIST` when running `backends/arm/scripts/build_portable_kernels.sh` that will decide the number of portable operators included in the build and are available at runtime. It must match with `.pte` file's requirements, otherwise you will get `Missing Operator` error at runtime.
306-
307-
For example, there in the command line above, to run SoftmaxModule, you only included the softmax CPU operator. Similarly, to run AddModule in a non-delegated manner you will need add op and so on. As you might have already realized, for the delegated operators, which will be executed by the Arm backend delegate, you do not need to include those operators in this list. This is only for *non-delegated* operators.
303+
These libraries are generated by the `backends/arm/scripts/build_executorch.sh` script called from the `run.sh` script.
308304

309305
### Building the executor_runner Bare-Metal Application
310306

311307
The SDK dir is the same one prepared [earlier](#setup-the-arm-ethos-u-software-development). And, you will be passing the `.pte` file (any one of them) generated above.
312308

313-
Note, you have to generate a new `executor-runner` binary if you want to change the model or the `.pte` file. This constraint is from the constrained bare-metal runtime environment you have for Corstone-300/Corstone-320 platforms.
309+
Note, you have to generate a new `executor-runner` binary if you want to change the model or the `.pte` file. This constraint is from the constrained bare-metal runtime environment you have for Corstone-300/Corstone-320 platforms. The build also generates a kernel registration library for the relevant operators which could not be delegated to the EthosU, see the [Kernel Library Selective Build documentation](https://docs.pytorch.org/executorch/stable/kernel-library-selective-build.html).
314310

315311
This step is executed by the build_executor_runner.sh script, which is invoked from the run.sh in the backends/arm/scripts folder.
316312

examples/arm/CMakeLists.txt

Lines changed: 0 additions & 66 deletions
This file was deleted.

examples/arm/ethos_u_minimal_example.ipynb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,9 @@
180180
"source": [
181181
"## Build executor runtime\n",
182182
"\n",
183-
"After the AOT compilation flow is done, the runtime can be cross compiled and linked to the produced .pte-file using the Arm cross-compilation toolchain. This is done in three steps:\n",
184-
"1. Build the executorch library and EthosUDelegate.\n",
185-
"2. Build any external kernels required. In this example this is not needed as the graph is fully delegated, but its included for completeness.\n",
186-
"3. Build and link the `arm_executor_runner`."
183+
"After the AOT compilation flow is done, the runtime can be cross compiled and linked to the produced .pte-file using the Arm cross-compilation toolchain. This is done in two steps:\n",
184+
"1. Build and install the executorch library and EthosUDelegate.\n",
185+
"2. Build and link the `arm_executor_runner` and generate kernel bindings for any non delegated ops."
187186
]
188187
},
189188
{
@@ -202,9 +201,6 @@
202201
"# Cross-compile executorch \n",
203202
"subprocess.run(os.path.join(script_dir, \"build_executorch.sh\"), shell=True, cwd=et_dir)\n",
204203
"\n",
205-
"# Cross-compile portable kernels\n",
206-
"subprocess.run(os.path.join(script_dir, \"build_portable_kernels.sh\"), shell=True, cwd=et_dir)\n",
207-
"\n",
208204
"# Cross-compile executorch runner\n",
209205
"args = f\"--pte={pte_path} --target={target}\"\n",
210206
"subprocess.run(os.path.join(script_dir, \"build_executor_runner.sh\") + \" \" + args, shell=True, cwd=et_dir)\n",
@@ -235,7 +231,7 @@
235231
],
236232
"metadata": {
237233
"kernelspec": {
238-
"display_name": ".venv",
234+
"display_name": ".venv (3.10.15)",
239235
"language": "python",
240236
"name": "python3"
241237
},

0 commit comments

Comments
 (0)