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/unary_elementwise_tester.h"
23 #include "tensorflow/lite/delegates/xnnpack/xnnpack_delegate.h"
24
25 namespace tflite {
26 namespace xnnpack {
27
28 TEST(Neg, 4D) {
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 UnaryElementwiseTester()
43 .Shape({batch, height, width, channels})
44 .Test(BuiltinOperator_NEG, xnnpack_delegate.get());
45 }
46
47 TEST(Neg, 3D) {
48 std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
49 xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
50 TfLiteXNNPackDelegateDelete);
51
52 std::random_device random_device;
53 auto rng = std::mt19937(random_device());
54 auto shape_rng =
55 std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
56 const auto batch = shape_rng();
57 const auto width = shape_rng();
58 const auto channels = shape_rng();
59
60 UnaryElementwiseTester()
61 .Shape({batch, width, channels})
62 .Test(BuiltinOperator_NEG, xnnpack_delegate.get());
63 }
64
65 TEST(Neg, 2D) {
66 std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
67 xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
68 TfLiteXNNPackDelegateDelete);
69
70 std::random_device random_device;
71 auto rng = std::mt19937(random_device());
72 auto shape_rng =
73 std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
74 const auto batch = shape_rng();
75 const auto channels = shape_rng();
76
77 UnaryElementwiseTester()
78 .Shape({batch, channels})
79 .Test(BuiltinOperator_NEG, xnnpack_delegate.get());
80 }
81
82 TEST(Neg, 1D) {
83 std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
84 xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
85 TfLiteXNNPackDelegateDelete);
86
87 std::random_device random_device;
88 auto rng = std::mt19937(random_device());
89 auto shape_rng =
90 std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
91 const auto batch = shape_rng();
92
93 UnaryElementwiseTester().Shape({batch}).Test(BuiltinOperator_NEG,
94 xnnpack_delegate.get());
95 }
96
TEST(Neg,MultiThreading)97 TEST(Neg, MultiThreading) {
98 TfLiteXNNPackDelegateOptions delegate_options =
99 TfLiteXNNPackDelegateOptionsDefault();
100 delegate_options.num_threads = 2;
101 std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
102 xnnpack_delegate(TfLiteXNNPackDelegateCreate(&delegate_options),
103 TfLiteXNNPackDelegateDelete);
104
105 std::random_device random_device;
106 auto rng = std::mt19937(random_device());
107 auto shape_rng =
108 std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
109 const auto batch = shape_rng();
110 const auto height = shape_rng();
111 const auto width = shape_rng();
112 const auto channels = shape_rng();
113
114 UnaryElementwiseTester()
115 .Shape({batch, height, width, channels})
116 .Test(BuiltinOperator_NEG, xnnpack_delegate.get());
117 }
118
119 } // namespace xnnpack
120 } // namespace tflite
121