1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/lite/c/builtin_op_data.h"
17 #include "tensorflow/lite/c/common.h"
18 #include "tensorflow/lite/micro/all_ops_resolver.h"
19 #include "tensorflow/lite/micro/kernels/kernel_runner.h"
20 #include "tensorflow/lite/micro/test_helpers.h"
21 #include "tensorflow/lite/micro/testing/micro_test.h"
22
23 namespace tflite {
24 namespace testing {
25 namespace {
26
TestRound(const int * input_dims_data,const float * input_data,const float * expected_output_data,float * output_data)27 void TestRound(const int* input_dims_data, const float* input_data,
28 const float* expected_output_data, float* output_data) {
29 TfLiteIntArray* input_dims = IntArrayFromInts(input_dims_data);
30 TfLiteIntArray* output_dims = IntArrayFromInts(input_dims_data);
31 const int output_dims_count = ElementCount(*output_dims);
32 constexpr int inputs_size = 1;
33 constexpr int outputs_size = 1;
34 constexpr int tensors_size = inputs_size + outputs_size;
35 TfLiteTensor tensors[tensors_size] = {
36 CreateTensor(input_data, input_dims),
37 CreateTensor(output_data, output_dims),
38 };
39
40 int inputs_array_data[] = {1, 0};
41 TfLiteIntArray* inputs_array = IntArrayFromInts(inputs_array_data);
42 int outputs_array_data[] = {1, 1};
43 TfLiteIntArray* outputs_array = IntArrayFromInts(outputs_array_data);
44
45 const TfLiteRegistration registration = tflite::ops::micro::Register_ROUND();
46 micro::KernelRunner runner(registration, tensors, tensors_size, inputs_array,
47 outputs_array, nullptr);
48
49 TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, runner.InitAndPrepare());
50 TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, runner.Invoke());
51
52 for (int i = 0; i < output_dims_count; ++i) {
53 TF_LITE_MICRO_EXPECT_NEAR(expected_output_data[i], output_data[i], 1e-5f);
54 }
55 }
56
57 } // namespace
58 } // namespace testing
59 } // namespace tflite
60
61 TF_LITE_MICRO_TESTS_BEGIN
62
TF_LITE_MICRO_TEST(SingleDim)63 TF_LITE_MICRO_TEST(SingleDim) {
64 const int input_dims[] = {1, 6};
65 const float input_data[] = {8.5, 0.0, 3.5, 4.2, -3.5, -4.5};
66 const float golden[] = {8, 0, 4, 4, -4, -4};
67 float output_data[6];
68 tflite::testing::TestRound(input_dims, input_data, golden, output_data);
69 }
70
TF_LITE_MICRO_TEST(MultiDims)71 TF_LITE_MICRO_TEST(MultiDims) {
72 const int input_dims[] = {4, 2, 1, 1, 6};
73 const float input_data[] = {0.0001, 8.0001, 0.9999, 9.9999, 0.5, -0.0001,
74 -8.0001, -0.9999, -9.9999, -0.5, -2.5, 1.5};
75 const float golden[] = {0, 8, 1, 10, 0, 0, -8, -1, -10, -0, -2, 2};
76 float output_data[12];
77 tflite::testing::TestRound(input_dims, input_data, golden, output_data);
78 }
79
80 TF_LITE_MICRO_TESTS_END
81