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