From e9b6a39da62af30f392d9be8bfdbb34095f02d87 Mon Sep 17 00:00:00 2001 From: TFLM-bot Date: Wed, 12 Jul 2023 14:03:17 +0000 Subject: [PATCH 1/2] Sync from upstream TF. --- tensorflow/lite/kernels/kernel_util.h | 32 ++++++++++++++++++--------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/tensorflow/lite/kernels/kernel_util.h b/tensorflow/lite/kernels/kernel_util.h index 24061ab249b..e318118fb64 100644 --- a/tensorflow/lite/kernels/kernel_util.h +++ b/tensorflow/lite/kernels/kernel_util.h @@ -24,6 +24,9 @@ limitations under the License. #include "tensorflow/lite/core/c/builtin_op_data.h" #include "tensorflow/lite/core/c/common.h" +#ifndef NDEBUG +#include "tensorflow/lite/kernels/op_macros.h" +#endif namespace tflite { @@ -165,24 +168,31 @@ inline int NumIntermediates(const TfLiteNode* node) { } #endif // TF_LITE_STATIC_MEMORY -inline int64_t NumElements(const TfLiteIntArray* dims) { +inline int64_t NumElements(const int* dims, int num_dims) { int64_t count = 1; - for (int i = 0; i < dims->size; ++i) { - count *= dims->data[i]; + for (int i = 0; i < num_dims; ++i) { +#ifndef NDEBUG + if (count <= 0) { + break; + } + // Check that number of elements can fit in 32 bit int. Most of tflite + // assumes the result of `NumElements` is < MAX_INT and static or implicit + // casts to `int32_t` without any checks. It is more meaningful to check + // that the result fits into 32 bits than for standard overflow on 64 bit + // type. + TF_LITE_ASSERT(dims[i] < std::numeric_limits::max() / count); +#endif + count *= dims[i]; } return count; } -inline int64_t NumElements(const TfLiteTensor* t) { - return NumElements(t->dims); +inline int64_t NumElements(const TfLiteIntArray* dims) { + return NumElements(dims->data, dims->size); } -inline int64_t NumElements(const int* dims, int num_dims) { - int64_t count = 1; - for (int i = 0; i < num_dims; ++i) { - count *= dims[i]; - } - return count; +inline int64_t NumElements(const TfLiteTensor* t) { + return NumElements(t->dims); } // Determines whether tensor is constant. From 4601be7d1f3d07684a805f8cf075eac9982e499b Mon Sep 17 00:00:00 2001 From: RJ Ascani Date: Tue, 11 Jul 2023 16:34:09 +0000 Subject: [PATCH 2/2] Add TF_LITE_ASSERT macro In upstream TFLite, op_macros.h contains a TF_LITE_ASSERT macro that would print the failed assertion and then call abort(). This macro was removed from TFLM's version of op_macros.h because it relied on stdlib functions (namely, fprintf). As there were no usages of TF_LITE_ASSERT in shared code, this was not a problem. However, the latest sync introduces a usage of it within shared code, breaking the build. This commit adds a TFLM-friendly implementation of TF_LITE_ASSERT using MicroPrintf for the implementation. --- tensorflow/lite/kernels/BUILD | 2 +- tensorflow/lite/kernels/op_macros.h | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/tensorflow/lite/kernels/BUILD b/tensorflow/lite/kernels/BUILD index 6e1efcea01f..4b48db1ee83 100644 --- a/tensorflow/lite/kernels/BUILD +++ b/tensorflow/lite/kernels/BUILD @@ -14,7 +14,7 @@ cc_library( "op_macros.h", ], copts = tflite_copts(), - deps = ["//tensorflow/lite/micro:debug_log"], + deps = ["//tensorflow/lite/micro:micro_log"], ) cc_library( diff --git a/tensorflow/lite/kernels/op_macros.h b/tensorflow/lite/kernels/op_macros.h index 39e3119c971..9c4e2fd0fd5 100644 --- a/tensorflow/lite/kernels/op_macros.h +++ b/tensorflow/lite/kernels/op_macros.h @@ -15,11 +15,12 @@ limitations under the License. #ifndef TENSORFLOW_LITE_KERNELS_OP_MACROS_H_ #define TENSORFLOW_LITE_KERNELS_OP_MACROS_H_ +#include "tensorflow/lite/micro/micro_log.h" + #if !defined(TF_LITE_MCU_DEBUG_LOG) #include #define TFLITE_ABORT abort() #else -#include "tensorflow/lite/micro/micro_log.h" inline void AbortImpl() { MicroPrintf("HALTED"); while (1) { @@ -34,4 +35,15 @@ inline void AbortImpl() { #define TFLITE_ASSERT_FALSE TFLITE_ABORT #endif +#define TF_LITE_FATAL(msg) \ + do { \ + MicroPrintf("%s", (msg)); \ + TFLITE_ABORT; \ + } while (0) + +#define TF_LITE_ASSERT(x) \ + do { \ + if (!(x)) TF_LITE_FATAL(#x); \ + } while (0) + #endif // TENSORFLOW_LITE_KERNELS_OP_MACROS_H_