Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions .github/workflows/torch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ jobs:
strategy:
matrix:
target:
- {name: cortex-m7, cpu: cortex-m7, fpu: fpv5-sp-d16}
- {name: cortex-m55, cpu: cortex-m55, fpu: fpv5-d16}
- {name: cortex-m7, cpu: cortex-m7, fpu: fpv5-sp-d16, npu: ''}
- {name: cortex-m55, cpu: cortex-m55, fpu: fpv5-d16, npu: ''}
- {name: cortex-m55-ethosu, cpu: cortex-m55, fpu: fpv5-d16, npu: ethos-u}
fail-fast: true

steps:
Expand Down Expand Up @@ -76,6 +77,10 @@ jobs:
run: |
python -m pip install --break-system-packages --upgrade pip -r requirements.txt

- name: 'Checkout Ethos-U core driver'
if: matrix.target.npu == 'ethos-u'
run: source tools/ci.sh && ci_clone_ethosu_driver

- name: 'Update ExecuTorch submodules'
run: source tools/ci.sh && ci_update_submodules

Expand All @@ -87,13 +92,14 @@ jobs:
source tools/ci.sh
ci_build_target ${{ matrix.target.name }} \
${{ matrix.target.cpu }} \
${{ matrix.target.fpu }}
${{ matrix.target.fpu }} \
"${{ matrix.target.npu }}"

- name: 'Upload library'
uses: actions/upload-artifact@v4
with:
name: libtorch-${{ matrix.target.name }}
path: libtorch-*${{ matrix.target.name }}.a
path: libtorch-${{ matrix.target.name }}.a
retention-days: 1
if-no-files-found: error

Expand Down Expand Up @@ -141,6 +147,10 @@ jobs:
rsync -av --delete --include='*/' --include='LICENSE' --include='*.h' --exclude='*' --delete *-headers/ include
rm -fr *-headers

- name: 'Update license'
run: |
cp include/LICENSE lib/LICENSE

