From 9c1e664e361f4190697c1c3669391b22ddc6c15d Mon Sep 17 00:00:00 2001 From: Kasper Hjort Berthelsen Date: Wed, 22 May 2024 14:25:15 +0200 Subject: [PATCH] pkg/tflite-micro: Update tflite-micro to latest version --- pkg/tflite-micro/Makefile | 20 +++- pkg/tflite-micro/Makefile.dep | 3 + ...01-fix-build-with-private-descriptor.patch | 40 -------- .../patches/0001-fix-private-descriptor.patch | 91 +++++++++++++++++++ ...thout-over-reading-in-micro_allocati.patch | 38 -------- pkg/tflite-micro/tflite-core-c.mk | 5 + .../tflite-micro-arena_allocator.mk | 6 ++ .../tflite-micro-tflite_bridge.mk | 5 + .../external_modules/mnist/main_functions.cc | 43 ++++----- 9 files changed, 144 insertions(+), 107 deletions(-) delete mode 100644 pkg/tflite-micro/patches/0001-fix-build-with-private-descriptor.patch create mode 100644 pkg/tflite-micro/patches/0001-fix-private-descriptor.patch delete mode 100644 pkg/tflite-micro/patches/0002-Compare-names-without-over-reading-in-micro_allocati.patch create mode 100644 pkg/tflite-micro/tflite-core-c.mk create mode 100644 pkg/tflite-micro/tflite-micro-arena_allocator.mk create mode 100644 pkg/tflite-micro/tflite-micro-tflite_bridge.mk diff --git a/pkg/tflite-micro/Makefile b/pkg/tflite-micro/Makefile index 1b3da69b61..4f5b71a65b 100644 --- a/pkg/tflite-micro/Makefile +++ b/pkg/tflite-micro/Makefile @@ -1,7 +1,7 @@ PKG_NAME=tflite-micro PKG_URL=https://github.com/tensorflow/tflite-micro -# sync from 2022.04.07 -PKG_VERSION=1501b574b74fd7877aba30aa9d8b667f41b139c3 +# sync from 2024.05.21 +PKG_VERSION=8e22946b3faa51564df5dd9194f7540b2694892c PKG_LICENSE=Apache 2.0 include $(RIOTBASE)/pkg/pkg.mk @@ -14,23 +14,29 @@ CFLAGS += -Wno-unused-parameter TFLITE_MODULES := \ tflite-c \ tflite-core-api \ + tflite-core-c \ tflite-kernels \ tflite-kernels-internal \ tflite-kernels-internal-reference \ tflite-micro \ + tflite-micro-arena_allocator \ tflite-micro-kernels \ tflite-micro-memory-planner \ + tflite-micro-tflite_bridge \ tflite-schema \ # DIR_tflite-c := tensorflow/lite/c DIR_tflite-core-api := tensorflow/lite/core/api +DIR_tflite-core-c := tensorflow/lite/core/c DIR_tflite-kernels := tensorflow/lite/kernels DIR_tflite-kernels-internal := tensorflow/lite/kernels/internal DIR_tflite-kernels-internal-reference := tensorflow/lite/kernels/internal/reference DIR_tflite-micro := tensorflow/lite/micro +DIR_tflite-micro-arena_allocator := tensorflow/lite/micro/arena_allocator DIR_tflite-micro-kernels := tensorflow/lite/micro/kernels DIR_tflite-micro-memory-planner := tensorflow/lite/micro/memory_planner +DIR_tflite-micro-tflite_bridge := tensorflow/lite/micro/tflite_bridge DIR_tflite-schema := tensorflow/lite/schema TFLITE_MODULES_USED := $(filter $(TFLITE_MODULES),$(USEMODULE)) @@ -40,5 +46,13 @@ all: $(TFLITE_MODULES_USED) .PHONY: tflite-% -tflite-%: +# .PHONY: $(PKG_SOURCE_DIR)/tensorflow/lite/schema/schema_generated.h + +$(PKG_SOURCE_DIR)/tensorflow/lite/schema/schema_generated.h: $(PKG_SOURCE_DIR)/tensorflow/lite/schema/schema.fbs $(FLATC) + $(FLATC) --cpp -o "$(dir $@)" "$<" + +tflite-%: $(PKG_SOURCE_DIR)/tensorflow/lite/schema/schema_generated.h $(QQ)"$(MAKE)" -C $(PKG_SOURCE_DIR)/$(DIR_$@) -f $(CURDIR)/$@.mk + +$(FLATC): + $(Q)make -C "$(dir $@)" diff --git a/pkg/tflite-micro/Makefile.dep b/pkg/tflite-micro/Makefile.dep index 1d9bbd14a9..b4cab0836b 100644 --- a/pkg/tflite-micro/Makefile.dep +++ b/pkg/tflite-micro/Makefile.dep @@ -6,12 +6,15 @@ USEPKG += ruy USEMODULE += tflite-c USEMODULE += tflite-core-api +USEMODULE += tflite-core-c USEMODULE += tflite-kernels USEMODULE += tflite-kernels-internal USEMODULE += tflite-kernels-internal-reference USEMODULE += tflite-micro +USEMODULE += tflite-micro-arena_allocator USEMODULE += tflite-micro-kernels USEMODULE += tflite-micro-memory-planner +USEMODULE += tflite-micro-tflite_bridge USEMODULE += tflite-schema # This package doesn't work on riscv diff --git a/pkg/tflite-micro/patches/0001-fix-build-with-private-descriptor.patch b/pkg/tflite-micro/patches/0001-fix-build-with-private-descriptor.patch deleted file mode 100644 index 4e0fba20f4..0000000000 --- a/pkg/tflite-micro/patches/0001-fix-build-with-private-descriptor.patch +++ /dev/null @@ -1,40 +0,0 @@ -From 4e9723bbedf1317dec204397c6777f90ff76646d Mon Sep 17 00:00:00 2001 -From: Alexandre Abadie -Date: Thu, 7 Apr 2022 17:07:58 +0200 -Subject: [PATCH 1/1] fix build with private descriptor - ---- - tensorflow/lite/micro/memory_planner/greedy_memory_planner.h | 2 -- - tensorflow/lite/micro/micro_error_reporter.h | 3 --- - 2 files changed, 5 deletions(-) - -diff --git a/tensorflow/lite/micro/memory_planner/greedy_memory_planner.h b/tensorflow/lite/micro/memory_planner/greedy_memory_planner.h -index a34f3c5..b8269be 100644 ---- a/tensorflow/lite/micro/memory_planner/greedy_memory_planner.h -+++ b/tensorflow/lite/micro/memory_planner/greedy_memory_planner.h -@@ -158,8 +158,6 @@ class GreedyMemoryPlanner : public MicroMemoryPlanner { - - // Whether buffers have been added since the last plan was calculated. - bool need_to_calculate_offsets_; -- -- TF_LITE_REMOVE_VIRTUAL_DELETE - }; - - } // namespace tflite -diff --git a/tensorflow/lite/micro/micro_error_reporter.h b/tensorflow/lite/micro/micro_error_reporter.h -index 0e3b0c3..18a0d89 100644 ---- a/tensorflow/lite/micro/micro_error_reporter.h -+++ b/tensorflow/lite/micro/micro_error_reporter.h -@@ -46,9 +46,6 @@ class MicroErrorReporter : public ErrorReporter { - public: - ~MicroErrorReporter() override {} - int Report(const char* format, va_list args) override; -- -- private: -- TF_LITE_REMOVE_VIRTUAL_DELETE - }; - - } // namespace tflite --- -2.32.0 - diff --git a/pkg/tflite-micro/patches/0001-fix-private-descriptor.patch b/pkg/tflite-micro/patches/0001-fix-private-descriptor.patch new file mode 100644 index 0000000000..4b050cb376 --- /dev/null +++ b/pkg/tflite-micro/patches/0001-fix-private-descriptor.patch @@ -0,0 +1,91 @@ +From 8eef2021de2c2051c54cc1d9670f784485c65f13 Mon Sep 17 00:00:00 2001 +From: Kasper Hjort Berthelsen +Date: Tue, 21 May 2024 23:17:43 +0200 +Subject: [PATCH 1/1] fix private descriptor + +--- + tensorflow/lite/micro/memory_planner/greedy_memory_planner.h | 4 ++-- + tensorflow/lite/micro/memory_planner/linear_memory_planner.h | 4 ++-- + tensorflow/lite/micro/micro_allocator.h | 4 ++-- + tensorflow/lite/micro/tflite_bridge/micro_error_reporter.h | 1 - + 4 files changed, 6 insertions(+), 7 deletions(-) + +diff --git a/tensorflow/lite/micro/memory_planner/greedy_memory_planner.h b/tensorflow/lite/micro/memory_planner/greedy_memory_planner.h +index b2cdb617..77b9a1cc 100644 +--- a/tensorflow/lite/micro/memory_planner/greedy_memory_planner.h ++++ b/tensorflow/lite/micro/memory_planner/greedy_memory_planner.h +@@ -112,6 +112,8 @@ class GreedyMemoryPlanner : public MicroMemoryPlanner { + // that aren't being used during a phase of invocation are overwritten. + bool preserves_all_tensors() const override { return false; } + ++ TF_LITE_REMOVE_VIRTUAL_DELETE ++ + private: + // Whether a buffer is active in a given time range. + bool DoesEntryOverlapInTime(const ListEntry* entry, const int first_time_used, +@@ -161,8 +163,6 @@ class GreedyMemoryPlanner : public MicroMemoryPlanner { + + // Whether buffers have been added since the last plan was calculated. + bool need_to_calculate_offsets_; +- +- TF_LITE_REMOVE_VIRTUAL_DELETE + }; + + } // namespace tflite +diff --git a/tensorflow/lite/micro/memory_planner/linear_memory_planner.h b/tensorflow/lite/micro/memory_planner/linear_memory_planner.h +index 9850569f..4fbdf54e 100644 +--- a/tensorflow/lite/micro/memory_planner/linear_memory_planner.h ++++ b/tensorflow/lite/micro/memory_planner/linear_memory_planner.h +@@ -39,13 +39,13 @@ class LinearMemoryPlanner : public MicroMemoryPlanner { + // invocation. + bool preserves_all_tensors() const override { return true; } + ++ TF_LITE_REMOVE_VIRTUAL_DELETE ++ + private: + static constexpr int kMaxBufferCount = 1024; + size_t buffer_offsets_[kMaxBufferCount]; + int current_buffer_count_; + size_t next_free_offset_; +- +- TF_LITE_REMOVE_VIRTUAL_DELETE + }; + + } // namespace tflite +diff --git a/tensorflow/lite/micro/micro_allocator.h b/tensorflow/lite/micro/micro_allocator.h +index 4eff167d..b7895127 100644 +--- a/tensorflow/lite/micro/micro_allocator.h ++++ b/tensorflow/lite/micro/micro_allocator.h +@@ -291,6 +291,8 @@ class MicroAllocator { + int subgraph_idx, + bool allocate_temp); + ++ TF_LITE_REMOVE_VIRTUAL_DELETE ++ + private: + // Commits a memory plan for all non-persistent buffer allocations in the + // 'head' section of the memory arena. The eval_tensors pointer is the list of +@@ -339,8 +341,6 @@ class MicroAllocator { + // Holds the byte length of the memory plan with the largest head usage. Used + // to ensure that multi-tenant allocations can share the head for buffers. + size_t max_head_buffer_usage_ = 0; +- +- TF_LITE_REMOVE_VIRTUAL_DELETE + }; + + } // namespace tflite +diff --git a/tensorflow/lite/micro/tflite_bridge/micro_error_reporter.h b/tensorflow/lite/micro/tflite_bridge/micro_error_reporter.h +index d3702f46..186a226e 100644 +--- a/tensorflow/lite/micro/tflite_bridge/micro_error_reporter.h ++++ b/tensorflow/lite/micro/tflite_bridge/micro_error_reporter.h +@@ -28,7 +28,6 @@ class MicroErrorReporter : public ErrorReporter { + ~MicroErrorReporter() override {} + int Report(const char* format, va_list args) override; + +- private: + TF_LITE_REMOVE_VIRTUAL_DELETE + }; + +-- +2.34.1 + diff --git a/pkg/tflite-micro/patches/0002-Compare-names-without-over-reading-in-micro_allocati.patch b/pkg/tflite-micro/patches/0002-Compare-names-without-over-reading-in-micro_allocati.patch deleted file mode 100644 index ea94925e1f..0000000000 --- a/pkg/tflite-micro/patches/0002-Compare-names-without-over-reading-in-micro_allocati.patch +++ /dev/null @@ -1,38 +0,0 @@ -From 027ec148ae13362c00fb9f539f359a9cd8c70af4 Mon Sep 17 00:00:00 2001 -From: Marian Buschsieweke -Date: Fri, 11 Nov 2022 08:57:01 +0100 -Subject: [PATCH] Compare names without over-reading in - micro_allocation_info.cc - -This fixes: - - /home/maribu/Repos/software/RIOT/build/pkg/tflite-micro/tensorflow/lite/micro/micro_allocation_info.cc: In member function 'TfLiteStatus tflite::AllocationInfoBuilder::GetOfflinePlannedOffsets(const int32_t**)': - /home/maribu/Repos/software/RIOT/build/pkg/tflite-micro/tensorflow/lite/micro/micro_allocation_info.cc:294:18: error: 'int strncmp(const char*, const char*, size_t)' specified bound 23 exceeds source size 0 [-Werror=stringop-overread] - 294 | if (strncmp(metadata->name()->c_str(), kOfflineMemAllocMetadata, - | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - 295 | strlen(kOfflineMemAllocMetadata)) == 0) { - | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ---- - tensorflow/lite/micro/micro_allocation_info.cc | 6 ++++-- - 1 file changed, 4 insertions(+), 2 deletions(-) - -diff --git a/tensorflow/lite/micro/micro_allocation_info.cc b/tensorflow/lite/micro/micro_allocation_info.cc -index ab313e6..1e61135 100644 ---- a/tensorflow/lite/micro/micro_allocation_info.cc -+++ b/tensorflow/lite/micro/micro_allocation_info.cc -@@ -291,8 +291,10 @@ TfLiteStatus AllocationInfoBuilder::GetOfflinePlannedOffsets( - if (model_->metadata()) { - for (size_t i = 0; i < model_->metadata()->size(); ++i) { - auto metadata = model_->metadata()->Get(i); -- if (strncmp(metadata->name()->c_str(), kOfflineMemAllocMetadata, -- strlen(kOfflineMemAllocMetadata)) == 0) { -+ const size_t len_b = sizeof(kOfflineMemAllocMetadata) - 1; -+ size_t len_a = metadata->name()->size(); -+ if ((len_a == len_b) && -+ !memcmp(kOfflineMemAllocMetadata, metadata->name()->c_str(), len_b)) { - const flatbuffers::Vector>* buffers = - model_->buffers(); - auto* buffer = (*buffers)[metadata->buffer()]; --- -2.38.1 - diff --git a/pkg/tflite-micro/tflite-core-c.mk b/pkg/tflite-micro/tflite-core-c.mk new file mode 100644 index 0000000000..bb496416d6 --- /dev/null +++ b/pkg/tflite-micro/tflite-core-c.mk @@ -0,0 +1,5 @@ +MODULE = tflite-core-c + +SRCXXEXT = cc + +include $(RIOTBASE)/Makefile.base diff --git a/pkg/tflite-micro/tflite-micro-arena_allocator.mk b/pkg/tflite-micro/tflite-micro-arena_allocator.mk new file mode 100644 index 0000000000..48897a9b60 --- /dev/null +++ b/pkg/tflite-micro/tflite-micro-arena_allocator.mk @@ -0,0 +1,6 @@ +MODULE = tflite-micro-arena_allocator + +SRCXXEXT = cc +SRCXXEXCLUDE = $(wildcard *_test.$(SRCXXEXT)) + +include $(RIOTBASE)/Makefile.base diff --git a/pkg/tflite-micro/tflite-micro-tflite_bridge.mk b/pkg/tflite-micro/tflite-micro-tflite_bridge.mk new file mode 100644 index 0000000000..1368474e74 --- /dev/null +++ b/pkg/tflite-micro/tflite-micro-tflite_bridge.mk @@ -0,0 +1,5 @@ +MODULE = tflite-micro-tflite_bridge + +SRCXXEXT = cc + +include $(RIOTBASE)/Makefile.base diff --git a/tests/pkg/tflite-micro/external_modules/mnist/main_functions.cc b/tests/pkg/tflite-micro/external_modules/mnist/main_functions.cc index ffd753dae0..590fcb3309 100644 --- a/tests/pkg/tflite-micro/external_modules/mnist/main_functions.cc +++ b/tests/pkg/tflite-micro/external_modules/mnist/main_functions.cc @@ -17,17 +17,11 @@ #include #include "kernel_defines.h" -#if IS_USED(MODULE_TENSORFLOW_LITE) -#include "tensorflow/lite/micro/kernels/all_ops_resolver.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" -#include "tensorflow/lite/micro/micro_interpreter.h" -#include "tensorflow/lite/version.h" -#else -#include "tensorflow/lite/micro/all_ops_resolver.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" + +#include "tensorflow/lite/micro/micro_mutable_op_resolver.h" #include "tensorflow/lite/micro/micro_interpreter.h" #include "tensorflow/lite/micro/system_setup.h" -#endif + #include "tensorflow/lite/schema/schema_generated.h" #include "blob/digit.h" @@ -37,7 +31,6 @@ // Globals, used for compatibility with Arduino-style sketches. namespace { - tflite::ErrorReporter* error_reporter = nullptr; const tflite::Model* model = nullptr; tflite::MicroInterpreter* interpreter = nullptr; TfLiteTensor* input = nullptr; @@ -52,15 +45,6 @@ namespace { // The name of this function is important for Arduino compatibility. void setup() { -#if IS_USED(MODULE_TFLITE_MICRO) - tflite::InitializeTarget(); -#endif - - // Set up logging. Google style is to avoid globals or statics because of - // lifetime uncertainty, but since this has a trivial destructor it's okay. - // NOLINTNEXTLINE(runtime-global-variables) - static tflite::MicroErrorReporter micro_error_reporter; - error_reporter = µ_error_reporter; // Map the model into a usable data structure. This doesn't involve any // copying or parsing, it's a very lightweight operation. @@ -74,16 +58,23 @@ void setup() } // This pulls in all the operation implementations we need. - // NOLINTNEXTLINE(runtime-global-variables) -#if IS_USED(MODULE_TFLITE_MICRO) - static tflite::AllOpsResolver resolver; -#else - static tflite::ops::micro::AllOpsResolver resolver; -#endif + static tflite::MicroMutableOpResolver<4> resolver; + if (resolver.AddFullyConnected() != kTfLiteOk) { + return; + } + if (resolver.AddQuantize() != kTfLiteOk) { + return; + } + if (resolver.AddDequantize() != kTfLiteOk) { + return; + } + if (resolver.AddSoftmax() != kTfLiteOk) { + return; + } // Build an interpreter to run the model with. static tflite::MicroInterpreter static_interpreter( - model, resolver, tensor_arena, kTensorArenaSize, error_reporter); + model, resolver, tensor_arena, kTensorArenaSize); interpreter = &static_interpreter; // Allocate memory from the tensor_arena for the model's tensors.