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

Automated sync from github.com/tensorflow/tensorflow #2110

Merged
merged 2 commits into from
Jul 12, 2023
Merged
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
2 changes: 1 addition & 1 deletion tensorflow/lite/kernels/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
32 changes: 21 additions & 11 deletions tensorflow/lite/kernels/kernel_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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<int>::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.
Expand Down
14 changes: 13 additions & 1 deletion tensorflow/lite/kernels/op_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cstdlib>
#define TFLITE_ABORT abort()
#else
#include "tensorflow/lite/micro/micro_log.h"
inline void AbortImpl() {
MicroPrintf("HALTED");
while (1) {
Expand All @@ -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_
Loading