Skip to content

Commit

Permalink
More attempts to make things work
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsmeding committed May 29, 2023
1 parent 2fd6c61 commit 1f06366
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 112 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ libtf-dl/

# Generated from cabal.project.in
cabal.project

# Downloaded Bazel executable
bazel511
34 changes: 26 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
.PHONY: all help setup submodules tfbuild libtf-dl/lib/libtensorflow.so cabal.project
BAZEL_BIN_DIR := extra-deps/tensorflow-haskell/third_party/tensorflow/bazel-bin

.PHONY: all help setup submodules tflitebuild tf-lib-links cabal.project

all: help

help:
@echo "This Makefile defines the following targets:"
@echo " 'setup': all of the following:"
@echo " 'submodules': Sets up the Git submodules"
@echo " 'tfbuild': Builds tensorflow inside build/"
@echo " 'libtf-dl/lib/libtensorflow.so': Downloads libtensorflow.so"
@echo " 'tflitebuild': Builds tensorflow lite inside build/"
@echo " 'tfbuild': Builds full tensorflow with bazel"
@echo " 'tf-lib-links': Additional .so symlinks in bazel bin dir"
@echo " 'cabal.project': Creates cabal.project from cabal.project.in (with envsubst)"

setup: submodules tfbuild libtf-dl/lib/libtensorflow.so cabal.project
setup: submodules tflitebuild tfbuild cabal.project

submodules:
@if git status --porcelain | grep extra-deps >/dev/null; then \
Expand All @@ -19,14 +22,29 @@ submodules:
fi
git submodule update --init --recursive

tfbuild:
tflitebuild:
mkdir -p build
cd build && cmake ../extra-deps/tensorflow-haskell/third_party/tensorflow/tensorflow/lite -DBUILD_SHARED_LIBS=1
cd build && cmake --build . -j

libtf-dl/lib/libtensorflow.so:
mkdir libtf-dl
curl https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-2.10.1.tar.gz | tar xz -C libtf-dl
tfbuild: bazel511
cd extra-deps/tensorflow-haskell/third_party/tensorflow && ../../../../bazel511 build //tensorflow/tools/pip_package:build_pip_package
cd extra-deps/tensorflow-haskell/third_party/tensorflow && ../../../../run-with-PATH-dir.sh python=python3 -- bazel-bin/tensorflow/tools/pip_package/build_pip_package ../../../../accelerate-tensorflow-lite/tf-python-venv
virtualenv accelerate-tensorflow-lite/tf-python-venv
accelerate-tensorflow-lite/tf-python-venv/bin/pip3 install accelerate-tensorflow-lite/tf-python-venv/tensorflow-2.10.1-*.whl

bazel511:
curl -L https://github.com/bazelbuild/bazel/releases/download/5.1.1/bazel-5.1.1-linux-x86_64 >$@
chmod +x $@

tf-lib-links: $(BAZEL_BIN_DIR)/tensorflow/libtensorflow.so $(BAZEL_BIN_DIR)/tensorflow/libtensorflow.so.2 $(BAZEL_BIN_DIR)/tensorflow/libtensorflow.so.2.10

$(BAZEL_BIN_DIR)/tensorflow/libtensorflow.so:
ln -vs $@.2.10.1 $@
$(BAZEL_BIN_DIR)/tensorflow/libtensorflow.so.2:
ln -vs $@.10.1 $@
$(BAZEL_BIN_DIR)/tensorflow/libtensorflow.so.2.10:
ln -vs $@.1 $@

