Skip to content

Commit 211feab

Browse files
Sync from tflite-micro at a8c2ebf.
Signed-off-by: CFU-Playground-Bot <[email protected]>
1 parent 7e75e71 commit 211feab

File tree

111 files changed

+6379
-3835
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+6379
-3835
lines changed

conf/tflite-micro.version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8746ec9
1+
a8c2ebf

third_party/tflite-micro/tensorflow/lite/c/builtin_op_data.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
1+
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -15,8 +15,6 @@ limitations under the License.
1515
#ifndef TENSORFLOW_LITE_C_BUILTIN_OP_DATA_H_
1616
#define TENSORFLOW_LITE_C_BUILTIN_OP_DATA_H_
1717

18-
/// For documentation, see
19-
/// third_party/tensorflow/lite/core/c/builtin_op_data.h.
20-
#include "tensorflow/lite/core/c/builtin_op_data.h" // IWYU pragma: export
18+
#include "tensorflow/lite/core/c/builtin_op_data.h"
2119

2220
#endif // TENSORFLOW_LITE_C_BUILTIN_OP_DATA_H_
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
1+
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -12,15 +12,9 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License.
1414
==============================================================================*/
15-
16-
// This file declares types used by the pure C inference API defined in c_api.h,
17-
// some of which are also used in the C++ and C kernel and interpreter APIs.
18-
1915
#ifndef TENSORFLOW_LITE_C_C_API_TYPES_H_
2016
#define TENSORFLOW_LITE_C_C_API_TYPES_H_
2117

22-
/// For documentation, see
23-
/// third_party/tensorflow/lite/core/c/c_api_types.h.
24-
#include "tensorflow/lite/core/c/c_api_types.h" // IWYU pragma: export
18+
#include "tensorflow/lite/core/c/c_api_types.h"
2519

2620
#endif // TENSORFLOW_LITE_C_C_API_TYPES_H_

third_party/tflite-micro/tensorflow/lite/c/common.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ limitations under the License.
3636
#ifndef TENSORFLOW_LITE_C_COMMON_H_
3737
#define TENSORFLOW_LITE_C_COMMON_H_
3838

39-
/// For documentation, see
40-
/// third_party/tensorflow/lite/core/c/common.h.
41-
#include "tensorflow/lite/core/c/common.h" // IWYU pragma: export
39+
#include "tensorflow/lite/core/c/common.h"
40+
41+
// TfLiteOpaqueDelegate: allows delegation of nodes to alternative backends.
42+
// TfLiteOpaqueDelegate is an abstract type that is intended to have the same
43+
// role as TfLiteDelegate, but without necessarily exposing the implementation
44+
// details of how delegates are implemented.
45+
typedef TfLiteDelegate TfLiteOpaqueDelegate;
4246

4347
#endif // TENSORFLOW_LITE_C_COMMON_H_

third_party/tflite-micro/tensorflow/lite/core/c/common.cc

+24-4
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,11 @@ TfLiteStatus TfLiteTensorCopy(const TfLiteTensor* src, TfLiteTensor* dst) {
219219
return kTfLiteOk;
220220
}
221221

