Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add int16 support to RELU #2727

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
21 changes: 15 additions & 6 deletions tensorflow/lite/micro/kernels/activations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,23 @@ TfLiteStatus ReluEval(TfLiteContext* context, TfLiteNode* node) {
return kTfLiteOk;
}
case kTfLiteInt8: {
tflite::ReluQuantized(data, tflite::micro::GetTensorShape(input),
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<int8_t>(input),
tflite::micro::GetTensorData<int8_t>(output));
tflite::ReluQuantized<int8_t>(
data, tflite::micro::GetTensorShape(input),
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<int8_t>(input),
tflite::micro::GetTensorData<int8_t>(output));
return kTfLiteOk;
}
case kTfLiteInt16: {
tflite::ReluQuantized<int16_t>(
data, tflite::micro::GetTensorShape(input),
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<int16_t>(input),
tflite::micro::GetTensorData<int16_t>(output));
return kTfLiteOk;
}
default: {
MicroPrintf("Only float32 is supported currently, got %s",
MicroPrintf("Only float32/int8/int16 is supported currently, got %s",
TfLiteTypeGetName(input->type));
return kTfLiteError;
}
Expand Down Expand Up @@ -109,7 +118,7 @@ TfLiteStatus Relu6Eval(TfLiteContext* context, TfLiteNode* node) {
return kTfLiteOk;
}
default: {
MicroPrintf("Only float32 is supported currently, got %s",
MicroPrintf("Only float32/int8/int16 is supported currently, got %s",
TfLiteTypeGetName(input->type));
return kTfLiteError;
}
Expand Down
19 changes: 17 additions & 2 deletions tensorflow/lite/micro/kernels/activations.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ limitations under the License.

#include "tensorflow/lite/c/builtin_op_data.h"
#include "tensorflow/lite/c/common.h"
#include "tensorflow/lite/kernels/internal/common.h"
#include "tensorflow/lite/kernels/internal/types.h"

namespace tflite {
Expand All @@ -36,9 +37,23 @@ struct Relu6OpData {
int32_t zero;
};

template <typename T>
void ReluQuantized(const ReluOpData& data, const RuntimeShape& input_shape,
const RuntimeShape& output_shape, const int8_t* input_data,
int8_t* output_data);
const RuntimeShape& output_shape, const T* input_data,
T* output_data) {
const int flat_size = MatchingFlatSize(input_shape, output_shape);
for (int i = 0; i < flat_size; ++i) {
const int32_t val = static_cast<int32_t>(input_data[i]);
int32_t clamped =
data.params.output_offset +
MultiplyByQuantizedMultiplier(val - data.params.input_offset,
data.params.output_multiplier,
data.params.output_shift);
clamped = std::max(data.params.quantized_activation_min, clamped);
clamped = std::min(data.params.quantized_activation_max, clamped);
output_data[i] = static_cast<T>(clamped);
}
}

template <typename T>
void CalculateReluOpData(const TfLiteTensor* input, TfLiteTensor* output,
Expand Down
17 changes: 0 additions & 17 deletions tensorflow/lite/micro/kernels/activations_common.cc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In ReluPrepare the following code should be used to conform with TfLite (LiteRT):

  if (input->type == kTfLiteInt8) {
    CalculateReluOpData<int8_t>(input, output, data);
  }
  else if (input->type == kTfLiteInt16) {
    TF_LITE_ENSURE_EQ(context, input->params.zero_point, 0);
    TF_LITE_ENSURE_EQ(context, output->params.zero_point, 0);
    CalculateReluOpData<int16_t>(input, output, data);
  }

I am very surprised (and concerned) that the unit test passed, even though CalculateReluIOpData<int16_t> was not being called.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might have had something to do with the chosen input data values. I changed them so that they need to are actually int16 values and then the test failed. Could be just a coincidence that it happened to work or there might be something else going elsewhere

Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,6 @@ namespace tflite {
const int kActivationsInputTensor = 0;
const int kActivationsOutputTensor = 0;

void ReluQuantized(const ReluOpData& data, const RuntimeShape& input_shape,
const RuntimeShape& output_shape, const int8_t* input_data,
int8_t* output_data) {
const int flat_size = MatchingFlatSize(input_shape, output_shape);
for (int i = 0; i < flat_size; ++i) {
const int32_t val = static_cast<int32_t>(input_data[i]);
int32_t clamped =
data.params.output_offset +
MultiplyByQuantizedMultiplier(val - data.params.input_offset,
data.params.output_multiplier,
data.params.output_shift);
clamped = std::max(data.params.quantized_activation_min, clamped);
clamped = std::min(data.params.quantized_activation_max, clamped);
output_data[i] = static_cast<int8_t>(clamped);
}
}

template <typename T>
void CalculateReluOpData(const TfLiteTensor* input, TfLiteTensor* output,
ReluOpData* data) {
Expand Down
62 changes: 62 additions & 0 deletions tensorflow/lite/micro/kernels/activations_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,46 @@ void TestReluInt8(int* input_dims_data, const float* input_data,
}
}

void TestReluInt16(int* input_dims_data, const float* input_data,
int16_t* input_data_quantized, const float input_scale,
const int input_zero_point, const float* golden,
int16_t* golden_quantized, int* output_dims_data,
const float output_scale, const int output_zero_point,
int16_t* output_data) {
TfLiteIntArray* input_dims = IntArrayFromInts(input_dims_data);
TfLiteIntArray* output_dims = IntArrayFromInts(output_dims_data);
const int output_elements_count = ElementCount(*output_dims);
constexpr int inputs_size = 1;
constexpr int outputs_size = 1;
constexpr int tensors_size = inputs_size + outputs_size;
TfLiteTensor tensors[tensors_size] = {
CreateQuantizedTensor(input_data, input_data_quantized, input_dims,
input_scale, input_zero_point),
CreateQuantizedTensor(output_data, output_dims, output_scale,
output_zero_point),
};

int inputs_array_data[] = {1, 0};
TfLiteIntArray* inputs_array = IntArrayFromInts(inputs_array_data);
int outputs_array_data[] = {1, 1};
TfLiteIntArray* outputs_array = IntArrayFromInts(outputs_array_data);

const TFLMRegistration registration = Register_RELU();
micro::KernelRunner runner(registration, tensors, tensors_size, inputs_array,
outputs_array,
/*builtin_data=*/nullptr);

TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, runner.InitAndPrepare());
TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, runner.Invoke());

Quantize(golden, golden_quantized, output_elements_count, output_scale,
output_zero_point);

for (int i = 0; i < output_elements_count; ++i) {
TF_LITE_MICRO_EXPECT_EQ(golden_quantized[i], output_data[i]);
}
}

void TestRelu6Int8(int* input_dims_data, const float* input_data,
int8_t* input_data_quantized, const float input_scale,
const int input_zero_point, const float* golden,
Expand Down Expand Up @@ -265,6 +305,28 @@ TF_LITE_MICRO_TEST(SimpleReluTestInt8) {
output_zero_point, output_data);
}

TF_LITE_MICRO_TEST(SimpleReluTestInt16) {
const int elements_count = 10;

int input_shape[] = {2, 1, 5};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be 2, 2, 5

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

const float input_data[] = {1, 2, 3, 4, 5, -1, -2, -3, -4, -5};
int16_t input_quantized[elements_count];
int output_shape[] = {2, 1, 5};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be 2, 2, 5

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

const float golden[] = {1, 2, 3, 4, 5, 0, 0, 0, 0, 0};
int16_t golden_quantized[elements_count];
int16_t output_data[elements_count];

const float input_scale = 0.5f;
const int input_zero_point = 0;
const float output_scale = 0.5f;
const int output_zero_point = 0;

tflite::testing::TestReluInt16(input_shape, input_data, input_quantized,
input_scale, input_zero_point, golden,
golden_quantized, output_shape, output_scale,
output_zero_point, output_data);
}

TF_LITE_MICRO_TEST(SimpleRelu6TestInt8) {
const int elements_count = 10;

Expand Down
Loading