Skip to content

Commit d80d8d1

Browse files
Sync from tflite-micro at c1a7015.
Signed-off-by: CFU-Playground-Bot <[email protected]>
1 parent 9ed5c57 commit d80d8d1

File tree

217 files changed

+8471
-4769
lines changed

Some content is hidden

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

217 files changed

+8471
-4769
lines changed

conf/tflite-micro.version

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

third_party/tflite-micro/tensorflow/lite/builtin_ops.h

+3
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ typedef enum {
186186
kTfLiteBuiltinAtan2 = 156,
187187
kTfLiteBuiltinUnsortedSegmentMin = 157,
188188
kTfLiteBuiltinSign = 158,
189+
kTfLiteBuiltinBitcast = 159,
190+
kTfLiteBuiltinBitwiseXor = 160,
191+
kTfLiteBuiltinRightShift = 161,
189192
} TfLiteBuiltinOperator;
190193

191194
#ifdef __cplusplus

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ 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"
4240

4341
#endif // TENSORFLOW_LITE_C_COMMON_H_

third_party/tflite-micro/tensorflow/lite/core/api/flatbuffer_conversions.cc

+38-1
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ TfLiteStatus ParseOpDataTfLite(const Operator* op, BuiltinOperator op_type,
256256
return ParseElu(op, error_reporter, allocator, builtin_data);
257257
}
258258

259+
case BuiltinOperator_EMBEDDING_LOOKUP: {
260+
return ParseEmbeddingLookup(op, error_reporter, allocator, builtin_data);
261+
}
262+
259263
case BuiltinOperator_EXP: {
260264
return ParseExp(op, error_reporter, allocator, builtin_data);
261265
}
@@ -542,6 +546,14 @@ TfLiteStatus ParseOpDataTfLite(const Operator* op, BuiltinOperator op_type,
542546
return ParseZerosLike(op, error_reporter, allocator, builtin_data);
543547
}
544548

