Skip to content

Commit 1614ce1

Browse files
authored
wasi-nn: Enable GPU support (bytecodealliance#1922)
- Split logic in several dockers - runtime: wasi-nn-cpu and wasi-nn- Nvidia-gpu. - compilation: wasi-nn-compile. Prepare the testing wasm and generates the TFLites. - Implement GPU support for TFLite with Opencl.
1 parent fe3347d commit 1614ce1

File tree

10 files changed

+178
-56
lines changed

10 files changed

+178
-56
lines changed

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

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,69 @@ By only including this file in your WASM application you will bind WASI-NN into
1717
To run the tests we assume that the current directory is the root of the repository.
1818

1919

20-
1. Build the docker image,
20+
### Build the runtime
21+
22+
Build the runtime base image,
2123

2224
```
23-
docker build -t wasi-nn -f core/iwasm/libraries/wasi-nn/test/Dockerfile .
25+
docker build -t wasi-nn-base -f core/iwasm/libraries/wasi-nn/test/Dockerfile.base .
2426
```
2527

26-
2. Run the container
28+
Build the runtime image for your execution target type.
29+
30+
`EXECUTION_TYPE` can be:
31+
* `cpu`
32+
* `nvidia-gpu`
2733

2834
```
29-
docker run wasi-nn
35+
EXECUTION_TYPE=cpu
36+
docker build -t wasi-nn-${EXECUTION_TYPE} -f core/iwasm/libraries/wasi-nn/test/Dockerfile.${EXECUTION_TYPE} .
37+
```
38+
39+
40+
### Build wasm app
41+
3042
```
43+
docker build -t wasi-nn-compile -f core/iwasm/libraries/wasi-nn/test/Dockerfile.compile .
44+
```
45+
46+
```
47+
docker run -v $PWD/core/iwasm/libraries/wasi-nn:/wasi-nn wasi-nn-compile
48+
```
49+
50+
51+
### Run wasm app
3152

3253
If all the tests have run properly you will the the following message in the terminal,
3354

3455
```
3556
Tests: passed!
3657
```
3758

59+
* CPU
60+
61+
```
62+
docker run \
63+
-v $PWD/core/iwasm/libraries/wasi-nn/test:/assets wasi-nn-cpu \
64+
--dir=/assets \
65+
--env="TARGET=cpu" \
66+
/assets/test_tensorflow.wasm
67+
```
68+
69+
* (NVIDIA) GPU
70+
71+
```
72+
docker run \
73+
--runtime=nvidia \
74+
-v $PWD/core/iwasm/libraries/wasi-nn/test:/assets wasi-nn-nvidia-gpu \
75+
--dir=/assets \
76+
--env="TARGET=gpu" \
77+
/assets/test_tensorflow.wasm
78+
```
79+
80+
Requirements:
81+
* [NVIDIA docker](https://github.com/NVIDIA/nvidia-docker).
82+
3883
## What is missing
3984

4085
Supported:
@@ -43,5 +88,5 @@ Supported:
4388
* Only 1 model at a time.
4489
* `graph` and `graph-execution-context` are ignored.
4590
* Graph encoding: `tensorflowlite`.
46-
* Execution target: `cpu`.
91+
* Execution target: `cpu` and `gpu`.
4792
* Tensor type: `fp32`.

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <tensorflow/lite/model.h>
1717
#include <tensorflow/lite/optional_debug_tools.h>
1818
#include <tensorflow/lite/error_reporter.h>
19+
#include <tensorflow/lite/delegates/gpu/delegate.h>
1920

2021
/* Global variables */
2122

@@ -45,8 +46,8 @@ tensorflowlite_load(graph_builder_array *builder, graph_encoding encoding,
4546
return invalid_argument;
4647
}
4748

48-
if (target != cpu) {
49-
NN_ERR_PRINTF("Only CPU target is supported.");
49+
if (target != cpu && target != gpu) {
50+
NN_ERR_PRINTF("Only CPU and GPU target is supported.");
5051
return invalid_argument;
5152
}
5253

@@ -79,6 +80,29 @@ tensorflowlite_load(graph_builder_array *builder, graph_encoding encoding,
7980
return missing_memory;
8081
}
8182

83+
bool use_default = false;
84+
switch (target) {
85+
case gpu:
86+
{
87+
// https://www.tensorflow.org/lite/performance/gpu
88+
auto options = TfLiteGpuDelegateOptionsV2Default();
89+
options.inference_preference =
90+
TFLITE_GPU_INFERENCE_PREFERENCE_SUSTAINED_SPEED;
91+
options.inference_priority1 =
92+
TFLITE_GPU_INFERENCE_PRIORITY_MIN_LATENCY;
93+
auto *delegate = TfLiteGpuDelegateV2Create(&options);
94+
if (interpreter->ModifyGraphWithDelegate(delegate) != kTfLiteOk) {
95+
NN_ERR_PRINTF("Error when enabling GPU delegate.");
96+
use_default = true;
97+
}
98+
break;
99+
}
100+
default:
101+
use_default = true;
102+
}
103+
if (use_default)
104+
NN_WARN_PRINTF("Default encoding is CPU.");
105+
82106
return success;
83107
}
84108

core/iwasm/libraries/wasi-nn/test/.dockerignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

core/iwasm/libraries/wasi-nn/test/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ project (iwasm)
77

88
set (CMAKE_VERBOSE_MAKEFILE OFF)
99
# Reset default linker flags
10-
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
11-
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
1210
set (CMAKE_C_STANDARD 99)
1311
set (CMAKE_CXX_STANDARD 14)
12+
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
13+
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
1414

1515
if (NOT DEFINED WAMR_BUILD_PLATFORM)
1616
set (WAMR_BUILD_PLATFORM "linux")
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
RUN apt-get update && apt-get install -y \
9+
cmake build-essential git
10+
11+
WORKDIR /home/wamr
12+
13+
COPY . .
14+
15+
WORKDIR /home/wamr/core/iwasm/libraries/wasi-nn/test/build
16+
17+
RUN cmake \
18+
-DWAMR_BUILD_WASI_NN=1 \
19+
-DTFLITE_ENABLE_GPU=ON \
20+
..
21+
22+
RUN make -j $(grep -c ^processor /proc/cpuinfo)
Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,23 @@
11
# Copyright (C) 2019 Intel Corporation. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

4-
FROM ubuntu:22.04
4+
FROM ubuntu:20.04
55

66
ENV DEBIAN_FRONTEND=noninteractive
77

88
RUN apt-get update && apt-get install -y \
99
cmake build-essential git wget python3.10 python3-pip
1010

11-
ARG WASI_SDK_VER=16
11+
ARG WASI_SDK_VER=19
1212
RUN wget -c --progress=dot:giga https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_SDK_VER}/wasi-sdk-${WASI_SDK_VER}.0-linux.tar.gz -P /opt \
1313
&& tar xf /opt/wasi-sdk-${WASI_SDK_VER}.0-linux.tar.gz -C /opt \
1414
&& ln -fs /opt/wasi-sdk-${WASI_SDK_VER}.0 /opt/wasi-sdk \
1515
&& rm /opt/wasi-sdk-${WASI_SDK_VER}.0-linux.tar.gz
1616

17-
WORKDIR /home/wamr
18-
19-
COPY core/deps/install_tensorflow.sh core/deps/install_tensorflow.sh
20-
RUN ./core/deps/install_tensorflow.sh
17+
WORKDIR /wasi-nn/test
2118

2219
COPY core/iwasm/libraries/wasi-nn/test/requirements.txt .
23-
RUN pip3 install -r requirements.txt
24-
25-
COPY core core
26-
COPY build-scripts build-scripts
27-
COPY product-mini product-mini
28-
29-
WORKDIR /home/wamr/core/iwasm/libraries/wasi-nn/test/build
30-
31-
RUN cmake -DWAMR_BUILD_WASI_NN=1 ..
32-
RUN make -j $(grep -c ^processor /proc/cpuinfo)
33-
34-
WORKDIR /home/wamr/core/iwasm/libraries/wasi-nn/test
3520

36-
RUN ./build.sh
21+
RUN pip3 install -r requirements.txt && rm requirements.txt
3722

38-
ENTRYPOINT [ "./build/iwasm", "--dir=.", "test_tensorflow.wasm" ]
23+
ENTRYPOINT [ "bash", "./build.sh" ]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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
5+
6+
COPY --from=wasi-nn-base /home/wamr/core/iwasm/libraries/wasi-nn/test/build/iwasm /run/iwasm
7+
8+
ENTRYPOINT [ "/run/iwasm" ]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright (C) 2019 Intel Corporation. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
FROM nvidia/cuda:11.3.0-runtime-ubuntu20.04
5+
6+
RUN apt-get update && apt-get install -y --no-install-recommends \
7+
ocl-icd-libopencl1 \
8+
ocl-icd-opencl-dev \
9+
clinfo && \
10+
rm -rf /var/lib/apt/lists/*
11+
12+
RUN mkdir -p /etc/OpenCL/vendors && \
13+
echo "libnvidia-opencl.so.1" > /etc/OpenCL/vendors/nvidia.icd
14+
15+
ENV NVIDIA_VISIBLE_DEVICES=all
16+
ENV NVIDIA_DRIVER_CAPABILITIES=compute,utility
17+
18+
COPY --from=wasi-nn-base /home/wamr/core/iwasm/libraries/wasi-nn/test/build/iwasm /run/iwasm
19+
20+
ENTRYPOINT [ "/run/iwasm" ]

core/iwasm/libraries/wasi-nn/test/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
-Wl,--allow-undefined \
88
-Wl,--strip-all,--no-entry \
99
--sysroot=/opt/wasi-sdk/share/wasi-sysroot \
10-
-I/home/wamr/core/iwasm/libraries/wasi-nn \
10+
-I.. \
1111
-o test_tensorflow.wasm test_tensorflow.c
1212

1313
# TFLite models to use in the tests

0 commit comments

Comments
 (0)