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
48 changes: 37 additions & 11 deletions tensorflow/lite/micro/kernels/xtensa/add.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,31 @@ TfLiteStatus EvalAdd(TfLiteContext* context, TfLiteNode* node,
tflite::ArithmeticParams op_params;
SetActivationParams(data->output_activation_min_f32,
data->output_activation_max_f32, &op_params);
#if defined(INCLUDE_FLOAT_OPT) && (defined(HIFI3) || defined(HIFI4) || defined(HIFI5) || defined(HIFI_IQ))
int err;
const RuntimeShape extended_input1_shape =
RuntimeShape::ExtendedShape(4, tflite::micro::GetTensorShape(input1));
const RuntimeShape extended_input2_shape =
RuntimeShape::ExtendedShape(4, tflite::micro::GetTensorShape(input2));
const RuntimeShape extended_output_shape =
RuntimeShape::ExtendedShape(4, tflite::micro::GetTensorShape(output));

err = xa_nn_elm_add_broadcast_4D_f32xf32_f32(
tflite::micro::GetTensorData<float>(output),
extended_output_shape.DimsData(),
tflite::micro::GetTensorData<float>(input1),
extended_input1_shape.DimsData(),
tflite::micro::GetTensorData<float>(input2),
extended_input2_shape.DimsData());
TF_LITE_ENSURE(context, err == 0);

err = xa_nn_vec_activation_min_max_f32_f32(
tflite::micro::GetTensorData<float>(output),
tflite::micro::GetTensorData<float>(output),
data->output_activation_min_f32, data->output_activation_max_f32,
extended_output_shape.FlatSize());
TF_LITE_ENSURE(context, err == 0);
#else // defined(INCLUDE_FLOAT_OPT) && (defined(HIFI3) || defined(HIFI4) || defined(HIFI5) || defined(HIFI_IQ))
if (data->requires_broadcast) {
reference_ops::BroadcastAdd4DSlow(
op_params, tflite::micro::GetTensorShape(input1),
Expand All @@ -65,6 +90,7 @@ TfLiteStatus EvalAdd(TfLiteContext* context, TfLiteNode* node,
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<float>(output));
}
#endif // defined(INCLUDE_FLOAT_OPT) && (defined(HIFI3) || defined(HIFI4) || defined(HIFI5))
} break;
case kTfLiteInt32: {
tflite::ArithmeticParams op_params;
Expand Down Expand Up @@ -113,11 +139,11 @@ TfLiteStatus EvalAddQuantized(TfLiteContext* context, TfLiteNode* node,
op_params.output_shift = data->output_shift;
SetActivationParams(data->output_activation_min, data->output_activation_max,
&op_params);
#if !(defined(HIFI3) || defined(HIFI4) || defined(HIFI5))
#if !(defined(HIFI4) || defined(HIFI5) || defined(HIFI_IQ))
bool need_broadcast = reference_ops::ProcessBroadcastShapes(
tflite::micro::GetTensorShape(input1),
tflite::micro::GetTensorShape(input2), &op_params);
#endif // !defined(HIFI3) && !defined(HIFI4) && !defined(HIFI5)
#endif

switch (output->type) {
case kTfLiteInt8: {
Expand All @@ -126,7 +152,7 @@ TfLiteStatus EvalAddQuantized(TfLiteContext* context, TfLiteNode* node,
*(reinterpret_cast<XtensaAddOpData*>(node->user_data));
AddEvalQuantizedVision(context, node, *params, op_data, input1, input2,
output);
#elif defined(HIFI3) || defined(HIFI4) || defined(HIFI5) // defined(VISION_P6)
#elif defined(HIFI4) || defined(HIFI5) || defined(HIFI_IQ) // #if defined(VISION_P6)
int err;
const RuntimeShape extended_input1_shape =
RuntimeShape::ExtendedShape(4, tflite::micro::GetTensorShape(input1));
Expand All @@ -145,12 +171,12 @@ TfLiteStatus EvalAddQuantized(TfLiteContext* context, TfLiteNode* node,
extended_input1_shape.DimsData(), op_params.input1_offset,
op_params.input1_shift, op_params.input1_multiplier,
tflite::micro::GetTensorData<int8_t>(input2),
extended_input2_shape.DimsData(), op_params.input2_offset,
op_params.input2_shift, op_params.input2_multiplier,
op_params.left_shift);
extended_input2_shape.DimsData(),
op_params.input2_offset, op_params.input2_shift,
op_params.input2_multiplier, op_params.left_shift);

TF_LITE_ENSURE(context, err == 0);
#else // defined(VISION_P6)
#else // defined(VISION_P6)
if (need_broadcast) {
reference_integer_ops::BroadcastAdd4DSlow(
op_params, tflite::micro::GetTensorShape(input1),
Expand All @@ -168,11 +194,11 @@ TfLiteStatus EvalAddQuantized(TfLiteContext* context, TfLiteNode* node,
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<int8_t>(output));
}
#endif // defined(VISION_P6)
#endif // defined(VISION_P6)
break;
}
case kTfLiteInt16: {
#if defined(HIFI3) || defined(HIFI4) || defined(HIFI5)
#if defined(HIFI4) || defined(HIFI5) || defined(HIFI_IQ)
int err;
const RuntimeShape extended_input1_shape =
RuntimeShape::ExtendedShape(4, tflite::micro::GetTensorShape(input1));
Expand All @@ -196,7 +222,7 @@ TfLiteStatus EvalAddQuantized(TfLiteContext* context, TfLiteNode* node,
op_params.left_shift);

TF_LITE_ENSURE(context, err == 0);
#else // defined(HIFI3) || defined(HIFI4) || defined(HIFI5)
#else // #if defined(HIFI4) || defined(HIFI5)
if (need_broadcast) {
reference_ops::BroadcastAdd4DSlow(
op_params, tflite::micro::GetTensorShape(input1),
Expand All @@ -214,7 +240,7 @@ TfLiteStatus EvalAddQuantized(TfLiteContext* context, TfLiteNode* node,
tflite::micro::GetTensorData<int16_t>(output),
false);
}
#endif // defined(HIFI3) || defined(HIFI4) || defined(HIFI5)
#endif // #if defined(HIFI4) || defined(HIFI5)
break;
}
default:
Expand Down
12 changes: 6 additions & 6 deletions tensorflow/lite/micro/kernels/xtensa/dequantize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ TfLiteStatus DequantizeEval(TfLiteContext* context, TfLiteNode* node) {

switch (input->type) {
case kTfLiteInt8: {
#if HIFI_VFPU && (defined(HIFI5) || defined(HIFI4) || defined(HIFI3))
#if defined(INCLUDE_FLOAT_OPT) && (defined(HIFI3) || defined(HIFI4) || defined(HIFI5) || defined(HIFI_IQ))
int err;
const int8_t* input_data_ptr;
float* output_data_ptr;
Expand All @@ -61,17 +61,17 @@ TfLiteStatus DequantizeEval(TfLiteContext* context, TfLiteNode* node) {
output_data_ptr, input_data_ptr, data->quantization_params.zero_point,
data->quantization_params.scale, flat_size);
TF_LITE_ENSURE(context, (err == 0));
#else // HIFI_VFPU && (defined(HIFI5) || defined(HIFI4) || defined(HIFI3))
#else // defined(INCLUDE_FLOAT_OPT) && (defined(HIFI3) || defined(HIFI4) || defined(HIFI5) || defined(HIFI_IQ))
reference_ops::Dequantize(data->quantization_params,
tflite::micro::GetTensorShape(input),
tflite::micro::GetTensorData<int8_t>(input),
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<float>(output));
#endif // HIFI_VFPU && (defined(HIFI5) || defined(HIFI4) || defined(HIFI3))
#endif // defined(INCLUDE_FLOAT_OPT) && (defined(HIFI3) || defined(HIFI4) || defined(HIFI5) || defined(HIFI_IQ))
break;
}
case kTfLiteInt16: {
#if HIFI_VFPU && (defined(HIFI5) || defined(HIFI4) || defined(HIFI3))
#if defined(INCLUDE_FLOAT_OPT) && (defined(HIFI3) || defined(HIFI4) || defined(HIFI5) || defined(HIFI_IQ))
int err;
const int16_t* input_data_ptr;
float* output_data_ptr;
Expand All @@ -84,13 +84,13 @@ TfLiteStatus DequantizeEval(TfLiteContext* context, TfLiteNode* node) {
output_data_ptr, input_data_ptr, data->quantization_params.zero_point,
data->quantization_params.scale, flat_size);
TF_LITE_ENSURE(context, (err == 0));
#else // HIFI_VFPU && (defined(HIFI5) || defined(HIFI4) || defined(HIFI3))
#else // defined(INCLUDE_FLOAT_OPT) && (defined(HIFI3) || defined(HIFI4) || defined(HIFI5) || defined(HIFI_IQ))
reference_ops::Dequantize(data->quantization_params,
tflite::micro::GetTensorShape(input),
tflite::micro::GetTensorData<int16_t>(input),
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<float>(output));
#endif // HIFI_VFPU && (defined(HIFI5) || defined(HIFI4) || defined(HIFI3))
#endif // defined(INCLUDE_FLOAT_OPT) && (defined(HIFI3) || defined(HIFI4) || defined(HIFI5) || defined(HIFI_IQ))
break;
}
case kTfLiteUInt8:
Expand Down
139 changes: 139 additions & 0 deletions tensorflow/lite/micro/kernels/xtensa/hifimini/fixedpoint_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/* 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.
==============================================================================*/

#ifndef TENSORFLOW_LITE_MICRO_KERNELS_XTENSA_HIFIMINI_FIXEDPOINT_UTILS_H_
#define TENSORFLOW_LITE_MICRO_KERNELS_XTENSA_HIFIMINI_FIXEDPOINT_UTILS_H_

#if defined(HIFIMINI)
#include <xtensa/tie/xt_hifi2.h>

#include <algorithm>
#include <cmath>
#include <cstdint>

#include "tensorflow/lite/kernels/internal/compatibility.h"
#include "tensorflow/lite/micro/kernels/xtensa/xtensa.h"

namespace tflite {

// INT24 MIN/MAX
#define INT24_MIN -8388608
#define INT24_MAX 8388607

// Multiply 24bit value by a quantized multiplier (w/ shift) and returns a 48bit
// aligned value in the QR register.
inline ae_q56s MultiplyByQuantizedMultiplier(ae_p24x2s x_24x2,
int32_t quantized_multiplier,
int shift) {
// A value with 1 sign bit, N integer bits and M fractional bits is
// represented as QN+1.M since the sign bit is included in the integer bits.
//
// The Q notation in this method explains the values represented in each
// variable, along with an implicit division since the quantized_multiplier
// represents a value between 0.5 and 1.0 (Q1.X-1 where X is the bit precision
// of the type).
//
// Load the quantized multiplier into the PR register.
// NOTE: This method assumes that this param has been calculated for 24bit
// space - not 32bits.
// Q32.0 / 2^23 -> Q24.0 / 2^23 representing a Q1.23 multiplier.
ae_p24x2s quantized_multiplier_24x2 = AE_MOVPA24(quantized_multiplier);
// Shift right by 23 - 16 bits minus the specified shift. This is because we
// keep 16 fractional bits until the end to perform rounding. Subtract shift
// since shift is a left shift, and the 23-16 is a right shift.
int shift_amount = 7 - shift;

// Find the product of x and the quantized_multiplier.
// Q24.0 / 2^23 * Q24.0 = Q48.0 / 2^23
// Q48.0 / 2^23 >> 7 = Q48.0 / 2^16
ae_q56s result_56 = AE_MULP24S_HH(x_24x2, quantized_multiplier_24x2);

// Shift right if shift amount is positive, left if shift amount is negative.
if (shift_amount >= 0) {
result_56 = AE_Q56S_SRA(result_56, shift_amount);
} else {
result_56 = AE_Q56S_SLA(result_56, -shift_amount);
}

// Round off the bottom 16 bits.
// Q48.0 / 2^16 -> Q32.0 aligned to 48 bits.
result_56 = AE_ROUNDSQ32SYM(result_56);
return result_56;
}

// Multiply 32bit value by a quantized multiplier (w/ shift) and returns a 48bit
// aligned value in the QR register.
inline ae_q56s MultiplyByQuantizedMultiplierResult48Bit(
int32_t x, int32_t quantized_multiplier, int shift) {
// Convert x into a 2x24bit PR register file. If x is outside the numerical
// limits of a 24bit integer, the "fractional" or lower 8bits are discarded.
// If x is within the range of a 24 bit integer, the "signed" or upper 8bits
// are discarded.
ae_p24x2s x_24x2;
if (x > INT24_MIN && x < INT24_MAX) {
x_24x2 = AE_MOVPA24(x);
} else {
x_24x2 = static_cast<ae_p24s>(*reinterpret_cast<ae_p24f*>(&x));
shift += 8;
}

return MultiplyByQuantizedMultiplier(x_24x2, quantized_multiplier, shift);
}

// Calculate quantization params for 24bit runtimes.
inline void QuantizeMultiplierForInt24(float multiplier,
int32_t* quantized_multiplier,
int* shift) {
if (multiplier == 0.0f) {
*quantized_multiplier = 0;
*shift = 0;
return;
}

// Special cased to 24bit:
const float q = std::frexp(multiplier, shift);
auto q_fixed = static_cast<int64_t>(std::round(q * (1 << 23)));

TFLITE_CHECK(q_fixed <= (1 << 23));
if (q_fixed == (1 << 23)) {
q_fixed /= 2;
++*shift;
}
TFLITE_CHECK_LE(q_fixed, INT24_MAX);

// Ensure shift does not exceed 24-bit range.
TFLITE_CHECK_LE(*shift, 23);
if (*shift < -23) {
*shift = 0;
q_fixed = 0;
}
*quantized_multiplier = static_cast<int32_t>(q_fixed);
}

// Convert a floating point number to a Q representation for 24 bit integers.
inline int CreateQConstantForInt24(int integer_bits, float f) {
const float min_bounds = static_cast<float>(INT24_MIN);
const float max_bounds = static_cast<float>(INT24_MAX);

int fractional_bits = 23 - integer_bits;
float raw = std::round(f * static_cast<float>(1 << fractional_bits));
raw = std::max(raw, min_bounds);
raw = std::min(raw, max_bounds);
return static_cast<int>(raw);
}

} // namespace tflite
#endif // defined(HIFIMINI)
#endif // TENSORFLOW_LITE_MICRO_KERNELS_XTENSA_HIFIMINI_FIXEDPOINT_UTILS_H_
Loading
Loading