blob: d4a3280c549c71d4a5a0fe583f3a10bbb1565e72 [file] [log] [blame]
XNNPACK Teamb455b122019-09-27 18:10:33 -07001// Copyright (c) Facebook, Inc. and its affiliates.
2// All rights reserved.
3//
4// Copyright 2019 Google LLC
5//
6// This source code is licensed under the BSD-style license found in the
7// LICENSE file in the root directory of this source tree.
8
9#pragma once
10
11#include <gtest/gtest.h>
12
13#include <algorithm>
14#include <cassert>
15#include <cmath>
16#include <cstddef>
17#include <cstdlib>
18#include <functional>
Marat Dukhan5ce30d92020-04-14 03:31:26 -070019#include <limits>
XNNPACK Teamb455b122019-09-27 18:10:33 -070020#include <random>
21#include <vector>
22
23#include <xnnpack.h>
24
25
26class LeakyReLUOperatorTester {
27 public:
28 inline LeakyReLUOperatorTester& channels(size_t channels) {
29 assert(channels != 0);
30 this->channels_ = channels;
31 return *this;
32 }
33
34 inline size_t channels() const {
35 return this->channels_;
36 }
37
38 inline LeakyReLUOperatorTester& input_stride(size_t input_stride) {
39 assert(input_stride != 0);
40 this->input_stride_ = input_stride;
41 return *this;
42 }
43
44 inline size_t input_stride() const {
45 if (this->input_stride_ == 0) {
46 return this->channels_;
47 } else {
48 assert(this->input_stride_ >= this->channels_);
49 return this->input_stride_;
50 }
51 }
52
53 inline LeakyReLUOperatorTester& output_stride(size_t output_stride) {
54 assert(output_stride != 0);
55 this->output_stride_ = output_stride;
56 return *this;
57 }
58
59 inline size_t output_stride() const {
60 if (this->output_stride_ == 0) {
61 return this->channels_;
62 } else {
63 assert(this->output_stride_ >= this->channels_);
64 return this->output_stride_;
65 }
66 }
67
68 inline LeakyReLUOperatorTester& batch_size(size_t batch_size) {
69 assert(batch_size != 0);
70 this->batch_size_ = batch_size;
71 return *this;
72 }
73
74 inline size_t batch_size() const {
75 return this->batch_size_;
76 }
77
78 inline LeakyReLUOperatorTester& negative_slope(float negative_slope) {
79 assert(negative_slope > 0.0f);
80 assert(negative_slope < 1.0f);
81 this->negative_slope_ = negative_slope;
82 return *this;
83 }
84
85 inline float negative_slope() const {
86 return this->negative_slope_;
87 }
88
89 inline LeakyReLUOperatorTester& input_scale(float input_scale) {
90 assert(input_scale > 0.0f);
91 assert(std::isnormal(input_scale));
92 this->input_scale_ = input_scale;
93 return *this;
94 }
95
96 inline float input_scale() const {
97 return this->input_scale_;
98 }
99
100 inline LeakyReLUOperatorTester& input_zero_point(uint8_t input_zero_point) {
101 this->input_zero_point_ = input_zero_point;
102 return *this;
103 }
104
105 inline uint8_t input_zero_point() const {
106 return this->input_zero_point_;
107 }
108
109 inline LeakyReLUOperatorTester& output_scale(float output_scale) {
110 assert(output_scale > 0.0f);
111 assert(std::isnormal(output_scale));
112 this->output_scale_ = output_scale;
113 return *this;
114 }
115
116 inline float output_scale() const {
117 return this->output_scale_;
118 }
119
120 inline LeakyReLUOperatorTester& output_zero_point(uint8_t output_zero_point) {
121 this->output_zero_point_ = output_zero_point;
122 return *this;
123 }
124
125 inline uint8_t output_zero_point() const {
126 return this->output_zero_point_;
127 }
128
129 inline LeakyReLUOperatorTester& qmin(uint8_t qmin) {
130 this->qmin_ = qmin;
131 return *this;
132 }
133
134 inline uint8_t qmin() const {
135 return this->qmin_;
136 }
137
138 inline LeakyReLUOperatorTester& qmax(uint8_t qmax) {
139 this->qmax_ = qmax;
140 return *this;
141 }
142
143 inline uint8_t qmax() const {
144 return this->qmax_;
145 }
146
147 inline LeakyReLUOperatorTester& iterations(size_t iterations) {
148 this->iterations_ = iterations;
149 return *this;
150 }
151
152 inline size_t iterations() const {
153 return this->iterations_;
154 }
155
Marat Dukhan28813332020-06-10 18:05:38 -0700156 void TestF32() const {
157 std::random_device random_device;
158 auto rng = std::mt19937(random_device());
159 auto f32rng = std::bind(std::uniform_real_distribution<float>(-1.0f, 1.0f), std::ref(rng));
160
161 std::vector<float> input(XNN_EXTRA_BYTES / sizeof(float) + (batch_size() - 1) * input_stride() + channels());
162 std::vector<float> output((batch_size() - 1) * output_stride() + channels());
163 std::vector<float> output_ref(batch_size() * channels());
164 for (size_t iteration = 0; iteration < iterations(); iteration++) {
165 std::generate(input.begin(), input.end(), std::ref(f32rng));
166 std::fill(output.begin(), output.end(), std::nanf(""));
167
168 // Compute reference results.
169 for (size_t i = 0; i < batch_size(); i++) {
170 for (size_t c = 0; c < channels(); c++) {
171 const float x = input[i * input_stride() + c];
172 const float y = std::signbit(x) ? x * negative_slope() : x;
173 output_ref[i * channels() + c] = y;
174 }
175 }
176
177 // Create, setup, run, and destroy Leaky ReLU operator.
178 ASSERT_EQ(xnn_status_success, xnn_initialize(nullptr /* allocator */));
179 xnn_operator_t leaky_relu_op = nullptr;
180
181 ASSERT_EQ(xnn_status_success,
182 xnn_create_leaky_relu_nc_f32(
183 channels(), input_stride(), output_stride(),
184 negative_slope(),
185 0, &leaky_relu_op));
186 ASSERT_NE(nullptr, leaky_relu_op);
187
188 // Smart pointer to automatically delete leaky_relu_op.
189 std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_leaky_relu_op(leaky_relu_op, xnn_delete_operator);
190
191 ASSERT_EQ(xnn_status_success,
192 xnn_setup_leaky_relu_nc_f32(
193 leaky_relu_op,
194 batch_size(),
195 input.data(), output.data(),
196 nullptr /* thread pool */));
197
198 ASSERT_EQ(xnn_status_success,
199 xnn_run_operator(leaky_relu_op, nullptr /* thread pool */));
200
201 // Verify results.
202 for (size_t i = 0; i < batch_size(); i++) {
203 for (size_t c = 0; c < channels(); c++) {
204 ASSERT_EQ(output[i * output_stride() + c], output_ref[i * channels() + c])
205 << "at batch " << i << " / " << batch_size() << ", channel " << c << " / " << channels()
206 << ", input " << input[i * input_stride() + c] << ", negative slope " << negative_slope();
207 }
208 }
209 }
210 }
211
Marat Dukhan08b7a972020-07-14 18:17:29 -0700212 void TestQU8() const {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700213 std::random_device random_device;
214 auto rng = std::mt19937(random_device());
Marat Dukhan5ce30d92020-04-14 03:31:26 -0700215 auto u8rng = std::bind(std::uniform_int_distribution<uint32_t>(0, std::numeric_limits<uint8_t>::max()), rng);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700216
217 std::vector<uint8_t> input((batch_size() - 1) * input_stride() + channels());
218 std::vector<uint8_t> output((batch_size() - 1) * output_stride() + channels());
219 std::vector<float> output_ref(batch_size() * channels());
220 for (size_t iteration = 0; iteration < iterations(); iteration++) {
221 std::generate(input.begin(), input.end(), std::ref(u8rng));
222 std::fill(output.begin(), output.end(), 0xA5);
223
224 // Compute reference results.
225 for (size_t i = 0; i < batch_size(); i++) {
226 for (size_t c = 0; c < channels(); c++) {
227 const float x = input_scale() * (int32_t(input[i * input_stride() + c]) - int32_t(input_zero_point()));
228 float y = (x < 0.0f ? x * negative_slope() : x) / output_scale();
229 y = std::min<float>(y, int32_t(qmax()) - int32_t(output_zero_point()));
230 y = std::max<float>(y, int32_t(qmin()) - int32_t(output_zero_point()));
231 output_ref[i * channels() + c] = y + float(int32_t(output_zero_point()));
232 }
233 }
234
Marat Dukhan28813332020-06-10 18:05:38 -0700235 // Create, setup, run, and destroy Leaky ReLU operator.
Marat Dukhan04f03be2019-11-19 12:36:47 -0800236 ASSERT_EQ(xnn_status_success, xnn_initialize(nullptr /* allocator */));
XNNPACK Teamb455b122019-09-27 18:10:33 -0700237 xnn_operator_t leaky_relu_op = nullptr;
238
239 ASSERT_EQ(xnn_status_success,
Marat Dukhan08b7a972020-07-14 18:17:29 -0700240 xnn_create_leaky_relu_nc_qu8(
XNNPACK Teamb455b122019-09-27 18:10:33 -0700241 channels(), input_stride(), output_stride(),
242 negative_slope(),
243 input_zero_point(), input_scale(),
244 output_zero_point(), output_scale(),
245 qmin(), qmax(),
246 0, &leaky_relu_op));
247 ASSERT_NE(nullptr, leaky_relu_op);
248
249 // Smart pointer to automatically delete leaky_relu_op.
250 std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_leaky_relu_op(leaky_relu_op, xnn_delete_operator);
251
252 ASSERT_EQ(xnn_status_success,
Marat Dukhan08b7a972020-07-14 18:17:29 -0700253 xnn_setup_leaky_relu_nc_qu8(
XNNPACK Teamb455b122019-09-27 18:10:33 -0700254 leaky_relu_op,
255 batch_size(),
256 input.data(), output.data(),
257 nullptr /* thread pool */));
258
259 ASSERT_EQ(xnn_status_success,
260 xnn_run_operator(leaky_relu_op, nullptr /* thread pool */));
261
262 // Verify results.
263 for (size_t i = 0; i < batch_size(); i++) {
264 for (size_t c = 0; c < channels(); c++) {
265 ASSERT_NEAR(float(int32_t(output[i * output_stride() + c])), output_ref[i * channels() + c], 0.6f);
266 }
267 }
268 }
269 }
270
271 private:
272 size_t batch_size_{1};
273 size_t channels_{1};
274 size_t input_stride_{0};
275 size_t output_stride_{0};
276 float negative_slope_{0.5f};
277 float output_scale_{0.75f};
278 uint8_t output_zero_point_{133};
279 float input_scale_{1.25f};
280 uint8_t input_zero_point_{121};
281 uint8_t qmin_{0};
282 uint8_t qmax_{255};
283 size_t iterations_{15};
284};