cabal.project: cabal.project.in
envsubst '$$PWD' <$< >$@
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@ Please feel free to contact me through [GitHub](https://github.com/AccelerateHS/
```sh
# The following 4 commands can also be done in one go using `make setup`:
make submodules # git submodule update --init --recursive
make tfbuild # build Tensorflow Lite inside newly-created build/ directory
make libtf-dl/lib/libtensorflow.so # download a libtensorflow, because Bazel is a nuisance
make tflitebuild # build Tensorflow Lite inside newly-created build/ directory
make tfbuild # build Tensorflow with Bazel (Bazel is auto-downloaded)
make tf-lib-links # extra symlinks in Bazel bin folder because Bazel is dumb
make cabal.project # rewrites $PWD in cabal.project.in to make cabal.project

env LD_LIBRARY_PATH=$PWD/libtf-dl/lib cabal build all
LIBDIR="$PWD/extra-deps/tensorflow-haskell/third_party/tensorflow/bazel-bin/tensorflow"

env LD_LIBRARY_PATH="$LIBDIR" cabal build all

# To run tests:
env LD_LIBRARY_PATH="$LIBDIR:$PWD/build" cabal run nofib-tensorflow-lite
```

This uses the Tensorflow submodule already contained within the tensorflow-haskell submodule.
Expand Down
6 changes: 5 additions & 1 deletion accelerate-tensorflow-lite/accelerate-tensorflow-lite.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ extra-source-files:
cbits/edgetpu.h
data-files:
converter.py
tf-python-venv/**/*.py
tf-python-venv/**/*.pyc

source-repository head
type: git
Expand All @@ -42,14 +44,16 @@ library
System.IO.Temp
System.Process.Extra
Paths_accelerate_tensorflow_lite
autogen-modules:
Paths_accelerate_tensorflow_lite
hs-source-dirs:
src
ghc-options: -O2 -Wall -Werror=tabs
cc-options: -O3 -Wall -march=native
cxx-sources:
cbits/edgetpu.cc
extra-libraries:
tensorflow-lite
tensorflow-lite edgetpu
build-depends:
accelerate ==1.3.*
, accelerate-tensorflow
Expand Down
98 changes: 0 additions & 98 deletions accelerate-tensorflow-lite/package.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ edgetpu_compile path = do
tflite_model :: TF.GraphDef -> Builder -> IO FilePath
tflite_model graph xs = do
convert <- getDataFileName "converter.py"
python_exe <- getDataFileName "tf-python-venv/bin/python3"
tmp_dir <- getTemporaryDirectory
(pb_file, pb_h) <- openBinaryTempFile tmp_dir "model.pb" -- TODO: be exception safe
(tf_file, tf_h) <- openBinaryTempFile tmp_dir "model.tflite" -- TODO: be exception safe
Expand All @@ -127,7 +128,7 @@ tflite_model graph xs = do
inputs = filter (T.isPrefixOf "input") names
outputs = filter (T.isPrefixOf "output") names
--
cp = (proc "python3" flags) { std_in = NoStream, std_out = NoStream, std_err = CreatePipe }
cp = (proc python_exe flags) { std_in = NoStream, std_out = NoStream, std_err = CreatePipe }
flags = [ convert
, "--graph_def_file=" ++ pb_file
, "--output_file=" ++ tf_file
Expand Down
3 changes: 2 additions & 1 deletion cabal.project.in
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ package accelerate-tensorflow-lite

package tensorflow
extra-lib-dirs:
$PWD/libtf-dl/lib
$PWD/lib-links
$PWD/extra-deps/tensorflow-haskell/third_party/tensorflow/bazel-bin/tensorflow

package accelerate
flags: +nofib
41 changes: 41 additions & 0 deletions run-with-PATH-dir.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash
set -euo pipefail

tmpdir=$(mktemp -d)
trap "rm -rf '$tmpdir'" EXIT

if [[ $# -ge 2 && ( $1 = '-h' || $1 = '--help' ) ]]; then
echo "Usage: $0 [name=path/to/target] -- command..."
exit 0
fi

while true; do
arg=$1
shift

if [[ $arg = '--' ]]; then
break
fi
if ! echo "$arg" | grep -F '=' >/dev/null; then
echo "$0: no '--' found before end of remapping list"
exit 1
fi

key=${arg%%=*}
value=${arg#*=}
if echo "$key" | grep -F '/' >/dev/null; then
echo "$0: name cannot contain '/'"
exit 1
fi
if [[ -e "$tmpdir/$key" ]]; then
echo "$0: duplicate name '$key'"
exit 1
fi
if echo "$value" | grep -F '/' >/dev/null; then
ln -s "$(realpath "$value")" "$tmpdir/$key"
else
ln -s "$(which "$value")" "$tmpdir/$key"
fi
done

env PATH="$tmpdir:$PATH" "$@"

0 comments on commit 1f06366

Please sign in to comment.