Skip to content

Commit

Permalink
Sync from tflite-micro at e87b788.
Browse files Browse the repository at this point in the history
Signed-off-by: CFU-Playground-Bot <[email protected]>
  • Loading branch information
cfu-playground-bot committed Mar 31, 2023
1 parent 9ed5c57 commit ac10632
Show file tree
Hide file tree
Showing 216 changed files with 7,942 additions and 4,659 deletions.
2 changes: 1 addition & 1 deletion conf/tflite-micro.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8746ec9
e87b788
1 change: 1 addition & 0 deletions third_party/tflite-micro/tensorflow/lite/builtin_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ typedef enum {
kTfLiteBuiltinAtan2 = 156,
kTfLiteBuiltinUnsortedSegmentMin = 157,
kTfLiteBuiltinSign = 158,
kTfLiteBuiltinBitcast = 159,
} TfLiteBuiltinOperator;

#ifdef __cplusplus
Expand Down
6 changes: 2 additions & 4 deletions third_party/tflite-micro/tensorflow/lite/c/builtin_op_data.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -15,8 +15,6 @@ limitations under the License.
#ifndef TENSORFLOW_LITE_C_BUILTIN_OP_DATA_H_
#define TENSORFLOW_LITE_C_BUILTIN_OP_DATA_H_

/// For documentation, see
/// third_party/tensorflow/lite/core/c/builtin_op_data.h.
#include "tensorflow/lite/core/c/builtin_op_data.h" // IWYU pragma: export
#include "tensorflow/lite/core/c/builtin_op_data.h"

#endif // TENSORFLOW_LITE_C_BUILTIN_OP_DATA_H_
10 changes: 2 additions & 8 deletions third_party/tflite-micro/tensorflow/lite/c/c_api_types.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -12,15 +12,9 @@ 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.
==============================================================================*/

// This file declares types used by the pure C inference API defined in c_api.h,
// some of which are also used in the C++ and C kernel and interpreter APIs.

#ifndef TENSORFLOW_LITE_C_C_API_TYPES_H_
#define TENSORFLOW_LITE_C_C_API_TYPES_H_

/// For documentation, see
/// third_party/tensorflow/lite/core/c/c_api_types.h.
#include "tensorflow/lite/core/c/c_api_types.h" // IWYU pragma: export
#include "tensorflow/lite/core/c/c_api_types.h"

#endif // TENSORFLOW_LITE_C_C_API_TYPES_H_
4 changes: 1 addition & 3 deletions third_party/tflite-micro/tensorflow/lite/c/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ limitations under the License.
#ifndef TENSORFLOW_LITE_C_COMMON_H_
#define TENSORFLOW_LITE_C_COMMON_H_

/// For documentation, see
/// third_party/tensorflow/lite/core/c/common.h.
#include "tensorflow/lite/core/c/common.h" // IWYU pragma: export
#include "tensorflow/lite/core/c/common.h"

#endif // TENSORFLOW_LITE_C_COMMON_H_
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ TfLiteStatus ParseOpDataTfLite(const Operator* op, BuiltinOperator op_type,
return ParseElu(op, error_reporter, allocator, builtin_data);
}

case BuiltinOperator_EMBEDDING_LOOKUP: {
return ParseEmbeddingLookup(op, error_reporter, allocator, builtin_data);
}

