-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from spider-gazelle/gpu-delegate
feat: add GPU delegate support
- Loading branch information
Showing
14 changed files
with
462 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# Use an image supported by https://rocm.docs.amd.com/projects/install-on-linux/en/latest/tutorial/quick-start.html | ||
FROM ubuntu:22.04 as build | ||
|
||
# Install necessary packages | ||
RUN apt-get update && \ | ||
apt-get install -y \ | ||
build-essential \ | ||
cmake \ | ||
git \ | ||
wget \ | ||
unzip \ | ||
libtool \ | ||
zlib1g-dev \ | ||
vim-common \ | ||
curl \ | ||
unzip \ | ||
zlib1g \ | ||
python3 \ | ||
python3-pip \ | ||
python3-dev \ | ||
libopenblas-dev \ | ||
opencl-headers \ | ||
clinfo \ | ||
ocl-icd-opencl-dev \ | ||
clang \ | ||
libclang-dev \ | ||
libc++-dev \ | ||
linux-headers-generic \ | ||
software-properties-common \ | ||
libabsl-dev \ | ||
libusb-1.0-0-dev \ | ||
gnupg2 && \ | ||
apt-get clean | ||
|
||
# Install Bazelisk for building TensorFlow | ||
ARG TARGETARCH | ||
RUN wget -O /usr/local/bin/bazel https://github.com/bazelbuild/bazelisk/releases/download/v1.10.1/bazelisk-linux-$TARGETARCH && \ | ||
chmod +x /usr/local/bin/bazel | ||
|
||
ENV TMP=/tmp | ||
|
||
# Clone TensorFlow repository | ||
# https://www.tensorflow.org/install/source#gpu (lib compatibility list) | ||
RUN git clone --depth 1 --branch "v2.16.1" https://github.com/tensorflow/tensorflow | ||
|
||
# ======================= | ||
# build edge TPU delegate | ||
# ======================= | ||
|
||
WORKDIR /tensorflow | ||
RUN git clone https://github.com/google-coral/libedgetpu | ||
WORKDIR /tensorflow/libedgetpu | ||
|
||
# Build TensorFlow Lite GPU delegate (excluding Android, linux only) | ||
ARG TARGETPLATFORM | ||
RUN if [ "$TARGETPLATFORM" = "linux/amd64" ]; then \ | ||
make libedgetpu-direct CPU=k8 && \ | ||
mkdir -p /usr/local/lib && \ | ||
cp /tensorflow/libedgetpu/out/direct/k8/libedgetpu.so.1.0 /usr/local/lib/libedgetpu.so; \ | ||
elif [ "$TARGETPLATFORM" = "linux/arm64" ]; then \ | ||
make libedgetpu-direct CPU=aarch64 && \ | ||
mkdir -p /usr/local/lib && \ | ||
cp /tensorflow/libedgetpu/out/direct/aarch64/libedgetpu.so.1.0 /usr/local/lib/libedgetpu.so; \ | ||
else \ | ||
echo "Unknown platform"; \ | ||
exit 1; \ | ||
fi | ||
|
||
# ================================== | ||
# Build tensorflow lite GPU delegate | ||
# ================================== | ||
|
||
WORKDIR /tensorflow | ||
|
||
# Configure TensorFlow build (excluding Android) | ||
RUN ./configure <<EOF | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
EOF | ||
|
||
# Build TensorFlow Lite GPU delegate (excluding Android, linux only) | ||
RUN bazel build //tensorflow/lite/delegates/gpu:libtensorflowlite_gpu_delegate.so \ | ||
--config=opt \ | ||
--config=monolithic \ | ||
--copt=-g \ | ||
--cxxopt=-std=c++17 \ | ||
--copt=-DMESA_EGL_NO_X11_HEADERS \ | ||
--copt=-DEGL_NO_X11 \ | ||
--copt=-DCL_DELEGATE_NO_GL \ | ||
--define=with_xla_support=false \ | ||
--define=with_flex_support=false \ | ||
--define=no_tensorflow_py_deps=true \ | ||
--config=noaws \ | ||
--config=nogcp \ | ||
--config=nohdfs \ | ||
--verbose_failures | ||
|
||
# Copy the built shared libraries to /usr/local/lib | ||
RUN mkdir -p /usr/local/lib && \ | ||
cp bazel-bin/tensorflow/lite/delegates/gpu/libtensorflowlite_gpu_delegate.so /usr/local/lib/ | ||
|
||
# ================================= | ||
# Build tensorflow lite using cmake | ||
# ================================= | ||
|
||
RUN mkdir tflite_build | ||
WORKDIR /tensorflow/tflite_build | ||
RUN cmake /tensorflow/tensorflow/lite/c -DTFLITE_ENABLE_GPU=ON | ||
RUN cmake --build . -j4 || true | ||
RUN echo "---------- WE ARE BUILDING AGAIN!! ----------" | ||
RUN cmake --build . -j1 | ||
|
||
# copy the shard lib into place | ||
RUN cp ./libtensorflowlite_c.so /usr/local/lib/ | ||
|
||
# ====================== | ||
# Set up the final stage | ||
# ====================== | ||
FROM scratch | ||
|
||
# Copy the built libraries from the build stage | ||
COPY --from=build /usr/local/lib/libedgetpu.so /usr/local/lib/libedgetpu.so | ||
COPY --from=build /usr/local/lib/libtensorflowlite_c.so /usr/local/lib/libtensorflowlite_c.so | ||
COPY --from=build /usr/local/lib/libtensorflowlite_gpu_delegate.so /usr/local/lib/libtensorflowlite_gpu_delegate.so |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/bin/sh | ||
|
||
SHARDS_INSTALL=IS_LIB | ||
IS_LOCAL=./ext/libtensorflowlite_c.so | ||
if test -f "$IS_LOCAL"; then | ||
echo "--" | ||
echo "tensorflow lite library installed, skipping installation" | ||
echo "--" | ||
exit 0 | ||
fi | ||
|
||
echo "--" | ||
echo "downloading images... (requires docker)" | ||
echo "--" | ||
|
||
mkdir -p ./ext | ||
docker pull stakach/tensorflowlite:latest | ||
docker create --name tflite_tmp stakach/tensorflowlite:latest true | ||
|
||
echo "--" | ||
echo "copying library into place.." | ||
echo "--" | ||
|
||
docker cp tflite_tmp:/usr/local/lib/libedgetpu.so ./ext/libedgetpu.so | ||
docker cp tflite_tmp:/usr/local/lib/libtensorflowlite_c.so ./ext/libtensorflowlite_c.so | ||
docker cp tflite_tmp:/usr/local/lib/libtensorflowlite_gpu_delegate.so ./ext/libtensorflowlite_gpu_delegate.so | ||
docker rm tflite_tmp | ||
|
||
# we'll put the lib into a few different places so it'll run when using crystal normally | ||
|
||
# Temp location crystal runs applications from | ||
mkdir -p ~/.cache/crystal/ | ||
cp ./ext/libedgetpu.so ~/.cache/crystal/ | ||
cp ./ext/libtensorflowlite_c.so ~/.cache/crystal/ | ||
cp ./ext/libtensorflowlite_gpu_delegate.so ~/.cache/crystal/ | ||
|
||
# other locations you might be running the application from | ||
# check if being installed as a lib | ||
if [ "$1" = "$SHARDS_INSTALL" ]; then | ||
echo "copying into parent directory.." | ||
mkdir -p ../../bin | ||
ln -s ./ext/libedgetpu.so ../../bin/libedgetpu.so | ||
ln -s ./ext/libedgetpu.so ../../libedgetpu.so | ||
|
||
ln -s ./ext/libtensorflowlite_c.so ../../bin/libtensorflowlite_c.so | ||
ln -s ./ext/libtensorflowlite_c.so ../../libtensorflowlite_c.so | ||
|
||
ln -s ./ext/libtensorflowlite_gpu_delegate.so ../../bin/libtensorflowlite_gpu_delegate.so | ||
ln -s ./ext/libtensorflowlite_gpu_delegate.so ../../libtensorflowlite_gpu_delegate.so | ||
else | ||
echo "run manually, assuming library development" | ||
fi | ||
|
||
echo "--" | ||
echo "Done" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
@[Include( | ||
"tensorflow/lite/builtin_ops.h", | ||
"tensorflow/lite/core/c/c_api_types.h", | ||
"tensorflow/lite/core/c/c_api.h", | ||
"tensorflow/lite/core/c/c_api_experimental.h", | ||
"tensorflow/lite/delegates/gpu/delegate_options.h", | ||
"tensorflow/lite/delegates/gpu/delegate.h", | ||
flags: " | ||
-I/{tensorflow_dir}/tensorflow/ | ||
-I/{tensorflow_dir}/tensorflow/bazel-genfiles | ||
-I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1 | ||
", | ||
prefix: %w(TFL_ TfLite kTfLite) | ||
)] | ||
@[Link("tensorflowlite_c", ldflags: "-L#{__DIR__}/../../ext/ -Wl,-rpath='$ORIGIN'")] | ||
lib LibTensorflowLite | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.