Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 1 | # |
Måns Nilsson | f309c20 | 2024-03-05 11:37:00 +0100 | [diff] [blame] | 2 | # SPDX-FileCopyrightText: Copyright 2021-2024 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the License); you may |
| 7 | # not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 19 | add_library(tflu STATIC) |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 20 | |
Måns Nilsson | f309c20 | 2024-03-05 11:37:00 +0100 | [diff] [blame] | 21 | # Tensorflow Lite Micro (TFLM) build variables. |
| 22 | set(TFLM_PATH "${TENSORFLOW_PATH}/tensorflow/lite/micro") |
| 23 | set(TFLM_BUILD_TYPE "release" CACHE STRING "TFLM build type, can be release, release_with_logs or debug") |
| 24 | set(TFLM_OPTIMIZATION_LEVEL "-O2" CACHE STRING "TFLM kernel optimization level") |
| 25 | set(TFLM_OPTIMIZE_KERNELS_FOR "speed" CACHE STRING "TFLM kernel implementation optimization type, can be speed or size") |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 26 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 27 | ############################################################################# |
| 28 | # Helpers |
| 29 | ############################################################################# |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 30 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 31 | include(FetchContent) |
| 32 | |
| 33 | # Download third party |
| 34 | macro(download_third_party target) |
Kristofer Jonsson | eb1d711 | 2022-11-17 14:31:51 +0100 | [diff] [blame] | 35 | cmake_policy(PUSH) |
| 36 | |
| 37 | if(POLICY CMP0135) |
| 38 | cmake_policy(SET CMP0135 NEW) |
| 39 | endif() |
| 40 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 41 | cmake_parse_arguments(DOWNLOAD "" "URL;URL_MD5;SOURCE_DIR" "" ${ARGN}) |
| 42 | |
| 43 | message("Downloading ${DOWNLOAD_URL}") |
| 44 | |
| 45 | FetchContent_Declare(${target} |
| 46 | URL ${DOWNLOAD_URL} |
| 47 | URL_MD5 ${DOWNLOAD_MD5} |
| 48 | SOURCE_DIR ${DOWNLOAD_SOURCE_DIR} |
| 49 | ${PATCH_COMMAND}) |
| 50 | |
| 51 | FetchContent_GetProperties(${target}) |
| 52 | if (NOT ${target}_POPULATED) |
| 53 | FetchContent_Populate(${target}) |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 54 | endif() |
Kristofer Jonsson | eb1d711 | 2022-11-17 14:31:51 +0100 | [diff] [blame] | 55 | |
| 56 | cmake_policy(POP) |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 57 | endmacro() |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 58 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 59 | function(tensorflow_source_exists RESULT TARGET SOURCE) |
| 60 | get_target_property(SOURCES ${TARGET} SOURCES) |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 61 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 62 | # Loop over source files already added to this target |
| 63 | foreach(TMP ${SOURCES}) |
| 64 | get_filename_component(SOURCE_NAME ${SOURCE} NAME) |
| 65 | get_filename_component(TMP_NAME ${TMP} NAME) |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 66 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 67 | # Check if file already exists |
| 68 | if (${SOURCE_NAME} STREQUAL ${TMP_NAME}) |
| 69 | set(${RESULT} TRUE PARENT_SCOPE) |
| 70 | return() |
| 71 | endif() |
| 72 | endforeach() |
| 73 | |
| 74 | set(${RESULT} FALSE PARENT_SCOPE) |
| 75 | endfunction() |
| 76 | |
| 77 | function(tensorflow_target_sources_glob TARGET GLOB UNIQUE) |
| 78 | foreach (EXPR ${ARGN}) |
| 79 | # Find files matching globbing expression |
| 80 | file(${GLOB} SOURCES ${EXPR}) |
| 81 | |
| 82 | # Remove tests |
| 83 | list(FILTER SOURCES EXCLUDE REGEX ".*_test\.cc") |
| 84 | |
| 85 | # Add files to target |
| 86 | foreach(SOURCE ${SOURCES}) |
| 87 | tensorflow_source_exists(SOURCE_EXISTS ${TARGET} ${SOURCE}) |
| 88 | if (NOT ${UNIQUE} OR NOT ${SOURCE_EXISTS}) |
| 89 | target_sources(${TARGET} PRIVATE ${SOURCE}) |
| 90 | endif() |
| 91 | endforeach() |
| 92 | endforeach() |
| 93 | endfunction() |
| 94 | |
| 95 | ############################################################################# |
| 96 | # Download thirdparty |
| 97 | ############################################################################# |
| 98 | |
| 99 | # Flatbuffers |
| 100 | # Synch revision with 'tensorflow/lite/micro/tools/make/flatbuffers_download.sh' |
| 101 | download_third_party(tensorflow-flatbuffers |
Adrian Lundell | ba17178 | 2023-10-23 10:37:16 +0200 | [diff] [blame] | 102 | URL "https://github.com/google/flatbuffers/archive/v23.5.26.zip" |
| 103 | URL_MD5 e87e8acd8e2d53653387ad78720316e2) |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 104 | |
| 105 | target_include_directories(tflu PUBLIC |
| 106 | ${tensorflow-flatbuffers_SOURCE_DIR}/include) |
| 107 | |
| 108 | target_compile_definitions(tflu PUBLIC |
| 109 | FLATBUFFERS_LOCALE_INDEPENDENT=0) |
| 110 | |
| 111 | # Gemlowp |
| 112 | # Synch revision with 'tensorflow/lite/micro/tools/make/third_party_downloads.inc' |
| 113 | download_third_party(tensorflow-gemlowp |
| 114 | URL "https://github.com/google/gemmlowp/archive/719139ce755a0f31cbf1c37f7f98adcc7fc9f425.zip" |
| 115 | URL_MD5 7e8191b24853d75de2af87622ad293ba) |
| 116 | |
| 117 | target_include_directories(tflu PUBLIC |
| 118 | ${tensorflow-gemlowp_SOURCE_DIR}) |
| 119 | |
| 120 | # Ruy |
| 121 | # Synch revision with 'tensorflow/lite/micro/tools/make/third_party_downloads.inc' |
| 122 | download_third_party(tensorflow-ruy |
| 123 | URL "https://github.com/google/ruy/archive/d37128311b445e758136b8602d1bbd2a755e115d.zip" |
| 124 | URL_MD5 abf7a91eb90d195f016ebe0be885bb6e) |
| 125 | |
| 126 | target_include_directories(tflu PUBLIC |
| 127 | ${tensorflow-ruy_SOURCE_DIR}) |
| 128 | |
| 129 | ############################################################################# |
| 130 | # CMSIS-NN |
| 131 | ############################################################################# |
| 132 | |
Kristofer Jonsson | 02856bd | 2021-11-24 14:08:10 +0100 | [diff] [blame] | 133 | if (NOT ${CORE_SOFTWARE_ACCELERATOR} STREQUAL "CPU") |
Kristofer Jonsson | d0a0882 | 2022-10-10 12:36:03 +0200 | [diff] [blame] | 134 | add_subdirectory(${CMSIS_NN_PATH} cmsis_nn) |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 135 | |
Kristofer Jonsson | 02856bd | 2021-11-24 14:08:10 +0100 | [diff] [blame] | 136 | target_compile_options(cmsis-nn PRIVATE |
Måns Nilsson | f309c20 | 2024-03-05 11:37:00 +0100 | [diff] [blame] | 137 | ${TFLM_OPTIMIZATION_LEVEL}) |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 138 | |
Kristofer Jonsson | 02856bd | 2021-11-24 14:08:10 +0100 | [diff] [blame] | 139 | tensorflow_target_sources_glob(tflu GLOB TRUE |
Måns Nilsson | f309c20 | 2024-03-05 11:37:00 +0100 | [diff] [blame] | 140 | ${TFLM_PATH}/kernels/cmsis_nn/*.cc) |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 141 | |
Kristofer Jonsson | 02856bd | 2021-11-24 14:08:10 +0100 | [diff] [blame] | 142 | target_include_directories(tflu PUBLIC |
Kristofer Jonsson | d0a0882 | 2022-10-10 12:36:03 +0200 | [diff] [blame] | 143 | ${CMSIS_NN_PATH}) |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 144 | |
Kristofer Jonsson | 02856bd | 2021-11-24 14:08:10 +0100 | [diff] [blame] | 145 | target_compile_definitions(tflu PUBLIC |
| 146 | CMSIS_NN) |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 147 | |
Kristofer Jonsson | 02856bd | 2021-11-24 14:08:10 +0100 | [diff] [blame] | 148 | target_link_libraries(tflu PUBLIC |
| 149 | cmsis-nn) |
| 150 | endif() |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 151 | |
| 152 | ############################################################################# |
| 153 | # Ethos-U |
| 154 | ############################################################################# |
| 155 | |
| 156 | if(TARGET ethosu_core_driver) |
| 157 | tensorflow_target_sources_glob(tflu GLOB TRUE |
Måns Nilsson | f309c20 | 2024-03-05 11:37:00 +0100 | [diff] [blame] | 158 | ${TFLM_PATH}/kernels/ethos_u/*.cc) |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 159 | |
| 160 | target_link_libraries(tflu PUBLIC |
| 161 | ethosu_core_driver) |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 162 | endif() |
| 163 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 164 | ############################################################################# |
| 165 | # Cortex-M generic |
| 166 | ############################################################################# |
| 167 | |
| 168 | tensorflow_target_sources_glob(tflu GLOB TRUE |
Måns Nilsson | f309c20 | 2024-03-05 11:37:00 +0100 | [diff] [blame] | 169 | ${TFLM_PATH}/cortex_m_generic/*.cc) |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 170 | |
| 171 | target_include_directories(tflu PRIVATE |
Måns Nilsson | f309c20 | 2024-03-05 11:37:00 +0100 | [diff] [blame] | 172 | ${TFLM_PATH}/cortex_m_generic) |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 173 | |
Måns Nilsson | 84c4788 | 2022-04-11 14:00:04 +0200 | [diff] [blame] | 174 | # For DWT/PMU counters |
| 175 | target_link_libraries(tflu PRIVATE cmsis_device) |
| 176 | target_compile_definitions(tflu PRIVATE ${ARM_CPU}) |
| 177 | |
Måns Nilsson | e48fa7a | 2022-08-02 14:52:21 +0200 | [diff] [blame] | 178 | if(("${ARM_CPU}" STREQUAL "ARMCM55") OR ("${ARM_CPU}" STREQUAL "ARMCM85")) |
Måns Nilsson | 84c4788 | 2022-04-11 14:00:04 +0200 | [diff] [blame] | 179 | target_compile_definitions(tflu PRIVATE |
| 180 | ARM_MODEL_USE_PMU_COUNTERS) |
| 181 | endif() |
| 182 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 183 | ############################################################################# |
| 184 | # Tensorflow micro lite |
| 185 | ############################################################################# |
| 186 | |
| 187 | tensorflow_target_sources_glob(tflu GLOB TRUE |
Måns Nilsson | f309c20 | 2024-03-05 11:37:00 +0100 | [diff] [blame] | 188 | ${TFLM_PATH}/*.cc |
| 189 | ${TFLM_PATH}/arena_allocator/*.cc |
| 190 | ${TFLM_PATH}/memory_planner/*.cc |
| 191 | ${TFLM_PATH}/kernels/*.cc |
| 192 | ${TFLM_PATH}/tflite_bridge/*.cc) |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 193 | |
| 194 | tensorflow_target_sources_glob(tflu GLOB_RECURSE FALSE |
Måns Nilsson | f309c20 | 2024-03-05 11:37:00 +0100 | [diff] [blame] | 195 | ${TFLM_PATH}/../c/*.cc |
| 196 | ${TFLM_PATH}/../core/*.cc |
| 197 | ${TFLM_PATH}/../core/api/*.cc |
| 198 | ${TFLM_PATH}/../kernels/*.cc |
| 199 | ${TFLM_PATH}/../schema/*.cc) |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 200 | |
| 201 | target_include_directories(tflu PUBLIC |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 202 | ${TENSORFLOW_PATH}) |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 203 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 204 | target_compile_definitions(tflu PUBLIC |
| 205 | TF_LITE_STATIC_MEMORY |
Måns Nilsson | f309c20 | 2024-03-05 11:37:00 +0100 | [diff] [blame] | 206 | $<$<STREQUAL:${TFLM_BUILD_TYPE},release>:NDEBUG;TF_LITE_STRIP_ERROR_STRINGS> |
| 207 | $<$<STREQUAL:${TFLM_BUILD_TYPE},release_with_logs>:NDEBUG> |
| 208 | $<$<STREQUAL:${TFLM_OPTIMIZE_KERNELS_FOR},speed>:KERNELS_OPTIMIZED_FOR_SPEED> |
| 209 | $<$<STREQUAL:${TFLM_OPTIMIZE_KERNELS_FOR},size>:KERNELS_OPTIMIZED_FOR_SIZE>) |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 210 | |
Kristofer Jonsson | e4f09e5 | 2021-11-26 16:13:58 +0100 | [diff] [blame] | 211 | target_compile_options(tflu |
| 212 | PRIVATE |
Måns Nilsson | f309c20 | 2024-03-05 11:37:00 +0100 | [diff] [blame] | 213 | ${TFLM_OPTIMIZATION_LEVEL} |
Kristofer Jonsson | e4f09e5 | 2021-11-26 16:13:58 +0100 | [diff] [blame] | 214 | -fno-unwind-tables |
| 215 | -ffunction-sections |
| 216 | -fdata-sections |
| 217 | -fmessage-length=0 |
| 218 | -funsigned-char |
| 219 | "$<$<COMPILE_LANGUAGE:CXX>:-fno-rtti;-fno-exceptions;-fno-threadsafe-statics>" |
| 220 | |
| 221 | -Wall |
| 222 | -Wextra |
| 223 | |
| 224 | -Wdouble-promotion |
| 225 | -Wmissing-field-initializers |
| 226 | -Wshadow |
| 227 | -Wstrict-aliasing |
| 228 | -Wswitch |
| 229 | -Wunused-variable |
| 230 | -Wunused-function |
| 231 | -Wvla |
| 232 | |
| 233 | PUBLIC |
| 234 | -Wno-cast-align |
| 235 | -Wno-null-dereference |
| 236 | -Wno-unused-parameter |
| 237 | -Wno-switch-default |
| 238 | ) |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 239 | |
| 240 | # Install libraries and header files |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 241 | install(TARGETS tflu DESTINATION "lib") |