222-
void TfLiteTensorResizeMaybeCopy(size_t num_bytes, TfLiteTensor* tensor,
223-
bool preserve_data) {
222+
TfLiteStatus TfLiteTensorResizeMaybeCopy(size_t num_bytes, TfLiteTensor* tensor,
223+
bool preserve_data) {
224224
if (tensor->allocation_type != kTfLiteDynamic &&
225225
tensor->allocation_type != kTfLitePersistentRo) {
226-
return;
226+
return kTfLiteOk;
227227
}
228228
#ifdef TF_LITE_TENSORFLOW_PROFILER
229229
tflite::PauseHeapMonitoring(/*pause=*/true);
@@ -258,9 +258,15 @@ void TfLiteTensorResizeMaybeCopy(size_t num_bytes, TfLiteTensor* tensor,
258258
tflite::PauseHeapMonitoring(/*pause=*/false);
259259
#endif
260260
tensor->bytes = num_bytes;
261+
if (tensor->data.data == nullptr && num_bytes != 0) {
262+
// We are done allocating but tensor is pointing to null and a valid size
263+
// was requested, so we error.
264+
return kTfLiteError;
265+
}
266+
return kTfLiteOk;
261267
}
262268

263-
void TfLiteTensorRealloc(size_t num_bytes, TfLiteTensor* tensor) {
269+
TfLiteStatus TfLiteTensorRealloc(size_t num_bytes, TfLiteTensor* tensor) {
264270
return TfLiteTensorResizeMaybeCopy(num_bytes, tensor, true);
265271
}
266272
#endif // TF_LITE_STATIC_MEMORY
@@ -331,4 +337,18 @@ void TfLiteOpaqueDelegateDelete(TfLiteOpaqueDelegate* opaque_delegate) {
331337
delete tflite_delegate;
332338
}
333339

340+
void* TfLiteOpaqueDelegateGetData(const TfLiteOpaqueDelegate* delegate) {
341+
if (!delegate) return nullptr;
342+
343+
// The following cast is safe only because this code is part of the
344+
// TF Lite runtime implementation. Apps using TF Lite should not rely on
345+
// 'TfLiteOpaqueDelegate' and 'TfLiteDelegate' being equivalent.
346+
const auto* tflite_delegate =
347+
reinterpret_cast<const TfLiteDelegate*>(delegate);
348+
349+
if (!tflite_delegate->opaque_delegate_builder) return tflite_delegate->data_;
350+
351+
return tflite_delegate->opaque_delegate_builder->data;
352+
}
353+
334354
} // extern "C"

third_party/tflite-micro/tensorflow/lite/core/c/common.h

+27-9
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ limitations under the License.
4242
#ifndef TENSORFLOW_LITE_CORE_C_COMMON_H_
4343
#define TENSORFLOW_LITE_CORE_C_COMMON_H_
4444

45+
#include <stdarg.h>
4546
#include <stdbool.h>
4647
#include <stddef.h>
4748
#include <stdint.h>
@@ -648,23 +649,26 @@ void TfLiteTensorReset(TfLiteType type, const char* name, TfLiteIntArray* dims,
648649
TfLiteStatus TfLiteTensorCopy(const TfLiteTensor* src, TfLiteTensor* dst);
649650

650651
// Change the size of the memory block owned by `tensor` to `num_bytes`.
651-
// Tensors with allocation types other than kTfLiteDynamic will be ignored.
652+
// Tensors with allocation types other than `kTfLiteDynamic` will be ignored and
653+
// a kTfLiteOk will be returned.
652654
// `tensor`'s internal data buffer will be assigned a pointer
653655
// which can safely be passed to free or realloc if `num_bytes` is zero.
654-
// Behaviour is undefined if `tensor` is NULL.
655656
// If `preserve_data` is true, tensor data will be unchanged in the range from
656-
// the start of the region up to the minimum of the old and new sizes.
657-
void TfLiteTensorResizeMaybeCopy(size_t num_bytes, TfLiteTensor* tensor,
658-
bool preserve_data);
657+
// the start of the region up to the minimum of the old and new sizes. In the
658+
// case of NULL tensor, or an error allocating new memory, returns
659+
// `kTfLiteError`.
660+
TfLiteStatus TfLiteTensorResizeMaybeCopy(size_t num_bytes, TfLiteTensor* tensor,
661+
bool preserve_data);
659662

660663
// Change the size of the memory block owned by `tensor` to `num_bytes`.
661-
// Tensors with allocation types other than kTfLiteDynamic will be ignored.
664+
// Tensors with allocation types other than kTfLiteDynamic will be ignored and
665+
// a kTfLiteOk will be returned.
662666
// `tensor`'s internal data buffer will be assigned a pointer
663667
// which can safely be passed to free or realloc if `num_bytes` is zero.
664-
// Behaviour is undefined if `tensor` is NULL.
665668
// Tensor data will be unchanged in the range from the start of the region up to
666-
// the minimum of the old and new sizes.
667-
void TfLiteTensorRealloc(size_t num_bytes, TfLiteTensor* tensor);
669+
// the minimum of the old and new sizes. In the case
670+
// of NULL tensor, or an error allocating new memory, returns `kTfLiteError`.
671+
TfLiteStatus TfLiteTensorRealloc(size_t num_bytes, TfLiteTensor* tensor);
668672
#endif // TF_LITE_STATIC_MEMORY
669673

670674
// WARNING: This is an experimental interface that is subject to change.
@@ -1135,6 +1139,20 @@ TfLiteOpaqueDelegate* TfLiteOpaqueDelegateCreate(
11351139
// 'delegate' is a null pointer.
11361140
void TfLiteOpaqueDelegateDelete(TfLiteOpaqueDelegate* delegate);
11371141

1142+
// Returns a pointer to the data associated with the provided opaque 'delegate'.
1143+
//
1144+
// A null pointer will be returned when:
1145+
// - The 'delegate' is null.
1146+
// - The 'data' field of the 'TfLiteOpaqueDelegateBuilder' used to construct the
1147+
// 'delegate' was null.
1148+
// - Or in case of any other error.
1149+
// - The 'delegate' has been constructed via a 'TfLiteOpaqueDelegateBuilder',
1150+
// but the 'data' field of the 'TfLiteOpaqueDelegateBuilder' is null.
1151+
//
1152+
// The data_ field of 'delegate' will be returned if the
1153+
// 'opaque_delegate_builder' field is null.
1154+
void* TfLiteOpaqueDelegateGetData(const TfLiteOpaqueDelegate* delegate);
1155+
11381156
#ifdef __cplusplus
11391157
} // extern "C"
11401158
#endif // __cplusplus

0 commit comments

Comments
 (0)