- name: 'Create Pull Request'
uses: peter-evans/create-pull-request@v5
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/build/
/libtorch-*.a
/*-headers/
/ethos-u-sdk/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# About This Repository
This repository builds PyTorch ExecuTorch runtime libraries from the upstream repository. It is meant to be included only as a submodule in the main OpenMV firmware repository. All files in this repository are auto-generated by github workflows.

The libraries contain the ExecuTorch runtime, the portable, quantized and Cortex-M (CMSIS-NN) kernels selected in `tools/ci.sh`, and a single `et_register_all()` function that registers all kernels (the firmware does not run static initializers). CMSIS-NN is built into a separate `libtorch-cmsis-nn-<cpu>.a` library.
The libraries contain the ExecuTorch runtime, the portable, quantized and Cortex-M (CMSIS-NN) kernels selected in `tools/ci.sh`, and a single `et_register_all()` function that registers all kernels (the firmware does not run static initializers). CMSIS-NN is not included, it's provided by the firmware (the version is set in `tools/ci.sh`).

## Contributing to the project

Expand Down
Binary file removed lib/libtorch-cmsis-nn-cortex-m55.a
Binary file not shown.
Binary file removed lib/libtorch-cmsis-nn-cortex-m7.a
Binary file not shown.
27 changes: 27 additions & 0 deletions patches/0003-openmv-Register-the-Ethos-U-backend-explicitly.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
diff --git a/backends/arm/runtime/EthosUBackend.cpp b/backends/arm/runtime/EthosUBackend.cpp
index bd400fb..87530fb 100644
--- a/backends/arm/runtime/EthosUBackend.cpp
+++ b/backends/arm/runtime/EthosUBackend.cpp
@@ -510,13 +510,15 @@ void calculate_dimensions(
}
}

-namespace {
-auto EthosUBackend_backend = EthosUBackend();
-Backend EthosUBackend_id{"EthosUBackend", &EthosUBackend_backend};
-static executorch::runtime::Error EthosUBackend_registered =
- register_backend(EthosUBackend_id);
-
-} // namespace
+// OpenMV: register explicitly instead of from a static initializer, which
+// does not run on all ports. The backend is constructed in static storage so
+// no destructor is registered.
+executorch::runtime::Error register_ethos_u_backend() {
+ alignas(EthosUBackend) static uint8_t backend_storage[sizeof(EthosUBackend)];
+ static Backend backend_id{
+ "EthosUBackend", new (backend_storage) EthosUBackend()};
+ return register_backend(backend_id);
+}

} // namespace arm
} // namespace backends
52 changes: 44 additions & 8 deletions tools/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
########################################################################################
ET_REPO_PATH=executorch
ET_SUBMODULES="third-party/json third-party/gflags third-party/flatbuffers third-party/flatcc backends/xnnpack/third-party/FXdiv"
CMSIS_NN_VERSION=v8.0.0
ETHOSU_DRIVER_URL="https://gitlab.arm.com/artificial-intelligence/ethos-u/ethos-u-core-driver.git"
ETHOSU_DRIVER_TAG=26.08
ETHOS_SDK_PATH=ethos-u-sdk
ETHOSU_DRIVER_PATH=${ETHOS_SDK_PATH}/core_software/core_driver
TOOLCHAIN_PATH=${HOME}/cache/gcc
export PATH=${TOOLCHAIN_PATH}/bin:${PATH}
TOOLCHAIN_URL="https://developer.arm.com/-/media/Files/downloads/gnu/14.3.rel1/binrel/arm-gnu-toolchain-14.3.rel1-x86_64-arm-none-eabi.tar.xz"
Expand Down Expand Up @@ -119,6 +124,13 @@ ci_update_submodules() {
git -C ${ET_REPO_PATH} submodule update --init --depth=1 ${ET_SUBMODULES}
}

# Clone the Ethos-U core driver. Only its headers are used, the driver is provided by
# the firmware. The version must match the firmware's.
########################################################################################
ci_clone_ethosu_driver() {
git clone --depth=1 --branch ${ETHOSU_DRIVER_TAG} ${ETHOSU_DRIVER_URL} ${ETHOSU_DRIVER_PATH}
}

# Apply ExecuTorch patches.
########################################################################################
ci_apply_patches() {
Expand All @@ -133,6 +145,7 @@ ci_build_target() {
TARGET_NAME=${1}
TARGET_CPU=${2}
TARGET_FPU=${3}
TARGET_NPU=${4}

BUILD_DIR=$(pwd)/build/${TARGET_NAME}
GEN_DIR=${BUILD_DIR}/gen
Expand All @@ -141,10 +154,22 @@ ci_build_target() {

TARGET_FLAGS="-mthumb -mcpu=${TARGET_CPU} -mtune=${TARGET_CPU} -mfpu=${TARGET_FPU} -mfloat-abi=hard \
-fdata-sections -ffunction-sections -Wno-psabi"

# The Ethos-U delegate is built against the core driver headers, with the same
# configuration as the firmware's driver.
NPU_ARGS="-DEXECUTORCH_BUILD_ARM_BAREMETAL=OFF"
NPU_LIBS=""
if [ "${TARGET_NPU}" == "ethos-u" ]; then
TARGET_FLAGS="${TARGET_FLAGS} -DETHOSU -DETHOSU_MULTI_VARIANT"
NPU_ARGS="-DEXECUTORCH_BUILD_ARM_BAREMETAL=ON -DETHOS_SDK_PATH=$(pwd)/${ETHOS_SDK_PATH}"
NPU_LIBS="executorch_delegate_ethos_u"
fi
TARGET_CXXFLAGS="${TARGET_FLAGS} -fno-rtti -fno-exceptions -fno-unwind-tables \
-fno-use-cxa-atexit -fno-threadsafe-statics"

# Build the runtime, the prim ops (executorch), the kernel libraries and CMSIS-NN.
# Build the runtime, the prim ops (executorch) and the kernel libraries. CMSIS-NN
# is provided by the firmware; it is only downloaded here for its headers, and its
# version must match the firmware's.
# Kernel registration is generated below (manual registration), so the upstream
# ops_lib targets, which register from static initializers, are not built.
cmake -S ${ET_DIR} -B ${BUILD_DIR} \
Expand All @@ -162,7 +187,7 @@ ci_build_target() {
-DCMAKE_POSITION_INDEPENDENT_CODE=OFF \
-DPYTHON_EXECUTABLE=${PYTHON} \
-DEXECUTORCH_BUILD_PRESET_FILE=${ET_DIR}/tools/cmake/preset/arm_baremetal.cmake \
-DEXECUTORCH_BUILD_ARM_BAREMETAL=OFF \
${NPU_ARGS} \
-DEXECUTORCH_BUILD_PTHREADPOOL=OFF \
-DEXECUTORCH_BUILD_CPUINFO=OFF \
-DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=OFF \
Expand All @@ -171,10 +196,11 @@ ci_build_target() {
-DEXECUTORCH_ENABLE_LOGGING=OFF \
-DEXECUTORCH_ENABLE_PROGRAM_VERIFICATION=OFF \
-DEXECUTORCH_OPTIMIZE_SIZE=OFF \
-DEXECUTORCH_PAL_DEFAULT=minimal
-DEXECUTORCH_PAL_DEFAULT=minimal \
-DCMSIS_NN_VERSION=${CMSIS_NN_VERSION}
cmake --build ${BUILD_DIR} -j$(nproc) --target \
executorch_core executorch portable_kernels kernels_util_all_deps \
quantized_kernels cortex_m_kernels cmsis-nn
quantized_kernels cortex_m_kernels ${NPU_LIBS}

# Generate one manual registration function per kernel library. The libraries are
# generated separately because the codegen indexes kernels by name without the
Expand Down Expand Up @@ -204,7 +230,13 @@ ci_build_target() {
done
popd

# Entry point called by the firmware to register all kernels.
# Entry point called by the firmware to register all kernels and backends.
REGISTER_NPU_DECL=""
REGISTER_NPU_CALL=""
if [ "${TARGET_NPU}" == "ethos-u" ]; then
REGISTER_NPU_DECL="namespace executorch::backends::arm { executorch::runtime::Error register_ethos_u_backend(); }"
REGISTER_NPU_CALL="if (err == Error::Ok) { err = executorch::backends::arm::register_ethos_u_backend(); }"
fi
cat > ${GEN_DIR}/register_all.cpp << EOF
#include <executorch/runtime/core/error.h>

Expand All @@ -221,6 +253,8 @@ Error register_prim_ops();
} // namespace executor
} // namespace torch

${REGISTER_NPU_DECL}

extern "C" int et_register_all(void) {
static bool registered = false;
if (registered) {
Expand All @@ -237,6 +271,7 @@ extern "C" int et_register_all(void) {
if (err == Error::Ok) {
err = torch::executor::register_cortex_m_kernels();
}
${REGISTER_NPU_CALL}

registered = (err == Error::Ok);
return static_cast<int>(err);
Expand All @@ -255,8 +290,7 @@ EOF
done
arm-none-eabi-ar rcs ${GEN_DIR}/libregister.a ${REGISTER_OBJS}

# Merge everything except CMSIS-NN into a single archive. CMSIS-NN is kept in a
# separate archive so the firmware can share one CMSIS-NN between libraries.
# Merge everything into a single archive.
{
echo "CREATE libtorch-${TARGET_NAME}.a"
for lib in libexecutorch_core.a \
Expand All @@ -268,10 +302,12 @@ EOF
gen/libregister.a; do
echo "ADDLIB ${BUILD_DIR}/${lib}"
done
if [ "${TARGET_NPU}" == "ethos-u" ]; then
echo "ADDLIB ${BUILD_DIR}/backends/arm/libexecutorch_delegate_ethos_u.a"
fi
echo "SAVE"
echo "END"
} | arm-none-eabi-ar -M
cp ${BUILD_DIR}/backends/cortex_m/cmsis_nn-build/libcmsis-nn.a libtorch-cmsis-nn-${TARGET_NAME}.a
}

# Build Headers.
Expand Down
Loading