Skip to content

Commit

Permalink
Sync from tflite-micro at e50c14f.
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 Jan 18, 2023
1 parent 8d9a3cf commit 8d94303
Show file tree
Hide file tree
Showing 16 changed files with 1,616 additions and 70 deletions.
2 changes: 1 addition & 1 deletion conf/tflite-micro.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8746ec9
e50c14f
14 changes: 14 additions & 0 deletions third_party/tflite-micro/tensorflow/lite/core/c/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,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 nullptr;

return tflite_delegate->opaque_delegate_builder->data;
}

} // extern "C"
11 changes: 11 additions & 0 deletions third_party/tflite-micro/tensorflow/lite/core/c/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,17 @@ 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.
void* TfLiteOpaqueDelegateGetData(const TfLiteOpaqueDelegate* delegate);

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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ TfLiteRegistration Register_MIRROR_PAD();
TfLiteRegistration Register_NEG();
TfLiteRegistration Register_PRELU();
TfLiteRegistration Register_MUL();
TfLiteRegistration Register_PACK();
TfLiteRegistration Register_PAD();
TfLiteRegistration Register_PADV2();
TfLiteRegistration Register_QUANTIZE();
Expand Down Expand Up @@ -113,7 +114,6 @@ TfLiteRegistration Register_LOGICAL_NOT();
TfLiteRegistration Register_MAXIMUM();
TfLiteRegistration Register_MINIMUM();
TfLiteRegistration Register_NOT_EQUAL();
TfLiteRegistration Register_PACK();
TfLiteRegistration Register_RESHAPE();
TfLiteRegistration Register_RESIZE_NEAREST_NEIGHBOR();
TfLiteRegistration Register_ROUND();
Expand Down
11 changes: 3 additions & 8 deletions third_party/tflite-micro/tensorflow/lite/micro/kernels/pack.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2019 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 @@ -20,9 +20,7 @@ limitations under the License.
#include "tensorflow/lite/micro/micro_log.h"

