Skip to content

Commit

Permalink
Sync from tflite-micro at 8889685.
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 Feb 20, 2023
1 parent bd03b3e commit fed8118
Show file tree
Hide file tree
Showing 80 changed files with 4,450 additions and 3,621 deletions.
2 changes: 1 addition & 1 deletion conf/tflite-micro.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8746ec9
8889685
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_
10 changes: 7 additions & 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,12 @@ 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"

// TfLiteOpaqueDelegate: allows delegation of nodes to alternative backends.
// TfLiteOpaqueDelegate is an abstract type that is intended to have the same
// role as TfLiteDelegate, but without necessarily exposing the implementation
// details of how delegates are implemented.
typedef TfLiteDelegate TfLiteOpaqueDelegate;

#endif // TENSORFLOW_LITE_C_COMMON_H_
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"
36 changes: 27 additions & 9 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 @@ -1135,6 +1139,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 fed8118

Please sign in to comment.