case BuiltinOperator_EXP: {
return ParseExp(op, error_reporter, allocator, builtin_data);
}
Expand Down Expand Up @@ -855,7 +859,6 @@ TfLiteStatus ParseOpDataTfLite(const Operator* op, BuiltinOperator op_type,
case BuiltinOperator_CUSTOM:
case BuiltinOperator_DENSIFY:
case BuiltinOperator_DYNAMIC_UPDATE_SLICE:
case BuiltinOperator_EMBEDDING_LOOKUP:
case BuiltinOperator_EQUAL:
case BuiltinOperator_HASHTABLE_FIND:
case BuiltinOperator_HASHTABLE_IMPORT:
Expand Down Expand Up @@ -885,6 +888,7 @@ TfLiteStatus ParseOpDataTfLite(const Operator* op, BuiltinOperator op_type,
case BuiltinOperator_UNSORTED_SEGMENT_SUM:
case BuiltinOperator_ATAN2:
case BuiltinOperator_SIGN:
case BuiltinOperator_BITCAST:
case BuiltinOperator_WHERE:
return kTfLiteOk;
case BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES:
Expand Down Expand Up @@ -1335,6 +1339,14 @@ TfLiteStatus ParseElu(const Operator*, ErrorReporter*, BuiltinDataAllocator*,
return kTfLiteOk;
}

// We have this parse function instead of directly returning kTfLiteOk from the
// switch-case in ParseOpData because this function is used as part of the
// selective registration for the OpResolver implementation in micro.
TfLiteStatus ParseEmbeddingLookup(const Operator*, ErrorReporter*,
BuiltinDataAllocator*, void**) {
return kTfLiteOk;
}

// We have this parse function instead of directly returning kTfLiteOk from the
// switch-case in ParseOpData because this function is used as part of the
// selective registration for the OpResolver implementation in micro.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ TfLiteStatus ParseDiv(const Operator* op, ErrorReporter* error_reporter,
TfLiteStatus ParseElu(const Operator* op, ErrorReporter* error_reporter,
BuiltinDataAllocator* allocator, void** builtin_data);

TfLiteStatus ParseEmbeddingLookup(const Operator* op,
ErrorReporter* error_reporter,
BuiltinDataAllocator* allocator,
void** builtin_data);

TfLiteStatus ParseEqual(const Operator* op, ErrorReporter* error_reporter,
BuiltinDataAllocator* allocator, void** builtin_data);

Expand Down
28 changes: 24 additions & 4 deletions third_party/tflite-micro/tensorflow/lite/core/c/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,11 @@ TfLiteStatus TfLiteTensorCopy(const TfLiteTensor* src, TfLiteTensor* dst) {
return kTfLiteOk;
}

void TfLiteTensorResizeMaybeCopy(size_t num_bytes, TfLiteTensor* tensor,
bool preserve_data) {
TfLiteStatus TfLiteTensorResizeMaybeCopy(size_t num_bytes, TfLiteTensor* tensor,
bool preserve_data) {
if (tensor->allocation_type != kTfLiteDynamic &&
tensor->allocation_type != kTfLitePersistentRo) {
return;
return kTfLiteOk;
}
#ifdef TF_LITE_TENSORFLOW_PROFILER
tflite::PauseHeapMonitoring(/*pause=*/true);
Expand Down Expand Up @@ -258,9 +258,15 @@ void TfLiteTensorResizeMaybeCopy(size_t num_bytes, TfLiteTensor* tensor,
tflite::PauseHeapMonitoring(/*pause=*/false);
#endif
tensor->bytes = num_bytes;
if (tensor->data.data == nullptr && num_bytes != 0) {
// We are done allocating but tensor is pointing to null and a valid size
// was requested, so we error.
return kTfLiteError;
}
return kTfLiteOk;
}

void TfLiteTensorRealloc(size_t num_bytes, TfLiteTensor* tensor) {
TfLiteStatus TfLiteTensorRealloc(size_t num_bytes, TfLiteTensor* tensor) {
return TfLiteTensorResizeMaybeCopy(num_bytes, tensor, true);
}
#endif // TF_LITE_STATIC_MEMORY
Expand Down Expand Up @@ -331,4 +337,18 @@ void TfLiteOpaqueDelegateDelete(TfLiteOpaqueDelegate* opaque_delegate) {
delete tflite_delegate;
}

void* TfLiteOpaqueDelegateGetData(const TfLiteOpaqueDelegate* delegate) {
if (!delegate) return nullptr;

// The following cast is safe only because this code is part of the
// TF Lite runtime implementation. Apps using TF Lite should not rely on
// 'TfLiteOpaqueDelegate' and 'TfLiteDelegate' being equivalent.
const auto* tflite_delegate =
reinterpret_cast<const TfLiteDelegate*>(delegate);

if (!tflite_delegate->opaque_delegate_builder) return tflite_delegate->data_;

return tflite_delegate->opaque_delegate_builder->data;
}

} // extern "C"
81 changes: 70 additions & 11 deletions third_party/tflite-micro/tensorflow/lite/core/c/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ limitations under the License.
#ifndef TENSORFLOW_LITE_CORE_C_COMMON_H_
#define TENSORFLOW_LITE_CORE_C_COMMON_H_

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

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

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

// WARNING: This is an experimental interface that is subject to change.
Expand Down Expand Up @@ -955,12 +959,53 @@ typedef struct TfLiteRegistration {
// ops. We keep it inside of `TfLiteRegistration` and use it to route
// callbacks properly.
TfLiteRegistrationExternal* registration_external;

// Retrieves asynchronous kernel.
//
// If the `async_kernel` field is nullptr, it means the operation described by
// this TfLiteRegistration object does not support asynchronous execution.
// Otherwise, the function that the field points to should only be called for
// delegate kernel nodes, i.e. `node` should be a delegate kernel node created
// by applying a delegate.
// If the function returns nullptr, that means that the underlying delegate
// does not support asynchronous execution for this `node`.
struct TfLiteAsyncKernel* (*async_kernel)(TfLiteContext* context,
TfLiteNode* node);
} TfLiteRegistration;

/// \private
// Old version of `TfLiteRegistration` to maintain binary backward
// compatibility.
// The legacy registration type must be a POD struct type whose field types must
// be a prefix of the field types in TfLiteRegistration, and offset of the first
// field in TfLiteRegistration that is not present in the legacy registration
// type must be greater than or equal to the size of the legacy registration
// type.
// WARNING: This structure is deprecated / not an official part of the
// API. It should be only used for binary backward compatibility.
typedef struct TfLiteRegistration_V2 {
void* (*init)(TfLiteContext* context, const char* buffer, size_t length);
void (*free)(TfLiteContext* context, void* buffer);
TfLiteStatus (*prepare)(TfLiteContext* context, TfLiteNode* node);
TfLiteStatus (*invoke)(TfLiteContext* context, TfLiteNode* node);
const char* (*profiling_string)(const TfLiteContext* context,
const TfLiteNode* node);
int32_t builtin_code;
const char* custom_name;
int version;
TfLiteRegistrationExternal* registration_external;
} TfLiteRegistration_V2;

/// \private
// Old version of `TfLiteRegistration` to maintain binary backward
// compatibility.
// WARNING: This structure is deprecated / not an official part of the API.
// It should be only used for binary backward compatibility.
// The legacy registration type must be a POD struct type whose field types must
// be a prefix of the field types in TfLiteRegistration, and offset of the first
// field in TfLiteRegistration that is not present in the legacy registration
// type must be greater than or equal to the size of the legacy registration
// type.
// WARNING: This structure is deprecated / not an official part of the
// API. It should be only used for binary backward compatibility.
typedef struct TfLiteRegistration_V1 {
void* (*init)(TfLiteContext* context, const char* buffer, size_t length);
void (*free)(TfLiteContext* context, void* buffer);
Expand Down Expand Up @@ -1135,6 +1180,20 @@ TfLiteOpaqueDelegate* TfLiteOpaqueDelegateCreate(
// 'delegate' is a null pointer.
void TfLiteOpaqueDelegateDelete(TfLiteOpaqueDelegate* delegate);

// Returns a pointer to the data associated with the provided opaque 'delegate'.
//
// A null pointer will be returned when:
// - The 'delegate' is null.
// - The 'data' field of the 'TfLiteOpaqueDelegateBuilder' used to construct the
// 'delegate' was null.
// - Or in case of any other error.
// - The 'delegate' has been constructed via a 'TfLiteOpaqueDelegateBuilder',
// but the 'data' field of the 'TfLiteOpaqueDelegateBuilder' is null.
//
// The data_ field of 'delegate' will be returned if the
// 'opaque_delegate_builder' field is null.
void* TfLiteOpaqueDelegateGetData(const TfLiteOpaqueDelegate* delegate);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
Expand Down
Loading

0 comments on commit ac10632

Please sign in to comment.