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 | Bin 1444 -> 0 bytes .../patches/0001-fix-private-descriptor.patch | Bin 0 -> 3672 bytes ...thout-over-reading-in-micro_allocati.patch | Bin 2136 -> 0 bytes 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, 53 insertions(+), 29 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 4e0fba20f4e3d2456d3ab93564506c6acfd89e01..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1444 zcmbu9QE!_t6oudYEAC^v1Tc_5LZ_+GO1DalR<&98Fiptd>)?rCWSpc`+x_;LG+Wm# zt11N{kS@N*NB+*`RI3FHa2y9=o+B52=*Jw3fDXcOTtsmki!l`D#QSSY4y`<{FXTX3wi`3HK1d3nHCVhFU9~X>61> zsBP2V*FA(*#99fg=?YsYN>tvFLi0TMQlcqQ+h7TSx`mRh5OTyC80W~Lm34t&mw?^KKo=ia zGfR|J#j>*b<8G&vD^)>lBb6DI{r;9IAzCzTc0?TF0L)Ia^Rro+U8d7d*J*Zrb~*bz zInPeg^K_OHcsS~8J#2i{Y=LdVy1hl0;#VbQcRK%j>N}TjlRz=dX%V`vKVp=R#;;1k zZowDH?{4Wx52F4UdiMDT=MBoV8^6o#^gUsIUbcj{{F9E)kAFK44neK#C`*oT_s}hp xwSh<7gTfA|>Bd&S0ipDQnLeyoR!LiBdfq^5dQ98G2fshacsx2lW=!xf$*Yh1%kkRK*(C#<7C0{TD$9zUg`bzn_ZiR zwiH4u5mkz_-kq79d7gQuPo-GH1hELbz|S$r0&nbR<1owod>X}G5l_N!GR~rL;g8@% zRPY9C@MG}2nf-&n^L*zinVG@gOw}mi`9~o&JVL2gSSqaGk58+Q;*jgc%|HF+JYgDV zuxPLkfe$C_luQI61f!WB&%zP(JTmFLY0{6F=^4C#b+mZ=9Q>jG0Sdl_TJjB9fE-mO zd98(XoWWq=fW}G*S(M`Qu;dztYo19lT;p2E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0h2_e#Gs)3|P8P`Z2M6at&2;GsnN~ z#`xW^czN0Of&VNDFTJ3t&WwQVJVyf@*v{Wt>!v9BFF-y3LdTlvK5ST08nhD()$Xie Y^57Cpe>?@Y(+JJtPT-D0*LR%%0m-+&pa1{> literal 0 HcmV?d00001 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 ea94925e1fe525ebd0b4a0f75e8e8178ded6c506..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2136 zcmcgt+iv4F5PjEIOkXyVZCN+lv6a}!W{b1|n(kr~v=2dQP}0z5BZ&q{B|%&C+dH&m zZ-QL|H42n4K$Dy~Ji|FNx>B-&Fh0dR8ci8SNivD^Fyz^ckCPcs#xpj{i)nruGCqPE zS;H-EAUcIGOt(LX!!UBL2quLarUa|uveEfk3w*{$g!5{OYr&n<6V$7qAY^V<0>$`6bAh*cwn&BQt?tg2X;4TcMn~h!gUQ5Rv9Yb zO`Tg(aB|0gBG+5POrxPiPt*5%*4IS((%z0LO@Bc1krRO$Kb5Smv7nVkqupHA+ECOc ziI=9=^G?$LGp=|xN~38CsFYMGoDdJtM%8(>>6qnfrg{TtX?6=O{=lX2PC#!k7hGTg znQZC;;so#|M=UgG*{B@BnnDQo{;%6B<^)bzmK)zD-RAsZ8!iAbfxm1Pr+Ao}xFIk!64Inrymz0SzH|MD>Hmg_x`6nt>vVKN;?j`I&p_qi+p 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.