Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions tensorflow/lite/micro/kernels/xtensa/pooling.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ TfLiteStatus AverageEval(TfLiteContext* context, TfLiteNode* node) {
auto* params = reinterpret_cast<TfLitePoolParams*>(node->builtin_data);

TFLITE_DCHECK(node->user_data != nullptr);
#if defined(HIFI5)
#if defined(HIFI4) || defined(HIFI5) || defined(HIFI_IQ)
auto* op_data = static_cast<const XtensaOpDataPooling*>(node->user_data);
const OpDataPooling* reference_op_data = &(op_data->reference_op_data);
#else
Expand All @@ -53,8 +53,8 @@ TfLiteStatus AverageEval(TfLiteContext* context, TfLiteNode* node) {
break;
}
case kTfLiteInt8: {
#if defined(HIFI5)
AverageEvalQuantizedHifi(context, node, params, op_data, input, output);
#if defined(HIFI4) || defined(HIFI5) || defined(HIFI_IQ)
AverageEvalQuantizedInt8Hifi(context, node, params, op_data, input, output);
#elif defined(VISION_P6)
const auto& op_data =
*(reinterpret_cast<XtensaOpDataPooling*>(node->user_data));
Expand All @@ -66,8 +66,12 @@ TfLiteStatus AverageEval(TfLiteContext* context, TfLiteNode* node) {
break;
}
case kTfLiteInt16: {
#if defined(HIFI4) || defined(HIFI5) || defined(HIFI_IQ)
AverageEvalQuantizedInt16Hifi(context, node, params, op_data, input, output);
#else
AveragePoolingEvalQuantized<int16_t>(context, node, params,
reference_op_data, input, output);
#endif
break;
}
default: {
Expand All @@ -84,28 +88,29 @@ TfLiteStatus MaxEval(TfLiteContext* context, TfLiteNode* node) {
auto* params = reinterpret_cast<TfLitePoolParams*>(node->builtin_data);

TFLITE_DCHECK(node->user_data != nullptr);
#if defined(HIFI5)
const TfLiteEvalTensor* input =
micro::GetEvalInput(context, node, kPoolingInputTensor);
TfLiteEvalTensor* output =
micro::GetEvalOutput(context, node, kPoolingOutputTensor);

#if defined(HIFI4) || defined(HIFI5) || defined(HIFI_IQ)
const OpDataPooling* reference_op_data;
auto* op_data = static_cast<const XtensaOpDataPooling*>(node->user_data);
const OpDataPooling* reference_op_data = &(op_data->reference_op_data);
reference_op_data = &(op_data->reference_op_data);
#else
const OpDataPooling* reference_op_data =
static_cast<const OpDataPooling*>(node->user_data);
#endif

const TfLiteEvalTensor* input =
micro::GetEvalInput(context, node, kPoolingInputTensor);
TfLiteEvalTensor* output =
micro::GetEvalOutput(context, node, kPoolingOutputTensor);

switch (input->type) {
case kTfLiteFloat32: {
MaxPoolingEvalFloat(context, node, params, reference_op_data, input,
output);
break;
}
case kTfLiteInt8: {
#if defined(HIFI5)
MaxEvalQuantizedHifi(context, node, params, op_data, input, output);
#if defined(HIFI4) || defined(HIFI5) || defined(HIFI_IQ)
MaxEvalQuantizedInt8Hifi(context, node, params, op_data, input, output);
#elif defined(VISION_P6)
const auto& op_data =
*(reinterpret_cast<XtensaOpDataPooling*>(node->user_data));
Expand All @@ -117,8 +122,12 @@ TfLiteStatus MaxEval(TfLiteContext* context, TfLiteNode* node) {
break;
}
case kTfLiteInt16: {
#if defined(HIFI4) || defined(HIFI5) || defined(HIFI_IQ)
MaxEvalQuantizedInt16Hifi(context, node, params, op_data, input, output);
#else
MaxPoolingEvalQuantized<int16_t>(context, node, params, reference_op_data,
input, output);
#endif
break;
}
default: {
Expand All @@ -133,7 +142,7 @@ TfLiteStatus MaxEval(TfLiteContext* context, TfLiteNode* node) {
} // namespace

TFLMRegistration Register_AVERAGE_POOL_2D() {
#if defined(HIFI5)
#if defined(HIFI4) || defined(HIFI5) || defined(HIFI_IQ)
return tflite::micro::RegisterOp(XtensaPoolingInit, AveragePrepareHifi,
AverageEval);
#elif defined(VISION_P6)
Expand All @@ -146,7 +155,7 @@ TFLMRegistration Register_AVERAGE_POOL_2D() {
}

TFLMRegistration Register_MAX_POOL_2D() {
#if defined(HIFI5)
#if defined(HIFI4) || defined(HIFI5) || defined(HIFI_IQ)
return tflite::micro::RegisterOp(XtensaPoolingInit, MaxPrepareHifi, MaxEval);
#elif defined(VISION_P6)
return tflite::micro::RegisterOp(XtensaPoolingInit, MaxPoolingPrepareVision,
Expand Down
125 changes: 125 additions & 0 deletions tensorflow/lite/micro/kernels/xtensa/pooling_int16.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/* Copyright 2023 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.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
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.
==============================================================================*/

#include "tensorflow/lite/c/builtin_op_data.h"
#include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
#include "tensorflow/lite/kernels/kernel_util.h"
#include "tensorflow/lite/micro/kernels/kernel_util.h"
#include "tensorflow/lite/micro/kernels/pooling.h"
#include "tensorflow/lite/micro/kernels/xtensa/xtensa.h"
#include "tensorflow/lite/micro/kernels/xtensa/xtensa_pooling.h"
#include "tensorflow/lite/micro/micro_log.h"

namespace tflite {

TfLiteStatus AverageEvalQuantizedInt16Hifi(TfLiteContext* context,
const TfLiteNode* node,
const TfLitePoolParams* params,
const XtensaOpDataPooling* data,
const TfLiteEvalTensor* input,
TfLiteEvalTensor* output) {
TFLITE_DCHECK(input->type == kTfLiteInt16);

const RuntimeShape& input_shape = tflite::micro::GetTensorShape(input);
const RuntimeShape& output_shape = tflite::micro::GetTensorShape(output);
const int batches = MatchingDim(input_shape, 0, output_shape, 0);
const int depth = MatchingDim(input_shape, 3, output_shape, 3);
const int input_height = input_shape.Dims(1);
const int input_width = input_shape.Dims(2);
const int output_height = output_shape.Dims(1);
const int output_width = output_shape.Dims(2);

void* p_scratch = static_cast<void*>(
context->GetScratchBuffer(context, data->scratch_tensor_index));

const int16_t* inp_data_ptr = tflite::micro::GetTensorData<int16_t>(input);
int16_t* out_data_ptr = tflite::micro::GetTensorData<int16_t>(output);

for (int batch = 0; batch < batches; ++batch) {
TF_LITE_ENSURE_EQ(
context,
xa_nn_avgpool_16(
&out_data_ptr[output_height * output_width * depth * batch],
const_cast<int16_t*>(
&inp_data_ptr[output_height * output_width * depth * batch]),
input_height, input_width, depth, params->filter_height,
params->filter_width, params->stride_width, params->stride_height,
data->reference_op_data.padding.width,
data->reference_op_data.padding.height, output_height, output_width,
0, 0, p_scratch),
0);
}

const int out_length = batches * output_height * output_width * depth;
TF_LITE_ENSURE_EQ(
context,
xa_nn_vec_activation_min_max_16_16(
out_data_ptr, out_data_ptr, data->reference_op_data.activation_min,
data->reference_op_data.activation_max, out_length),
0);

return kTfLiteOk;
}

TfLiteStatus MaxEvalQuantizedInt16Hifi(TfLiteContext* context,
const TfLiteNode* node,
const TfLitePoolParams* params,
const XtensaOpDataPooling* data,
const TfLiteEvalTensor* input,
TfLiteEvalTensor* output) {
TFLITE_DCHECK(input->type == kTfLiteInt16);

const RuntimeShape& input_shape = tflite::micro::GetTensorShape(input);
const RuntimeShape& output_shape = tflite::micro::GetTensorShape(output);
const int batches = MatchingDim(input_shape, 0, output_shape, 0);
const int depth = MatchingDim(input_shape, 3, output_shape, 3);
const int input_height = input_shape.Dims(1);
const int input_width = input_shape.Dims(2);
const int output_height = output_shape.Dims(1);
const int output_width = output_shape.Dims(2);

void* p_scratch = static_cast<void*>(
context->GetScratchBuffer(context, data->scratch_tensor_index));

const int16_t* inp_data_ptr = tflite::micro::GetTensorData<int16_t>(input);
int16_t* out_data_ptr = tflite::micro::GetTensorData<int16_t>(output);

for (int batch = 0; batch < batches; ++batch) {
TF_LITE_ENSURE_EQ(
context,
xa_nn_maxpool_16(
&out_data_ptr[output_height * output_width * depth * batch],
const_cast<int16_t*>(
&inp_data_ptr[output_height * output_width * depth * batch]),
input_height, input_width, depth, params->filter_height,
params->filter_width, params->stride_width, params->stride_height,
data->reference_op_data.padding.width,
data->reference_op_data.padding.height, output_height, output_width,
0, 0, p_scratch),
0);
}

const int out_length = batches * output_height * output_width * depth;
TF_LITE_ENSURE_EQ(
context,
xa_nn_vec_activation_min_max_16_16(
out_data_ptr, out_data_ptr, data->reference_op_data.activation_min,
data->reference_op_data.activation_max, out_length),
0);

return kTfLiteOk;
}

} // namespace tflite
Loading
Loading