1# Copyright 2023-2024 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 6cmake_minimum_required(VERSION 3.20) 7project(arm_executor_runner) 8 9option(SEMIHOSTING "Enable semihosting" OFF) 10option(ET_ARM_BAREMETAL_METHOD_ALLOCATOR_POOL_SIZE "Set ET_ARM_BAREMETAL_METHOD_ALLOCATOR_POOL_SIZE to specify memory alloction pool size" OFF) 11option(ET_ARM_BAREMETAL_TEMP_ALLOCATOR_POOL_SIZE "Set ET_ARM_BAREMETAL_TEMP_ALLOCATOR_POOL_SIZE to specify temp alloction pool size" OFF) 12 13 14if(NOT DEFINED ET_PTE_FILE_PATH AND NOT ${SEMIHOSTING}) 15 message( 16 FATAL_ERROR 17 "ET_PTE_FILE_PATH must specify a model .pte, for bare metal systems the " 18 "model is built into the binary." 19 ) 20endif() 21 22set(TARGET_BOARD "corstone-300" CACHE STRING "Target board") 23 24# Example ExecuTorch demo for bare metal Cortex-M based systems 25set(ET_DIR_PATH 26 "../../.." 27 CACHE PATH "Path to ExecuTorch dir" 28) 29set(ET_BUILD_DIR_PATH 30 "${ET_DIR_PATH}/cmake-out" 31 CACHE PATH "Path to ExecuTorch build dir" 32) 33set(ET_INCLUDE_PATH 34 "${ET_DIR_PATH}/.." 35 CACHE PATH "Path to ExecuTorch headers" 36) 37set(ET_PTE_FILE_PATH 38 "" 39 CACHE PATH "Path to ExecuTorch model pte" 40) 41set(ETHOS_SDK_PATH 42 "${ET_DIR_PATH}/examples/arm/ethos-u-scratch/ethos-u" 43 CACHE PATH "Path to Ethos-U bare metal driver/env" 44) 45set(PYTHON_EXECUTABLE 46 "python" 47 CACHE PATH "Define to override python executable used" 48) 49 50get_filename_component(ET_BUILD_DIR_PATH ${ET_BUILD_DIR_PATH} REALPATH) 51get_filename_component(ET_DIR_PATH ${ET_DIR_PATH} REALPATH) 52get_filename_component(ET_INCLUDE_PATH ${ET_INCLUDE_PATH} REALPATH) 53get_filename_component(ETHOS_SDK_PATH ${ETHOS_SDK_PATH} REALPATH) 54if(NOT ${SEMIHOSTING}) 55 get_filename_component(ET_PTE_FILE_PATH ${ET_PTE_FILE_PATH} REALPATH) 56endif() 57 58# Dependencies from the Ethos-U Core This is the platform target of 59# Corstone-300, that includes ethosu_core_driver and bare-metal bringup 60# libraries. We link against ethosu_target_init which includes all of these 61# dependencies. 62 63if(TARGET_BOARD STREQUAL "corstone-300") 64 add_subdirectory(${ETHOS_SDK_PATH}/core_platform/targets/corstone-300 target) 65 target_compile_definitions(ethosu_target_common INTERFACE 66 # ETHOSU_MODEL=0 place pte file/data in SRAM area 67 # ETHOSU_MODEL=1 place pte file/data in DDR area 68 ETHOSU_MODEL=1 69 # Configure NPU architecture timing adapters 70 # Ethos_U55_High_End_Embedded 71 # This is just example numbers and you should make this match your hardware 72 # SRAM 73 ETHOSU_TA_MAXR_0=8 74 ETHOSU_TA_MAXW_0=8 75 ETHOSU_TA_MAXRW_0=0 76 ETHOSU_TA_RLATENCY_0=32 77 ETHOSU_TA_WLATENCY_0=32 78 ETHOSU_TA_PULSE_ON_0=3999 79 ETHOSU_TA_PULSE_OFF_0=1 80 ETHOSU_TA_BWCAP_0=4000 81 ETHOSU_TA_PERFCTRL_0=0 82 ETHOSU_TA_PERFCNT_0=0 83 ETHOSU_TA_MODE_0=1 84 ETHOSU_TA_HISTBIN_0=0 85 ETHOSU_TA_HISTCNT_0=0 86 # Flash 87 ETHOSU_TA_MAXR_1=2 88 ETHOSU_TA_MAXW_1=0 89 ETHOSU_TA_MAXRW_1=0 90 ETHOSU_TA_RLATENCY_1=64 91 ETHOSU_TA_WLATENCY_1=0 92 ETHOSU_TA_PULSE_ON_1=320 93 ETHOSU_TA_PULSE_OFF_1=80 94 ETHOSU_TA_BWCAP_1=50 95 ETHOSU_TA_PERFCTRL_1=0 96 ETHOSU_TA_PERFCNT_1=0 97 ETHOSU_TA_MODE_1=1 98 ETHOSU_TA_HISTBIN_1=0 99 ETHOSU_TA_HISTCNT_1=0 100 ) 101elseif(TARGET_BOARD STREQUAL "corstone-320") 102 add_subdirectory(${ETHOS_SDK_PATH}/core_platform/targets/corstone-320 target) 103 target_compile_definitions(ethosu_target_common INTERFACE 104 # ETHOSU_MODEL=0 place pte file/data in SRAM area 105 # ETHOSU_MODEL=1 place pte file/data in DDR area 106 ETHOSU_MODEL=1 107 # Configure NPU architecture timing adapters 108 # Ethos_U85_SYS_DRAM_Mid 109 # This is just example numbers and you should make this match your hardware 110 # SRAM 111 ETHOSU_TA_MAXR_0=8 112 ETHOSU_TA_MAXW_0=8 113 ETHOSU_TA_MAXRW_0=0 114 ETHOSU_TA_RLATENCY_0=32 115 ETHOSU_TA_WLATENCY_0=32 116 ETHOSU_TA_PULSE_ON_0=3999 117 ETHOSU_TA_PULSE_OFF_0=1 118 ETHOSU_TA_BWCAP_0=4000 119 ETHOSU_TA_PERFCTRL_0=0 120 ETHOSU_TA_PERFCNT_0=0 121 ETHOSU_TA_MODE_0=1 122 ETHOSU_TA_HISTBIN_0=0 123 ETHOSU_TA_HISTCNT_0=0 124 # DRAM 125 ETHOSU_TA_MAXR_1=64 126 ETHOSU_TA_MAXW_1=32 127 ETHOSU_TA_MAXRW_1=0 128 ETHOSU_TA_RLATENCY_1=500 129 ETHOSU_TA_WLATENCY_1=250 130 ETHOSU_TA_PULSE_ON_1=4000 131 ETHOSU_TA_PULSE_OFF_1=1000 132 ETHOSU_TA_BWCAP_1=3750 133 ETHOSU_TA_PERFCTRL_1=0 134 ETHOSU_TA_PERFCNT_1=0 135 ETHOSU_TA_MODE_1=1 136 ETHOSU_TA_HISTBIN_1=0 137 ETHOSU_TA_HISTCNT_1=0 138 ) 139else() 140 message(FATAL_ERROR "Unsupported TARGET_BOARD: ${TARGET_BOARD}") 141endif() 142 143# Dependencies from the ExecuTorch build 144add_library(executorch STATIC IMPORTED) 145set_property( 146 TARGET executorch PROPERTY IMPORTED_LOCATION 147 "${ET_BUILD_DIR_PATH}/libexecutorch.a" 148) 149 150add_library(executorch_core STATIC IMPORTED) 151set_property( 152 TARGET executorch_core 153 PROPERTY IMPORTED_LOCATION "${ET_BUILD_DIR_PATH}/libexecutorch_core.a" 154) 155target_link_libraries(executorch INTERFACE executorch_core) 156 157add_library(executorch_delegate_ethos_u STATIC IMPORTED) 158set_property( 159 TARGET executorch_delegate_ethos_u 160 PROPERTY IMPORTED_LOCATION 161 "${ET_BUILD_DIR_PATH}/backends/arm/libexecutorch_delegate_ethos_u.a" 162) 163 164add_library(portable_ops_lib STATIC IMPORTED) 165set_property( 166 TARGET portable_ops_lib 167 PROPERTY IMPORTED_LOCATION 168 "${ET_BUILD_DIR_PATH}/examples/arm/libarm_portable_ops_lib.a" 169) 170add_library(portable_kernels STATIC IMPORTED) 171set_property( 172 TARGET portable_kernels 173 PROPERTY IMPORTED_LOCATION 174 "${ET_BUILD_DIR_PATH}/kernels/portable/libportable_kernels.a" 175) 176 177add_library(quantized_ops_lib STATIC IMPORTED) 178set_property( 179 TARGET quantized_ops_lib 180 PROPERTY IMPORTED_LOCATION 181 "${ET_BUILD_DIR_PATH}/kernels/quantized/libquantized_ops_lib.a" 182) 183add_library(quantized_kernels STATIC IMPORTED) 184set_property( 185 TARGET quantized_kernels 186 PROPERTY IMPORTED_LOCATION 187 "${ET_BUILD_DIR_PATH}/kernels/quantized/libquantized_kernels.a" 188) 189 190add_library(extension_runner_util STATIC IMPORTED) 191set_property( 192 TARGET extension_runner_util 193 PROPERTY 194 IMPORTED_LOCATION 195 "${ET_BUILD_DIR_PATH}/extension/runner_util/libextension_runner_util.a" 196) 197 198# Convert pte to header 199if(NOT ${SEMIHOSTING}) 200 add_custom_target( 201 gen_model_header DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/model_pte.h 202 ) 203 204 add_custom_command( 205 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/model_pte.h 206 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/pte_to_header.py --pte 207 ${ET_PTE_FILE_PATH} --outdir ${CMAKE_CURRENT_BINARY_DIR} 208 DEPENDS ${ET_PTE_FILE_PATH} 209 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 210 ) 211endif() 212 213# The arm_executor_runner executable 214add_executable(arm_executor_runner) 215 216target_sources( 217 arm_executor_runner PRIVATE arm_executor_runner.cpp arm_perf_monitor.cpp 218) 219 220# Include the target's bare-metal linker script 221ethosu_eval_link_options(arm_executor_runner) 222 223# Need whole-archive to ensure C++ ctor's are called - this may be wasteful for 224# bin size as we link in a number of other symbols 225target_link_libraries( 226 arm_executor_runner 227 extension_runner_util 228 ethosu_target_init 229 executorch 230 "-Wl,--whole-archive" 231 executorch_delegate_ethos_u 232 quantized_ops_lib 233 portable_ops_lib 234 quantized_kernels 235 portable_kernels 236 "-Wl,--no-whole-archive" 237) 238 239# ET headers and generated headers includes 240target_include_directories( 241 arm_executor_runner PRIVATE ${ET_INCLUDE_PATH} ${CMAKE_CURRENT_BINARY_DIR} 242) 243 244if(SEMIHOSTING) 245 target_compile_definitions(arm_executor_runner PUBLIC SEMIHOSTING) 246else() 247 add_dependencies(arm_executor_runner gen_model_header) 248endif() 249 250if(ET_ARM_BAREMETAL_METHOD_ALLOCATOR_POOL_SIZE) 251 target_compile_definitions(arm_executor_runner PUBLIC ET_ARM_BAREMETAL_METHOD_ALLOCATOR_POOL_SIZE=${ET_ARM_BAREMETAL_METHOD_ALLOCATOR_POOL_SIZE}) 252endif() 253 254if(ET_ARM_BAREMETAL_TEMP_ALLOCATOR_POOL_SIZE) 255 target_compile_definitions(arm_executor_runner PUBLIC ET_ARM_BAREMETAL_TEMP_ALLOCATOR_POOL_SIZE=${ET_ARM_BAREMETAL_TEMP_ALLOCATOR_POOL_SIZE}) 256endif() 257 258# Fixup compilation of retarget.c 259if(SEMIHOSTING) 260 # Remove this when MLBEDSW-8910 is closed. 261 set_source_files_properties( 262 ${ETHOS_SDK_PATH}/core_platform/targets/${TARGET_BOARD}/retarget.c 263 PROPERTIES HEADER_FILE_ONLY TRUE 264 ) 265endif() 266