Skip to content

Commit 6774d7c

Browse files
committed
[cadence][hifi] Fix cadence backend example for i.MXRT600
Fixing the build of the cadence backend example for i.MXRT600. Guideline updated to reference a particular NXP MCUXpresso SDK (25.12) and added selective kernel registration, based on model used as the full executorch op library exceeds the i.MXRT600 memory.
1 parent 36e8ed9 commit 6774d7c

6 files changed

Lines changed: 68 additions & 28 deletions

File tree

backends/cadence/cadence.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ endif()
1515

1616
set(TOOLCHAIN_HOME ${XTENSA_TOOLCHAIN_PATH}/$ENV{TOOLCHAIN_VER}/XtensaTools)
1717

18+
set(CMAKE_SYSTEM_NAME Generic-ELF)
19+
set(CMAKE_SYSTEM_PROCESSOR xtensa)
20+
1821
set(LINKER ld)
1922
set(BINTOOLS gnu)
2023

backends/cadence/executor_runner.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ static uint8_t method_allocator_pool[18 * 1024U]; // 4 MB
4949

5050
using executorch::runtime::Error;
5151
using executorch::runtime::Result;
52+
using executorch::runtime::EValue;
5253

5354
void LED_INIT();
5455
void LED_TOGGLE();

backends/cadence/hifi/operators/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ generate_bindings_for_kernels(
170170
LIB_NAME "cadence_ops_lib" OPS_SCHEMA_YAML FUNCTIONS_YAML
171171
${CMAKE_CURRENT_SOURCE_DIR}/../../aot/functions_hifi.yaml
172172
)
173-
message("Generated files ${gen_command_sources}")
173+
message("Generated files: ${gen_command_sources}")
174174

