Skip to content

Commit 89be562

Browse files
authored
wasi-nn: Add external delegation to support several NPU/GPU (#2162)
Add VX delegation as an external delegation of TFLite, so that several NPU/GPU (from VeriSilicon, NXP, Amlogic) can be controlled via WASI-NN. Test Code can work with the X86 simulator.
1 parent 5a23ae4 commit 89be562

File tree

4 files changed

+159
-3
lines changed

4 files changed

+159
-3
lines changed

build-scripts/config_common.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,13 @@ if (WAMR_BUILD_WASI_NN EQUAL 1)
341341
message (" WASI-NN: GPU enabled")
342342
add_definitions (-DWASI_NN_ENABLE_GPU=1)
343343
endif ()
344+
if (WAMR_BUILD_WASI_NN_ENABLE_EXT EQUAL 1)
345+
message (" WASI-NN: External Delegation enabled")
346+
add_definitions (-DWASI_NN_ENABLE_EXTERNAL_DELEGATE=1)
347+
endif ()
348+
if (DEFINED WASI_NN_EXT_DELEGATE_PATH)
349+
add_definitions (-DWASI_NN_EXT_DELEGATE_PATH="${WASI_NN_EXT_DELEGATE_PATH}")
350+
endif ()
344351
endif ()
345352
if (WAMR_BUILD_ALLOC_WITH_USER_DATA EQUAL 1)
346353
add_definitions(-DWASM_MEM_ALLOC_WITH_USER_DATA=1)

core/iwasm/libraries/wasi-nn/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Build the runtime image for your execution target type.
2424
`EXECUTION_TYPE` can be:
2525
* `cpu`
2626
* `nvidia-gpu`
27+
* `vx-delegate`
2728

2829
```
2930
EXECUTION_TYPE=cpu
@@ -71,6 +72,18 @@ docker run \
7172
/assets/test_tensorflow.wasm
7273
```
7374

75+
* vx-delegate for NPU (x86 simulater)
76+
77+
```
78+
docker run \
79+
-v $PWD/core/iwasm/libraries/wasi-nn/test:/assets wasi-nn-vx-delegate \
80+
--dir=/assets \
81+
--env="TARGET=gpu" \
82+
/assets/test_tensorflow.wasm
83+
```
84+
85+
86+
7487
Requirements:
7588
* [NVIDIA docker](https://github.com/NVIDIA/nvidia-docker).
7689

core/iwasm/libraries/wasi-nn/src/wasi_nn_tensorflowlite.cpp

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
#include <tensorflow/lite/delegates/gpu/delegate.h>
2222
#endif
2323

24+
#if defined(WASI_NN_ENABLE_EXTERNAL_DELEGATE)
25+
#include <tensorflow/lite/delegates/external/external_delegate.h>
26+
#endif
27+
2428
/* Maximum number of graphs per WASM instance */
2529
#define MAX_GRAPHS_PER_INST 10
2630
/* Maximum number of graph execution context per WASM instance*/
@@ -42,6 +46,7 @@ typedef struct {
4246
uint32_t current_interpreters;
4347
Interpreter interpreters[MAX_GRAPH_EXEC_CONTEXTS_PER_INST];
4448
korp_mutex g_lock;
49+
TfLiteDelegate *delegate;
4550
} TFLiteContext;
4651

4752
/* Utils */
@@ -194,18 +199,40 @@ tensorflowlite_init_execution_context(void *tflite_ctx, graph g,
194199
#if defined(WASI_NN_ENABLE_GPU)
195200
NN_WARN_PRINTF("GPU enabled.");
196201
// https://www.tensorflow.org/lite/performance/gpu
197-
auto options = TfLiteGpuDelegateOptionsV2Default();
202+
TfLiteGpuDelegateOptionsV2 options =
203+
TfLiteGpuDelegateOptionsV2Default();
198204
options.inference_preference =
199205
TFLITE_GPU_INFERENCE_PREFERENCE_SUSTAINED_SPEED;
200206
options.inference_priority1 =
201207
TFLITE_GPU_INFERENCE_PRIORITY_MIN_LATENCY;
202-
auto *delegate = TfLiteGpuDelegateV2Create(&options);
208+
tfl_ctx->delegate = TfLiteGpuDelegateV2Create(&options);
209+
if (tfl_ctx->delegate == NULL) {
210+
NN_ERR_PRINTF("Error when generating GPU delegate.");
211+
use_default = true;
212+
return missing_memory;
213+
}
203214
if (tfl_ctx->interpreters[*ctx]
204-
.interpreter->ModifyGraphWithDelegate(delegate)
215+
.interpreter->ModifyGraphWithDelegate(tfl_ctx->delegate)
205216
!= kTfLiteOk) {
206217
NN_ERR_PRINTF("Error when enabling GPU delegate.");
207218
use_default = true;
208219
}
220+
#elif defined(WASI_NN_ENABLE_EXTERNAL_DELEGATE)
221+
NN_WARN_PRINTF("external delegation enabled.");
222+
TfLiteExternalDelegateOptions options =
223+
TfLiteExternalDelegateOptionsDefault(WASI_NN_EXT_DELEGATE_PATH);
224+
tfl_ctx->delegate = TfLiteExternalDelegateCreate(&options);
225+
if (tfl_ctx->delegate == NULL) {
226+
NN_ERR_PRINTF("Error when generating External delegate.");
227+
use_default = true;
228+
return missing_memory;
229+
}
230+
if (tfl_ctx->interpreters[*ctx]
231+
.interpreter->ModifyGraphWithDelegate(tfl_ctx->delegate)
232+
!= kTfLiteOk) {
233+
NN_ERR_PRINTF("Error when enabling External delegate.");
234+
use_default = true;
235+
}
209236
#else
210237
NN_WARN_PRINTF("GPU not enabled.");
211238
use_default = true;
@@ -350,6 +377,8 @@ tensorflowlite_initialize(void **tflite_ctx)
350377
NN_ERR_PRINTF("Error while initializing the lock");
351378
}
352379

380+
tfl_ctx->delegate = NULL;
381+
353382
*tflite_ctx = (void *)tfl_ctx;
354383
}
355384

@@ -364,6 +393,14 @@ tensorflowlite_destroy(void *tflite_ctx)
364393
*/
365394
TFLiteContext *tfl_ctx = (TFLiteContext *)tflite_ctx;
366395

396+
if (tfl_ctx->delegate != NULL) {
397+
#if defined(WASI_NN_ENABLE_GPU)
398+
TfLiteGpuDelegateV2Delete(tfl_ctx->delegate);
399+
#elif defined(WASI_NN_ENABLE_EXTERNAL_DELEGATE)
400+
TfLiteExternalDelegateDelete(tfl_ctx->delegate);
401+
#endif
402+
}
403+
367404
NN_DBG_PRINTF("Freeing memory.");
368405
for (int i = 0; i < MAX_GRAPHS_PER_INST; ++i) {
369406
tfl_ctx->models[i].model.reset();
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Copyright (C) 2019 Intel Corporation. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
FROM ubuntu:20.04 AS base
5+
6+
ENV DEBIAN_FRONTEND=noninteractive
7+
8+
9+
RUN apt-get update && apt-get install -y \
10+
cmake build-essential git curl libssl-dev python3
11+
12+
13+
# Build TensorFlow Lite VX delegate default built for x86-64 simulator
14+
WORKDIR /tmp
15+
RUN git clone https://github.com/VeriSilicon/TIM-VX.git tim-vx
16+
RUN git clone https://github.com/VeriSilicon/tflite-vx-delegate.git
17+
RUN git clone https://github.com/tensorflow/tensorflow.git
18+
19+
20+
# Build TIM-VX
21+
WORKDIR /tmp/tim-vx/host_build
22+
RUN cmake -DCMAKE_INSTALL_PREFIX=/usr/local ../
23+
RUN make -j$(grep -c ^processor /proc/cpuinfo)
24+
RUN make install
25+
26+
WORKDIR /tmp/tim-vx
27+
#RUN mkdir -p prebuilt-sdk/x86_64_linux/lib/include
28+
#RUN cp prebuilt-sdk/x86_64_linux/include/CL prebuilt-sdk/x86_64_linux/lib/include -fr
29+
30+
31+
# Build TensorFlow Lite
32+
WORKDIR /tmp/tensorflow/build
33+
RUN cmake \
34+
-DBUILD_SHARED_LIBS=ON=on \
35+
-DTFLITE_ENABLE_RUY=on \
36+
-DTFLITE_ENABLE_NNAPI=off \
37+
-DTFLITE_ENABLE_XNNPACK=on \
38+
-DTFLITE_ENABLE_EXTERNAL_DELEGATE=on \
39+
../tensorflow/lite/
40+
RUN make -j$(grep -c ^processor /proc/cpuinfo)
41+
RUN make install
42+
RUN cp --no-preserve=ownership -d lib*.so* /usr/local/lib
43+
RUN cp -r --no-preserve=ownership -d flatbuffers/include/flatbuffers /usr/local/include
44+
# install header files
45+
RUN install -d /usr/local/include/tensorflow/lite && \
46+
cd /tmp/tensorflow/tensorflow/lite && \
47+
cp --parents \
48+
$(find . -name "*.h*") \
49+
/usr/local/include/tensorflow/lite
50+
# install version.h from core
51+
RUN install -d /usr/local/include/tensorflow/core/public && \
52+
cp /tmp/tensorflow/tensorflow/core/public/version.h /usr/local/include/tensorflow/core/public
53+
54+
55+
# Build Vx Delegate default built for x86-64 simulator
56+
WORKDIR /tmp/tflite-vx-delegate/build
57+
RUN cmake \
58+
-DBUILD_SHARED_LIBS=ON \
59+
-DFETCHCONTENT_SOURCE_DIR_TENSORFLOW=/tmp/tensorflow \
60+
-DTFLITE_LIB_LOC=/usr/local/lib/libtensorflow-lite.so \
61+
-DTIM_VX_INSTALL=/usr/local \
62+
-DCMAKE_INSTALL_PREFIX=/usr/ \
63+
../
64+
RUN make vx_delegate -j$(grep -c ^processor /proc/cpuinfo)
65+
RUN make install
66+
RUN cp --no-preserve=ownership -d lib*.so* /usr/lib
67+
# install header files
68+
RUN install -d /usr/local/include/tensorflow-lite-vx-delegate && \
69+
cd /tmp/tflite-vx-delegate/ && \
70+
cp --parents \
71+
$(find . -name "*.h*") \
72+
/usr/local/include/tensorflow-lite-vx-delegate
73+
74+
ENV VIVANTE_SDK_DIR=/tmp/tim-vx/prebuilt-sdk/x86_64_linux/
75+
ENV VSIMULATOR_CONFIG=czl
76+
77+
ENV LD_LIBRARY_PATH=/tmp/tim-vx/prebuilt-sdk/x86_64_linux/lib:/usr/local/lib:/lib/x86_64-linux-gnu/:/lib64/:/usr/lib:$LD_LIBRARY_PATH
78+
79+
80+
# Build WASI-NN
81+
WORKDIR /home/wamr
82+
83+
COPY . .
84+
85+
WORKDIR /home/wamr/core/iwasm/libraries/wasi-nn/test/build
86+
87+
RUN cmake \
88+
-DCMAKE_LIBRARY_PATH=${CMAKE_LIBRARY_PATH}:/usr/local/lib/ \
89+
-DCMAKE_INCLUDE_PATH=${CMAKE_INCLUDE_PATH}:/usr/local/include/ \
90+
-DWAMR_BUILD_WASI_NN=1 \
91+
-DWAMR_BUILD_WASI_NN_ENABLE_EXT=1 \
92+
-DWASI_NN_EXT_DELEGATE_PATH="/usr/lib/libvx_delegate.so" \
93+
..
94+
95+
RUN make -j $(grep -c ^processor /proc/cpuinfo)
96+
97+
RUN cp /home/wamr/core/iwasm/libraries/wasi-nn/test/build/iwasm /run/iwasm
98+
99+
ENTRYPOINT [ "/run/iwasm" ]

0 commit comments

Comments
 (0)