Skip to content

Commit 0a85714

Browse files
authored
Resurrect Tensorflow v2 (#99)
* gh: add tflow as submodule * gha: build tflow lite as part of the tests, and use actions/checkout * func: add TF minimal func and cmakelists * inv: update tasks to build tflow * make tensorflow installation path independent * gha: fix workflow file * gha: use new formatting task and actions checkout * gha: fix failing test * set safe directory * gh: bump code version * fix after rebase
1 parent 370d04a commit 0a85714

File tree

12 files changed

+158
-4
lines changed

12 files changed

+158
-4
lines changed

.env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
SYSROOT_VERSION=0.1.8
2-
SYSROOT_CLI_IMAGE=faasm/cpp-sysroot:0.1.8
1+
SYSROOT_VERSION=0.1.9
2+
SYSROOT_CLI_IMAGE=faasm/cpp-sysroot:0.1.9
33
COMPOSE_PROJECT_NAME=cpp-dev

.github/workflows/tests.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
if: github.event.pull_request.draft == false
2020
runs-on: ubuntu-20.04
2121
container:
22-
image: faasm/cpp-sysroot:0.1.8
22+
image: faasm/cpp-sysroot:0.1.9
2323
steps:
2424
# --- Update code ---
2525
- name: "Checkout code"
@@ -52,6 +52,8 @@ jobs:
5252
run: ./bin/inv_wrapper.sh libpng
5353
- name: "Build ImageMagick"
5454
run: ./bin/inv_wrapper.sh imagemagick
55+
- name: "Build Tensorflow lite"
56+
run: ./bin/inv_wrapper.sh tensorflow
5557
# --- Build functions to wasm ---
5658
- name: "Build the functions"
5759
run: ./bin/inv_wrapper.sh func.local --clean

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@
3232
path = third-party/zlib
3333
url = https://github.com/faasm/zlib.git
3434
branch = faasm
35+
[submodule "third-party/tensorflow"]
36+
path = third-party/tensorflow
37+
url = https://github.com/faasm/tensorflow.git
38+
branch = faasm

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1.8
1+
0.1.9

faasmtools/build.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
FAASM_LOCAL_DIR = environ.get("FAASM_LOCAL_DIR", "/usr/local/faasm")
88
FAASM_NATIVE_DIR = join(FAASM_LOCAL_DIR, "native")
99
WASM_SYSROOT = join(FAASM_LOCAL_DIR, "llvm-sysroot")
10+
WASM_HEADER_INSTALL = "{}/include".format(WASM_SYSROOT)
1011
WASM_LIB_INSTALL = "{}/lib/wasm32-wasi".format(WASM_SYSROOT)
1112
WASM_TOOLCHAIN_ROOT = "/usr/local/faasm/toolchain"
1213
WASM_TOOLCHAIN_TOOLS = join(WASM_TOOLCHAIN_ROOT, "tools")

func/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,4 @@ add_subdirectory(dynlink)
9595
add_subdirectory(ffmpeg)
9696
add_subdirectory(mpi)
9797
add_subdirectory(omp)
98+
add_subdirectory(tf)

func/tf/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Set where the library has been built
2+
if (FAASM_BUILD_TYPE STREQUAL "wasm")
3+
set(TF_LIB ${CMAKE_SYSROOT}/lib/wasm32-wasi/libtensorflow-lite.a)
4+
else()
5+
message(FATAL_ERROR "Tensorflow can only be built for WebAssembly")
6+
endif()
7+
8+
faasm_func(minimal minimal.cpp)
9+
10+
set(TF_HEADERS
11+
${CMAKE_SYSROOT}/include/tensorflow
12+
${CMAKE_SYSROOT}/include/tensorflow/tensorflow/lite/tools/make/downloads/flatbuffers/include
13+
)
14+
15+
target_include_directories(minimal PUBLIC ${TF_HEADERS})
16+
target_link_libraries(minimal faasm ${TF_LIB})
17+
add_custom_target(tf_all_funcs DEPENDS minimal)

func/tf/minimal.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <faasm/faasm.h>
2+
#include <faasm/input.h>
3+
4+
#include "tensorflow/lite/interpreter.h"
5+
#include "tensorflow/lite/kernels/register.h"
6+
#include "tensorflow/lite/model.h"
7+
#include "tensorflow/lite/optional_debug_tools.h"
8+
9+
#define INPUT_FILE_PATH "faasm://tflite/sample_model.tflite"
10+
#define TFLITE_MINIMAL_CHECK(x) \
11+
if (!(x)) { \
12+
fprintf(stderr, "Error at %s:%d\n", __FILE__, __LINE__); \
13+
exit(1); \
14+
}
15+
16+
/**
17+
* This is an example that is minimal to read a model
18+
* from disk to check if the lib was compiled successfully.
19+
*
20+
* Example inspired from:
21+
* https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/examples/minimal/minimal.cc
22+
*/
23+
24+
int main()
25+
{
26+
// Load model from file
27+
std::unique_ptr<tflite::FlatBufferModel> model =
28+
tflite::FlatBufferModel::BuildFromFile(INPUT_FILE_PATH);
29+
TFLITE_MINIMAL_CHECK(model != nullptr);
30+
31+
// Build the interpreter with the InterpreterBuilder.
32+
tflite::ops::builtin::BuiltinOpResolver resolver;
33+
tflite::InterpreterBuilder builder(*model, resolver);
34+
std::unique_ptr<tflite::Interpreter> interpreter;
35+
builder(&interpreter);
36+
TFLITE_MINIMAL_CHECK(interpreter != nullptr);
37+
38+
// Allocate tensor buffers.
39+
TFLITE_MINIMAL_CHECK(interpreter->AllocateTensors() == kTfLiteOk);
40+
printf("=== Pre-invoke Interpreter State ===\n");
41+
tflite::PrintInterpreterState(interpreter.get());
42+
43+
return 0;
44+
}

tasks/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from . import libfake
1717
from . import libffi
1818
from . import libpng
19+
from . import tensorflow
1920
from . import zlib
2021

2122
ns = Collection(
@@ -35,5 +36,6 @@
3536
libfake,
3637
libffi,
3738
libpng,
39+
tensorflow,
3840
zlib,
3941
)

tasks/func.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,4 @@ def local(ctx, clean=False, debug=False):
181181
user(ctx, "ffmpeg", clean, debug)
182182
user(ctx, "mpi", clean, debug)
183183
user(ctx, "omp", clean, debug)
184+
user(ctx, "tf", clean, debug)

0 commit comments

Comments
 (0)