|
1 | | -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. |
| 1 | +/* Copyright 2025 The TensorFlow Authors. All Rights Reserved. |
2 | 2 |
|
3 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 4 | you may not use this file except in compliance with the License. |
@@ -129,6 +129,46 @@ void TestReluInt8(int* input_dims_data, const float* input_data, |
129 | 129 | } |
130 | 130 | } |
131 | 131 |
|
| 132 | +void TestReluInt16(int* input_dims_data, const float* input_data, |
| 133 | + int16_t* input_data_quantized, const float input_scale, |
| 134 | + const int input_zero_point, const float* golden, |
| 135 | + int16_t* golden_quantized, int* output_dims_data, |
| 136 | + const float output_scale, const int output_zero_point, |
| 137 | + int16_t* output_data) { |
| 138 | + TfLiteIntArray* input_dims = IntArrayFromInts(input_dims_data); |
| 139 | + TfLiteIntArray* output_dims = IntArrayFromInts(output_dims_data); |
| 140 | + const int output_elements_count = ElementCount(*output_dims); |
| 141 | + constexpr int inputs_size = 1; |
| 142 | + constexpr int outputs_size = 1; |
| 143 | + constexpr int tensors_size = inputs_size + outputs_size; |
| 144 | + TfLiteTensor tensors[tensors_size] = { |
| 145 | + CreateQuantizedTensor(input_data, input_data_quantized, input_dims, |
| 146 | + input_scale, input_zero_point), |
| 147 | + CreateQuantizedTensor(output_data, output_dims, output_scale, |
| 148 | + output_zero_point), |
| 149 | + }; |
| 150 | + |
| 151 | + int inputs_array_data[] = {1, 0}; |
| 152 | + TfLiteIntArray* inputs_array = IntArrayFromInts(inputs_array_data); |
| 153 | + int outputs_array_data[] = {1, 1}; |
| 154 | + TfLiteIntArray* outputs_array = IntArrayFromInts(outputs_array_data); |
| 155 | + |
| 156 | + const TFLMRegistration registration = Register_RELU(); |
| 157 | + micro::KernelRunner runner(registration, tensors, tensors_size, inputs_array, |
| 158 | + outputs_array, |
| 159 | + /*builtin_data=*/nullptr); |
| 160 | + |
| 161 | + TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, runner.InitAndPrepare()); |
| 162 | + TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, runner.Invoke()); |
| 163 | + |
| 164 | + Quantize(golden, golden_quantized, output_elements_count, output_scale, |
| 165 | + output_zero_point); |
| 166 | + |
| 167 | + for (int i = 0; i < output_elements_count; ++i) { |
| 168 | + TF_LITE_MICRO_EXPECT_EQ(golden_quantized[i], output_data[i]); |
| 169 | + } |
| 170 | +} |
| 171 | + |
132 | 172 | void TestRelu6Int8(int* input_dims_data, const float* input_data, |
133 | 173 | int8_t* input_data_quantized, const float input_scale, |
134 | 174 | const int input_zero_point, const float* golden, |
@@ -265,6 +305,29 @@ TF_LITE_MICRO_TEST(SimpleReluTestInt8) { |
265 | 305 | output_zero_point, output_data); |
266 | 306 | } |
267 | 307 |
|
| 308 | +TF_LITE_MICRO_TEST(SimpleReluTestInt16) { |
| 309 | + const int elements_count = 10; |
| 310 | + |
| 311 | + int input_shape[] = {2, 2, 5}; |
| 312 | + const float input_data[] = {256, 257, 258, 259, 260, |
| 313 | + -256, -257, -258, -259, -260}; |
| 314 | + int16_t input_quantized[elements_count]; |
| 315 | + int output_shape[] = {2, 2, 5}; |
| 316 | + const float golden[] = {256, 257, 258, 259, 260, 0, 0, 0, 0, 0}; |
| 317 | + int16_t golden_quantized[elements_count]; |
| 318 | + int16_t output_data[elements_count]; |
| 319 | + |
| 320 | + const float input_scale = 0.5f; |
| 321 | + const int input_zero_point = 0; |
| 322 | + const float output_scale = 0.5f; |
| 323 | + const int output_zero_point = 0; |
| 324 | + |
| 325 | + tflite::testing::TestReluInt16(input_shape, input_data, input_quantized, |
| 326 | + input_scale, input_zero_point, golden, |
| 327 | + golden_quantized, output_shape, output_scale, |
| 328 | + output_zero_point, output_data); |
| 329 | +} |
| 330 | + |
268 | 331 | TF_LITE_MICRO_TEST(SimpleRelu6TestInt8) { |
269 | 332 | const int elements_count = 10; |
270 | 333 |
|
|
0 commit comments