-
Notifications
You must be signed in to change notification settings - Fork 2
/
CMakeLists.txt
475 lines (409 loc) · 14.6 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
cmake_minimum_required(VERSION 3.18)
project(tritonarmnntflitebackend LANGUAGES C CXX)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
set(TARGET_ARCH ${CMAKE_HOST_SYSTEM_PROCESSOR})
# Triton Options
option(TRITON_ENABLE_STATS "Include statistics collections in backend" ON)
option(TRITON_ENABLE_MALI_GPU
"Enable Arm MALI GPU support in backend utilities" OFF)
set(TRITON_BACKEND_REPO_TAG
"main"
CACHE STRING "Tag for triton-inference-server/backend repo")
set(TRITON_CORE_REPO_TAG
"main"
CACHE STRING "Tag for triton-inference-server/core repo")
set(TRITON_COMMON_REPO_TAG
"main"
CACHE STRING "Tag for triton-inference-server/common repo")
# TFLite Options
set(TFLITE_TAG
"v2.10.0"
CACHE STRING "Version of TFLite to build")
set(BAZEL_BINARY
"/usr/bin/bazel"
CACHE STRING "Location of bazel binary on host")
option(TFLITE_BAZEL_BUILD "Build tflite bazel" OFF)
option(TFLITE_ENABLE_XNNPACK "Build backend with xnnpack delgate support" ON)
option(TFLITE_ENABLE_FLEX_OPS
"Build tflite with flex ops support (must build with bazel)" OFF)
# ArmNN Options
set(ARMNN_VERSION
"23.02"
CACHE STRING "Tag for ArmNN repo")
set(ARMNN_BUILD_TYPE
${CMAKE_BUILD_TYPE}
CACHE STRING "Set ArmNN build type")
option(ARMNN_DELEGATE_ENABLE "Build ArmNN Delegate" ON)
# ACL Options
set(ACL_VERSION
${ARMNN_VERSION}
CACHE STRING "Tag for ACL repo")
set(ACL_DEBUG
"0"
CACHE STRING "Whether to compile ACL in debug mode")
set(ACL_CC_COMPILER
${CMAKE_C_COMPILER}
CACHE STRING "CC compiler to use for scons acl build")
set(ACL_CXX_COMPILER
${CMAKE_CXX_COMPILER}
CACHE STRING "CXX compiler to use for scons acl build")
set(ACL_ENABLE_NEON
"1"
CACHE STRING "Enable Neon Backend in build")
set(ACL_ENABLE_CL
"0"
CACHE STRING "Enable OpenCL Backend in build")
set(FLATBUFFERS_VERSION
"2.0.6"
CACHE STRING "Version of Flatbuffers to use")
set(JOBS
"8"
CACHE STRING "Number of parallel jobs for builds")
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(ACL_DEBUG "1")
endif()
if(TRITON_ENABLE_MALI_GPU)
set(ACL_ENABLE_CL "1")
endif()
# Ensure bazel build set if enabling flex ops delegate
if(TFLITE_ENABLE_FLEX_OPS AND (NOT (TFLITE_BAZEL_BUILD)))
message(
FATAL_ERROR
"Please set TFLITE_BAZEL_BUILD if you wish to build flex ops delegate")
endif()
if(NOT DEFINED ACL_ARCH)
if(${TARGET_ARCH} MATCHES "armv7")
set(ACL_ARCH "armv7a")
elseif(${TARGET_ARCH} MATCHES "armhf")
set(ACL_ARCH "armv7a-hf")
elseif(${TARGET_ARCH} MATCHES "aarch64")
set(ACL_ARCH "arm64-v8a")
elseif(${TARGET_ARCH} MATCHES "(x86)|(X86)|(amd64)|(AMD64)")
set(ARMNN_DELEGATE_ENABLE OFF)
else()
message(FATAL_ERROR "Unsupported target architecture: ${TARGET_ARCH}")
endif()
endif()
# ACL before 21.05 compiled with only gcc7
if(NOT (ACL_VERSION VERSION_GREATER "21.05"))
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7)
set(ACL_CC_COMPILER "gcc-7")
set(ACL_CXX_COMPILER "g++-7")
endif()
endif()
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_CXX_FLAGS "-funsafe-math-optimizations")
set(CMAKE_C_FLAGS "-funsafe-math-optimizations")
set(ACL_BUILD_FLAGS "-j${JOBS}" "arch=${ACL_ARCH}" "debug=${ACL_DEBUG}" "neon=${ACL_ENABLE_NEON}" "opencl=${ACL_ENABLE_CL}" "embed_kernels=1" "extra_cxx_flags=-fPIC" "benchmark_tests=0" "build=native" "validation_tests=0" "examples=0")
if(NOT (ACL_VERSION VERSION_GREATER "21.05"))
list(APPEND ACL_BUILD_FLAGS "internal_only=0")
endif()
#
# Dependencies
#
# FetchContent's composibility isn't very good. We must include the transitive
# closure of all repos so that we can override the tag.
#
include(FetchContent)
FetchContent_Declare(
repo-common
GIT_REPOSITORY https://github.com/triton-inference-server/common.git
GIT_TAG ${TRITON_COMMON_REPO_TAG}
GIT_SHALLOW ON)
FetchContent_Declare(
repo-core
GIT_REPOSITORY https://github.com/triton-inference-server/core.git
GIT_TAG ${TRITON_CORE_REPO_TAG}
GIT_SHALLOW ON)
FetchContent_Declare(
repo-backend
GIT_REPOSITORY https://github.com/triton-inference-server/backend.git
GIT_TAG ${TRITON_BACKEND_REPO_TAG}
GIT_SHALLOW ON)
set(MAKE_AVAILABLE_LIST repo-common repo-core repo-backend)
if(NOT TFLITE_BAZEL_BUILD)
FetchContent_Declare(
tensorflow-lite
GIT_REPOSITORY https://github.com/tensorflow/tensorflow.git
GIT_TAG ${TFLITE_TAG}
GIT_SHALLOW ON
PATCH_COMMAND
git apply ${CMAKE_CURRENT_SOURCE_DIR}/patches/xnnpack_commit.patch ||
true # patch updates the commit hash for xnnpack for tensorflow v2.4.1
SOURCE_SUBDIR tensorflow/lite
)
list(APPEND MAKE_AVAILABLE_LIST tensorflow-lite)
endif()
FetchContent_MakeAvailable(${MAKE_AVAILABLE_LIST})
#
# Shared library implementing the Triton Backend API
#
configure_file(src/libtriton_armnn_tflite.ldscript
libtriton_armnn_tflite.ldscript COPYONLY)
include(ExternalProject)
set(TFLITE_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/external/tensorflow_lite)
if(TFLITE_BAZEL_BUILD)
set(BAZEL_BUILD_FLAGS "--config=monolithic" "-c" "opt")
if(TFLITE_ENABLE_RUY)
list(APPEND BAZEL_BUILD_FLAGS "--define=tflite_with_ruy=true")
else()
list(APPEND BAZEL_BUILD_FLAGS "--define=tflite_with_ruy=false")
endif()
if(TFLITE_ENABLE_XNNPACK)
set(BAZEL_BUILD_PATCH
"${CMAKE_CURRENT_SOURCE_DIR}/patches/tflite_bazel_build_with_xnnpack.patch"
)
else()
set(BAZEL_BUILD_PATCH
"${CMAKE_CURRENT_SOURCE_DIR}/patches/tflite_bazel_build.patch")
endif()
if(TFLITE_ENABLE_FLEX_OPS)
set(TFLITE_PATCH_COMMAND "git" "apply" ${BAZEL_BUILD_PATCH} "||" "true")
else()
set(TFLITE_PATCH_COMMAND "")
endif()
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
list(APPEND BAZEL_BUILD_FLAGS "-c" "dbg" "--copt=-g")
else()
list(APPEND BAZEL_BUILD_FLAGS "--strip=always")
endif()
# Build tensorflow lite shared lib using bazel (required for flex ops
# delegate)
ExternalProject_Add(
tensorflow-lite
PREFIX ${TFLITE_LOCATION}
GIT_REPOSITORY https://github.com/tensorflow/tensorflow.git
GIT_TAG ${TFLITE_TAG}
GIT_SHALLOW ON
PATCH_COMMAND ${TFLITE_PATCH_COMMAND}
CONFIGURE_COMMAND ""
BUILD_COMMAND ${BAZEL_BINARY} build ${BAZEL_BUILD_FLAGS}
//tensorflow/lite:libtensorflowlite.so
BUILD_IN_SOURCE ON
INSTALL_COMMAND "")
endif()
if(TFLITE_BAZEL_BUILD)
set(TENSORFLOW_ROOT ${TFLITE_LOCATION}/src/tensorflow-lite)
set(TFLITE_LIB_ROOT ${TFLITE_LOCATION}/src/tensorflow-lite/bazel-bin)
else()
set(TENSORFLOW_ROOT ${CMAKE_CURRENT_BINARY_DIR}/_deps/tensorflow-lite-src)
set(TFLITE_LIB_ROOT ${CMAKE_CURRENT_BINARY_DIR}/_deps/tensorflow-lite-build)
endif()
if(ARMNN_DELEGATE_ENABLE)
#
# Build ArmNN dependencies using custom command
#
set(FLATBUFFERS_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/external/flatbuffers)
set(ACL_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/external/acl)
set(ARMNN_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/external/armnn)
file(MAKE_DIRECTORY ${ARMNN_LOCATION})
if(ACL_VERSION VERSION_EQUAL "21.08")
set(ACL_PATCH_COMMAND "git" "apply" "${CMAKE_CURRENT_SOURCE_DIR}/patches/acl_21_08.patch" "||" "true")
elseif(ACL_VERSION VERSION_EQUAL "21.11")
set(ACL_PATCH_COMMAND "git" "apply" "${CMAKE_CURRENT_SOURCE_DIR}/patches/acl_21_11.patch" "||" "true")
else()
set(ACL_PATCH_COMMAND "")
endif()
# Flatbuffers (note that tflite brings in it's own flatbuffers, so we rename
# it here)
ExternalProject_Add(
flatbuffers-armnn
URL https://github.com/google/flatbuffers/archive/v${FLATBUFFERS_VERSION}.zip
CMAKE_ARGS
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER}
-DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER}
-DCMAKE_INSTALL_PREFIX:PATH=${FLATBUFFERS_LOCATION}/flatbuffers-${FLATBUFFERS_VERSION}/install
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
-DFLATBUFFERS_BUILD_TESTS=OFF
)
# ACL (note requires GCC 7 if using v21.02)
ExternalProject_Add(
acl
PREFIX ${ACL_LOCATION}
GIT_REPOSITORY https://review.mlplatform.org/ml/ComputeLibrary
GIT_TAG v${ACL_VERSION}
GIT_SHALLOW ON
PATCH_COMMAND ${ACL_PATCH_COMMAND}
CONFIGURE_COMMAND ""
BUILD_COMMAND
CC=${ACL_CC_COMPILER} CXX=${ACL_CXX_COMPILER} scons ${ACL_BUILD_FLAGS}
BUILD_IN_SOURCE ON
INSTALL_COMMAND "")
set(ARMNN_PATCH_COMMAND "")
if(ARMNN_VERSION VERSION_LESS "22")
# Never fail as patch is always applied after repo clone
set(ARMNN_PATCH_COMMAND
"git" "apply"
"${CMAKE_CURRENT_SOURCE_DIR}/patches/tflite_armnn_delegate.patch" "||"
"true")
elseif(ARMNN_VERSION VERSION_GREATER_EQUAL "22" AND ARMNN_VERSION VERSION_LESS "23")
set(ARMNN_PATCH_COMMAND
"git" "apply"
"${CMAKE_CURRENT_SOURCE_DIR}/patches/tflite_armnn_delegate22.patch" "||"
"true")
elseif(ARMNN_VERSION VERSION_GREATER_EQUAL "23")
set(ARMNN_PATCH_COMMAND
"git" "apply"
"${CMAKE_CURRENT_SOURCE_DIR}/patches/tflite_armnn_delegate23.patch" "||"
"true")
endif()
# ArmNN and its tflite delegate
ExternalProject_Add(
armnn
PREFIX ${ARMNN_LOCATION}
GIT_REPOSITORY https://review.mlplatform.org/ml/armnn
GIT_TAG v${ARMNN_VERSION}
GIT_SHALLOW ON
PATCH_COMMAND ${ARMNN_PATCH_COMMAND}
CMAKE_ARGS
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER}
-DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER}
-DCMAKE_INSTALL_PREFIX:PATH=${ARMNN_LOCATION}
-DBUILD_ARMNN_TFLITE_DELEGATE=1
-DTENSORFLOW_ROOT=${TENSORFLOW_ROOT}
-DTFLITE_LIB_ROOT=${TFLITE_LIB_ROOT}
-DARMCOMPUTE_ROOT=${ACL_LOCATION}/src/acl
-DARMCOMPUTENEON=${ACL_ENABLE_NEON}
-DARMCOMPUTECL=${ACL_ENABLE_CL}
-DBUILD_UNIT_TESTS=0
-DCMAKE_BUILD_TYPE=${ARMNN_BUILD_TYPE}
-DBUILD_ARMNN_SERIALIZER=0
-DARMNNREF=1
-DFLATBUFFERS_ROOT=${FLATBUFFERS_LOCATION}/flatbuffers-${FLATBUFFERS_VERSION}/install
DEPENDS flatbuffers-armnn acl tensorflow-lite)
endif()
#
# Handle libs for TFLite Backend
#
add_library(triton-armnn-tflite-backend SHARED
src/tflite.cc src/tflite_utils.cc src/tflite_utils.h)
if(ARMNN_DELEGATE_ENABLE)
add_dependencies(triton-armnn-tflite-backend armnn)
endif()
add_library(TritonArmNNTFLiteBackend::triton-armnn-tflite-backend ALIAS
triton-armnn-tflite-backend)
set(BACKEND_INCLUDE_DIRS
${CMAKE_CURRENT_SOURCE_DIR}/src ${TENSORFLOW_ROOT} # for tflite headers
${ARMNN_LOCATION}/include # for armnn headers
${ARMNN_LOCATION}/src/armnn/delegate/include # for delegate headers
)
# As per https://review.mlplatform.org/c/ml/armnn/+/7327
if(ARMNN_VERSION VERSION_GREATER_EQUAL "22.05")
list(APPEND BACKEND_INCLUDE_DIRS ${ARMNN_LOCATION}/src/armnn/profiling)
endif()
if(TFLITE_BAZEL_BUILD)
list(APPEND BACKEND_INCLUDE_DIRS
${TENSORFLOW_ROOT}/bazel-tensorflow-lite/external/flatbuffers/include)
else()
list(APPEND BACKEND_INCLUDE_DIRS
${TFLITE_LIB_ROOT}/flatbuffers/include)
endif()
target_include_directories(triton-armnn-tflite-backend
PRIVATE ${BACKEND_INCLUDE_DIRS})
target_compile_features(triton-armnn-tflite-backend PRIVATE cxx_std_11)
target_compile_options(
triton-armnn-tflite-backend
PRIVATE
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
-Wall
-Wextra
-Wno-unused-parameter
-Wno-type-limits
-Wno-comment
-Werror>)
# ARMNN_DELEGATE_ENABLE exposed in header so set PUBLIC
if(${ARMNN_DELEGATE_ENABLE})
target_compile_definitions(triton-armnn-tflite-backend
PUBLIC ARMNN_DELEGATE_ENABLE=1)
endif() # ARMNN_DELEGATE_ENABLE
set_target_properties(
triton-armnn-tflite-backend
PROPERTIES
POSITION_INDEPENDENT_CODE ON
OUTPUT_NAME triton_armnn_tflite
SKIP_BUILD_RPATH TRUE
BUILD_WITH_INSTALL_RPATH TRUE
INSTALL_RPATH_USE_LINK_PATH FALSE
INSTALL_RPATH "$\{ORIGIN\}"
LINK_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/libtriton_armnn_tflite.ldscript
LINK_FLAGS
"-Wl,--no-as-needed,--version-script libtriton_armnn_tflite.ldscript")
set(BACKEND_LINK_LIBS
triton-core-serverapi triton-core-backendapi triton-core-serverstub
triton-backend-utils ${CMAKE_DL_LIBS})
if(TFLITE_BAZEL_BUILD)
# Link the tensorflow lite library from bazel tfile build
target_link_libraries(
triton-armnn-tflite-backend
PRIVATE "-L${TFLITE_LOCATION}/src/tensorflow-lite/bazel-bin/tensorflow/lite"
-ltensorflowlite)
else()
list(APPEND BACKEND_LINK_LIBS tensorflow-lite)
endif()
target_link_libraries(triton-armnn-tflite-backend PRIVATE ${BACKEND_LINK_LIBS})
if(ARMNN_DELEGATE_ENABLE)
# Link the armnn lib
target_link_libraries(
triton-armnn-tflite-backend PRIVATE "-L${ARMNN_LOCATION}/lib" -larmnn
-larmnnDelegate)
endif()
#
# Install
#
include(GNUInstallDirs)
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/TritonArmNNTFLiteBackend)
install(
TARGETS triton-armnn-tflite-backend
EXPORT triton-armnn-tflite-backend-targets
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/backends/armnn_tflite
ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/backends/armnn_tflite)
if(ARMNN_DELEGATE_ENABLE)
# Install ArmNN libraries and license
install(
DIRECTORY ${ARMNN_LOCATION}/lib/
DESTINATION ${CMAKE_INSTALL_PREFIX}/backends/armnn_tflite
FILES_MATCHING
PATTERN "*.so*")
install(
FILES ${ARMNN_LOCATION}/src/armnn/LICENSE
RENAME armnn.LICENSE
DESTINATION ${CMAKE_INSTALL_PREFIX}/backends/armnn_tflite)
endif()
if(TFLITE_BAZEL_BUILD)
# Install tflite library
install(
FILES
${TFLITE_LOCATION}/src/tensorflow-lite/bazel-bin/tensorflow/lite/libtensorflowlite.so
DESTINATION ${CMAKE_INSTALL_PREFIX}/backends/armnn_tflite)
endif()
# Install Tensorflow license
install(
FILES ${TENSORFLOW_ROOT}/LICENSE
RENAME tensorflow.LICENSE
DESTINATION ${CMAKE_INSTALL_PREFIX}/backends/armnn_tflite)
install(
EXPORT triton-armnn-tflite-backend-targets
FILE TritonArmNNTFLiteBackendTargets.cmake
NAMESPACE TritonArmNNTFLiteBackend::
DESTINATION ${INSTALL_CONFIGDIR})
include(CMakePackageConfigHelpers)
configure_package_config_file(
${CMAKE_CURRENT_LIST_DIR}/cmake/TritonArmNNTFLiteBackendConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/TritonArmNNTFLiteBackendConfig.cmake
INSTALL_DESTINATION ${INSTALL_CONFIGDIR})
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/TritonArmNNTFLiteBackendConfig.cmake
DESTINATION ${INSTALL_CONFIGDIR})
#
# Export from build tree
#
export(
EXPORT triton-armnn-tflite-backend-targets
FILE ${CMAKE_CURRENT_BINARY_DIR}/TritonArmNNTFLiteBackendTargets.cmake
NAMESPACE TritonArmNNTFLiteBackend::)
export(PACKAGE TritonArmNNTFLiteBackend)