-
Notifications
You must be signed in to change notification settings - Fork 821
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
||
|
There was a problem hiding this comment.
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):I am very surprised (and concerned) that the unit test passed, even though
CalculateReluIOpData<int16_t>
was not being called.There was a problem hiding this comment.
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