blob: 9fc8aad552ea5be5abbccb58511c7a361c4b3356 [file] [log] [blame]
Jens Elofssona5e90fd2021-06-01 19:06:30 +02001#
Måns Nilssonf309c202024-03-05 11:37:00 +01002# SPDX-FileCopyrightText: Copyright 2021-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
Jens Elofssona5e90fd2021-06-01 19:06:30 +02003#
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 Jonssond55ecdc2021-10-27 17:07:04 +020019add_library(tflu STATIC)
Jens Elofssona5e90fd2021-06-01 19:06:30 +020020
Måns Nilssonf309c202024-03-05 11:37:00 +010021# Tensorflow Lite Micro (TFLM) build variables.
22set(TFLM_PATH "${TENSORFLOW_PATH}/tensorflow/lite/micro")
23set(TFLM_BUILD_TYPE "release" CACHE STRING "TFLM build type, can be release, release_with_logs or debug")
24set(TFLM_OPTIMIZATION_LEVEL "-O2" CACHE STRING "TFLM kernel optimization level")
25set(TFLM_OPTIMIZE_KERNELS_FOR "speed" CACHE STRING "TFLM kernel implementation optimization type, can be speed or size")
Jens Elofssona5e90fd2021-06-01 19:06:30 +020026
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +020027#############################################################################
28# Helpers
29#############################################################################
Jens Elofssona5e90fd2021-06-01 19:06:30 +020030
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +020031include(FetchContent)
32
33# Download third party
34macro(download_third_party target)
Kristofer Jonssoneb1d7112022-11-17 14:31:51 +010035 cmake_policy(PUSH)
36
37 if(POLICY CMP0135)
38 cmake_policy(SET CMP0135 NEW)
39 endif()
40
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +020041 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 Elofssona5e90fd2021-06-01 19:06:30 +020054 endif()
Kristofer Jonssoneb1d7112022-11-17 14:31:51 +010055
56 cmake_policy(POP)
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +020057endmacro()
Jens Elofssona5e90fd2021-06-01 19:06:30 +020058
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +020059function(tensorflow_source_exists RESULT TARGET SOURCE)
60 get_target_property(SOURCES ${TARGET} SOURCES)
Jens Elofssona5e90fd2021-06-01 19:06:30 +020061
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +020062 # 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 Elofssona5e90fd2021-06-01 19:06:30 +020066
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +020067 # 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)
75endfunction()
76
77function(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()
93endfunction()
94
95#############################################################################
96# Download thirdparty
97#############################################################################
98
99# Flatbuffers
100# Synch revision with 'tensorflow/lite/micro/tools/make/flatbuffers_download.sh'
101download_third_party(tensorflow-flatbuffers
Adrian Lundellba171782023-10-23 10:37:16 +0200102 URL "https://github.com/google/flatbuffers/archive/v23.5.26.zip"
103 URL_MD5 e87e8acd8e2d53653387ad78720316e2)
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200104
105target_include_directories(tflu PUBLIC
106 ${tensorflow-flatbuffers_SOURCE_DIR}/include)
107
108target_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'
113download_third_party(tensorflow-gemlowp
114 URL "https://github.com/google/gemmlowp/archive/719139ce755a0f31cbf1c37f7f98adcc7fc9f425.zip"
115 URL_MD5 7e8191b24853d75de2af87622ad293ba)
116
117target_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'
122download_third_party(tensorflow-ruy
123 URL "https://github.com/google/ruy/archive/d37128311b445e758136b8602d1bbd2a755e115d.zip"
124 URL_MD5 abf7a91eb90d195f016ebe0be885bb6e)
125
126target_include_directories(tflu PUBLIC
127 ${tensorflow-ruy_SOURCE_DIR})
128
129#############################################################################
130# CMSIS-NN
131#############################################################################
132
Kristofer Jonsson02856bd2021-11-24 14:08:10 +0100133if (NOT ${CORE_SOFTWARE_ACCELERATOR} STREQUAL "CPU")
Kristofer Jonssond0a08822022-10-10 12:36:03 +0200134 add_subdirectory(${CMSIS_NN_PATH} cmsis_nn)
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200135
Kristofer Jonsson02856bd2021-11-24 14:08:10 +0100136 target_compile_options(cmsis-nn PRIVATE
Måns Nilssonf309c202024-03-05 11:37:00 +0100137 ${TFLM_OPTIMIZATION_LEVEL})
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200138
Kristofer Jonsson02856bd2021-11-24 14:08:10 +0100139 tensorflow_target_sources_glob(tflu GLOB TRUE
Måns Nilssonf309c202024-03-05 11:37:00 +0100140 ${TFLM_PATH}/kernels/cmsis_nn/*.cc)
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200141
Kristofer Jonsson02856bd2021-11-24 14:08:10 +0100142 target_include_directories(tflu PUBLIC
Kristofer Jonssond0a08822022-10-10 12:36:03 +0200143 ${CMSIS_NN_PATH})
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200144
Kristofer Jonsson02856bd2021-11-24 14:08:10 +0100145 target_compile_definitions(tflu PUBLIC
146 CMSIS_NN)
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200147
Kristofer Jonsson02856bd2021-11-24 14:08:10 +0100148 target_link_libraries(tflu PUBLIC
149 cmsis-nn)
150endif()
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200151
152#############################################################################
153# Ethos-U
154#############################################################################
155
156if(TARGET ethosu_core_driver)
157 tensorflow_target_sources_glob(tflu GLOB TRUE
Måns Nilssonf309c202024-03-05 11:37:00 +0100158 ${TFLM_PATH}/kernels/ethos_u/*.cc)
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200159
160 target_link_libraries(tflu PUBLIC
161 ethosu_core_driver)
Jens Elofssona5e90fd2021-06-01 19:06:30 +0200162endif()
163
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200164#############################################################################
165# Cortex-M generic
166#############################################################################
167
168tensorflow_target_sources_glob(tflu GLOB TRUE
Måns Nilssonf309c202024-03-05 11:37:00 +0100169 ${TFLM_PATH}/cortex_m_generic/*.cc)
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200170
171target_include_directories(tflu PRIVATE
Måns Nilssonf309c202024-03-05 11:37:00 +0100172 ${TFLM_PATH}/cortex_m_generic)
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200173
Måns Nilsson84c47882022-04-11 14:00:04 +0200174# For DWT/PMU counters
175target_link_libraries(tflu PRIVATE cmsis_device)
176target_compile_definitions(tflu PRIVATE ${ARM_CPU})
177
Måns Nilssone48fa7a2022-08-02 14:52:21 +0200178if(("${ARM_CPU}" STREQUAL "ARMCM55") OR ("${ARM_CPU}" STREQUAL "ARMCM85"))
Måns Nilsson84c47882022-04-11 14:00:04 +0200179 target_compile_definitions(tflu PRIVATE
180 ARM_MODEL_USE_PMU_COUNTERS)
181endif()
182
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200183#############################################################################
184# Tensorflow micro lite
185#############################################################################
186
187tensorflow_target_sources_glob(tflu GLOB TRUE
Måns Nilssonf309c202024-03-05 11:37:00 +0100188 ${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 Jonssond55ecdc2021-10-27 17:07:04 +0200193
194tensorflow_target_sources_glob(tflu GLOB_RECURSE FALSE
Måns Nilssonf309c202024-03-05 11:37:00 +0100195 ${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 Jonssond55ecdc2021-10-27 17:07:04 +0200200
201target_include_directories(tflu PUBLIC
Jens Elofssona5e90fd2021-06-01 19:06:30 +0200202 ${TENSORFLOW_PATH})
Jens Elofssona5e90fd2021-06-01 19:06:30 +0200203
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200204target_compile_definitions(tflu PUBLIC
205 TF_LITE_STATIC_MEMORY
Måns Nilssonf309c202024-03-05 11:37:00 +0100206 $<$<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 Elofssona5e90fd2021-06-01 19:06:30 +0200210
Kristofer Jonssone4f09e52021-11-26 16:13:58 +0100211target_compile_options(tflu
212 PRIVATE
Måns Nilssonf309c202024-03-05 11:37:00 +0100213 ${TFLM_OPTIMIZATION_LEVEL}
Kristofer Jonssone4f09e52021-11-26 16:13:58 +0100214 -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 Elofssona5e90fd2021-06-01 19:06:30 +0200239
240# Install libraries and header files
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200241install(TARGETS tflu DESTINATION "lib")