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 <cstdint>
17 #include <functional>
18 #include <memory>
19 #include <random>
20
21 #include <gtest/gtest.h>
22 #include "tensorflow/lite/delegates/xnnpack/quantized_leaky_relu_tester.h"
23 #include "tensorflow/lite/delegates/xnnpack/xnnpack_delegate.h"
24
25 namespace tflite {
26 namespace xnnpack {
27
28 TEST(UnsignedQuantizedLeakyRelu, 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 QuantizedLeakyReluTester()
43 .Shape({batch, height, width, channels})
44 .InputZeroPoint(11)
45 .InputScale(1.75f)
46 .OutputZeroPoint(-7)
47 .OutputScale(2.25f)
48 .Test(xnnpack_delegate.get());
49 }
50
51 TEST(UnsignedQuantizedLeakyRelu, 3D) {
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, 5), std::ref(rng));
60 const auto batch = shape_rng();
61 const auto width = shape_rng();
62 const auto channels = shape_rng();
63
64 QuantizedLeakyReluTester()
65 .Shape({batch, width, channels})
66 .InputZeroPoint(11)
67 .InputScale(1.75f)
68 .OutputZeroPoint(-7)
69 .OutputScale(2.25f)
70 .Test(xnnpack_delegate.get());
71 }
72
73 TEST(UnsignedQuantizedLeakyRelu, 2D) {
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 channels = shape_rng();
84
85 QuantizedLeakyReluTester()
86 .Shape({batch, channels})
87 .InputZeroPoint(11)
88 .InputScale(1.75f)
89 .OutputZeroPoint(-7)
90 .OutputScale(2.25f)
91 .Test(xnnpack_delegate.get());
92 }
93
94 TEST(UnsignedQuantizedLeakyRelu, 1D) {
95 std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
96 xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
97 TfLiteXNNPackDelegateDelete);
98
99 std::random_device random_device;
100 auto rng = std::mt19937(random_device());
101 auto shape_rng =
102 std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
103 const auto batch = shape_rng();
104
105 QuantizedLeakyReluTester()
106 .Shape({batch})
107 .InputZeroPoint(11)
108 .InputScale(1.75f)
109 .OutputZeroPoint(-7)
110 .OutputScale(2.25f)
111 .Test(xnnpack_delegate.get());
112 }
113
TEST(UnsignedQuantizedLeakyRelu,NegativeSlope)114 TEST(UnsignedQuantizedLeakyRelu, NegativeSlope) {
115 std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
116 xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
117 TfLiteXNNPackDelegateDelete);
118
119 std::random_device random_device;
120 auto rng = std::mt19937(random_device());
121 auto shape_rng =
122 std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
123 const auto batch = shape_rng();
124 const auto height = shape_rng();
125 const auto width = shape_rng();
126 const auto channels = shape_rng();
127
128 QuantizedLeakyReluTester()
129 .Shape({batch, height, width, channels})
130 .InputZeroPoint(11)
131 .InputScale(1.75f)
132 .OutputZeroPoint(-7)
133 .OutputScale(2.25f)
134 .NegativeSlope(-0.75f)
135 .Test(xnnpack_delegate.get());
136 }
137
TEST(UnsignedQuantizedLeakyRelu,MultiThreading)138 TEST(UnsignedQuantizedLeakyRelu, MultiThreading) {
139 TfLiteXNNPackDelegateOptions delegate_options =
140 TfLiteXNNPackDelegateOptionsDefault();
141 delegate_options.num_threads = 2;
142 std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
143 xnnpack_delegate(TfLiteXNNPackDelegateCreate(&delegate_options),
144 TfLiteXNNPackDelegateDelete);
145
146 std::random_device random_device;
147 auto rng = std::mt19937(random_device());
148 auto shape_rng =
149 std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
150 const auto batch = shape_rng();
151 const auto height = shape_rng();
152 const auto width = shape_rng();
153 const auto channels = shape_rng();
154
155 QuantizedLeakyReluTester()
156 .Shape({batch, height, width, channels})
157 .InputZeroPoint(11)
158 .InputScale(1.75f)
159 .OutputZeroPoint(-7)
160 .OutputScale(2.25f)
161 .Test(xnnpack_delegate.get());
162 }
163
164 } // namespace xnnpack
165 } // namespace tflite
166