blob: 5317bfafe8ada03d67261508820e0dcd3b941b00 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001#----------------------------------------------------------------------------
2# Copyright (c) 2021 Arm Limited. All rights reserved.
3# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#----------------------------------------------------------------------------
17# minimum version of cmake = 3.15 for legit reason:
18# armclang support doesn't work work in previous releases
19cmake_minimum_required(VERSION 3.15.0)
20
21# Build in release mode by default
22if (NOT CMAKE_BUILD_TYPE STREQUAL Debug)
23 set(CMAKE_BUILD_TYPE Release CACHE INTERNAL "")
24endif()
25
26message(STATUS "Build type is set to ${CMAKE_BUILD_TYPE}")
27
28# Set language standards. TensorFlow Lite requires
29# std=c++11.
30set(CMAKE_C_STANDARD 99)
31set(CMAKE_CXX_STANDARD 11)
32
33# Make the standard a requirement => prevent fallback to previous
34# supported standard
35set(CMAKE_C_STANDARD_REQUIRED ON)
36set(CMAKE_CXX_STANDARD_REQUIRED ON)
37
38# We want to pass standard C/C++ flags, without gnu extensions
39set(CMAKE_C_EXTENSIONS OFF)
40set(CMAKE_CXX_EXTENSIONS OFF)
41
42project(arm_ethos_u55_eval
43 VERSION 21.03
44 DESCRIPTION "ARM Ethos-U55 Evaluation application for MPS3 FPGA Prototyping Board and FastModel")
45
46add_compile_definitions(PRJ_VER_STR="${PROJECT_VERSION}")
47add_compile_definitions(PRJ_DES_STR="${PROJECT_DESCRIPTION}")
48
49set(CMAKE_SCRIPTS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/scripts/cmake/)
50set(DOWNLOAD_DEP_DIR ${CMAKE_BINARY_DIR}/dependencies)
51
52include(${CMAKE_SCRIPTS_DIR}/source_gen_utils.cmake)
53include(${CMAKE_SCRIPTS_DIR}/util_functions.cmake)
54
55if (${CMAKE_BINARY_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
56 message(FATAL_ERROR "Source and build are in the same directory")
57else()
58 message(STATUS "Source directory: ${CMAKE_SOURCE_DIR}")
59 message(STATUS "Binary directory: ${CMAKE_BINARY_DIR}")
60endif()
61
62USER_OPTION(LOG_LEVEL "Log level for the application"
63 LOG_LEVEL_INFO
64 STRING)
65
66USER_OPTION(TENSORFLOW_SRC_PATH "Path to the root of the tensor flow directory"
67 "${CMAKE_CURRENT_SOURCE_DIR}/dependencies/tensorflow"
68 PATH)
69
70USER_OPTION(TARGET_PLATFORM "Target platform to execute evaluation application: mps3, simple_platform, native"
71 mps3
72 STRING)
73
74USER_OPTION(TARGET_SUBSYSTEM "Specify platform target subsystem: sse-200, sse-300 or none"
75 sse-300
76 STRING)
77
78USER_OPTION(ETHOS_U55_ENABLED "Select if Ethos-U55 is available for the platform and subsystem"
79 ON
80 BOOL)
81
82USER_OPTION(USE_CASE_BUILD "Optional. Defines the use-case to build from the available sources. By default, all use-cases are built."
83 all
84 STRING)
85
86USER_OPTION(CPU_PROFILE_ENABLED "Output CPU performance profiling information. Should be used only for MPS3 board."
87 OFF
88 BOOL)
89
90if (TARGET_PLATFORM STREQUAL mps3)
91 message(STATUS "Platform: MPS3 FPGA Prototyping Board or SSE-XXX FVP")
92elseif (TARGET_PLATFORM STREQUAL simple_platform)
93 message(STATUS "Platform: Simple platform within minimal peripherals")
94elseif (TARGET_PLATFORM STREQUAL native)
95 message(STATUS "Platform: Native (Linux based x86_64/aarch64 system)")
96else ()
97 message(FATAL_ERROR "Invalid platform specified: ${TARGET_PLATFORM}")
98endif ()
99
100enforce_compiler_version()
101setup_source_generator()
102
103set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
104set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
105set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
106set(SRC_PATH ${CMAKE_CURRENT_SOURCE_DIR}/source)
Isabella Gottardi85209832021-04-20 14:08:52 +0100107set(TEST_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/tests)
108list(APPEND USE_CASES_TESTS_SEARCH_DIR_LIST ${TEST_SRCS}/use_case)
alexander3c798932021-03-26 21:42:19 +0000109
110if (CPU_PROFILE_ENABLED)
111 set(PROFILING_OPT "${PROFILING_OPT} -DCPU_PROFILE_ENABLED")
112endif()
113
114# Include platform specific sources
115if (TARGET_PLATFORM STREQUAL native)
116 set(PLATFORM_SOURCES_CMAKE_FILE ${CMAKE_SCRIPTS_DIR}/${TARGET_PLATFORM}-sources.cmake)
117else ()
118 set(PLATFORM_SOURCES_CMAKE_FILE ${CMAKE_SCRIPTS_DIR}/bare-metal-sources.cmake)
119
120 USER_OPTION(CMSIS_SRC_PATH
121 "Path to CMSIS-5 sources"
122 "${CMAKE_CURRENT_SOURCE_DIR}/dependencies/cmsis"
123 PATH
124 )
125
126 if (CMAKE_BUILD_TYPE STREQUAL Debug AND CMAKE_CXX_COMPILER_ID STREQUAL ARMClang)
127 USER_OPTION(ARMCLANG_DEBUG_DWARF_LEVEL
128 "Dwarf conformance level for armclang toolchain"
129 "4" # Default = 4 (Arm-DS etc). For model debugger specify "3"
130 STRING
131 )
132 elseif (DEFINED ARMCLANG_DEBUG_DWARF_LEVEL)
133 message(WARNING "ARMCLANG_DEBUG_DWARF_LEVEL definition is unsupported"
134 "within current configuration. Removing definition...")
135 unset(ARMCLANG_DEBUG_DWARF_LEVEL CACHE)
136 endif()
137
138endif ()
139message(STATUS "Including ${PLATFORM_SOURCES_CMAKE_FILE}")
140include(${PLATFORM_SOURCES_CMAKE_FILE})
141
142if (${CMAKE_CROSSCOMPILING})
143 enable_language(ASM)
144
145 # For non-native builds, we build with CMSIS-DSP support.
146 include(${CMAKE_SCRIPTS_DIR}/cmsis-dsp.cmake)
147
148 # All CMSIS headers to be used:
149 set(CMSIS_HEADERS
150 ${CMSIS_DSP_INC_DIR}
151 ${CMSIS_CORE_INC_DIR}
152 ${CMSIS_SRC_PATH}/Device/ARM/ARMCM55/Include
153 ${CMSIS_SRC_PATH}/Device/ARM/ARMCM55/Include/Template)
154endif ()
155
156# If we need NPU libraries:
157if (ETHOS_U55_ENABLED)
158
159 message(STATUS "Using ARM Ethos-U55 - adding core-driver and timing-adapter-driver includes and libraries")
160 USER_OPTION(ETHOS_U55_TIMING_ADAPTER_SRC_PATH
161 "Path to Ethos-U55 timing adapter sources"
162 "${CMAKE_CURRENT_SOURCE_DIR}/dependencies/core-software/drivers/timing_adapter"
163 PATH
164 )
165
166 USER_OPTION(ETHOS_U55_DRIVER_SRC_PATH
167 "Path to Ethos-U55 core driver sources"
168 "${CMAKE_CURRENT_SOURCE_DIR}/dependencies/core_driver"
169 PATH
170 )
171
172 include_directories("${ETHOS_U55_TIMING_ADAPTER_SRC_PATH}/include/")
173
174 add_subdirectory("${ETHOS_U55_TIMING_ADAPTER_SRC_PATH}" ${CMAKE_BINARY_DIR}/timing-adapter)
175
176 set(ETHOSU_INCLUDES ${ETHOS_U55_TIMING_ADAPTER_SRC_PATH}/include
177 ${ETHOS_U55_DRIVER_SRC_PATH}/include)
178
179 list(APPEND ETHOS_U55_LIBS ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libtiming_adapter.a)
180endif ()
181
182include(${CMAKE_SCRIPTS_DIR}/tensorflow.cmake)
183
184set(DEP_TENSORFLOW_LITE_MICRO_SUB_DIR ${TENSORFLOW_SRC_PATH}/tensorflow/lite/micro)
185set(DEP_TENSORFLOW_LITE_MICRO_MAKE_DIR ${DEP_TENSORFLOW_LITE_MICRO_SUB_DIR}/tools/make/targets)
186set(DEP_FLATBUF_INCLUDE ${DEP_TENSORFLOW_LITE_MICRO_SUB_DIR}/tools/make/downloads/flatbuffers/include)
187
188set(TENSORFLOW_LIBRARY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${TENSORFLOW_LITE_MICRO_PLATFORM_LIB_NAME})
189
190set(DEP_TF_INCLUDE_DIRS
191 ${TENSORFLOW_SRC_PATH}
192 ${DEP_TENSORFLOW_LITE_MICRO_SUB_DIR}
193 ${ETHOSU_INCLUDES}
194 ${CMSIS_HEADERS}
195 )
196
197## All TPIP includes
198set(DEP_RUNTIME_INCLUDE_DIRS
199 ${DEP_TF_INCLUDE_DIRS}
200 ${DEP_FLATBUF_INCLUDE}
201 )
202
203# Our entry point into tensorflow world:
204file(GLOB_RECURSE SRC_TENSORFLOW_LITE_MICRO
205 ${SRC_PATH}/application/tensorflow-lite-micro/**/*.cc
206 ${SRC_PATH}/application/tensorflow-lite-micro/*.cc
207 )
208
209set(HAL_DIR ${SRC_PATH}/application/hal)
210
211# HAL API sources
212file(GLOB_RECURSE SRC_HAL
213 "${HAL_DIR}/hal.c"
214 )
215
216# Set platform specific HAL sources; these should be provided
217# by each platform's cmake include file
218list(APPEND SRC_HAL ${SRC_PLAT_HAL})
219
220# Include directories:
221set(APPLICATION_INCLUDE_DIRS
222 ${HAL_DIR}/include
223 ${SRC_PATH}/application/tensorflow-lite-micro/include
224 ${SRC_PATH}/application/main/include
225 ${PLAT_INCLUDE_DIRS}
226 )
227
228file(GLOB_RECURSE SRC_APPLICATION
229 "${SRC_PATH}/application/main/*.cc"
230 "${SRC_PATH}/application/main/*.cpp"
231 "${SRC_PATH}/application/main/*.c"
232 "${SRC_PATH}/application/main/**/*.cc"
233 "${SRC_PATH}/application/main/**/*.cpp"
234 "${SRC_PATH}/application/main/**/*.c"
235 )
236list(FILTER SRC_APPLICATION EXCLUDE REGEX ".*main\\.c.*$")
237
238list(JOIN USE_CASE_BUILD "" USE_CASE_BUILD_STR)
Isabella Gottardi85209832021-04-20 14:08:52 +0100239list(APPEND USE_CASES_SEARCH_DIR_LIST ${SRC_PATH}/use_case)
240message(STATUS "Use-cases source paths: ${USE_CASES_SEARCH_DIR_LIST}.")
alexander3c798932021-03-26 21:42:19 +0000241if (${USE_CASE_BUILD_STR} STREQUAL all)
Isabella Gottardi85209832021-04-20 14:08:52 +0100242 foreach(USE_CASES_SEARCH_DIR ${USE_CASES_SEARCH_DIR_LIST})
243 SUBDIRLIST(USE_CASES_SUBDIRS ${USE_CASES_SEARCH_DIR})
244 list(APPEND USE_CASES ${USE_CASES_SUBDIRS})
245 endforeach()
alexander3c798932021-03-26 21:42:19 +0000246else()
247 set(USE_CASES ${USE_CASE_BUILD})
248endif()
249
alexander3c798932021-03-26 21:42:19 +0000250if (NOT ${CMAKE_CROSSCOMPILING})
251
252 #Test TPIP
253 set(TEST_TPIP ${DOWNLOAD_DEP_DIR}/test)
254 file(MAKE_DIRECTORY ${TEST_TPIP})
255 set(TEST_TPIP_INCLUDE ${TEST_TPIP}/include)
256 file(MAKE_DIRECTORY ${TEST_TPIP_INCLUDE})
257
258 include(ExternalProject)
259
260 ExternalProject_Add(catch2-headers
261 URL https://github.com/catchorg/Catch2/releases/download/v2.11.1/catch.hpp
262 DOWNLOAD_NO_EXTRACT 1
263 CONFIGURE_COMMAND ""
264 BUILD_COMMAND bash -c "cp -R <DOWNLOAD_DIR>/catch.hpp ${TEST_TPIP_INCLUDE}"
265 INSTALL_COMMAND ""
266 )
267endif ()
268
269message(STATUS "Building use-cases: ${USE_CASES}.")
270foreach(use_case ${USE_CASES})
271
Isabella Gottardi85209832021-04-20 14:08:52 +0100272 set(SRC_USE_CASE "")
273 foreach(USE_CASES_SEARCH_DIR ${USE_CASES_SEARCH_DIR_LIST})
274 if (EXISTS ${USE_CASES_SEARCH_DIR}/${use_case})
275 message(STATUS "Found sources for use-case ${use_case}")
276 set(SRC_USE_CASE ${USE_CASES_SEARCH_DIR})
277 break()
278 endif ()
279 endforeach()
280
281 if (${SRC_USE_CASE} STREQUAL "")
282 message(FATAL_ERROR "Failed to find sources for ${use_case}!")
alexander3c798932021-03-26 21:42:19 +0000283 endif ()
284 # Executable application:
285 set(TARGET_NAME "ethos-u-${use_case}")
286
287 set(DEFAULT_MODEL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/resources/${use_case}/models)
288 set(SRC_GEN_DIR ${CMAKE_BINARY_DIR}/generated/${use_case}/src)
289 set(INC_GEN_DIR ${CMAKE_BINARY_DIR}/generated/${use_case}/include)
290
291 # Remove old files and recreate dirs
292 file(REMOVE_RECURSE ${SRC_GEN_DIR} ${INC_GEN_DIR})
293 file(MAKE_DIRECTORY ${SRC_GEN_DIR} ${INC_GEN_DIR})
294
295 file(GLOB_RECURSE UC_SRC
Isabella Gottardi85209832021-04-20 14:08:52 +0100296 "${SRC_USE_CASE}/${use_case}/src/*.cpp"
297 "${SRC_USE_CASE}/${use_case}/src/*.cc"
298 "${SRC_USE_CASE}/${use_case}/src/*.c"
299 "${SRC_USE_CASE}/${use_case}/src/**/*.cpp"
300 "${SRC_USE_CASE}/${use_case}/src/**/*.cc"
301 "${SRC_USE_CASE}/${use_case}/src/**/*.c"
alexander3c798932021-03-26 21:42:19 +0000302 )
303
304 set(UC_INCLUDE
Isabella Gottardi85209832021-04-20 14:08:52 +0100305 ${SRC_USE_CASE}/${use_case}/include
alexander3c798932021-03-26 21:42:19 +0000306 )
307
308 file(GLOB UC_CMAKE_FILE
Isabella Gottardi85209832021-04-20 14:08:52 +0100309 "${SRC_USE_CASE}/${use_case}/*.cmake"
alexander3c798932021-03-26 21:42:19 +0000310 )
311
312 include(${UC_CMAKE_FILE})
313
314 file(GLOB_RECURSE SRC_GEN
315 "${SRC_GEN_DIR}/*.cc"
316 "${SRC_GEN_DIR}/*.cpp"
317 "${SRC_GEN_DIR}/*.c"
318 )
319
320 set(SRC_MAIN
321 "${SRC_PATH}/application/main/Main.cc"
322 )
323
324 set(UC_LIB_NAME lib${TARGET_NAME})
325
326 # Consolidated application static lib:
327 add_library(${UC_LIB_NAME} STATIC
328 ${SRC_APPLICATION}
329 ${SRC_TENSORFLOW_LITE_MICRO}
330 ${SRC_HAL}
331 ${UC_SRC}
332 ${SRC_GEN}
333 )
334 target_include_directories(${UC_LIB_NAME} PUBLIC
335 ${APPLICATION_INCLUDE_DIRS}
336 ${DEP_RUNTIME_INCLUDE_DIRS}
337 ${UC_INCLUDE}
338 ${INC_GEN_DIR}
339 )
340
341 # Set the activation buffer size
342 target_compile_definitions(${UC_LIB_NAME} PUBLIC
343 "ACTIVATION_BUF_SZ=${${use_case}_ACTIVATION_BUF_SZ}")
344
345 add_dependencies(${UC_LIB_NAME} tensorflow-lite-micro)
346
347 if (${CMAKE_CROSSCOMPILING})
348 # If we are building timing adapter, set the dependency:
349 if (ETHOS_U55_ENABLED)
350 message(STATUS "Adding timing_adapter as a dependency to ${UC_LIB_NAME}")
351 add_dependencies(${UC_LIB_NAME} timing_adapter)
352 endif()
353
354 # If building with CMSIS-DSP support:
355 if (DEFINED CMSIS_DSP_TARGET)
356 message(STATUS "Adding ${CMSIS_DSP_TARGET} as a dependency to ${UC_LIB_NAME}")
357 add_dependencies(${UC_LIB_NAME} ${CMSIS_DSP_TARGET})
358 endif()
359 endif()
360
361 target_link_libraries(${UC_LIB_NAME} PUBLIC
362 ${TENSORFLOW_LIBRARY}
363 $<$<BOOL:${ETHOS_U55_ENABLED}>:${ETHOS_U55_LIBS}>
364 $<$<BOOL:${CMSIS_DSP_LIB}>:${CMSIS_DSP_LIB}>)
365
366 add_executable(${TARGET_NAME} ${SRC_MAIN})
367
368 target_link_libraries(${TARGET_NAME} ${UC_LIB_NAME})
369
370 if (${CMAKE_CROSSCOMPILING})
371 set_target_properties(${TARGET_NAME} PROPERTIES SUFFIX ".axf")
372 endif()
373
374 if (${TARGET_PLATFORM} STREQUAL mps3)
375
376 SET(SECTORS_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/sectors/${use_case})
377 file(REMOVE_RECURSE ${SECTORS_DIR})
378 file(MAKE_DIRECTORY ${SECTORS_DIR})
379
380 add_custom_command(TARGET ${TARGET_NAME}
381 POST_BUILD
382 COMMAND fromelf --bin --output=${SECTORS_DIR}/
383 ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TARGET_NAME}.axf)
384
385 add_custom_target(
386 run-${use_case} ALL
387 COMMAND ${PYTHON} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/py/gen_fpga_mem_map.py
388 --scatter_file_path ${SCAT_FILE}
389 --target_subsystem ${TARGET_SUBSYSTEM}
390 --output_file_path ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/images-${use_case}.txt
391 COMMENT "Generating FPGA mappings file")
392 elseif (${TARGET_PLATFORM} STREQUAL native)
Isabella Gottardi85209832021-04-20 14:08:52 +0100393
394 # If native build tests
395 set(TEST_SRC_USE_CASE "")
396 foreach(USE_CASES_TESTS_SEARCH_DIR ${USE_CASES_TESTS_SEARCH_DIR_LIST})
397
398 if (EXISTS ${USE_CASES_TESTS_SEARCH_DIR}/${use_case})
399 message(STATUS "Found tests for use-case ${use_case} at ${USE_CASES_TESTS_SEARCH_DIR}/${use_case}.")
400 set(TEST_SRC_USE_CASE ${USE_CASES_TESTS_SEARCH_DIR})
401 break()
402 endif ()
403 endforeach()
404
alexander3c798932021-03-26 21:42:19 +0000405 # Add tests only if they exists for the usecase
Isabella Gottardi85209832021-04-20 14:08:52 +0100406 if (NOT ${TEST_SRC_USE_CASE} STREQUAL "")
alexander3c798932021-03-26 21:42:19 +0000407
408 set(TEST_RESOURCES_INCLUDE
409 "${TEST_SRCS}/utils/"
410 "${TEST_SRCS}/resources/golden_fv/"
411 )
412
413 # Define Test sources and new target to run unit tests
414 file(GLOB_RECURSE TEST_SOURCES
415 "${TEST_SRCS}/common/*.cpp"
416 "${TEST_SRCS}/common/*.cc"
417 "${TEST_SRCS}/utils/*.cc"
418 "${TEST_SRCS}/utils/*.cpp"
Isabella Gottardi85209832021-04-20 14:08:52 +0100419 "${TEST_SRC_USE_CASE}/${use_case}/*.cpp"
420 "${TEST_SRC_USE_CASE}/${use_case}/*.cc"
421 "${TEST_SRC_USE_CASE}/${use_case}/*.c"
422 "${TEST_SRC_USE_CASE}/${use_case}/**/*.cpp"
423 "${TEST_SRC_USE_CASE}/${use_case}/**/*.cc"
424 "${TEST_SRC_USE_CASE}/${use_case}/**/*.c"
alexander3c798932021-03-26 21:42:19 +0000425 )
426
427 if (DEFINED ${use_case}_TEST_IFM AND DEFINED ${use_case}_TEST_OFM)
428 message(STATUS "Test vectors are available for ${${use_case}_MODEL_TFLITE_PATH} "
429 "Input: ${${use_case}_TEST_IFM} "
430 "Output: ${${use_case}_TEST_OFM}")
431
432 set(TEST_SRC_GEN_DIR ${CMAKE_BINARY_DIR}/generated/${use_case}/tests/src)
433 set(TEST_INC_GEN_DIR ${CMAKE_BINARY_DIR}/generated/${use_case}/tests/include)
434 file(GLOB_RECURSE TEST_SOURCES_GEN
435 "${TEST_SRC_GEN_DIR}/*.cc"
436 "${TEST_SRC_GEN_DIR}/**/*.cc"
437 )
438 message(STATUS "Adding ${TEST_SOURCES_GEN} to test sources")
439 list(APPEND TEST_SOURCES ${TEST_SOURCES_GEN})
440 list(APPEND TEST_RESOURCES_INCLUDE ${TEST_INC_GEN_DIR})
441 endif()
442
443 set(TEST_TARGET_NAME "${CMAKE_PROJECT_NAME}-${use_case}-tests")
444 add_executable(${TEST_TARGET_NAME} ${TEST_SOURCES})
445 target_include_directories(${TEST_TARGET_NAME} PUBLIC
446 ${TEST_TPIP_INCLUDE} ${TEST_RESOURCES_INCLUDE})
447 target_link_libraries(${TEST_TARGET_NAME} libethos-u-${use_case})
448 target_compile_definitions(${TEST_TARGET_NAME} PRIVATE
449 "ACTIVATION_BUF_SZ=${${use_case}_ACTIVATION_BUF_SZ}"
450 TESTS)
451
452 add_dependencies(
453 "${TEST_TARGET_NAME}"
454 "catch2-headers"
455 )
Isabella Gottardi85209832021-04-20 14:08:52 +0100456
alexander3c798932021-03-26 21:42:19 +0000457 endif ()
458 endif ()
459endforeach()
460
461print_useroptions()