namespace tflite {
namespace ops {
namespace micro {
namespace pack {

namespace {

constexpr int kOutputTensor = 0;
Expand Down Expand Up @@ -106,12 +104,9 @@ TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
}

} // namespace
} // namespace pack

TfLiteRegistration Register_PACK() {
return tflite::micro::RegisterOp(nullptr, nullptr, pack::Eval);
return tflite::micro::RegisterOp(nullptr, nullptr, Eval);
}

} // namespace micro
} // namespace ops
} // namespace tflite
16 changes: 13 additions & 3 deletions third_party/tflite-micro/tensorflow/lite/micro/kernels/pooling.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2021 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 Down Expand Up @@ -43,7 +43,12 @@ TfLiteStatus AverageEval(TfLiteContext* context, TfLiteNode* node) {
AveragePoolingEvalFloat(context, node, params, data, input, output);
break;
case kTfLiteInt8:
AveragePoolingEvalQuantized(context, node, params, data, input, output);
AveragePoolingEvalQuantized<int8_t>(context, node, params, data, input,
output);
break;
case kTfLiteInt16:
AveragePoolingEvalQuantized<int16_t>(context, node, params, data, input,
output);
break;
default:
MicroPrintf("Input type %s is not currently supported",
Expand Down Expand Up @@ -71,7 +76,12 @@ TfLiteStatus MaxEval(TfLiteContext* context, TfLiteNode* node) {
MaxPoolingEvalFloat(context, node, params, data, input, output);
break;
case kTfLiteInt8:
MaxPoolingEvalQuantized(context, node, params, data, input, output);
MaxPoolingEvalQuantized<int8_t>(context, node, params, data, input,
output);
break;
case kTfLiteInt16:
MaxPoolingEvalQuantized<int16_t>(context, node, params, data, input,
output);
break;
default:
MicroPrintf("Type %s not currently supported.",
Expand Down
61 changes: 59 additions & 2 deletions third_party/tflite-micro/tensorflow/lite/micro/kernels/pooling.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ limitations under the License.

#include "tensorflow/lite/c/builtin_op_data.h"
#include "tensorflow/lite/c/common.h"
#include "tensorflow/lite/kernels/internal/reference/integer_ops/pooling.h"
#include "tensorflow/lite/kernels/internal/reference/pooling.h"
#include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
#include "tensorflow/lite/kernels/kernel_util.h"
#include "tensorflow/lite/kernels/padding.h"
#include "tensorflow/lite/micro/kernels/kernel_util.h"
#include "tensorflow/lite/micro/kernels/micro_ops.h"
#include "tensorflow/lite/micro/micro_log.h"

namespace tflite {

Expand Down Expand Up @@ -50,27 +57,69 @@ void AveragePoolingEvalFloat(const TfLiteContext* context,
const TfLiteEvalTensor* input,
TfLiteEvalTensor* output);

template <typename T>
void AveragePoolingEvalQuantized(TfLiteContext* context, const TfLiteNode* node,
const TfLitePoolParams* params,
const OpDataPooling* data,
const TfLiteEvalTensor* input,
TfLiteEvalTensor* output);
TfLiteEvalTensor* output) {
TFLITE_DCHECK(input->type == kTfLiteInt8 || input->type == kTfLiteInt16);

PoolParams op_params;
op_params.stride_height = params->stride_height;
op_params.stride_width = params->stride_width;
op_params.filter_height = params->filter_height;
op_params.filter_width = params->filter_width;
op_params.padding_values.height = data->padding.height;
op_params.padding_values.width = data->padding.width;
op_params.quantized_activation_min = data->activation_min;
op_params.quantized_activation_max = data->activation_max;

reference_integer_ops::AveragePool(op_params,
tflite::micro::GetTensorShape(input),
tflite::micro::GetTensorData<T>(input),
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<T>(output));
}

void MaxPoolingEvalFloat(TfLiteContext* context, TfLiteNode* node,
TfLitePoolParams* params, const OpDataPooling* data,
const TfLiteEvalTensor* input,
TfLiteEvalTensor* output);

template <typename T>
void MaxPoolingEvalQuantized(TfLiteContext* context, TfLiteNode* node,
TfLitePoolParams* params,
const OpDataPooling* data,
const TfLiteEvalTensor* input,
TfLiteEvalTensor* output);
TfLiteEvalTensor* output) {
TFLITE_DCHECK(input->type == kTfLiteInt8 || input->type == kTfLiteInt16);

tflite::PoolParams op_params;
op_params.stride_height = params->stride_height;
op_params.stride_width = params->stride_width;
op_params.filter_height = params->filter_height;
op_params.filter_width = params->filter_width;
op_params.padding_values.height = data->padding.height;
op_params.padding_values.width = data->padding.width;
op_params.quantized_activation_min = data->activation_min;
op_params.quantized_activation_max = data->activation_max;

reference_integer_ops::MaxPool(op_params,
tflite::micro::GetTensorShape(input),
tflite::micro::GetTensorData<T>(input),
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<T>(output));
}

#if defined(CMSIS_NN)
TfLiteRegistration Register_AVERAGE_POOL_2D_INT8();

TfLiteRegistration Register_MAX_POOL_2D_INT8();

TfLiteRegistration Register_AVERAGE_POOL_2D_INT16();

TfLiteRegistration Register_MAX_POOL_2D_INT16();
#else
inline TfLiteRegistration Register_AVERAGE_POOL_2D_INT8() {
return tflite::Register_AVERAGE_POOL_2D();
Expand All @@ -79,6 +128,14 @@ inline TfLiteRegistration Register_AVERAGE_POOL_2D_INT8() {
inline TfLiteRegistration Register_MAX_POOL_2D_INT8() {
return tflite::Register_MAX_POOL_2D();
}

inline TfLiteRegistration Register_AVERAGE_POOL_2D_INT16() {
return tflite::Register_AVERAGE_POOL_2D();
}

inline TfLiteRegistration Register_MAX_POOL_2D_INT16() {
return tflite::Register_MAX_POOL_2D();
}
#endif
} // namespace tflite

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2021 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 Down Expand Up @@ -69,10 +69,14 @@ TfLiteStatus PoolingPrepare(TfLiteContext* context, TfLiteNode* node) {
if (input->type == kTfLiteFloat32) {
CalculateActivationRange(params->activation, &data->activation_min_f32,
&data->activation_max_f32);
} else if (input->type == kTfLiteInt8) {
} else if (input->type == kTfLiteInt8 || input->type == kTfLiteInt16) {
CalculateActivationRangeQuantized(context, params->activation, output,
&data->activation_min,
&data->activation_max);
} else {
MicroPrintf("Type %s (%d) not supported.", TfLiteTypeGetName(input->type),
input->type);
return kTfLiteError;
}

micro_context->DeallocateTempTfLiteTensor(input);
Expand Down Expand Up @@ -102,30 +106,6 @@ void AveragePoolingEvalFloat(const TfLiteContext* context,
tflite::micro::GetTensorData<float>(output));
}

void AveragePoolingEvalQuantized(TfLiteContext* context, const TfLiteNode* node,
const TfLitePoolParams* params,
const OpDataPooling* data,
const TfLiteEvalTensor* input,
TfLiteEvalTensor* output) {
TFLITE_DCHECK(input->type == kTfLiteInt8);

PoolParams op_params;
op_params.stride_height = params->stride_height;
op_params.stride_width = params->stride_width;
op_params.filter_height = params->filter_height;
op_params.filter_width = params->filter_width;
op_params.padding_values.height = data->padding.height;
op_params.padding_values.width = data->padding.width;
op_params.quantized_activation_min = data->activation_min;
op_params.quantized_activation_max = data->activation_max;

reference_integer_ops::AveragePool(
op_params, tflite::micro::GetTensorShape(input),
tflite::micro::GetTensorData<int8_t>(input),
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<int8_t>(output));
}

void MaxPoolingEvalFloat(TfLiteContext* context, TfLiteNode* node,
TfLitePoolParams* params, const OpDataPooling* data,
const TfLiteEvalTensor* input,
Expand All @@ -145,26 +125,4 @@ void MaxPoolingEvalFloat(TfLiteContext* context, TfLiteNode* node,
tflite::micro::GetTensorData<float>(output));
}

void MaxPoolingEvalQuantized(TfLiteContext* context, TfLiteNode* node,
TfLitePoolParams* params,
const OpDataPooling* data,
const TfLiteEvalTensor* input,
TfLiteEvalTensor* output) {
tflite::PoolParams op_params;
op_params.stride_height = params->stride_height;
op_params.stride_width = params->stride_width;
op_params.filter_height = params->filter_height;
op_params.filter_width = params->filter_width;
op_params.padding_values.height = data->padding.height;
op_params.padding_values.width = data->padding.width;
op_params.quantized_activation_min = data->activation_min;
op_params.quantized_activation_max = data->activation_max;

reference_integer_ops::MaxPool(op_params,
tflite::micro::GetTensorShape(input),
tflite::micro::GetTensorData<int8_t>(input),
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<int8_t>(output));
}

} // namespace tflite
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,16 @@ cc_library(
hdrs = ["conv_test_data.h"],
deps = ["//tensorflow/lite/c:common"],
)

cc_library(
name = "lstm_test_data",
srcs = ["lstm_test_data.cc"],
hdrs = [
"lstm_test_data.h",
],
deps = [
"//tensorflow/lite/c:common",
"//tensorflow/lite/micro:test_helpers",
"//tensorflow/lite/micro/kernels:lstm_shared",
],
)
Loading

0 comments on commit 8d94303

Please sign in to comment.