Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Efforts to support latest tflite-micro #71

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions .vscode/c_cpp_properties.json

This file was deleted.

65 changes: 0 additions & 65 deletions .vscode/launch.json

This file was deleted.

34 changes: 0 additions & 34 deletions .vscode/tasks.json

This file was deleted.

30 changes: 16 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ IF(NOT TF_DIR)
SET(TF_DIR "../tensorflow" CACHE STRING "TensorFlow source directory")
ENDIF()

GET_FILENAME_COMPONENT(tf_fullpath ${TF_DIR} REALPATH)
GET_FILENAME_COMPONENT(TF_ABSPATH ${TF_DIR} REALPATH)

IF(NOT GET_TF_SRC)
if(EXISTS "${tf_fullpath}")
SET(TFL_SRC ${TF_DIR}/tensorflow/lite)
if(EXISTS "${TF_ABSPATH}")
SET(TFL_SRC ${TF_ABSPATH}/tensorflow/lite)
SET(TFLM_SRC ${TFL_SRC}/micro)
SET(TFLMD_SRC ${TFLM_SRC}/tools/make/downloads)
SET(TF_INCS
${TF_DIR}
${TFLMD_SRC}/flatbuffers/include
${TFLMD_SRC}/ruy
${TF_ABSPATH}
${TFLMD_SRC}/flatbuffers/include
${TFLMD_SRC}/ruy
)
IF(WIN32)
SET(TF_LIB ${TFLM_SRC}/tools/make/gen/windows_x86_64/lib/libtensorflow-microlite.a)
ELSE()
SET(TF_LIB ${TFLM_SRC}/tools/make/gen/linux_x86_64/lib/libtensorflow-microlite.a)
ENDIF()
ELSE()
MESSAGE(FATAL_ERROR "\
No valid TensorFlow source directory provided, default path \
Expand All @@ -32,17 +37,18 @@ ELSE()
SET(TF_INCS
${TFLite_INCLUDE_DIRS}
)
SET(TF_LIB tensorflow-microlite)
ENDIF()

SET(TF_LIB tensorflow-microlite)

SET(COMPILER_HEADERS
${PROJECT_SOURCE_DIR}/src/CodeWriter.h
${PROJECT_SOURCE_DIR}/src/Compiler.h
${PROJECT_SOURCE_DIR}/src/CustomOperators.h
${PROJECT_SOURCE_DIR}/src/MemMap.h
${PROJECT_SOURCE_DIR}/src/RecordAllocations.h
${PROJECT_SOURCE_DIR}/src/TypeToString.h
${PROJECT_SOURCE_DIR}/src/BuiltinAllocations.h
${PROJECT_SOURCE_DIR}/src/ModelInfo.h
)

SET(COMPILER_SRCS
Expand All @@ -52,6 +58,7 @@ SET(COMPILER_SRCS
${PROJECT_SOURCE_DIR}/src/MemMap.cc
${PROJECT_SOURCE_DIR}/src/RecordAllocations.cc
${PROJECT_SOURCE_DIR}/src/TypeToString.cc
${PROJECT_SOURCE_DIR}/src/BuiltinAllocations.cc
${PROJECT_SOURCE_DIR}/src/main.cc
)

Expand All @@ -63,16 +70,11 @@ TARGET_INCLUDE_DIRECTORIES(${PROJECT_NAME} PUBLIC
${TF_INCS}
)

IF(WIN32)
TARGET_LINK_DIRECTORIES(${PROJECT_NAME} PUBLIC ${TFLM_SRC}/tools/make/gen/windows_x86_64/lib)
ELSE()
TARGET_LINK_DIRECTORIES(${PROJECT_NAME} PUBLIC ${TFLM_SRC}/tools/make/gen/linux_x86_64/lib)
ENDIF()

TARGET_LINK_LIBRARIES(${PROJECT_NAME} PUBLIC ${TF_LIB})

TARGET_COMPILE_DEFINITIONS(${PROJECT_NAME} PUBLIC
TF_LITE_STATIC_MEMORY
TFLMC_USE_INTERPRETER_HOOKS
TF_LITE_DISABLE_X86_NEON
SUFFICIENT_ARENA_SIZE=128*1024*1024
)
Expand Down
27 changes: 18 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
TF_DIR=../tensorflow
include common.mk

.PHONY: tflite all
.PHONY: tflite all

all: compiler examples
all: compiler$(EXE_SUFFIX) examples

$(TF_MICROLITE_LIB): tflite

tflite:
$(MAKE) -C $(TF_DIR) -f tensorflow/lite/micro/tools/make/Makefile microlite

COMPILER_OBJS = src/main.o src/Compiler.o src/CodeWriter.o src/TypeToString.o src/RecordAllocations.o src/MemMap.o src/CustomOperators.o
COMPILER_OBJS = src/main.o src/Compiler.o src/CodeWriter.o src/TypeToString.o src/RecordAllocations.o src/MemMap.o src/CustomOperators.o src/BuiltinAllocations.o

compiler: $(COMPILER_OBJS) tflite
$(CXX) $(LDOPTS) -o $@ $(COMPILER_OBJS) $(LIBS)
compiler$(EXE_SUFFIX): $(COMPILER_OBJS) $(TF_MICROLITE_LIB)
$(CXX) $(CXXFLAGS) $(LDOPTS) -o $@ $(COMPILER_OBJS) $(LIBS)

clean: clean-compiler clean-examples
$(MAKE) -C $(TF_DIR) -f tensorflow/lite/micro/tools/make/makefile clean
$(MAKE) -C $(TF_DIR) -f tensorflow/lite/micro/tools/make/Makefile clean

FORMAT_FILES := $(shell find src -regex '.*\(h\|cpp\)')

format:
clang-format -i $(FORMAT_FILES)

.PHONY: examples clean-examples clean-compiler
examples:
cd examples && $(MAKE)

examples: tflite
$(MAKE) -C examples all

run_examples: tflite
$(MAKE) -C examples run_all

regenerate: compiler$(EXE_SUFFIX)
$(MAKE) -C examples regenerate

clean-examples:
$(MAKE) -C examples clean

clean-compiler:
$(RM) src/*.o compiler
$(RM) src/*.o compiler$(EXE_SUFFIX)

10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ CMake using the option `TF_TAG`.
e.g.

``` bash
cmake -DGET_TF_SRC=ON TF_TAG=v2.2.0 ..
cmake -DGET_TF_SRC=ON -DTF_TAG=v2.2.0 ..
```

Similarly a Git commit hash can be provided using `TF_COMMIT`. Note that
Expand All @@ -57,7 +57,7 @@ Similarly a Git commit hash can be provided using `TF_COMMIT`. Note that
e.g.

```bash
cmake -DGET_TF_SRC=ON TF_COMMIT=0fecf6f89fd7bacc1ec4213b946a254e885b82ac ..
cmake -DGET_TF_SRC=ON -DTF_COMMIT=0fecf6f89fd7bacc1ec4213b946a254e885b82ac ..
```

To checkout a different TensorFlow code base without clearing the CMake cache
Expand All @@ -67,7 +67,7 @@ source to be checked-out again.
e.g.

```bash
cmake -DGET_TF_SRC=ON -DTF_RECACHE=ON TF_COMMIT=0fecf6f89fd7bacc1ec4213b946a254e885b82ac ..
cmake -DGET_TF_SRC=ON -DTF_RECACHE=ON -DTF_COMMIT=0fecf6f89fd7bacc1ec4213b946a254e885b82ac ..
```

## Providing TensorFlow Source Manually
Expand All @@ -79,7 +79,7 @@ providing the argument `TF_DIR`.
e.g.

``` bash
cmake -DTF_DIR=../../my_tf_source ..
cmake -DTF_DIR=../my_tensorflow ..
```

## Additional Targets
Expand Down Expand Up @@ -117,7 +117,7 @@ make format
./compiler hello_world.tflite hello_compiled.cpp hello_
```

- for a quick view into the generated code see [`compiled_hello.cpp`](https://github.com/cpetig/tflite_micro_compiler/blob/master/examples/compiled_hello.cpp)
- for a quick view into the generated code see [`compiled_hello_world.cc`](https://github.com/cpetig/tflite_micro_compiler/blob/master/examples/compiled_hello_world.cc)

You can compare calling into interpreter and compiled code between [`hello_world.cc`](https://github.com/cpetig/tflite_micro_compiler/blob/master/examples/hello_world.cc)
and [`hello_world2.cc`](https://github.com/cpetig/tflite_micro_compiler/blob/master/examples/hello_world2.cc)
Expand Down
Loading