175175
gen_operators_lib(
176176
LIB_NAME "cadence_ops_lib" KERNEL_LIBS custom_ops DEPS aten_ops_cadence

docs/source/backends-cadence.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ In order to be able to succesfully build and run ExecuTorch on a Xtensa HiFi4 DS
4747
- Needed to flash the board with the firmware images. You can install this on the same platform that you installed the MCUXpresso IDE on.
4848
- Note: depending on the version of the NXP board, another probe than JLink might be installed. In any case, flashing is done using the MCUXpresso IDE in a similar way.
4949
- [MCUXpresso SDK](https://mcuxpresso.nxp.com/en/select?device=EVK-MIMXRT685)
50-
- Download this SDK to your Linux machine, extract it and take a note of the path where you store it. You'll need this later.
50+
- Download this SDK, version 25.12.00 to your Linux machine, extract it and take a note of the path where you store it. You'll need this later.
51+
- Note there are 2 variants of the SDK - the CLI only ARMGCC and for the MCUXpresso IDE. The SDK variant for the MCUXpresso IDE contains the SDK only for the i.MX RT600 platforms, whereas the ARMGCC toolchain contains the SDK for multiple SoCs. That means you need to adjust the env variables accordingly.
52+
- If not noted otherwise, the links in this tutorial are valid for MCUXpresso SDK 25.12.00, MCUXPresso IDE variant.
5153
- [Xtensa compiler](https://tensilicatools.com/platform/i-mx-rt600/)
5254
- Download this to your Linux machine. This is needed to build ExecuTorch for the HiFi4 DSP.
5355
- For cases with optimized kernels, the [nnlib repo](https://github.com/foss-xtensa/nnlib-hifi4).
@@ -264,7 +266,8 @@ export XTENSA_TOOLCHAIN=/home/user_name/cadence/XtDevTools/install/tools
264266
export TOOLCHAIN_VER=RI-2023.11-linux
265267
# The Xtensa core that you're targeting.
266268
# For HiFi4 (NXP RT600):
267-
export XTENSA_CORE=VANILLA_HIFI
269+
#export XTENSA_CORE=VANILLA_HIFI
270+
export XTENSA_CORE=nxp_rt600_RI23_11_newlib
268271
# For Fusion G3:
269272
# export XTENSA_CORE=VANILLA_G3
270273
# For Vision P6:
@@ -278,11 +281,11 @@ The Cadence backend supports multiple DSP families:
278281
- **Vision P-Series DSPs**: Core `VANILLA_VISION`, enable with `-DEXECUTORCH_VISION_OPT=ON`
279282
```
280283

281-
***Step 2***. Clone the [nnlib repo](https://github.com/foss-xtensa/nnlib-hifi4), which contains optimized kernels and primitives for HiFi4 DSPs, with `git clone git@github.com:foss-xtensa/nnlib-hifi4.git`.
284+
***Step 2***. Run the `backends/cadence/install_requirements.sh`. This clones the Cadence Neural Network Library (nn-lib) .
282285

283286
***Step 3***. Run the CMake build.
284287
In order to run the CMake build, you need the path to the following:
285-
- The Program generated in the previous step
288+
- The Program generated in the previous step, typically will have the name `CadenceDemoModel.pte`.
286289
- Path to the NXP SDK root. This should have been installed already in the [Setting up Developer Environment](#setting-up-developer-environment) section. This is the directory that contains the folders such as boards, components, devices, and other.
287290

288291
```bash
@@ -307,7 +310,7 @@ cmake -DCMAKE_BUILD_TYPE=Debug \
307310
-DCMAKE_PREFIX_PATH=<path_to_executorch>/cmake-out \
308311
-DMODEL_PATH=<path_to_program_file_generated_in_previous_step> \
309312
-DNXP_SDK_ROOT_DIR=<path_to_nxp_sdk_root> \
310-
-DNN_LIB_BASE_DIR=<path_to_nnlib_cloned_in_step_2> \
313+
-DNN_LIB_BASE_DIR=<path_to_executorch>/backends/cadence/hifi/third-party/nnlib/nnlib-hifi4 \
311314
-Bcmake-out/examples/cadence \
312315
examples/cadence
313316

@@ -316,8 +319,8 @@ cmake --build cmake-out/examples/cadence -j8 -t cadence_executorch_example
316319

317320
After having succesfully run the above step you should see two binary files in their CMake output directory.
318321
```bash
319-
> ls cmake-xt/*.bin
320-
cmake-xt/dsp_data_release.bin cmake-xt/dsp_text_release.bin
322+
> ls cmake-out/examples/cadence/*.bin
323+
cmake-out/examples/cadence/dsp_data_release.bin cmake-out/examples/cadence/dsp_text_release.bin
321324
```
322325

323326
## Deploying and Running on Device

examples/cadence/CMakeLists.txt

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ if(NOT PYTHON_EXECUTABLE)
2626
endif()
2727

2828
# Let files say "include <executorch/path/to/header.h>".
29-
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
29+
set(_common_include_directories
30+
${EXECUTORCH_ROOT}/.. ${EXECUTORCH_ROOT}/runtime/core/portable_type/c10
31+
)
32+
33+
add_compile_definitions(C10_USING_CUSTOM_GENERATED_MACROS)
3034

3135
# Find prebuilt executorch lib
3236
find_package(executorch CONFIG REQUIRED)
@@ -66,8 +70,8 @@ set(SOURCES
6670
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/system_MIMXRT685S_dsp.c
6771
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/utilities/debug_console_lite/fsl_assert.c
6872
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/utilities/debug_console_lite/fsl_debug_console.c
69-
${NXP_SDK_ROOT_DIR}/boards/evkmimxrt685/dsp_examples/mu_polling/dsp/board_hifi4.c
70-
${NXP_SDK_ROOT_DIR}/boards/evkmimxrt685/dsp_examples/mu_polling/dsp/pin_mux.c
73+
${NXP_SDK_ROOT_DIR}/boards/evkmimxrt685/dsp_examples/mu_polling/hifi4/board_hifi4.c
74+
${NXP_SDK_ROOT_DIR}/boards/evkmimxrt685/dsp_examples/mu_polling/hifi4/pin_mux.c
7175
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/utilities/str/fsl_str.c
7276
)
7377

@@ -78,37 +82,63 @@ target_include_directories(
7882
PUBLIC ${NXP_SDK_ROOT_DIR}
7983
${NXP_SDK_ROOT_DIR}/components/uart
8084
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/drivers
85+
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/periph
8186
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/utilities/debug_console_lite
8287
${NXP_SDK_ROOT_DIR}/components/lists
8388
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S
8489
${NXP_SDK_ROOT_DIR}/CMSIS/Core/Include
8590
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/utilities/str
86-
${NXP_SDK_ROOT_DIR}/boards/evkmimxrt685/dsp_examples/mu_polling/dsp
91+
${NXP_SDK_ROOT_DIR}/boards/evkmimxrt685/dsp_examples/mu_polling/hifi4
8792
)
8893

89-
add_library(extension_runner_util STATIC IMPORTED)
90-
set_property(
91-
TARGET extension_runner_util
92-
PROPERTY
93-
IMPORTED_LOCATION
94-
"${CMAKE_CURRENT_LIST_DIR}/../../cmake-out/extension/runner_util/libextension_runner_util.a"
94+
add_subdirectory(
95+
${EXECUTORCH_ROOT}/backends/cadence/hifi/third-party/nnlib
96+
${CMAKE_CURRENT_BINARY_DIR}/backends/cadence/hifi/third-party/nnlib
9597
)
9698

97-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/hifi/operators)
98-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/hifi/kernels)
99+
add_subdirectory(
100+
${EXECUTORCH_ROOT}/backends/cadence/hifi/operators
101+
${CMAKE_CURRENT_BINARY_DIR}/backends/cadence/hifi/operators
102+
)
103+
add_subdirectory(
104+
${EXECUTORCH_ROOT}/backends/cadence/hifi/kernels
105+
${CMAKE_CURRENT_BINARY_DIR}/backends/cadence/hifi/kernels
106+
)
107+
108+
# Generate C++ bindings to register kernels into both PyTorch (for AOT) and
109+
# Executorch (for runtime). In order to fit the model into i.MXRT600 memory, we
110+
# use selective kernel registration based on MODEL_PATH, and lik to
111+
# cadence_ops_lib_selective.
112+
gen_selected_ops(
113+
LIB_NAME "cadence_ops_lib_selective" OPS_FROM_MODEL "${MODEL_PATH}"
114+
)
115+
generate_bindings_for_kernels(
116+
LIB_NAME "cadence_ops_lib_selective" OPS_SCHEMA_YAML FUNCTIONS_YAML
117+
${EXECUTORCH_ROOT}/backends/cadence/aot/functions_hifi.yaml
118+
)
119+
message("Generated files: ${gen_command_sources}")
120+
121+
gen_operators_lib(
122+
LIB_NAME "cadence_ops_lib_selective" KERNEL_LIBS custom_ops DEPS
123+
aten_ops_cadence
124+
)
99125

100126
# Generate the model header file
101127
add_custom_command(
102128
OUTPUT ${CMAKE_BINARY_DIR}/model_pte.h
103-
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/utils/gen_header.py
104-
--model_path ${MODEL_PATH} --header_output_path ${CMAKE_BINARY_DIR}
129+
COMMAND
130+
${PYTHON_EXECUTABLE} ${EXECUTORCH_ROOT}/backends/cadence/utils/gen_header.py
131+
--model_path ${MODEL_PATH} --header_output_path ${CMAKE_BINARY_DIR}
105132
COMMENT "Converting .pte model to header file..."
106-
DEPENDS ${CMAKE_CURRENT_LIST_DIR}/utils/gen_header.py
133+
DEPENDS ${EXECUTORCH_ROOT}/backends/cadence/utils/gen_header.py
107134
)
108135

109136
add_custom_target(gen_model_header DEPENDS ${CMAKE_BINARY_DIR}/model_pte.h)
110137

111-
add_executable(cadence_executorch_example executor_runner.cpp)
138+
add_executable(
139+
cadence_executorch_example
140+
${EXECUTORCH_ROOT}/backends/cadence/executor_runner.cpp
141+
)
112142
add_dependencies(cadence_executorch_example gen_model_header)
113143

114144
# lint_cmake: -linelength
@@ -120,22 +150,24 @@ target_include_directories(
120150
target_link_options(
121151
cadence_executorch_example PRIVATE
122152
-mlsp=${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/xtensa/min-rt
153+
-Wl,--orphan-handling=place
123154
)
124155
target_link_libraries(
125-
cadence_executorch_example dsp_mu_polling_libs cadence_ops_lib
156+
cadence_executorch_example dsp_mu_polling_libs cadence_ops_lib_selective
126157
extension_runner_util executorch
127158
)
128159

129160
add_custom_command(
130161
TARGET cadence_executorch_example
131162
POST_BUILD
132163
COMMAND
133-
${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/utils/post_compilation.py
134-
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME} ${CMAKE_BINARY_DIR}
164+
${PYTHON_EXECUTABLE}
165+
${EXECUTORCH_ROOT}/backends/cadence/utils/post_compilation.py
166+
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.elf ${CMAKE_BINARY_DIR}
135167
COMMENT
136168
"Generating .bin files that can be used to flash the DSP with. Copy over
137169
the dsp_text_release.bin and dsp_data_release.bin that are generated into
138170
your NXP MCUXpresso IDE workspace and flash the DSP with these binaries."
139171
DEPENDS
140-
${CMAKE_CURRENT_LIST_DIR}/utils/post_compilation.py
172+
${EXECUTORCH_ROOT}/backends/cadence/utils/post_compilation.py
141173
)

tools/cmake/Codegen.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ function(generate_bindings_for_kernels)
158158
)
159159
endif()
160160

161+
message("Command - ${_gen_command}")
161162
add_custom_command(
162163
COMMENT "Generating code for kernel registration"
163164
OUTPUT ${_gen_command_sources}

0 commit comments

Comments
 (0)