Skip to content

Commit 4fff4cd

Browse files
committed
Feature/flexops
1 parent ccc57c3 commit 4fff4cd

File tree

2 files changed

+154
-36
lines changed

2 files changed

+154
-36
lines changed

CMakeLists.txt

Lines changed: 125 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ set(TRITON_COMMON_REPO_TAG "main" CACHE STRING "Tag for triton-inference-server/
1818

1919
# TFLite Options
2020
set(TFLITE_TAG "v2.4.1" CACHE STRING "Version of TFLite to build")
21+
set(BAZEL_BINARY "/usr/bin/bazel" CACHE STRING "Location of bazel binary on host")
22+
option(TFLITE_BAZEL_BUILD "Build tflite bazel" OFF)
2123
option(TFLITE_ENABLE_RUY "Use RUY library in TFLite build" ON)
24+
option(TFLITE_ENABLE_FLEX_OPS "Build tflite with flex ops support (must build with bazel)" OFF)
2225

2326
# ArmNN Options
2427
set(ARMNN_TAG "v21.08" CACHE STRING "Tag for ArmNN repo")
@@ -44,6 +47,11 @@ if (TRITON_ENABLE_MALI_GPU)
4447
set(ACL_ENABLE_CL "1")
4548
endif()
4649

50+
# Ensure bazel build set if enabling flex ops delegate
51+
if (TFLITE_ENABLE_FLEX_OPS AND (NOT(TFLITE_BAZEL_BUILD)))
52+
message(FATAL_ERROR "Please set TFLITE_BAZEL_BUILD if you wish to build flex ops delegate")
53+
endif()
54+
4755
if (NOT DEFINED ACL_ARCH)
4856
if (${TARGET_ARCH} MATCHES "armv7")
4957
set(ACL_ARCH "armv7a")
@@ -95,15 +103,23 @@ FetchContent_Declare(
95103
GIT_TAG ${TRITON_BACKEND_REPO_TAG}
96104
GIT_SHALLOW ON
97105
)
98-
FetchContent_Declare(
99-
tensorflow-lite
100-
GIT_REPOSITORY https://github.com/tensorflow/tensorflow.git
101-
GIT_TAG ${TFLITE_TAG}
102-
GIT_SHALLOW ON
103-
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
104-
SOURCE_SUBDIR tensorflow/lite
105-
)
106-
FetchContent_MakeAvailable(repo-common repo-core repo-backend tensorflow-lite)
106+
107+
set(MAKE_AVAILABLE_LIST repo-common repo-core repo-backend)
108+
109+
if (NOT TFLITE_BAZEL_BUILD)
110+
FetchContent_Declare(
111+
tensorflow-lite
112+
GIT_REPOSITORY https://github.com/tensorflow/tensorflow.git
113+
GIT_TAG ${TFLITE_TAG}
114+
GIT_SHALLOW ON
115+
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
116+
SOURCE_SUBDIR tensorflow/lite
117+
)
118+
119+
LIST(APPEND MAKE_AVAILABLE_LIST tensorflow-lite)
120+
endif()
121+
122+
FetchContent_MakeAvailable(${MAKE_AVAILABLE_LIST})
107123

108124
#
109125
# Shared library implementing the Triton Backend API
@@ -112,6 +128,50 @@ configure_file(src/libtriton_armnn_tflite.ldscript libtriton_armnn_tflite.ldscri
112128

113129
include(ExternalProject)
114130

131+
set(TFLITE_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/external/tensorflow_lite)
132+
133+
if (TFLITE_BAZEL_BUILD)
134+
SET(BAZEL_BUILD_FLAGS
135+
"--define=tflite_with_xnnpack=true"
136+
"--define=with_select_tf_ops=true"
137+
"--config=monolithic"
138+
"-c" "opt"
139+
)
140+
141+
if (TFLITE_ENABLE_RUY)
142+
LIST(APPEND BAZEL_BUILD_FLAGS "--define=tflite_with_ruy=true")
143+
else()
144+
LIST(APPEND BAZEL_BUILD_FLAGS "--define=tflite_with_ruy=false")
145+
endif()
146+
147+
if (TFLITE_ENABLE_FLEX_OPS)
148+
LIST(APPEND BAZEL_BUILD_FLAGS "--define=with_select_tf_ops=true")
149+
else()
150+
LIST(APPEND BAZEL_BUILD_FLAGS "--define=with_select_tf_ops=false")
151+
endif()
152+
153+
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
154+
LIST(APPEND BAZEL_BUILD_FLAGS "-c" "dbg" "--copt=-g")
155+
else()
156+
LIST(APPEND BAZEL_BUILD_FLAGS "--strip=always")
157+
endif()
158+
159+
# Build tensorflow lite shared lib using bazel (required for flex ops delegate)
160+
ExternalProject_Add(
161+
tensorflow-lite
162+
PREFIX ${TFLITE_LOCATION}
163+
GIT_REPOSITORY https://github.com/tensorflow/tensorflow.git
164+
GIT_TAG ${TFLITE_TAG}
165+
GIT_SHALLOW ON
166+
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
167+
CONFIGURE_COMMAND ""
168+
BUILD_COMMAND ${BAZEL_BINARY} build ${BAZEL_BUILD_FLAGS} //tensorflow/lite:libtensorflowlite.so
169+
BUILD_IN_SOURCE ON
170+
INSTALL_COMMAND ""
171+
)
172+
endif()
173+
174+
115175
if (ARMNN_DELEGATE_ENABLE)
116176
#
117177
# Build ArmNN dependencies using custom command
@@ -142,15 +202,26 @@ if (ARMNN_DELEGATE_ENABLE)
142202
INSTALL_COMMAND ""
143203
)
144204

205+
if (TFLITE_BAZEL_BUILD)
206+
SET(ARMNN_PATCH_COMMAND "")
207+
SET(TENSORFLOW_ROOT ${TFLITE_LOCATION}/src/tensorflow-lite)
208+
SET(TFLITE_LIB_ROOT ${TFLITE_LOCATION}/src/tensorflow-lite/bazel-bin)
209+
else()
210+
# Never fail as patch is always applied after repo clone
211+
SET(ARMNN_PATCH_COMMAND "git" "apply" "${CMAKE_CURRENT_SOURCE_DIR}/patches/tflite_armnn_delegate.patch" "||" "true")
212+
SET(TENSORFLOW_ROOT ${CMAKE_CURRENT_BINARY_DIR}/_deps/tensorflow-lite-src)
213+
SET(TFLITE_LIB_ROOT ${CMAKE_CURRENT_BINARY_DIR}/_deps/tensorflow-lite-build)
214+
endif()
215+
145216
# ArmNN and its tflite delegate
146217
ExternalProject_Add(
147218
armnn
148219
PREFIX ${ARMNN_LOCATION}
149220
GIT_REPOSITORY https://review.mlplatform.org/ml/armnn
150221
GIT_TAG ${ARMNN_TAG}
151222
GIT_SHALLOW ON
152-
PATCH_COMMAND git apply ${CMAKE_CURRENT_SOURCE_DIR}/patches/tflite_armnn_delegate.patch || true # Never fail as patch is always applied after repo clone
153-
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${ARMNN_LOCATION} -DBUILD_ARMNN_TFLITE_DELEGATE=1 -DTENSORFLOW_ROOT=${CMAKE_CURRENT_BINARY_DIR}/_deps/tensorflow-lite-src -DTFLITE_LIB_ROOT=${CMAKE_CURRENT_BINARY_DIR}/_deps/tensorflow-lite-build -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
223+
PATCH_COMMAND ${ARMNN_PATCH_COMMAND}
224+
CMAKE_ARGS -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
154225
DEPENDS flatbuffers-armnn acl tensorflow-lite
155226
)
156227
endif()
@@ -174,15 +245,23 @@ add_library(
174245
TritonArmNNTFLiteBackend::triton-armnn-tflite-backend ALIAS triton-armnn-tflite-backend
175246
)
176247

177-
target_include_directories(
178-
triton-armnn-tflite-backend
179-
PRIVATE
248+
SET(BACKEND_INCLUDE_DIRS
180249
${CMAKE_CURRENT_SOURCE_DIR}/src
181-
${CMAKE_CURRENT_BINARY_DIR}/_deps/tensorflow-lite-src
250+
${TENSORFLOW_ROOT} # for tflite headers
182251
${ARMNN_LOCATION}/include # for armnn headers
183252
${ARMNN_LOCATION}/src/armnn/delegate/include # for delegate headers
184253
)
185254

255+
if (TFLITE_BAZEL_BUILD)
256+
LIST(APPEND BACKEND_INCLUDE_DIRS ${TENSORFLOW_ROOT}/bazel-tensorflow-lite/external/flatbuffers/include)
257+
endif()
258+
259+
target_include_directories(
260+
triton-armnn-tflite-backend
261+
PRIVATE
262+
${BACKEND_INCLUDE_DIRS}
263+
)
264+
186265
target_compile_features(triton-armnn-tflite-backend PRIVATE cxx_std_11)
187266
target_compile_options(
188267
triton-armnn-tflite-backend PRIVATE
@@ -211,15 +290,30 @@ set_target_properties(
211290
LINK_FLAGS "-Wl,--no-as-needed,--version-script libtriton_armnn_tflite.ldscript"
212291
)
213292

293+
SET(BACKEND_LINK_LIBS
294+
triton-core-serverapi
295+
triton-core-backendapi
296+
triton-core-serverstub
297+
triton-backend-utils
298+
${CMAKE_DL_LIBS}
299+
)
300+
301+
if (TFLITE_BAZEL_BUILD)
302+
# Link the tensorflow lite library
303+
target_link_libraries(
304+
triton-armnn-tflite-backend
305+
PRIVATE
306+
"-L${TFLITE_LOCATION}/src/tensorflow-lite/bazel-bin/tensorflow/lite" # From bazel tflite build
307+
-ltensorflowlite
308+
)
309+
else()
310+
LIST(APPEND BACKEND_LINK_LIBS tensorflow-lite)
311+
endif()
312+
214313
target_link_libraries(
215314
triton-armnn-tflite-backend
216315
PRIVATE
217-
triton-core-serverapi # from repo-core
218-
triton-core-backendapi # from repo-core
219-
triton-core-serverstub # from repo-core
220-
triton-backend-utils # from repo-backend
221-
tensorflow-lite
222-
${CMAKE_DL_LIBS}
316+
${BACKEND_LINK_LIBS}
223317
)
224318

225319
if (ARMNN_DELEGATE_ENABLE)
@@ -264,10 +358,19 @@ if (ARMNN_DELEGATE_ENABLE)
264358
)
265359
endif()
266360

361+
if (TFLITE_BAZEL_BUILD)
362+
# Install tflite library
363+
install(
364+
FILES
365+
${TFLITE_LOCATION}/src/tensorflow-lite/bazel-bin/tensorflow/lite/libtensorflowlite.so
366+
DESTINATION ${CMAKE_INSTALL_PREFIX}/backends/armnn_tflite
367+
)
368+
endif()
369+
267370
# Install Tensorflow license
268371
install(
269372
FILES
270-
${CMAKE_CURRENT_BINARY_DIR}/_deps/tensorflow-lite-src/LICENSE
373+
${TENSORFLOW_ROOT}/LICENSE
271374
RENAME tensorflow.LICENSE
272375
DESTINATION ${CMAKE_INSTALL_PREFIX}/backends/armnn_tflite
273376
)

Dockerfile

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ FROM ubuntu:${UBUNTU_VERSION} as armnn_tflite_backend
66
ARG TRITON_REPO_TAG=main
77

88
# Cmake Version options
9-
ARG CMAKE_VERSION=3.19
9+
ARG CMAKE_VERSION=3.21.1
1010

1111
ENV DEBIAN_FRONTEND=noninteractive
1212

@@ -19,22 +19,38 @@ RUN apt-get update && \
1919
curl \
2020
autoconf \
2121
libtool \
22+
python3-dev \
23+
python3-pip \
24+
python3-numpy \
2225
build-essential \
2326
libssl-dev \
27+
zlib1g-dev \
28+
default-jdk \
29+
libtool \
30+
zip \
31+
unzip \
2432
xxd \
2533
rapidjson-dev \
26-
unzip
34+
software-properties-common \
35+
unzip && \
36+
wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | \
37+
gpg --dearmor - | \
38+
tee /etc/apt/trusted.gpg.d/kitware.gpg >/dev/null && \
39+
apt-add-repository 'deb https://apt.kitware.com/ubuntu/ focal main' && \
40+
apt-get update && \
41+
apt-get install -y --no-install-recommends \
42+
cmake-data=${CMAKE_VERSION}-0kitware1ubuntu20.04.1 cmake=${CMAKE_VERSION}-0kitware1ubuntu20.04.1 && \
43+
pip3 install -U pip wheel && \
44+
rm -rf /var/lib/apt/lists/*
2745

28-
# Install cmake from source
29-
RUN build=1 && \
30-
mkdir /temp && \
31-
cd /temp && \
32-
wget https://cmake.org/files/v${CMAKE_VERSION}/cmake-${CMAKE_VERSION}.$build.tar.gz && \
33-
tar -xzvf cmake-${CMAKE_VERSION}.$build.tar.gz && \
34-
cd cmake-${CMAKE_VERSION}.$build/ && \
35-
./bootstrap --parallel=$(nproc) && \
36-
make -j$(nproc) && \
37-
make install
46+
# Install Bazel from source
47+
RUN wget -O bazel-3.1.0-dist.zip https://github.com/bazelbuild/bazel/releases/download/3.1.0/bazel-3.1.0-dist.zip && \
48+
unzip -d bazel bazel-3.1.0-dist.zip && \
49+
rm bazel-3.1.0-dist.zip && \
50+
cd bazel && \
51+
ln -s /usr/bin/python3 /usr/bin/python && \
52+
env EXTRA_BAZEL_ARGS="--host_javabase=@local_jdk//:jdk" bash ./compile.sh && \
53+
cp output/bazel /usr/bin/bazel
3854

3955
# Build ArmNN TFLite Backend
4056
WORKDIR /opt/armnn_tflite_backend
@@ -49,6 +65,5 @@ RUN mkdir build && \
4965
-DTRITON_ENABLE_GPU=OFF \
5066
-DTRITON_ENABLE_MALI_GPU=ON \
5167
-DTFLITE_ENABLE_RUY=ON \
52-
-DJOBS=$(nproc) \
53-
&& \
68+
-DJOBS=$(nproc) && \
5469
make -j$(nproc) install

0 commit comments

Comments
 (0)