1 /* Copyright 2017 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
ValidateShape(TfLiteTensor * tensors,const int tensor_count,int32_t * output_data,const int32_t * expected_output,int output_dims_count)27 void ValidateShape(TfLiteTensor* tensors, const int tensor_count,
28 int32_t* output_data, const int32_t* expected_output,
29 int output_dims_count) {
30 int inputs_array_data[] = {1, 0};
31 TfLiteIntArray* inputs_array = IntArrayFromInts(inputs_array_data);
32 int outputs_array_data[] = {1, 1};
33 TfLiteIntArray* outputs_array = IntArrayFromInts(outputs_array_data);
34
35 const TfLiteRegistration registration = tflite::Register_SHAPE();
36 micro::KernelRunner runner(registration, tensors, tensor_count, inputs_array,
37 outputs_array, nullptr);
38
39 TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, runner.InitAndPrepare());
40 TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, runner.Invoke());
41
42 for (int i = 0; i < output_dims_count; ++i) {
43 TF_LITE_MICRO_EXPECT_EQ(expected_output[i], output_data[i]);
44 }
45 }
46
TestShape(const int * input_dims_data,const float * input_data,const int * output_dims_data,const int32_t * expected_output_data,int32_t * output_data)47 void TestShape(const int* input_dims_data, const float* input_data,
48 const int* output_dims_data, const int32_t* expected_output_data,
49 int32_t* output_data) {
50 TfLiteIntArray* input_dims = IntArrayFromInts(input_dims_data);
51 TfLiteIntArray* output_dims = IntArrayFromInts(output_dims_data);
52 const int output_dims_count = ElementCount(*output_dims);
53
54 constexpr int inputs_size = 1;
55 constexpr int outputs_size = 1;
56 constexpr int tensors_size = inputs_size + outputs_size;
57 TfLiteTensor tensors[tensors_size] = {
58 CreateTensor(input_data, input_dims),
59 CreateTensor(output_data, output_dims, true),
60 };
61
62 ValidateShape(tensors, tensors_size, output_data, expected_output_data,
63 output_dims_count);
64 }
65
66 } // namespace
67 } // namespace testing
68 } // namespace tflite
69
70 TF_LITE_MICRO_TESTS_BEGIN
71
TF_LITE_MICRO_TEST(TestShape0)72 TF_LITE_MICRO_TEST(TestShape0) {
73 int input_shape[] = {1, 5};
74 float input_values[] = {1, 3, 1, 3, 5};
75 int output_dims[] = {1, 1}; // this is actually input_shapes shape
76 int32_t expected_output_data[] = {5};
77 int32_t output_data[1];
78
79 tflite::testing::TestShape(input_shape, input_values, output_dims,
80 expected_output_data, output_data);
81 }
82
TF_LITE_MICRO_TEST(TestShape1)83 TF_LITE_MICRO_TEST(TestShape1) {
84 int input_shape[] = {2, 4, 3};
85 float input_values[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
86 int output_dims[] = {2, 1, 1};
87 int32_t expected_output_data[] = {4, 3};
88 int32_t output_data[2];
89
90 tflite::testing::TestShape(input_shape, input_values, output_dims,
91 expected_output_data, output_data);
92 }
93
TF_LITE_MICRO_TEST(TestShape2)94 TF_LITE_MICRO_TEST(TestShape2) {
95 int input_shape[] = {2, 12, 1};
96 float input_values[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
97 int output_dims[] = {2, 1, 1};
98 int32_t expected_output_data[] = {12, 1};
99 int32_t output_data[2];
100
101 tflite::testing::TestShape(input_shape, input_values, output_dims,
102 expected_output_data, output_data);
103 }
104
TF_LITE_MICRO_TEST(TestShape3)105 TF_LITE_MICRO_TEST(TestShape3) {
106 int input_shape[] = {2, 2, 6};
107 float input_values[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
108 int output_dims[] = {2, 1, 1};
109 int32_t expected_output_data[] = {2, 6};
110 int32_t output_data[2];
111
112 tflite::testing::TestShape(input_shape, input_values, output_dims,
113 expected_output_data, output_data);
114 }
115
TF_LITE_MICRO_TEST(TestShape4)116 TF_LITE_MICRO_TEST(TestShape4) {
117 int input_shape[] = {2, 2, 2, 3};
118 float input_values[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
119 int output_dims[] = {3, 1, 1, 1};
120 int32_t expected_output_data[] = {2, 2, 3};
121 int32_t output_data[3];
122
123 tflite::testing::TestShape(input_shape, input_values, output_dims,
124 expected_output_data, output_data);
125 }
126
TF_LITE_MICRO_TEST(TestShape5)127 TF_LITE_MICRO_TEST(TestShape5) {
128 int input_shape[] = {1, 1};
129 float input_values[] = {1};
130 int output_dims[] = {1, 1};
131 int32_t expected_output_data[] = {1};
132 int32_t output_data[1];
133
134 tflite::testing::TestShape(input_shape, input_values, output_dims,
135 expected_output_data, output_data);
136 }
137
138 TF_LITE_MICRO_TESTS_END
139