• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2022 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 <algorithm>
17 #include <cstdint>
18 #include <functional>
19 #include <memory>
20 #include <random>
21 
22 #include <gtest/gtest.h>
23 #include "tensorflow/lite/delegates/xnnpack/concatenation_tester.h"
24 #include "tensorflow/lite/delegates/xnnpack/xnnpack_delegate.h"
25 
26 namespace tflite {
27 namespace xnnpack {
28 
29 TEST(SignedQuantizedConcatenation, 1D_2_inputs) {
30   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
31       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
32                        TfLiteXNNPackDelegateDelete);
33 
34   std::random_device random_device;
35   auto rng = std::mt19937(random_device());
36   auto shape_rng =
37       std::bind(std::uniform_int_distribution<int32_t>(2, 10), std::ref(rng));
38   const std::vector<int32_t> shape1({shape_rng()});
39   const std::vector<int32_t> shape2({shape_rng()});
40 
41   for (int i = -1; i < 1; i++) {
42     // clang-format off
43     ConcatenationTester()
44         .InputShapes({shape1, shape2})
45         .Axis(i)
46         .Test(TensorType_INT8, xnnpack_delegate.get());
47     // clang-format on
48   }
49 }
50 
51 TEST(SignedQuantizedConcatenation, 2D_2_inputs) {
52   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
53       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
54                        TfLiteXNNPackDelegateDelete);
55 
56   std::random_device random_device;
57   auto rng = std::mt19937(random_device());
58   auto shape_rng =
59       std::bind(std::uniform_int_distribution<int32_t>(2, 10), std::ref(rng));
60 
61   for (int i = -1; i < 2; i++) {
62     // All dimensions must be the same, except for axis.
63     const std::vector<int32_t> shape1({shape_rng(), shape_rng()});
64     auto shape2 = SameShapeDifferentAxis(shape1, i, shape_rng());
65 
66     // clang-format off
67     ConcatenationTester()
68         .InputShapes({shape1, shape2})
69         .Axis(i)
70         .Test(TensorType_INT8, xnnpack_delegate.get());
71     // clang-format on
72   }
73 }
74 
75 TEST(SignedQuantizedConcatenation, 3D_2_inputs) {
76   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
77       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
78                        TfLiteXNNPackDelegateDelete);
79 
80   std::random_device random_device;
81   auto rng = std::mt19937(random_device());
82   auto shape_rng =
83       std::bind(std::uniform_int_distribution<int32_t>(2, 10), std::ref(rng));
84 
85   for (int i = -1; i < 3; i++) {
86     // All dimensions must be the same, except for axis.
87     const std::vector<int32_t> shape1({shape_rng(), shape_rng(), shape_rng()});
88     auto shape2 = SameShapeDifferentAxis(shape1, i, shape_rng());
89 
90     // clang-format off
91     ConcatenationTester()
92         .InputShapes({shape1, shape2})
93         .Axis(i)
94         .Test(TensorType_INT8, xnnpack_delegate.get());
95     // clang-format on
96   }
97 }
98 
99 TEST(SignedQuantizedConcatenation, 4D_2_inputs) {
100   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
101       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
102                        TfLiteXNNPackDelegateDelete);
103 
104   std::random_device random_device;
105   auto rng = std::mt19937(random_device());
106   auto shape_rng =
107       std::bind(std::uniform_int_distribution<int32_t>(2, 10), std::ref(rng));
108 
109   for (int i = -1; i < 4; i++) {
110     // All dimensions must be the same, except for axis.
111     const std::vector<int32_t> shape1(
112         {shape_rng(), shape_rng(), shape_rng(), shape_rng()});
113     auto shape2 = SameShapeDifferentAxis(shape1, i, shape_rng());
114 
115     // clang-format off
116     ConcatenationTester()
117         .InputShapes({shape1, shape2})
118         .Axis(i)
119         .Test(TensorType_INT8, xnnpack_delegate.get());
120     // clang-format on
121   }
122 }
123 
124 TEST(SignedQuantizedConcatenation, 1D_of_3) {
125   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
126       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
127                        TfLiteXNNPackDelegateDelete);
128 
129   std::random_device random_device;
130   auto rng = std::mt19937(random_device());
131   auto shape_rng =
132       std::bind(std::uniform_int_distribution<int32_t>(2, 10), std::ref(rng));
133   const std::vector<int32_t> shape1({shape_rng()});
134   const std::vector<int32_t> shape2({shape_rng()});
135   const std::vector<int32_t> shape3({shape_rng()});
136 
137   for (int i = -1; i < 1; i++) {
138     // clang-format off
139     ConcatenationTester()
140         .InputShapes({shape1, shape2, shape3})
141         .Axis(i)
142         .Test(TensorType_INT8, xnnpack_delegate.get());
143     // clang-format on
144   }
145 }
146 
147 TEST(SignedQuantizedConcatenation, 2D_of_3) {
148   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
149       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
150                        TfLiteXNNPackDelegateDelete);
151 
152   std::random_device random_device;
153   auto rng = std::mt19937(random_device());
154   auto shape_rng =
155       std::bind(std::uniform_int_distribution<int32_t>(2, 10), std::ref(rng));
156 
157   for (int i = -1; i < 2; i++) {
158     // All dimensions must be the same, except for axis.
159     const std::vector<int32_t> shape1({shape_rng(), shape_rng()});
160     auto shape2 = SameShapeDifferentAxis(shape1, i, shape_rng());
161     auto shape3 = SameShapeDifferentAxis(shape1, i, shape_rng());
162 
163     // clang-format off
164     ConcatenationTester()
165         .InputShapes({shape1, shape2, shape3})
166         .Axis(i)
167         .Test(TensorType_INT8, xnnpack_delegate.get());
168     // clang-format on
169   }
170 }
171 
172 TEST(SignedQuantizedConcatenation, 3D_of_3) {
173   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
174       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
175                        TfLiteXNNPackDelegateDelete);
176 
177   std::random_device random_device;
178   auto rng = std::mt19937(random_device());
179   auto shape_rng =
180       std::bind(std::uniform_int_distribution<int32_t>(2, 10), std::ref(rng));
181 
182   for (int i = -1; i < 3; i++) {
183     // All dimensions must be the same, except for axis.
184     const std::vector<int32_t> shape1({shape_rng(), shape_rng(), shape_rng()});
185     auto shape2 = SameShapeDifferentAxis(shape1, i, shape_rng());
186     auto shape3 = SameShapeDifferentAxis(shape1, i, shape_rng());
187 
188     // clang-format off
189     ConcatenationTester()
190         .InputShapes({shape1, shape2, shape3})
191         .Axis(i)
192         .Test(TensorType_INT8, xnnpack_delegate.get());
193     // clang-format on
194   }
195 }
196 
197 TEST(SignedQuantizedConcatenation, 4D_of_3) {
198   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
199       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
200                        TfLiteXNNPackDelegateDelete);
201 
202   std::random_device random_device;
203   auto rng = std::mt19937(random_device());
204   auto shape_rng =
205       std::bind(std::uniform_int_distribution<int32_t>(2, 10), std::ref(rng));
206 
207   for (int i = -1; i < 4; i++) {
208     // All dimensions must be the same, except for axis.
209     const std::vector<int32_t> shape1(
210         {shape_rng(), shape_rng(), shape_rng(), shape_rng()});
211     auto shape2 = SameShapeDifferentAxis(shape1, i, shape_rng());
212     auto shape3 = SameShapeDifferentAxis(shape1, i, shape_rng());
213 
214     // clang-format off
215     ConcatenationTester()
216         .InputShapes({shape1, shape2, shape3})
217         .Axis(i)
218         .Test(TensorType_INT8, xnnpack_delegate.get());
219     // clang-format on
220   }
221 }
222 
223 TEST(SignedQuantizedConcatenation, 1D_of_4) {
224   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
225       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
226                        TfLiteXNNPackDelegateDelete);
227 
228   std::random_device random_device;
229   auto rng = std::mt19937(random_device());
230   auto shape_rng =
231       std::bind(std::uniform_int_distribution<int32_t>(2, 10), std::ref(rng));
232   const std::vector<int32_t> shape1({shape_rng()});
233   const std::vector<int32_t> shape2({shape_rng()});
234   const std::vector<int32_t> shape3({shape_rng()});
235   const std::vector<int32_t> shape4({shape_rng()});
236 
237   for (int i = -1; i < 1; i++) {
238     // clang-format off
239     ConcatenationTester()
240         .InputShapes({shape1, shape2, shape3, shape4})
241         .Axis(i)
242         .Test(TensorType_INT8, xnnpack_delegate.get());
243     // clang-format on
244   }
245 }
246 
247 TEST(SignedQuantizedConcatenation, 2D_of_4) {
248   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
249       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
250                        TfLiteXNNPackDelegateDelete);
251 
252   std::random_device random_device;
253   auto rng = std::mt19937(random_device());
254   auto shape_rng =
255       std::bind(std::uniform_int_distribution<int32_t>(2, 10), std::ref(rng));
256 
257   for (int i = -1; i < 2; i++) {
258     // All dimensions must be the same, except for axis.
259     const std::vector<int32_t> shape1({shape_rng(), shape_rng()});
260     auto shape2 = SameShapeDifferentAxis(shape1, i, shape_rng());
261     auto shape3 = SameShapeDifferentAxis(shape1, i, shape_rng());
262     auto shape4 = SameShapeDifferentAxis(shape1, i, shape_rng());
263 
264     // clang-format off
265     ConcatenationTester()
266         .InputShapes({shape1, shape2, shape3, shape4})
267         .Axis(i)
268         .Test(TensorType_INT8, xnnpack_delegate.get());
269     // clang-format on
270   }
271 }
272 
273 TEST(SignedQuantizedConcatenation, 3D_of_4) {
274   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
275       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
276                        TfLiteXNNPackDelegateDelete);
277 
278   std::random_device random_device;
279   auto rng = std::mt19937(random_device());
280   auto shape_rng =
281       std::bind(std::uniform_int_distribution<int32_t>(2, 10), std::ref(rng));
282 
283   for (int i = -1; i < 3; i++) {
284     // All dimensions must be the same, except for axis.
285     const std::vector<int32_t> shape1({shape_rng(), shape_rng(), shape_rng()});
286     auto shape2 = SameShapeDifferentAxis(shape1, i, shape_rng());
287     auto shape3 = SameShapeDifferentAxis(shape1, i, shape_rng());
288     auto shape4 = SameShapeDifferentAxis(shape1, i, shape_rng());
289 
290     // clang-format off
291     ConcatenationTester()
292         .InputShapes({shape1, shape2, shape3, shape4})
293         .Axis(i)
294         .Test(TensorType_INT8, xnnpack_delegate.get());
295     // clang-format on
296   }
297 }
298 
299 TEST(SignedQuantizedConcatenation, 4D_of_4) {
300   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
301       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
302                        TfLiteXNNPackDelegateDelete);
303 
304   std::random_device random_device;
305   auto rng = std::mt19937(random_device());
306   auto shape_rng =
307       std::bind(std::uniform_int_distribution<int32_t>(2, 10), std::ref(rng));
308 
309   for (int i = -1; i < 4; i++) {
310     // All dimensions must be the same, except for axis.
311     const std::vector<int32_t> shape1(
312         {shape_rng(), shape_rng(), shape_rng(), shape_rng()});
313     auto shape2 = SameShapeDifferentAxis(shape1, i, shape_rng());
314     auto shape3 = SameShapeDifferentAxis(shape1, i, shape_rng());
315     auto shape4 = SameShapeDifferentAxis(shape1, i, shape_rng());
316 
317     // clang-format off
318     ConcatenationTester()
319         .InputShapes({shape1, shape2, shape3, shape4})
320         .Axis(i)
321         .Test(TensorType_INT8, xnnpack_delegate.get());
322     // clang-format on
323   }
324 }
325 
326 }  // namespace xnnpack
327 }  // namespace tflite
328