This is an automated email from the ASF dual-hosted git repository. xiaoxiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
commit 71cbcb26180a3fb81af861edc3ae179cafa32a0c Author: renzhiyuan1 <renzhiyu...@xiaomi.com> AuthorDate: Fri Nov 17 19:01:19 2023 +0800 Add configurations files. Add configuration files of TFLite Micro, its dependent third-party libraries and CMSIS_NN. Signed-off-by: renzhiyuan1 <renzhiyu...@xiaomi.com> --- math/gemmlowp/Kconfig | 25 +++++++ math/gemmlowp/Make.defs | 23 ++++++ math/gemmlowp/Makefile | 41 +++++++++++ math/kissfft/Kconfig | 25 +++++++ math/kissfft/Make.defs | 23 ++++++ math/kissfft/Makefile | 45 ++++++++++++ math/kissfft/kissfft.patch | 116 ++++++++++++++++++++++++++++++ math/ruy/Kconfig | 25 +++++++ math/ruy/Make.defs | 23 ++++++ math/ruy/Makefile | 42 +++++++++++ mlearning/cmsis-nn/Kconfig | 25 +++++++ mlearning/cmsis-nn/Make.defs | 25 +++++++ mlearning/cmsis-nn/Makefile | 51 +++++++++++++ mlearning/tflite-micro/Kconfig | 24 +++++++ mlearning/tflite-micro/Make.defs | 23 ++++++ mlearning/tflite-micro/Makefile | 83 +++++++++++++++++++++ mlearning/tflite-micro/tflite-micro.patch | 14 ++++ system/flatbuffers/Kconfig | 25 +++++++ system/flatbuffers/Make.defs | 25 +++++++ system/flatbuffers/Makefile | 40 +++++++++++ system/flatbuffers/flatbuffers.patch | 100 ++++++++++++++++++++++++++ 21 files changed, 823 insertions(+) diff --git a/math/gemmlowp/Kconfig b/math/gemmlowp/Kconfig new file mode 100644 index 000000000..b4b4a5a4a --- /dev/null +++ b/math/gemmlowp/Kconfig @@ -0,0 +1,25 @@ +############################################################################ +# apps/math/gemmlowp/Kconfig +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +config MATH_GEMMLOWP + bool "Gemmlowp" + default n + ---help--- + Enable gemmlowp. diff --git a/math/gemmlowp/Make.defs b/math/gemmlowp/Make.defs new file mode 100644 index 000000000..ecd7abd03 --- /dev/null +++ b/math/gemmlowp/Make.defs @@ -0,0 +1,23 @@ +############################################################################ +# apps/math/gemmlowp/Make.defs +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +ifneq ($(CONFIG_MATH_GEMMLOWP),) +CONFIGURED_APPS += $(APPDIR)/math/gemmlowp +endif diff --git a/math/gemmlowp/Makefile b/math/gemmlowp/Makefile new file mode 100644 index 000000000..3f5b76456 --- /dev/null +++ b/math/gemmlowp/Makefile @@ -0,0 +1,41 @@ +############################################################################ +# apps/math/gemmlowp/Makefile +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include $(APPDIR)/Make.defs + +MODULE = $(CONFIG_MATH_GEMMLOWP) + +GEMMLOWP_VER = 719139ce755a0f31cbf1c37f7f98adcc7fc9f425 + +gemmlowp.zip: + $(Q) curl -L https://github.com/google/gemmlowp/archive/$(GEMMLOWP_VER).zip -o gemmlowp.zip + $(Q) unzip -o gemmlowp.zip + $(Q) mv gemmlowp-$(GEMMLOWP_VER) gemmlowp + +# Download and unpack tarball if no git repo found +ifeq ($(wildcard gemmlowp/.git),) +context:: gemmlowp.zip + +distclean:: + $(call DELDIR, gemmlowp) + $(call DELFILE, gemmlowp.zip) +endif + +include $(APPDIR)/Application.mk diff --git a/math/kissfft/Kconfig b/math/kissfft/Kconfig new file mode 100644 index 000000000..686c116e6 --- /dev/null +++ b/math/kissfft/Kconfig @@ -0,0 +1,25 @@ +############################################################################ +# apps/math/kissfft/Kconfig +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +config MATH_KISSFFT + bool "kissfft" + default n + ---help--- + Enable kissfft. diff --git a/math/kissfft/Make.defs b/math/kissfft/Make.defs new file mode 100644 index 000000000..3da1486c9 --- /dev/null +++ b/math/kissfft/Make.defs @@ -0,0 +1,23 @@ +############################################################################ +# apps/math/kissfft/Make.defs +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +ifneq ($(CONFIG_MATH_KISSFFT),) +CONFIGURED_APPS += $(APPDIR)/math/kissfft +endif diff --git a/math/kissfft/Makefile b/math/kissfft/Makefile new file mode 100644 index 000000000..3a3b2e824 --- /dev/null +++ b/math/kissfft/Makefile @@ -0,0 +1,45 @@ +############################################################################ +# apps/math/kissfft/Makefile +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include $(APPDIR)/Make.defs + +CSRCS += kissfft/tools/kiss_fftr.c +CSRCS += kissfft/kiss_fft.c + +CFLAGS += ${INCDIR_PREFIX}$(APPDIR)/math/kissfft/kissfft + +KISSFFT_VER = 130 +KISSFFT_UNPACK = kissfft + +kissfft.zip: + $(Q) curl -L https://github.com/mborgerding/kissfft/archive/refs/tags/v$(KISSFFT_VER).zip -o kissfft.zip + $(Q) unzip -o kissfft.zip + $(Q) mv kissfft-$(KISSFFT_VER) kissfft + $(Q) patch -d $(KISSFFT_UNPACK) -p1 < kissfft.patch + +# Download and unpack tarball if no git repo found +ifeq ($(wildcard kissfft/.git),) +context:: kissfft.zip +distclean:: + $(call DELDIR, kissfft) + $(call DELFILE, kissfft.zip) +endif + +include $(APPDIR)/Application.mk diff --git a/math/kissfft/kissfft.patch b/math/kissfft/kissfft.patch new file mode 100644 index 000000000..404d91f9e --- /dev/null +++ b/math/kissfft/kissfft.patch @@ -0,0 +1,116 @@ +diff --git a/_kiss_fft_guts.h b/_kiss_fft_guts.h +index ba66144..1a0f4c2 100644 +--- a/_kiss_fft_guts.h ++++ b/_kiss_fft_guts.h +@@ -1,3 +1,6 @@ ++#ifndef _KISS_FFT_GUTS_H ++#define _KISS_FFT_GUTS_H ++ + /* + Copyright (c) 2003-2010, Mark Borgerding + +@@ -135,7 +138,7 @@ struct kiss_fft_state{ + #else + # define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase) + # define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase) +-# define HALF_OF(x) ((x)*.5) ++# define HALF_OF(x) ((x)*(kiss_fft_scalar).5) + #endif + + #define kf_cexp(x,phase) \ +@@ -162,3 +165,4 @@ struct kiss_fft_state{ + #define KISS_FFT_TMP_ALLOC(nbytes) KISS_FFT_MALLOC(nbytes) + #define KISS_FFT_TMP_FREE(ptr) KISS_FFT_FREE(ptr) + #endif ++#endif // _KISS_FFT_GUTS_H +diff --git a/kiss_fft.c b/kiss_fft.c +index 465d6c9..9133a01 100644 +--- a/kiss_fft.c ++++ b/kiss_fft.c +@@ -375,7 +375,7 @@ void kiss_fft_stride(kiss_fft_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout, + //It just performs an out-of-place FFT into a temp buffer + kiss_fft_cpx * tmpbuf = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC( sizeof(kiss_fft_cpx)*st->nfft); + kf_work(tmpbuf,fin,1,in_stride, st->factors,st); +- memcpy(fout,tmpbuf,sizeof(kiss_fft_cpx)*st->nfft); ++ /* memcpy(fout,tmpbuf,sizeof(kiss_fft_cpx)*st->nfft); */ + KISS_FFT_TMP_FREE(tmpbuf); + }else{ + kf_work( fout, fin, 1,in_stride, st->factors,st ); +diff --git a/kiss_fft.h b/kiss_fft.h +index 64c50f4..24e4d0c 100644 +--- a/kiss_fft.h ++++ b/kiss_fft.h +@@ -7,7 +7,7 @@ + #include <string.h> + + #ifdef __cplusplus +-extern "C" { ++extern "C++" { + #endif + + /* +@@ -29,13 +29,13 @@ extern "C" { + #define KISS_FFT_MALLOC(nbytes) _mm_malloc(nbytes,16) + #define KISS_FFT_FREE _mm_free + #else +-#define KISS_FFT_MALLOC malloc +-#define KISS_FFT_FREE free ++#define KISS_FFT_MALLOC(X) (void*)(0x0) /* Patched. */ ++#define KISS_FFT_FREE(X) /* Patched. */ + #endif + + + #ifdef FIXED_POINT +-#include <sys/types.h> ++#include <stdint.h> /* Patched. */ + # if (FIXED_POINT == 32) + # define kiss_fft_scalar int32_t + # else +diff --git a/tools/kiss_fftr.c b/tools/kiss_fftr.c +index b8e238b..0d22a04 100644 +--- a/tools/kiss_fftr.c ++++ b/tools/kiss_fftr.c +@@ -31,7 +31,7 @@ kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenme + size_t subsize, memneeded; + + if (nfft & 1) { +- fprintf(stderr,"Real FFT optimization must be even.\n"); ++ /* fprintf(stderr,"Real FFT optimization must be even.\n"); */ + return NULL; + } + nfft >>= 1; +@@ -71,8 +71,8 @@ void kiss_fftr(kiss_fftr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *fr + kiss_fft_cpx fpnk,fpk,f1k,f2k,tw,tdc; + + if ( st->substate->inverse) { +- fprintf(stderr,"kiss fft usage error: improper alloc\n"); +- exit(1); ++ /* fprintf(stderr,"kiss fft usage error: improper alloc\n"); */ ++ return; /* exit(1); */ + } + + ncfft = st->substate->nfft; +@@ -126,8 +126,8 @@ void kiss_fftri(kiss_fftr_cfg st,const kiss_fft_cpx *freqdata,kiss_fft_scalar *t + int k, ncfft; + + if (st->substate->inverse == 0) { +- fprintf (stderr, "kiss fft usage error: improper alloc\n"); +- exit (1); ++ /* fprintf (stderr, "kiss fft usage error: improper alloc\n"); */ ++ return; /* exit (1); */ + } + + ncfft = st->substate->nfft; +diff --git a/tools/kiss_fftr.h b/tools/kiss_fftr.h +index 72e5a57..b888a28 100644 +--- a/tools/kiss_fftr.h ++++ b/tools/kiss_fftr.h +@@ -3,7 +3,7 @@ + + #include "kiss_fft.h" + #ifdef __cplusplus +-extern "C" { ++extern "C++" { + #endif + + diff --git a/math/ruy/Kconfig b/math/ruy/Kconfig new file mode 100644 index 000000000..71b4890e2 --- /dev/null +++ b/math/ruy/Kconfig @@ -0,0 +1,25 @@ +############################################################################ +# apps/math/ruy/Kconfig +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +config MATH_RUY + bool "Ruy" + default n + ---help--- + Enable ruy. diff --git a/math/ruy/Make.defs b/math/ruy/Make.defs new file mode 100644 index 000000000..bdba60a48 --- /dev/null +++ b/math/ruy/Make.defs @@ -0,0 +1,23 @@ +############################################################################ +# apps/math/ruy/Make.defs +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +ifneq ($(CONFIG_MATH_RUY),) +CONFIGURED_APPS += $(APPDIR)/math/ruy +endif diff --git a/math/ruy/Makefile b/math/ruy/Makefile new file mode 100644 index 000000000..992c794c2 --- /dev/null +++ b/math/ruy/Makefile @@ -0,0 +1,42 @@ +############################################################################ +# apps/math/ruy/Makefile +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include $(APPDIR)/Make.defs + +RUY_VER = 54774a7a2cf85963777289193629d4bd42de4a59 + +ruy.zip: + $(Q) curl -L https://github.com/google/ruy/archive/$(RUY_VER).zip -o ruy.zip + $(Q) unzip -o ruy.zip + $(Q) mv ruy-$(RUY_VER) ruy + +# Download and unpack tarball if no git repo found +ifeq ($(wildcard ruy/.git),) +context:: ruy.zip + +distclean:: + $(call DELDIR, ruy) + $(call DELFILE, ruy.zip) +endif + +CFLAGS += ${INCDIR_PREFIX}$(APPDIR)/math/ruy/ruy +CXXFLAGS += ${INCDIR_PREFIX}$(APPDIR)/math/ruy/ruy + +include $(APPDIR)/Application.mk diff --git a/mlearning/cmsis-nn/Kconfig b/mlearning/cmsis-nn/Kconfig new file mode 100644 index 000000000..1500f7d07 --- /dev/null +++ b/mlearning/cmsis-nn/Kconfig @@ -0,0 +1,25 @@ +############################################################################ +# apps/mlearning/cmsis-nn/Kconfig +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +config MLEARNING_CMSIS_NN + bool "CMSIS_NN Library" + default n + ---help--- + Enable CMSIS_NN. diff --git a/mlearning/cmsis-nn/Make.defs b/mlearning/cmsis-nn/Make.defs new file mode 100644 index 000000000..669ca73a2 --- /dev/null +++ b/mlearning/cmsis-nn/Make.defs @@ -0,0 +1,25 @@ +############################################################################ +# apps/mlearning/cmsis/Make.defs +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +ifneq ($(CONFIG_MLEARNING_CMSIS_NN),) +CONFIGURED_APPS += $(APPDIR)/mlearning/cmsis-nn +CFLAGS += ${INCDIR_PREFIX}$(APPDIR)/mlearning/cmsis-nn/cmsis-nn/Include +CXXFLAGS += ${INCDIR_PREFIX}$(APPDIR)/mlearning/cmsis-nn/cmsis-nn/Include +endif diff --git a/mlearning/cmsis-nn/Makefile b/mlearning/cmsis-nn/Makefile new file mode 100644 index 000000000..54b374ed4 --- /dev/null +++ b/mlearning/cmsis-nn/Makefile @@ -0,0 +1,51 @@ +############################################################################ +# apps/mlearning/cmsis-nn/Makefile +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include $(APPDIR)/Make.defs + +CMSIS_NN = cmsis-nn/Source + +CMSIS_NN_VER = bfc54edb61e873039ec0857cacc40df36b1d644e + +cmsis-nn.zip: + $(Q) curl -L https://github.com/ARM-software/CMSIS-NN/archive/$(CMSIS_NN_VER).zip -o cmsis-nn.zip + $(Q) unzip -o cmsis-nn.zip + $(Q) mv CMSIS-NN-$(CMSIS_NN_VER) cmsis-nn + +ifeq ($(wildcard cmsis-nn/.git),) +context:: cmsis-nn.zip +distclean:: + $(call DELDIR, cmsis-nn) + $(call DELFILE, cmsis-nn.zip) +endif + +CSRCS += $(wildcard $(CMSIS_NN)/ActivationFunctions/*.c) +CSRCS += $(wildcard $(CMSIS_NN)/BasicMathFunctions/*.c) +CSRCS += $(wildcard $(CMSIS_NN)/PoolingFunctions/*.c) +CSRCS += $(wildcard $(CMSIS_NN)/ConvolutionFunctions/*.c) +CSRCS += $(wildcard $(CMSIS_NN)/SVDFunctions/*.c) +CSRCS += $(wildcard $(CMSIS_NN)/FullyConnectedFunctions/*.c) +CSRCS += $(wildcard $(CMSIS_NN)/NNSupportFunctions/*.c) +CSRCS += $(wildcard $(CMSIS_NN)/ConcatenationFunctions/*.c) +CSRCS += $(wildcard $(CMSIS_NN)/SoftmaxFunctions/*.c) +CSRCS += $(CMSIS_NN)/LSTMFunctions/arm_lstm_unidirectional_s8_s16.c +CSRCS += $(CMSIS_NN)/ReshapeFunctions/arm_reshape_s8.c + +include $(APPDIR)/Application.mk diff --git a/mlearning/tflite-micro/Kconfig b/mlearning/tflite-micro/Kconfig new file mode 100644 index 000000000..15b841ef6 --- /dev/null +++ b/mlearning/tflite-micro/Kconfig @@ -0,0 +1,24 @@ +############################################################################ +# apps/mlearning/tflite-micro/Kconfig +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +config TFLITEMICRO + bool "TFLiteMicro" + default n + depends on SYSTEM_FLATBUFFERS && MATH_GEMMLOWP && MATH_KISSFFT && MATH_RUY diff --git a/mlearning/tflite-micro/Make.defs b/mlearning/tflite-micro/Make.defs new file mode 100644 index 000000000..bd3752539 --- /dev/null +++ b/mlearning/tflite-micro/Make.defs @@ -0,0 +1,23 @@ +############################################################################ +# apps/mlearning/tflite-micro/Make.defs +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +ifneq ($(CONFIG_TFLITEMICRO),) +CONFIGURED_APPS += $(APPDIR)/mlearning/tflite-micro +endif diff --git a/mlearning/tflite-micro/Makefile b/mlearning/tflite-micro/Makefile new file mode 100644 index 000000000..f9c5849e6 --- /dev/null +++ b/mlearning/tflite-micro/Makefile @@ -0,0 +1,83 @@ +############################################################################ +# apps/mlearning/tflite-micro/Makefile +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include $(APPDIR)/Make.defs + +TFLITE_MICRO_VER = main +TFLITE_MICRO_UNPACK = tflite-micro + +tflite-micro.zip: + $(Q) curl -L https://github.com/tensorflow/tflite-micro/archive/refs/heads/$(TFLITE_MICRO_VER).zip -o tflite-micro.zip + $(Q) unzip -o tflite-micro.zip + $(Q) mv tflite-micro-$(TFLITE_MICRO_VER) tflite-micro + $(Q) patch -d $(TFLITE_MICRO_UNPACK) -p1 < tflite-micro.patch + +# Download and unpack tarball if no git repo found +ifeq ($(wildcard tflite-micro/.git),) +context:: tflite-micro.zip +distclean:: + $(call DELDIR, tflite-micro) + $(call DELFILE, tflite-micro.zip) +endif + +CXXEXT := .cc + +COMMON_FLAGS += -DTF_LITE_STATIC_MEMORY +COMMON_FLAGS += -DTF_LITE_DISABLE_X86_NEON +COMMON_FLAGS += -Wno-undef +COMMON_FLAGS += -Wno-shadow + +SRC_FILES := $(wildcard $(APPDIR)/mlearning/tflite-micro/tflite-micro/tensorflow/lite/micro/kernels/*.cc) + +ifeq ($(CONFIG_MLEARNING_CMSIS_NN),y) +COMMON_FLAGS += -DCMSIS_NN +COMMON_FLAGS += ${INCDIR_PREFIX}$(APPDIR)/mlearning/cmsis-nn/cmsis-nn +NEW_SRC_FILES := $(wildcard $(APPDIR)/mlearning/tflite-micro/tflite-micro/tensorflow/lite/micro/kernels/cmsis_nn/*.cc) +SRC_FILES := $(filter-out $(notdir $(NEW_SRC_FILES)), $(notdir $(SRC_FILES))) +SRC_FILES := $(addprefix $(APPDIR)/mlearning/tflite-micro/tflite-micro/tensorflow/lite/micro/kernels/, $(SRC_FILES)) +SRC_FILES += $(NEW_SRC_FILES) +endif + +COMMON_FLAGS += ${INCDIR_PREFIX}$(APPDIR)/math/gemmlowp/gemmlowp +COMMON_FLAGS += ${INCDIR_PREFIX}$(APPDIR)/math/ruy/ruy +COMMON_FLAGS += ${INCDIR_PREFIX}$(APPDIR)/math/kissfft/kissfft +COMMON_FLAGS += ${INCDIR_PREFIX}$(APPDIR)/mlearning/tflite-micro/tflite-micro + +CFLAGS += ${COMMON_FLAGS} +CXXFLAGS += ${COMMON_FLAGS} + +SRC_FILES += $(wildcard $(APPDIR)/mlearning/tflite-micro/tflite-micro/tensorflow/lite/c/*.cc) +SRC_FILES += $(wildcard $(APPDIR)/mlearning/tflite-micro/tflite-micro/tensorflow/lite/schema/*.cc) +SRC_FILES += $(wildcard $(APPDIR)/mlearning/tflite-micro/tflite-micro/tensorflow/lite/core/c/*.cc) +SRC_FILES += $(wildcard $(APPDIR)/mlearning/tflite-micro/tflite-micro/tensorflow/lite/kernels/*.cc) +SRC_FILES += $(wildcard $(APPDIR)/mlearning/tflite-micro/tflite-micro/tensorflow/lite/kernels/internal/optimized/*.cc) +SRC_FILES += $(wildcard $(APPDIR)/mlearning/tflite-micro/tflite-micro/tensorflow/lite/kernels/internal/reference/*.cc) +SRC_FILES += $(wildcard $(APPDIR)/mlearning/tflite-micro/tflite-micro/tensorflow/lite/kernels/internal/*.cc) +SRC_FILES += $(wildcard $(APPDIR)/mlearning/tflite-micro/tflite-micro/tensorflow/lite/core/api/*.cc) +SRC_FILES += $(wildcard $(APPDIR)/mlearning/tflite-micro/tflite-micro/tensorflow/lite/micro/arena_allocator/*.cc) +SRC_FILES += $(wildcard $(APPDIR)/mlearning/tflite-micro/tflite-micro/tensorflow/lite/micro/memory_planner/*.cc) +SRC_FILES += $(wildcard $(APPDIR)/mlearning/tflite-micro/tflite-micro/tensorflow/lite/micro/*.cc) +SRC_FILES += $(wildcard $(APPDIR)/mlearning/tflite-micro/tflite-micro/tensorflow/lite/micro/tflite_bridge/*.cc) + +SRC_FILES := $(filter-out %test.cc, $(SRC_FILES)) + +CXXSRCS += $(SRC_FILES) + +include $(APPDIR)/Application.mk diff --git a/mlearning/tflite-micro/tflite-micro.patch b/mlearning/tflite-micro/tflite-micro.patch new file mode 100644 index 000000000..f38291a5a --- /dev/null +++ b/mlearning/tflite-micro/tflite-micro.patch @@ -0,0 +1,14 @@ +diff --git a/signal/src/circular_buffer.cc b/signal/src/circular_buffer.cc +index 7638d912..3261be56 100644 +--- a/signal/src/circular_buffer.cc ++++ b/signal/src/circular_buffer.cc +@@ -19,7 +19,9 @@ limitations under the License. + #include <stdlib.h> + #include <string.h> + ++#ifndef ASSERT + #define ASSERT assert ++#endif + + namespace tflite { + namespace tflm_signal { diff --git a/system/flatbuffers/Kconfig b/system/flatbuffers/Kconfig new file mode 100644 index 000000000..7ece716ba --- /dev/null +++ b/system/flatbuffers/Kconfig @@ -0,0 +1,25 @@ +############################################################################ +# apps/system/flatbuffers/Kconfig +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +config SYSTEM_FLATBUFFERS + bool "flatbuffers" + default n + ---help--- + Enable flatbuffers. diff --git a/system/flatbuffers/Make.defs b/system/flatbuffers/Make.defs new file mode 100644 index 000000000..7c313e524 --- /dev/null +++ b/system/flatbuffers/Make.defs @@ -0,0 +1,25 @@ +############################################################################ +# apps/system/flatbuffers/Make.defs +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +ifneq ($(CONFIG_SYSTEM_FLATBUFFERS),) +CONFIGURED_APPS += $(APPDIR)/system/flatbuffers +CFLAGS += ${INCDIR_PREFIX}$(APPDIR)/system/flatbuffers/flatbuffers/include +CXXFLAGS += ${INCDIR_PREFIX}$(APPDIR)/system/flatbuffers/flatbuffers/include +endif diff --git a/system/flatbuffers/Makefile b/system/flatbuffers/Makefile new file mode 100644 index 000000000..3b01f6bac --- /dev/null +++ b/system/flatbuffers/Makefile @@ -0,0 +1,40 @@ +############################################################################ +# apps/system/flatbuffers/Makefile +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include $(APPDIR)/Make.defs + +FLATBUFFERS_VER = 23.5.26 +FLATBUFFERS_UNPACK = flatbuffers + +flatbuffers.tar.gz: + $(Q) curl -L https://github.com/google/flatbuffers/archive/v$(FLATBUFFERS_VER).tar.gz -o flatbuffers.tar.gz + $(Q) tar -xzf flatbuffers.tar.gz + $(Q) mv flatbuffers-$(FLATBUFFERS_VER) flatbuffers + $(Q) patch -d $(FLATBUFFERS_UNPACK) -p1 < flatbuffers.patch + +# Download and unpack tarball if no git repo found +ifeq ($(wildcard flatbuffers/.git),) +context:: flatbuffers.tar.gz +distclean:: + $(call DELDIR, flatbuffers) + $(call DELFILE, flatbuffers.tar.gz) +endif + +include $(APPDIR)/Application.mk diff --git a/system/flatbuffers/flatbuffers.patch b/system/flatbuffers/flatbuffers.patch new file mode 100644 index 000000000..2017775aa --- /dev/null +++ b/system/flatbuffers/flatbuffers.patch @@ -0,0 +1,100 @@ +diff --git a/include/flatbuffers/base.h b/include/flatbuffers/base.h +index 5c4cae79..1a631641 100644 +--- a/include/flatbuffers/base.h ++++ b/include/flatbuffers/base.h +@@ -1,6 +1,16 @@ + #ifndef FLATBUFFERS_BASE_H_ + #define FLATBUFFERS_BASE_H_ + ++// For TFLM, we always want FLATBUFFERS_LOCALE_INDEPENDENT to be defined as 0. ++// We could achieve this by adding -DFLATBUFFERS_LOCALE_INDEPENDENT=0 to the ++// TFLM Makefile. However, for (at least) the Arduino, adding additional build ++// flags during the compilation can be a bit awkward. As such, we have instead ++// made a decision to change the default to be FLATBUFFERS_LOCALE_INDEPENDENT=0 ++// for TFLM to make it easier for external IDE integration. ++#ifndef FLATBUFFERS_LOCALE_INDEPENDENT ++#define FLATBUFFERS_LOCALE_INDEPENDENT 0 ++#endif ++ + // clang-format off + + // If activate should be declared and included first. +diff --git a/include/flatbuffers/default_allocator.h b/include/flatbuffers/default_allocator.h +index d4724122..975d9380 100644 +--- a/include/flatbuffers/default_allocator.h ++++ b/include/flatbuffers/default_allocator.h +@@ -39,24 +39,18 @@ class DefaultAllocator : public Allocator { + // This is to avoid having a statically or dynamically allocated default + // allocator, or having to move it between the classes that may own it. + inline uint8_t *Allocate(Allocator *allocator, size_t size) { +- return allocator ? allocator->allocate(size) +- : DefaultAllocator().allocate(size); ++ return allocator->allocate(size); + } + + inline void Deallocate(Allocator *allocator, uint8_t *p, size_t size) { +- if (allocator) +- allocator->deallocate(p, size); +- else +- DefaultAllocator().deallocate(p, size); ++ allocator->deallocate(p, size); + } + + inline uint8_t *ReallocateDownward(Allocator *allocator, uint8_t *old_p, + size_t old_size, size_t new_size, + size_t in_use_back, size_t in_use_front) { +- return allocator ? allocator->reallocate_downward(old_p, old_size, new_size, +- in_use_back, in_use_front) +- : DefaultAllocator().reallocate_downward( +- old_p, old_size, new_size, in_use_back, in_use_front); ++ return allocator->reallocate_downward(old_p, old_size, new_size, in_use_back, ++ in_use_front); + } + + } // namespace flatbuffers +diff --git a/include/flatbuffers/flexbuffers.h b/include/flatbuffers/flexbuffers.h +index 8e8cac14..52dae316 100644 +--- a/include/flatbuffers/flexbuffers.h ++++ b/include/flatbuffers/flexbuffers.h +@@ -495,9 +495,24 @@ class Reference { + return static_cast<double>(ReadUInt64(Indirect(), byte_width_)); + case FBT_NULL: return 0.0; + case FBT_STRING: { ++#if 1 ++#if !defined( _MSC_VER) ++#pragma GCC diagnostic push ++#pragma GCC diagnostic ignored "-Wnull-dereference" ++#endif ++ // See b/173239141 for additional context. Patched via ++ // micro/tools/make/flexbuffers_download.sh ++ // Introduce a segfault for an unsupported code path for TFLM. ++ return *(static_cast<double*>(nullptr)); ++#if !defined( _MSC_VER) ++#pragma GCC diagnostic pop ++#endif ++#else ++ // This is the original code + double d; + flatbuffers::StringToNumber(AsString().c_str(), &d); + return d; ++#endif + } + case FBT_VECTOR: return static_cast<double>(AsVector().size()); + case FBT_BOOL: +diff --git a/include/flatbuffers/util.h b/include/flatbuffers/util.h +index 1ccf3517..34a75193 100644 +--- a/include/flatbuffers/util.h ++++ b/include/flatbuffers/util.h +@@ -23,6 +23,12 @@ + #include "flatbuffers/base.h" + #include "flatbuffers/stl_emulation.h" + ++// For TFLM we always want to use FLATBUFFERS_PREFER_PRINTF=1. See ++// http://b/211811553 for more context. ++#ifndef FLATBUFFERS_PREFER_PRINTF ++#define FLATBUFFERS_PREFER_PRINTF 1 ++#endif ++ + #ifndef FLATBUFFERS_PREFER_PRINTF + # include <iomanip> + # include <sstream>