549+
case BuiltinOperator_BITWISE_XOR: {
550+
return ParseBitwiseXor(op, error_reporter, allocator, builtin_data);
551+
}
552+
553+
case BuiltinOperator_RIGHT_SHIFT: {
554+
return ParseRightShift(op, error_reporter, allocator, builtin_data);
555+
}
556+
545557
case BuiltinOperator_CAST: {
546558
return ParseCast(op, error_reporter, allocator, builtin_data);
547559
}
@@ -845,6 +857,7 @@ TfLiteStatus ParseOpDataTfLite(const Operator* op, BuiltinOperator op_type,
845857
*builtin_data = params.release();
846858
return kTfLiteOk;
847859
}
860+
848861
// Below are the ops with no builtin_data structure.
849862
// TODO(aselle): Implement call in BuiltinOptions, but nullptrs are
850863
// ok for now, since there is no call implementation either.
@@ -855,7 +868,6 @@ TfLiteStatus ParseOpDataTfLite(const Operator* op, BuiltinOperator op_type,
855868
case BuiltinOperator_CUSTOM:
856869
case BuiltinOperator_DENSIFY:
857870
case BuiltinOperator_DYNAMIC_UPDATE_SLICE:
858-
case BuiltinOperator_EMBEDDING_LOOKUP:
859871
case BuiltinOperator_EQUAL:
860872
case BuiltinOperator_HASHTABLE_FIND:
861873
case BuiltinOperator_HASHTABLE_IMPORT:
@@ -885,6 +897,7 @@ TfLiteStatus ParseOpDataTfLite(const Operator* op, BuiltinOperator op_type,
885897
case BuiltinOperator_UNSORTED_SEGMENT_SUM:
886898
case BuiltinOperator_ATAN2:
887899
case BuiltinOperator_SIGN:
900+
case BuiltinOperator_BITCAST:
888901
case BuiltinOperator_WHERE:
889902
return kTfLiteOk;
890903
case BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES:
@@ -1335,6 +1348,14 @@ TfLiteStatus ParseElu(const Operator*, ErrorReporter*, BuiltinDataAllocator*,
13351348
return kTfLiteOk;
13361349
}
13371350

1351+
// We have this parse function instead of directly returning kTfLiteOk from the
1352+
// switch-case in ParseOpData because this function is used as part of the
1353+
// selective registration for the OpResolver implementation in micro.
1354+
TfLiteStatus ParseEmbeddingLookup(const Operator*, ErrorReporter*,
1355+
BuiltinDataAllocator*, void**) {
1356+
return kTfLiteOk;
1357+
}
1358+
13381359
// We have this parse function instead of directly returning kTfLiteOk from the
13391360
// switch-case in ParseOpData because this function is used as part of the
13401361
// selective registration for the OpResolver implementation in micro.
@@ -2441,6 +2462,22 @@ TfLiteStatus ParseZerosLike(const Operator*, ErrorReporter*,
24412462
return kTfLiteOk;
24422463
}
24432464

2465+
// We have this parse function instead of directly returning kTfLiteOk from the
2466+
// switch-case in ParseOpData because this function is used as part of the
2467+
// selective registration for the OpResolver implementation in micro.
2468+
TfLiteStatus ParseBitwiseXor(const Operator*, ErrorReporter*,
2469+
BuiltinDataAllocator*, void**) {
2470+
return kTfLiteOk;
2471+
}
2472+
2473+
// We have this parse function instead of directly returning kTfLiteOk from the
2474+
// switch-case in ParseOpData because this function is used as part of the
2475+
// selective registration for the OpResolver implementation in micro.
2476+
TfLiteStatus ParseRightShift(const Operator*, ErrorReporter*,
2477+
BuiltinDataAllocator*, void**) {
2478+
return kTfLiteOk;
2479+
}
2480+
24442481
TfLiteStatus ParseOpData(const Operator* op, BuiltinOperator op_type,
24452482
ErrorReporter* error_reporter,
24462483
BuiltinDataAllocator* allocator, void** builtin_data) {

third_party/tflite-micro/tensorflow/lite/core/api/flatbuffer_conversions.h

+13
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ TfLiteStatus ParseDiv(const Operator* op, ErrorReporter* error_reporter,
151151
TfLiteStatus ParseElu(const Operator* op, ErrorReporter* error_reporter,
152152
BuiltinDataAllocator* allocator, void** builtin_data);
153153

154+
TfLiteStatus ParseEmbeddingLookup(const Operator* op,
155+
ErrorReporter* error_reporter,
156+
BuiltinDataAllocator* allocator,
157+
void** builtin_data);
158+
154159
TfLiteStatus ParseEqual(const Operator* op, ErrorReporter* error_reporter,
155160
BuiltinDataAllocator* allocator, void** builtin_data);
156161

@@ -407,6 +412,14 @@ TfLiteStatus ParseZerosLike(const Operator* op, ErrorReporter* error_reporter,
407412
BuiltinDataAllocator* allocator,
408413
void** builtin_data);
409414

415+
TfLiteStatus ParseBitwiseXor(const Operator* op, ErrorReporter* error_reporter,
416+
BuiltinDataAllocator* allocator,
417+
void** builtin_data);
418+
419+
TfLiteStatus ParseRightShift(const Operator* op, ErrorReporter* error_reporter,
420+
BuiltinDataAllocator* allocator,
421+
void** builtin_data);
422+
410423
} // namespace tflite
411424

412425
#endif // TENSORFLOW_LITE_CORE_API_FLATBUFFER_CONVERSIONS_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

+70-11
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.
@@ -955,12 +959,53 @@ typedef struct TfLiteRegistration {
955959
// ops. We keep it inside of `TfLiteRegistration` and use it to route
956960
// callbacks properly.
957961
TfLiteRegistrationExternal* registration_external;
962+
963+
// Retrieves asynchronous kernel.
964+
//
965+
// If the `async_kernel` field is nullptr, it means the operation described by
966+
// this TfLiteRegistration object does not support asynchronous execution.
967+
// Otherwise, the function that the field points to should only be called for
968+
// delegate kernel nodes, i.e. `node` should be a delegate kernel node created
969+
// by applying a delegate.
970+
// If the function returns nullptr, that means that the underlying delegate
971+
// does not support asynchronous execution for this `node`.
972+
struct TfLiteAsyncKernel* (*async_kernel)(TfLiteContext* context,
973+
TfLiteNode* node);
958974
} TfLiteRegistration;
959975

976+
/// \private
977+
// Old version of `TfLiteRegistration` to maintain binary backward
978+
// compatibility.
979+
// The legacy registration type must be a POD struct type whose field types must
980+
// be a prefix of the field types in TfLiteRegistration, and offset of the first
981+
// field in TfLiteRegistration that is not present in the legacy registration
982+
// type must be greater than or equal to the size of the legacy registration
983+
// type.
984+
// WARNING: This structure is deprecated / not an official part of the
985+
// API. It should be only used for binary backward compatibility.
986+
typedef struct TfLiteRegistration_V2 {
987+
void* (*init)(TfLiteContext* context, const char* buffer, size_t length);
988+
void (*free)(TfLiteContext* context, void* buffer);
989+
TfLiteStatus (*prepare)(TfLiteContext* context, TfLiteNode* node);
990+
TfLiteStatus (*invoke)(TfLiteContext* context, TfLiteNode* node);
991+
const char* (*profiling_string)(const TfLiteContext* context,
992+
const TfLiteNode* node);
993+
int32_t builtin_code;
994+
const char* custom_name;
995+
int version;
996+
TfLiteRegistrationExternal* registration_external;
997+
} TfLiteRegistration_V2;
998+
999+
/// \private
9601000
// Old version of `TfLiteRegistration` to maintain binary backward
9611001
// compatibility.
962-
// WARNING: This structure is deprecated / not an official part of the API.
963-
// It should be only used for binary backward compatibility.
1002+
// The legacy registration type must be a POD struct type whose field types must
1003+
// be a prefix of the field types in TfLiteRegistration, and offset of the first
1004+
// field in TfLiteRegistration that is not present in the legacy registration
1005+
// type must be greater than or equal to the size of the legacy registration
1006+
// type.
1007+
// WARNING: This structure is deprecated / not an official part of the
1008+
// API. It should be only used for binary backward compatibility.
9641009
typedef struct TfLiteRegistration_V1 {
9651010
void* (*init)(TfLiteContext* context, const char* buffer, size_t length);
9661011
void (*free)(TfLiteContext* context, void* buffer);
@@ -1135,6 +1180,20 @@ TfLiteOpaqueDelegate* TfLiteOpaqueDelegateCreate(
11351180
// 'delegate' is a null pointer.
11361181
void TfLiteOpaqueDelegateDelete(TfLiteOpaqueDelegate* delegate);
11371182

1183+
// Returns a pointer to the data associated with the provided opaque 'delegate'.
1184+
//
1185+
// A null pointer will be returned when:
1186+
// - The 'delegate' is null.
1187+
// - The 'data' field of the 'TfLiteOpaqueDelegateBuilder' used to construct the
1188+
// 'delegate' was null.
1189+
// - Or in case of any other error.
1190+
// - The 'delegate' has been constructed via a 'TfLiteOpaqueDelegateBuilder',
1191+
// but the 'data' field of the 'TfLiteOpaqueDelegateBuilder' is null.
1192+
//
1193+
// The data_ field of 'delegate' will be returned if the
1194+
// 'opaque_delegate_builder' field is null.
1195+
void* TfLiteOpaqueDelegateGetData(const TfLiteOpaqueDelegate* delegate);
1196+
11381197
#ifdef __cplusplus
11391198
} // extern "C"
11401199
#endif // __cplusplus

0 commit comments

Comments
 (0)