|
| 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 | +) |
0 commit comments