1 /* Copyright 2020 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 <cstdint>
17 #include <functional>
18 #include <memory>
19 #include <random>
20 
21 #include <gtest/gtest.h>
22 #include "tensorflow/lite/delegates/xnnpack/binary_elementwise_tester.h"
23 #include "tensorflow/lite/delegates/xnnpack/xnnpack_delegate.h"
24 
25 namespace tflite {
26 namespace xnnpack {
27 
28 TEST(Add, 4DBy4D) {
29   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
30       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
31                        TfLiteXNNPackDelegateDelete);
32 
33   std::random_device random_device;
34   auto rng = std::mt19937(random_device());
35   auto shape_rng =
36       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
37   const auto batch = shape_rng();
38   const auto height = shape_rng();
39   const auto width = shape_rng();
40   const auto channels = shape_rng();
41 
42   BinaryElementwiseTester()
43       .Input1Shape({batch, height, width, channels})
44       .Input2Shape({batch, height, width, channels})
45       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
46 }
47 
48 TEST(Add, 4DBy4DBroadcastChannels) {
49   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
50       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
51                        TfLiteXNNPackDelegateDelete);
52 
53   std::random_device random_device;
54   auto rng = std::mt19937(random_device());
55   auto shape_rng =
56       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
57   const auto batch = shape_rng();
58   const auto height = shape_rng();
59   const auto width = shape_rng();
60   const auto channels = shape_rng();
61 
62   BinaryElementwiseTester()
63       .Input1Shape({1, 1, 1, channels})
64       .Input2Shape({batch, height, width, channels})
65       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
66 
67   BinaryElementwiseTester()
68       .Input1Shape({batch, height, width, channels})
69       .Input2Shape({1, 1, 1, channels})
70       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
71 }
72 
73 TEST(Add, 4DBy4DBroadcastWidth) {
74   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
75       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
76                        TfLiteXNNPackDelegateDelete);
77 
78   std::random_device random_device;
79   auto rng = std::mt19937(random_device());
80   auto shape_rng =
81       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
82   const auto batch = shape_rng();
83   const auto height = shape_rng();
84   const auto width = shape_rng();
85   const auto channels = shape_rng();
86 
87   BinaryElementwiseTester()
88       .Input1Shape({1, 1, width, 1})
89       .Input2Shape({batch, height, width, channels})
90       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
91 
92   BinaryElementwiseTester()
93       .Input1Shape({batch, height, width, channels})
94       .Input2Shape({1, 1, width, 1})
95       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
96 }
97 
98 TEST(Add, 4DBy4DBroadcastHeight) {
99   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
100       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
101                        TfLiteXNNPackDelegateDelete);
102 
103   std::random_device random_device;
104   auto rng = std::mt19937(random_device());
105   auto shape_rng =
106       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
107   const auto batch = shape_rng();
108   const auto height = shape_rng();
109   const auto width = shape_rng();
110   const auto channels = shape_rng();
111 
112   BinaryElementwiseTester()
113       .Input1Shape({1, height, 1, 1})
114       .Input2Shape({batch, height, width, channels})
115       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
116 
117   BinaryElementwiseTester()
118       .Input1Shape({batch, height, width, channels})
119       .Input2Shape({1, height, 1, 1})
120       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
121 }
122 
123 TEST(Add, 4DBy4DBroadcastBatch) {
124   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
125       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
126                        TfLiteXNNPackDelegateDelete);
127 
128   std::random_device random_device;
129   auto rng = std::mt19937(random_device());
130   auto shape_rng =
131       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
132   const auto batch = shape_rng();
133   const auto height = shape_rng();
134   const auto width = shape_rng();
135   const auto channels = shape_rng();
136 
137   BinaryElementwiseTester()
138       .Input1Shape({batch, 1, 1, 1})
139       .Input2Shape({batch, height, width, channels})
140       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
141 
142   BinaryElementwiseTester()
143       .Input1Shape({batch, height, width, channels})
144       .Input2Shape({batch, 1, 1, 1})
145       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
146 }
147 
148 TEST(Add, 4DBy4DBroadcastHeightWidthChannels) {
149   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
150       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
151                        TfLiteXNNPackDelegateDelete);
152 
153   std::random_device random_device;
154   auto rng = std::mt19937(random_device());
155   auto shape_rng =
156       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
157   const auto batch = shape_rng();
158   const auto height = shape_rng();
159   const auto width = shape_rng();
160   const auto channels = shape_rng();
161 
162   BinaryElementwiseTester()
163       .Input1Shape({1, height, width, channels})
164       .Input2Shape({batch, height, width, channels})
165       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
166 
167   BinaryElementwiseTester()
168       .Input1Shape({batch, height, width, channels})
169       .Input2Shape({1, height, width, channels})
170       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
171 }
172 
173 TEST(Add, 4DBy3D) {
174   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
175       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
176                        TfLiteXNNPackDelegateDelete);
177 
178   std::random_device random_device;
179   auto rng = std::mt19937(random_device());
180   auto shape_rng =
181       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
182   const auto batch = shape_rng();
183   const auto height = shape_rng();
184   const auto width = shape_rng();
185   const auto channels = shape_rng();
186 
187   BinaryElementwiseTester()
188       .Input1Shape({height, width, channels})
189       .Input2Shape({batch, height, width, channels})
190       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
191 
192   BinaryElementwiseTester()
193       .Input1Shape({batch, height, width, channels})
194       .Input2Shape({height, width, channels})
195       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
196 }
197 
198 TEST(Add, 4DBy2D) {
199   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
200       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
201                        TfLiteXNNPackDelegateDelete);
202 
203   std::random_device random_device;
204   auto rng = std::mt19937(random_device());
205   auto shape_rng =
206       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
207   const auto batch = shape_rng();
208   const auto height = shape_rng();
209   const auto width = shape_rng();
210   const auto channels = shape_rng();
211 
212   BinaryElementwiseTester()
213       .Input1Shape({width, channels})
214       .Input2Shape({batch, height, width, channels})
215       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
216 
217   BinaryElementwiseTester()
218       .Input1Shape({batch, height, width, channels})
219       .Input2Shape({width, channels})
220       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
221 }
222 
223 TEST(Add, 4DBy1D) {
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, 5), std::ref(rng));
232   const auto batch = shape_rng();
233   const auto height = shape_rng();
234   const auto width = shape_rng();
235   const auto channels = shape_rng();
236 
237   BinaryElementwiseTester()
238       .Input1Shape({channels})
239       .Input2Shape({batch, height, width, channels})
240       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
241 
242   BinaryElementwiseTester()
243       .Input1Shape({batch, height, width, channels})
244       .Input2Shape({channels})
245       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
246 }
247 
248 TEST(Add, 4DBy0D) {
249   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
250       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
251                        TfLiteXNNPackDelegateDelete);
252 
253   std::random_device random_device;
254   auto rng = std::mt19937(random_device());
255   auto shape_rng =
256       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
257   const auto batch = shape_rng();
258   const auto height = shape_rng();
259   const auto width = shape_rng();
260   const auto channels = shape_rng();
261 
262   BinaryElementwiseTester()
263       .Input1Shape({})
264       .Input2Shape({batch, height, width, channels})
265       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
266 
267   BinaryElementwiseTester()
268       .Input1Shape({batch, height, width, channels})
269       .Input2Shape({})
270       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
271 }
272 
273 TEST(Add, 2DBy2D) {
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, 5), std::ref(rng));
282   const auto batch = shape_rng();
283   const auto channels = shape_rng();
284 
285   BinaryElementwiseTester()
286       .Input1Shape({batch, channels})
287       .Input2Shape({batch, channels})
288       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
289 }
290 
291 TEST(Add, 2DBy1D) {
292   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
293       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
294                        TfLiteXNNPackDelegateDelete);
295 
296   std::random_device random_device;
297   auto rng = std::mt19937(random_device());
298   auto shape_rng =
299       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
300   const auto batch = shape_rng();
301   const auto channels = shape_rng();
302 
303   BinaryElementwiseTester()
304       .Input1Shape({channels})
305       .Input2Shape({batch, channels})
306       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
307 
308   BinaryElementwiseTester()
309       .Input1Shape({batch, channels})
310       .Input2Shape({channels})
311       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
312 }
313 
314 TEST(Add, 2DBy0D) {
315   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
316       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
317                        TfLiteXNNPackDelegateDelete);
318 
319   std::random_device random_device;
320   auto rng = std::mt19937(random_device());
321   auto shape_rng =
322       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
323   const auto batch = shape_rng();
324   const auto channels = shape_rng();
325 
326   BinaryElementwiseTester()
327       .Input1Shape({})
328       .Input2Shape({batch, channels})
329       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
330 
331   BinaryElementwiseTester()
332       .Input1Shape({batch, channels})
333       .Input2Shape({})
334       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
335 }
336 
337 TEST(Add, 4DByStatic4D) {
338   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
339       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
340                        TfLiteXNNPackDelegateDelete);
341 
342   std::random_device random_device;
343   auto rng = std::mt19937(random_device());
344   auto shape_rng =
345       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
346   const auto batch = shape_rng();
347   const auto height = shape_rng();
348   const auto width = shape_rng();
349   const auto channels = shape_rng();
350 
351   BinaryElementwiseTester()
352       .Input1Shape({batch, height, width, channels})
353       .Input2Shape({batch, height, width, channels})
354       .Input1Static(true)
355       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
356 
357   BinaryElementwiseTester()
358       .Input1Shape({batch, height, width, channels})
359       .Input2Shape({batch, height, width, channels})
360       .Input2Static(true)
361       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
362 }
363 
364 TEST(Add, 4DByStatic4DBroadcastChannels) {
365   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
366       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
367                        TfLiteXNNPackDelegateDelete);
368 
369   std::random_device random_device;
370   auto rng = std::mt19937(random_device());
371   auto shape_rng =
372       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
373   const auto batch = shape_rng();
374   const auto height = shape_rng();
375   const auto width = shape_rng();
376   const auto channels = shape_rng();
377 
378   BinaryElementwiseTester()
379       .Input1Shape({1, 1, 1, channels})
380       .Input2Shape({batch, height, width, channels})
381       .Input1Static(true)
382       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
383 
384   BinaryElementwiseTester()
385       .Input1Shape({batch, height, width, channels})
386       .Input2Shape({1, 1, 1, channels})
387       .Input2Static(true)
388       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
389 }
390 
391 TEST(Add, 4DByStatic4DBroadcastWidth) {
392   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
393       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
394                        TfLiteXNNPackDelegateDelete);
395 
396   std::random_device random_device;
397   auto rng = std::mt19937(random_device());
398   auto shape_rng =
399       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
400   const auto batch = shape_rng();
401   const auto height = shape_rng();
402   const auto width = shape_rng();
403   const auto channels = shape_rng();
404 
405   BinaryElementwiseTester()
406       .Input1Shape({1, 1, width, 1})
407       .Input2Shape({batch, height, width, channels})
408       .Input1Static(true)
409       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
410 
411   BinaryElementwiseTester()
412       .Input1Shape({batch, height, width, channels})
413       .Input2Shape({1, 1, width, 1})
414       .Input2Static(true)
415       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
416 }
417 
418 TEST(Add, 4DByStatic4DBroadcastHeight) {
419   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
420       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
421                        TfLiteXNNPackDelegateDelete);
422 
423   std::random_device random_device;
424   auto rng = std::mt19937(random_device());
425   auto shape_rng =
426       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
427   const auto batch = shape_rng();
428   const auto height = shape_rng();
429   const auto width = shape_rng();
430   const auto channels = shape_rng();
431 
432   BinaryElementwiseTester()
433       .Input1Shape({1, height, 1, 1})
434       .Input2Shape({batch, height, width, channels})
435       .Input1Static(true)
436       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
437 
438   BinaryElementwiseTester()
439       .Input1Shape({batch, height, width, channels})
440       .Input2Shape({1, height, 1, 1})
441       .Input2Static(true)
442       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
443 }
444 
445 TEST(Add, 4DByStatic4DBroadcastBatch) {
446   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
447       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
448                        TfLiteXNNPackDelegateDelete);
449 
450   std::random_device random_device;
451   auto rng = std::mt19937(random_device());
452   auto shape_rng =
453       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
454   const auto batch = shape_rng();
455   const auto height = shape_rng();
456   const auto width = shape_rng();
457   const auto channels = shape_rng();
458 
459   BinaryElementwiseTester()
460       .Input1Shape({batch, 1, 1, 1})
461       .Input2Shape({batch, height, width, channels})
462       .Input1Static(true)
463       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
464 
465   BinaryElementwiseTester()
466       .Input1Shape({batch, height, width, channels})
467       .Input2Shape({batch, 1, 1, 1})
468       .Input2Static(true)
469       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
470 }
471 
472 TEST(Add, 4DByStatic4DBroadcastHeightWidthChannels) {
473   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
474       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
475                        TfLiteXNNPackDelegateDelete);
476 
477   std::random_device random_device;
478   auto rng = std::mt19937(random_device());
479   auto shape_rng =
480       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
481   const auto batch = shape_rng();
482   const auto height = shape_rng();
483   const auto width = shape_rng();
484   const auto channels = shape_rng();
485 
486   BinaryElementwiseTester()
487       .Input1Shape({1, height, width, channels})
488       .Input2Shape({batch, height, width, channels})
489       .Input1Static(true)
490       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
491 
492   BinaryElementwiseTester()
493       .Input1Shape({batch, height, width, channels})
494       .Input2Shape({1, height, width, channels})
495       .Input2Static(true)
496       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
497 }
498 
499 TEST(Add, 4DByStatic3D) {
500   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
501       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
502                        TfLiteXNNPackDelegateDelete);
503 
504   std::random_device random_device;
505   auto rng = std::mt19937(random_device());
506   auto shape_rng =
507       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
508   const auto batch = shape_rng();
509   const auto height = shape_rng();
510   const auto width = shape_rng();
511   const auto channels = shape_rng();
512 
513   BinaryElementwiseTester()
514       .Input1Shape({height, width, channels})
515       .Input2Shape({batch, height, width, channels})
516       .Input1Static(true)
517       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
518 
519   BinaryElementwiseTester()
520       .Input1Shape({batch, height, width, channels})
521       .Input2Shape({height, width, channels})
522       .Input2Static(true)
523       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
524 }
525 
526 TEST(Add, 4DByStatic2D) {
527   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
528       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
529                        TfLiteXNNPackDelegateDelete);
530 
531   std::random_device random_device;
532   auto rng = std::mt19937(random_device());
533   auto shape_rng =
534       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
535   const auto batch = shape_rng();
536   const auto height = shape_rng();
537   const auto width = shape_rng();
538   const auto channels = shape_rng();
539 
540   BinaryElementwiseTester()
541       .Input1Shape({width, channels})
542       .Input2Shape({batch, height, width, channels})
543       .Input1Static(true)
544       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
545 
546   BinaryElementwiseTester()
547       .Input1Shape({batch, height, width, channels})
548       .Input2Shape({width, channels})
549       .Input2Static(true)
550       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
551 }
552 
553 TEST(Add, 4DByStatic1D) {
554   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
555       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
556                        TfLiteXNNPackDelegateDelete);
557 
558   std::random_device random_device;
559   auto rng = std::mt19937(random_device());
560   auto shape_rng =
561       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
562   const auto batch = shape_rng();
563   const auto height = shape_rng();
564   const auto width = shape_rng();
565   const auto channels = shape_rng();
566 
567   BinaryElementwiseTester()
568       .Input1Shape({channels})
569       .Input2Shape({batch, height, width, channels})
570       .Input1Static(true)
571       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
572 
573   BinaryElementwiseTester()
574       .Input1Shape({batch, height, width, channels})
575       .Input2Shape({channels})
576       .Input2Static(true)
577       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
578 }
579 
580 TEST(Add, 4DByStatic0D) {
581   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
582       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
583                        TfLiteXNNPackDelegateDelete);
584 
585   std::random_device random_device;
586   auto rng = std::mt19937(random_device());
587   auto shape_rng =
588       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
589   const auto batch = shape_rng();
590   const auto height = shape_rng();
591   const auto width = shape_rng();
592   const auto channels = shape_rng();
593 
594   BinaryElementwiseTester()
595       .Input1Shape({})
596       .Input2Shape({batch, height, width, channels})
597       .Input1Static(true)
598       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
599 
600   BinaryElementwiseTester()
601       .Input1Shape({batch, height, width, channels})
602       .Input2Shape({})
603       .Input2Static(true)
604       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
605 }
606 
607 TEST(Add, 2DByStatic2D) {
608   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
609       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
610                        TfLiteXNNPackDelegateDelete);
611 
612   std::random_device random_device;
613   auto rng = std::mt19937(random_device());
614   auto shape_rng =
615       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
616   const auto batch = shape_rng();
617   const auto channels = shape_rng();
618 
619   BinaryElementwiseTester()
620       .Input1Shape({batch, channels})
621       .Input2Shape({batch, channels})
622       .Input1Static(true)
623       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
624 
625   BinaryElementwiseTester()
626       .Input1Shape({batch, channels})
627       .Input2Shape({batch, channels})
628       .Input2Static(true)
629       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
630 }
631 
632 TEST(Add, 2DByStatic1D) {
633   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
634       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
635                        TfLiteXNNPackDelegateDelete);
636 
637   std::random_device random_device;
638   auto rng = std::mt19937(random_device());
639   auto shape_rng =
640       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
641   const auto batch = shape_rng();
642   const auto channels = shape_rng();
643 
644   BinaryElementwiseTester()
645       .Input1Shape({channels})
646       .Input2Shape({batch, channels})
647       .Input1Static(true)
648       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
649 
650   BinaryElementwiseTester()
651       .Input1Shape({batch, channels})
652       .Input2Shape({channels})
653       .Input2Static(true)
654       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
655 }
656 
657 TEST(Add, 2DByStatic0D) {
658   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
659       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
660                        TfLiteXNNPackDelegateDelete);
661 
662   std::random_device random_device;
663   auto rng = std::mt19937(random_device());
664   auto shape_rng =
665       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
666   const auto batch = shape_rng();
667   const auto channels = shape_rng();
668 
669   BinaryElementwiseTester()
670       .Input1Shape({})
671       .Input2Shape({batch, channels})
672       .Input1Static(true)
673       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
674 
675   BinaryElementwiseTester()
676       .Input1Shape({batch, channels})
677       .Input2Shape({})
678       .Input2Static(true)
679       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
680 }
681 
TEST(Add,FP16Weights)682 TEST(Add, FP16Weights) {
683   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
684       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
685                        TfLiteXNNPackDelegateDelete);
686 
687   std::random_device random_device;
688   auto rng = std::mt19937(random_device());
689   auto shape_rng =
690       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
691   const auto batch = shape_rng();
692   const auto height = shape_rng();
693   const auto width = shape_rng();
694   const auto channels = shape_rng();
695 
696   BinaryElementwiseTester()
697       .Input1Shape({batch, height, width, channels})
698       .Input2Shape({batch, height, width, channels})
699       .Input1Static(true)
700       .FP16Weights()
701       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
702 
703   BinaryElementwiseTester()
704       .Input1Shape({batch, height, width, channels})
705       .Input2Shape({batch, height, width, channels})
706       .Input2Static(true)
707       .FP16Weights()
708       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
709 }
710 
TEST(Add,SparseWeights)711 TEST(Add, SparseWeights) {
712   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
713       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
714                        TfLiteXNNPackDelegateDelete);
715 
716   std::random_device random_device;
717   auto rng = std::mt19937(random_device());
718   auto shape_rng =
719       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
720   const auto batch = shape_rng();
721   const auto height = shape_rng();
722   const auto width = shape_rng();
723   const auto channels = shape_rng();
724 
725   BinaryElementwiseTester()
726       .Input1Shape({batch, height, width, channels})
727       .Input2Shape({batch, height, width, channels})
728       .Input1Static(true)
729       .SparseWeights()
730       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
731 
732   BinaryElementwiseTester()
733       .Input1Shape({batch, height, width, channels})
734       .Input2Shape({batch, height, width, channels})
735       .Input2Static(true)
736       .SparseWeights()
737       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
738 }
739 
TEST(Add,ReluActivation)740 TEST(Add, ReluActivation) {
741   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
742       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
743                        TfLiteXNNPackDelegateDelete);
744 
745   std::random_device random_device;
746   auto rng = std::mt19937(random_device());
747   auto shape_rng =
748       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
749   const auto batch = shape_rng();
750   const auto height = shape_rng();
751   const auto width = shape_rng();
752   const auto channels = shape_rng();
753 
754   BinaryElementwiseTester()
755       .Input1Shape({batch, height, width, channels})
756       .Input2Shape({batch, height, width, channels})
757       .ReluActivation()
758       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
759 }
760 
TEST(Add,Relu6Activation)761 TEST(Add, Relu6Activation) {
762   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
763       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
764                        TfLiteXNNPackDelegateDelete);
765 
766   std::random_device random_device;
767   auto rng = std::mt19937(random_device());
768   auto shape_rng =
769       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
770   const auto batch = shape_rng();
771   const auto height = shape_rng();
772   const auto width = shape_rng();
773   const auto channels = shape_rng();
774 
775   BinaryElementwiseTester()
776       .Input1Shape({batch, height, width, channels})
777       .Input2Shape({batch, height, width, channels})
778       .Relu6Activation()
779       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
780 }
781 
TEST(Add,ReluMinus1To1Activation)782 TEST(Add, ReluMinus1To1Activation) {
783   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
784       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
785                        TfLiteXNNPackDelegateDelete);
786 
787   std::random_device random_device;
788   auto rng = std::mt19937(random_device());
789   auto shape_rng =
790       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
791   const auto batch = shape_rng();
792   const auto height = shape_rng();
793   const auto width = shape_rng();
794   const auto channels = shape_rng();
795 
796   BinaryElementwiseTester()
797       .Input1Shape({batch, height, width, channels})
798       .Input2Shape({batch, height, width, channels})
799       .ReluMinus1To1Activation()
800       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
801 }
802 
TEST(Add,DISABLED_TanhActivation)803 TEST(Add, DISABLED_TanhActivation) {
804   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
805       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
806                        TfLiteXNNPackDelegateDelete);
807 
808   std::random_device random_device;
809   auto rng = std::mt19937(random_device());
810   auto shape_rng =
811       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
812   const auto batch = shape_rng();
813   const auto height = shape_rng();
814   const auto width = shape_rng();
815   const auto channels = shape_rng();
816 
817   BinaryElementwiseTester()
818       .Input1Shape({batch, height, width, channels})
819       .Input2Shape({batch, height, width, channels})
820       .TanhActivation()
821       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
822 }
823 
TEST(Add,DISABLED_SignBitActivation)824 TEST(Add, DISABLED_SignBitActivation) {
825   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
826       xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
827                        TfLiteXNNPackDelegateDelete);
828 
829   std::random_device random_device;
830   auto rng = std::mt19937(random_device());
831   auto shape_rng =
832       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
833   const auto batch = shape_rng();
834   const auto height = shape_rng();
835   const auto width = shape_rng();
836   const auto channels = shape_rng();
837 
838   BinaryElementwiseTester()
839       .Input1Shape({batch, height, width, channels})
840       .Input2Shape({batch, height, width, channels})
841       .SignBitActivation()
842       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
843 }
844 
TEST(Add,MultiThreading)845 TEST(Add, MultiThreading) {
846   TfLiteXNNPackDelegateOptions delegate_options =
847       TfLiteXNNPackDelegateOptionsDefault();
848   delegate_options.num_threads = 2;
849   std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
850       xnnpack_delegate(TfLiteXNNPackDelegateCreate(&delegate_options),
851                        TfLiteXNNPackDelegateDelete);
852 
853   std::random_device random_device;
854   auto rng = std::mt19937(random_device());
855   auto shape_rng =
856       std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
857   const auto batch = shape_rng();
858   const auto height = shape_rng();
859   const auto width = shape_rng();
860   const auto channels = shape_rng();
861 
862   BinaryElementwiseTester()
863       .Input1Shape({batch, height, width, channels})
864       .Input2Shape({batch, height, width, channels})
865       .Test(BuiltinOperator_ADD, xnnpack_delegate.get());
866 }
867 
868 }  // namespace xnnpack
869 }  // namespace tflite
870