diff --git a/.github/workflows/torch.yml b/.github/workflows/torch.yml index 7b17ce2..677522d 100644 --- a/.github/workflows/torch.yml +++ b/.github/workflows/torch.yml @@ -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: @@ -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 @@ -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 @@ -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: diff --git a/.gitignore b/.gitignore index e3f02f3..305dc49 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /build/ /libtorch-*.a /*-headers/ +/ethos-u-sdk/ diff --git a/README.md b/README.md index 3f8a07e..a8580f6 100644 --- a/README.md +++ b/README.md @@ -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-.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 diff --git a/lib/libtorch-cmsis-nn-cortex-m55.a b/lib/libtorch-cmsis-nn-cortex-m55.a deleted file mode 100644 index d583e65..0000000 Binary files a/lib/libtorch-cmsis-nn-cortex-m55.a and /dev/null differ diff --git a/lib/libtorch-cmsis-nn-cortex-m7.a b/lib/libtorch-cmsis-nn-cortex-m7.a deleted file mode 100644 index b388c9a..0000000 Binary files a/lib/libtorch-cmsis-nn-cortex-m7.a and /dev/null differ diff --git a/patches/0003-openmv-Register-the-Ethos-U-backend-explicitly.patch b/patches/0003-openmv-Register-the-Ethos-U-backend-explicitly.patch new file mode 100644 index 0000000..d0731c3 --- /dev/null +++ b/patches/0003-openmv-Register-the-Ethos-U-backend-explicitly.patch @@ -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 diff --git a/tools/ci.sh b/tools/ci.sh index ec7896c..c1f1e85 100644 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -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" @@ -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() { @@ -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 @@ -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} \ @@ -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 \ @@ -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 @@ -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 @@ -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) { @@ -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(err); @@ -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 \ @@ -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.