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

Feature/update tfm #70

Open
wants to merge 6 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
10 changes: 7 additions & 3 deletions .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ jobs:
matrix:
config:
- {name: "Linux", os: ubuntu-latest, cmake-generator: ""}
- {name: "Windows", os: windows-latest, cmake-generator: "-G \"MinGW Makefiles\""}
runs-on: ${{ matrix.config.os }}
name: ${{ matrix.config.name }}

Expand All @@ -17,10 +16,15 @@ jobs:
- name: Requirements
shell: bash
run: |
pip install pillow
cd ..
git clone https://github.com/tensorflow/tensorflow.git
git clone https://github.com/tensorflow/tflite-micro.git tensorflow
cd tensorflow
make -f tensorflow/lite/micro/tools/make/Makefile hello_world
git checkout f474248365ad48654ba8a27ac5bf49a6afbb80e7
git apply ../tflite_micro_compiler/patches/tflite-micro.patch
git apply ../tflite_micro_compiler/patches/tflite-micro-makefile.patch
make -Bf tensorflow/lite/micro/tools/make/Makefile NO_INTERPRETER=TRUE microlite -j2
make -Bf tensorflow/lite/micro/tools/make/Makefile microlite -j2
- name: Requirements (Windows)
if: matrix.config.os == 'windows-latest'
run: |
Expand Down
6 changes: 2 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ IF(NOT GET_TF_SRC)
${TFLMD_SRC}/ruy
)
IF(WIN32)
SET(TF_LIB ${TFLM_SRC}/tools/make/gen/windows_x86_64/lib/libtensorflow-microlite.a)
SET(TF_LIB ${TFLM_SRC}/tools/make/gen/windows_x86_64_default/lib/libtensorflow-microlite.a)
ELSE()
SET(TF_LIB ${TFLM_SRC}/tools/make/gen/linux_x86_64/lib/libtensorflow-microlite.a)
SET(TF_LIB ${TFLM_SRC}/tools/make/gen/linux_x86_64_default/lib/libtensorflow-microlite.a)
ENDIF()
ELSE()
MESSAGE(FATAL_ERROR "\
Expand All @@ -45,7 +45,6 @@ SET(COMPILER_HEADERS
${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
)

Expand All @@ -54,7 +53,6 @@ SET(COMPILER_SRCS
${PROJECT_SOURCE_DIR}/src/Compiler.cc
${PROJECT_SOURCE_DIR}/src/CustomOperators.cc
${PROJECT_SOURCE_DIR}/src/MemMap.cc
${PROJECT_SOURCE_DIR}/src/RecordAllocations.cc
${PROJECT_SOURCE_DIR}/src/TypeToString.cc
${PROJECT_SOURCE_DIR}/src/main.cc
)
Expand Down
11 changes: 8 additions & 3 deletions examples/generic_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ TARGET_INCLUDE_DIRECTORIES(${PROJECT_NAME} PUBLIC
)

IF(WIN32)
TARGET_LINK_DIRECTORIES(${PROJECT_NAME} PUBLIC ${TFLM_SRC}/tools/make/gen/windows_x86_64/lib)
TARGET_LINK_DIRECTORIES(${PROJECT_NAME} PUBLIC ${TFLM_SRC}/tools/make/gen/windows_x86_64_default/lib)
ELSE()
TARGET_LINK_DIRECTORIES(${PROJECT_NAME} PUBLIC ${TFLM_SRC}/tools/make/gen/linux_x86_64/lib)
TARGET_LINK_DIRECTORIES(${PROJECT_NAME} PUBLIC ${TFLM_SRC}/tools/make/gen/linux_x86_64_default/lib)
ENDIF()
TARGET_LINK_LIBRARIES(${PROJECT_NAME} PUBLIC tensorflow-microlite)
TARGET_LINK_LIBRARIES(${PROJECT_NAME} PUBLIC tensorflow-microlite-no-interpreter)

TARGET_COMPILE_DEFINITIONS(${PROJECT_NAME} PUBLIC
TF_LITE_STATIC_MEMORY
TF_LITE_DISABLE_X86_NEON
NO_INTERPRETER
)

TARGET_COMPILE_FEATURES(${PROJECT_NAME} PUBLIC
cxx_std_14
)
57 changes: 57 additions & 0 deletions patches/tflite-micro-makefile.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
diff --git a/tensorflow/lite/micro/tools/make/Makefile b/tensorflow/lite/micro/tools/make/Makefile
index e25b3bc..09ce481 100644
--- a/tensorflow/lite/micro/tools/make/Makefile
+++ b/tensorflow/lite/micro/tools/make/Makefile
@@ -150,6 +150,10 @@ COMMON_FLAGS := \
$(CC_WARNINGS) \
$(ADDITIONAL_DEFINES)

+ifeq ($(NO_INTERPRETER),TRUE)
+ COMMON_FLAGS += -DNO_INTERPRETER
+endif
+
ifeq ($(TARGET), $(HOST_OS))
# If we are not doing a cross-compilation then -DTF_LITE_USE_CTIME is what we
# want to have by default.
@@ -233,7 +237,11 @@ endif

# This library is the main target for this makefile. It will contain a minimal
# runtime that can be linked in to other programs.
-MICROLITE_LIB_NAME := libtensorflow-microlite.a
+ifeq ($(NO_INTERPRETER),TRUE)
+ MICROLITE_LIB_NAME := libtensorflow-microlite-no-interpreter.a
+else
+ MICROLITE_LIB_NAME := libtensorflow-microlite.a
+endif

# Where compiled objects are stored.
GENDIR := $(MAKEFILE_DIR)/gen/$(TARGET)_$(TARGET_ARCH)_$(BUILD_TYPE)/
@@ -469,6 +477,28 @@ THIRD_PARTY_KERNEL_CC_SRCS :=
# Load custom kernels.
include $(MAKEFILE_DIR)/additional_kernels.inc

+ifeq ($(NO_INTERPRETER),TRUE)
+ MICRO_LITE_NO_INTERPRETER_SRCS := \
+ tensorflow/lite/micro/fake_micro_context.cc \
+ tensorflow/lite/micro/micro_context.cc \
+ tensorflow/lite/micro/micro_allocator.cc \
+ tensorflow/lite/micro/micro_graph.cc \
+ tensorflow/lite/micro/micro_interpreter.cc
+
+ MICRO_LITE_UNSUPPORTED_KERNELS_SRCS := \
+ tensorflow/lite/micro/kernels/assign_variable.cc \
+ tensorflow/lite/micro/kernels/call_once.cc \
+ tensorflow/lite/micro/kernels/if.cc \
+ tensorflow/lite/micro/kernels/kernel_runner.cc \
+ tensorflow/lite/micro/kernels/read_variable.cc \
+ tensorflow/lite/micro/kernels/unidirectional_sequence_lstm.cc \
+ tensorflow/lite/micro/kernels/while.cc \
+ tensorflow/lite/micro/kernels/var_handle.cc
+
+ MICROLITE_CC_KERNEL_SRCS := $(filter-out $(MICRO_LITE_UNSUPPORTED_KERNELS_SRCS), $(MICROLITE_CC_KERNEL_SRCS))
+ MICROLITE_CC_BASE_SRCS := $(filter-out $(MICRO_LITE_NO_INTERPRETER_SRCS), $(MICROLITE_CC_BASE_SRCS))
+endif
+
MICROLITE_CC_SRCS := $(filter-out $(MICROLITE_TEST_SRCS), $(MICROLITE_CC_BASE_SRCS))
MICROLITE_CC_SRCS := $(filter-out $(MICROLITE_BENCHMARK_SRCS), $(MICROLITE_CC_SRCS))

52 changes: 52 additions & 0 deletions patches/tflite-micro.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
diff --git a/tensorflow/lite/micro/micro_context.h b/tensorflow/lite/micro/micro_context.h
index e7be654..2693ff2 100644
--- a/tensorflow/lite/micro/micro_context.h
+++ b/tensorflow/lite/micro/micro_context.h
@@ -20,6 +20,22 @@ limitations under the License.
#include "tensorflow/lite/micro/micro_allocator.h"
#include "tensorflow/lite/micro/micro_graph.h"

+#ifdef NO_INTERPRETER
+
+namespace tflite {
+ struct MicroContext{
+ TfLiteTensor* (*AllocateTempInputTensor)(const TfLiteNode* node, int index);
+ TfLiteTensor* (*AllocateTempOutputTensor)(const TfLiteNode* node, int index);
+ void (*DeallocateTempTfLiteTensor)(TfLiteTensor* tensor);
+ void* (*external_context)();
+ };
+ static inline MicroContext* GetMicroContext(const struct TfLiteContext* context){
+ return reinterpret_cast<MicroContext*>(context->impl_);
+ }
+}
+
+#else
+
namespace tflite {
// MicroContext is eventually going to become the API between TFLM and the
// kernels, replacing all the functions in TfLiteContext. The end state is code
@@ -158,4 +174,6 @@ void MicroContextReportOpError(struct TfLiteContext* context,

} // namespace tflite

+#endif // NO_INTERPRETER
+
#endif // TENSORFLOW_LITE_MICRO_MICRO_CONTEXT_H_
diff --git a/tensorflow/lite/micro/micro_interpreter.h b/tensorflow/lite/micro/micro_interpreter.h
index ae7fc8f..5c2fd6e 100644
--- a/tensorflow/lite/micro/micro_interpreter.h
+++ b/tensorflow/lite/micro/micro_interpreter.h
@@ -140,6 +140,13 @@ class MicroInterpreter {
// arena_used_bytes() + 16.
size_t arena_used_bytes() const { return allocator_.used_bytes(); }

+ size_t operators_size() const { return model_->subgraphs()->Get(0)->operators()->size(); }
+
+ // For debugging only.
+ const NodeAndRegistration node_and_registration(int node_index) {
+ return graph_.GetAllocations()[0].node_and_registrations[node_index];
+ }
+
protected:
const MicroAllocator& allocator() const { return allocator_; }
const TfLiteContext& context() const { return context_; }
30 changes: 28 additions & 2 deletions src/CodeWriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,25 @@ void tflmc::CodeWriter::writeBuiltin(tflite::BuiltinOperator op,
TfLiteAddParams const* p = (TfLiteAddParams const*)data;
out_ << to_string(p->activation) << " };";
} break;
case tflite::BuiltinOperator_MEAN: {
out_ << "TfLiteReducerParams " << name << " = { ";
TfLiteReducerParams const* p = (TfLiteReducerParams const*)data;
out_ << p->keep_dims << " };";
} break;
case tflite::BuiltinOperator_MUL: {
out_ << "TfLiteMulParams " << name << " = { ";
TfLiteMulParams const* p = (TfLiteMulParams const*)data;
out_ << to_string(p->activation) << " };";
} break;
case tflite::BuiltinOperator_PACK: {
out_ << "TfLitePackParams " << name << " = { ";
TfLitePackParams const* p = (TfLitePackParams const*)data;
out_ << p->values_count << ", " << p->axis << " };";
} break;
case tflite::BuiltinOperator_SHAPE: {
out_ << "TfLiteShapeParams " << name << " = { "
<< " };";
} break;
case tflite::BuiltinOperator_SUB: {
out_ << "TfLiteSubParams " << name << " = { ";
TfLiteSubParams const* p = (TfLiteSubParams const*)data;
Expand All @@ -130,6 +144,19 @@ void tflmc::CodeWriter::writeBuiltin(tflite::BuiltinOperator op,
(TfLiteConcatenationParams const*)data;
out_ << p->axis << ", " << to_string(p->activation) << " };";
} break;
case tflite::BuiltinOperator_STRIDED_SLICE: {
out_ << "TfLiteStridedSliceParams " << name << " = { ";
TfLiteStridedSliceParams const* p = (TfLiteStridedSliceParams const*)data;
out_ << p->begin_mask << ", " << p->end_mask << ", " << p->ellipsis_mask
<< ", " << p->new_axis_mask << ", " << p->shrink_axis_mask << " };";
} break;
case tflite::BuiltinOperator_TRANSPOSE_CONV: {
out_ << "TfLiteTransposeConvParams " << name << " = { ";
TfLiteTransposeConvParams const* p =
(TfLiteTransposeConvParams const*)data;
out_ << to_string(p->padding) << ", " << p->stride_width << ", "
<< p->stride_height << " };";
} break;
default: {
size_t datalen = GetBuiltinDataSize(op, subgraph_);
uint32_t alignment = datalen >= 4 ? 4 : datalen >= 2 ? 2 : 1;
Expand Down Expand Up @@ -224,8 +251,7 @@ static void dump_tensor_contents(std::ostream& out_, const TfLiteTensor& t,
int outer_dim = t.dims->data[0];
int middle_dim = t.dims->data[t.dims->size - 2];
int inner_dim = t.dims->data[t.dims->size - 1];
for (int i = 1; i < t.dims->size - 2; ++i)
outer_dim *= t.dims->data[i];
for (int i = 1; i < t.dims->size - 2; ++i) outer_dim *= t.dims->data[i];
for (int i = 0; i < outer_dim; ++i) {
// out_ << "\n ";
// uint32_t outer_index = inner_dim * middle_dim;
Expand Down
1 change: 0 additions & 1 deletion src/CodeWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <iostream>

#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/version.h"

namespace tflmc {

Expand Down
Loading