xref: /aosp_15_r20/external/XNNPACK/test/vadd-microkernel-tester.h (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // 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 <cstddef>
16 #include <cstdlib>
17 #include <functional>
18 #include <limits>
19 #include <random>
20 #include <vector>
21 
22 #include <xnnpack.h>
23 #include <xnnpack/microfnptr.h>
24 #include <xnnpack/microparams-init.h>
25 #include <xnnpack/requantization.h>
26 
27 
28 class VAddMicrokernelTester {
29  public:
batch_size(size_t batch_size)30   inline VAddMicrokernelTester& batch_size(size_t batch_size) {
31     assert(batch_size != 0);
32     this->batch_size_ = batch_size;
33     return *this;
34   }
35 
batch_size()36   inline size_t batch_size() const {
37     return this->batch_size_;
38   }
39 
inplace_a(bool inplace_a)40   inline VAddMicrokernelTester& inplace_a(bool inplace_a) {
41     this->inplace_a_ = inplace_a;
42     return *this;
43   }
44 
inplace_a()45   inline bool inplace_a() const {
46     return this->inplace_a_;
47   }
48 
inplace_b(bool inplace_b)49   inline VAddMicrokernelTester& inplace_b(bool inplace_b) {
50     this->inplace_b_ = inplace_b;
51     return *this;
52   }
53 
inplace_b()54   inline bool inplace_b() const {
55     return this->inplace_b_;
56   }
57 
a_scale(float a_scale)58   inline VAddMicrokernelTester& a_scale(float a_scale) {
59     assert(a_scale > 0.0f);
60     assert(std::isnormal(a_scale));
61     this->a_scale_ = a_scale;
62     return *this;
63   }
64 
a_scale()65   inline float a_scale() const {
66     return this->a_scale_;
67   }
68 
a_zero_point(uint8_t a_zero_point)69   inline VAddMicrokernelTester& a_zero_point(uint8_t a_zero_point) {
70     this->a_zero_point_ = a_zero_point;
71     return *this;
72   }
73 
a_zero_point()74   inline uint8_t a_zero_point() const {
75     return this->a_zero_point_;
76   }
77 
b_scale(float b_scale)78   inline VAddMicrokernelTester& b_scale(float b_scale) {
79     assert(b_scale > 0.0f);
80     assert(std::isnormal(b_scale));
81     this->b_scale_ = b_scale;
82     return *this;
83   }
84 
b_scale()85   inline float b_scale() const {
86     return this->b_scale_;
87   }
88 
b_zero_point(uint8_t b_zero_point)89   inline VAddMicrokernelTester& b_zero_point(uint8_t b_zero_point) {
90     this->b_zero_point_ = b_zero_point;
91     return *this;
92   }
93 
b_zero_point()94   inline uint8_t b_zero_point() const {
95     return this->b_zero_point_;
96   }
97 
y_scale(float y_scale)98   inline VAddMicrokernelTester& y_scale(float y_scale) {
99     assert(y_scale > 0.0f);
100     assert(std::isnormal(y_scale));
101     this->y_scale_ = y_scale;
102     return *this;
103   }
104 
y_scale()105   inline float y_scale() const {
106     return this->y_scale_;
107   }
108 
y_zero_point(uint8_t y_zero_point)109   inline VAddMicrokernelTester& y_zero_point(uint8_t y_zero_point) {
110     this->y_zero_point_ = y_zero_point;
111     return *this;
112   }
113 
y_zero_point()114   inline uint8_t y_zero_point() const {
115     return this->y_zero_point_;
116   }
117 
qmin(uint8_t qmin)118   inline VAddMicrokernelTester& qmin(uint8_t qmin) {
119     this->qmin_ = qmin;
120     return *this;
121   }
122 
qmin()123   inline uint8_t qmin() const {
124     return this->qmin_;
125   }
126 
qmax(uint8_t qmax)127   inline VAddMicrokernelTester& qmax(uint8_t qmax) {
128     this->qmax_ = qmax;
129     return *this;
130   }
131 
qmax()132   inline uint8_t qmax() const {
133     return this->qmax_;
134   }
135 
iterations(size_t iterations)136   inline VAddMicrokernelTester& iterations(size_t iterations) {
137     this->iterations_ = iterations;
138     return *this;
139   }
140 
iterations()141   inline size_t iterations() const {
142     return this->iterations_;
143   }
144 
Test(xnn_qu8_vadd_minmax_ukernel_function vadd_minmax,xnn_init_qu8_add_minmax_params_fn init_params)145   void Test(xnn_qu8_vadd_minmax_ukernel_function vadd_minmax, xnn_init_qu8_add_minmax_params_fn init_params) const {
146     std::random_device random_device;
147     auto rng = std::mt19937(random_device());
148     auto u8rng = std::bind(std::uniform_int_distribution<uint32_t>(0, std::numeric_limits<uint8_t>::max()), rng);
149 
150     std::vector<uint8_t> a(batch_size() + XNN_EXTRA_BYTES / sizeof(uint8_t));
151     std::vector<uint8_t> b(batch_size() + XNN_EXTRA_BYTES / sizeof(uint8_t));
152     std::vector<uint8_t> y(batch_size() + (inplace_a() || inplace_b() ? XNN_EXTRA_BYTES / sizeof(uint8_t) : 0));
153     std::vector<float> y_fp(batch_size());
154     std::vector<uint8_t> y_ref(batch_size());
155     for (size_t iteration = 0; iteration < iterations(); iteration++) {
156       std::generate(a.begin(), a.end(), std::ref(u8rng));
157       std::generate(b.begin(), b.end(), std::ref(u8rng));
158       if (inplace_a() || inplace_b()) {
159         std::generate(y.begin(), y.end(), std::ref(u8rng));
160       } else {
161         std::fill(y.begin(), y.end(), 0xA5);
162       }
163       const uint8_t* a_data = inplace_a() ? y.data() : a.data();
164       const uint8_t* b_data = inplace_b() ? y.data() : b.data();
165 
166       // Prepare parameters.
167       xnn_qu8_add_minmax_params quantization_params;
168       init_params(
169         &quantization_params,
170         a_zero_point(), b_zero_point(), y_zero_point(),
171         a_scale() / y_scale(), b_scale() / y_scale(),
172         qmin(), qmax());
173       xnn_qu8_add_minmax_params scalar_quantization_params;
174       xnn_init_qu8_add_minmax_scalar_params(
175         &scalar_quantization_params,
176         a_zero_point(), b_zero_point(), y_zero_point(),
177         a_scale() / y_scale(), b_scale() / y_scale(),
178         qmin(), qmax());
179 
180       // Compute reference results.
181       for (size_t i = 0; i < batch_size(); i++) {
182         y_fp[i] = float(y_zero_point()) +
183           float(int32_t(a_data[i]) - int32_t(a_zero_point())) * (a_scale() / y_scale()) +
184           float(int32_t(b_data[i]) - int32_t(b_zero_point())) * (b_scale() / y_scale());
185         y_fp[i] = std::min<float>(y_fp[i], float(qmax()));
186         y_fp[i] = std::max<float>(y_fp[i], float(qmin()));
187         y_ref[i] = xnn_qu8_quantize_add(a_data[i], b_data[i], scalar_quantization_params);
188       }
189 
190       // Call optimized micro-kernel.
191       vadd_minmax(batch_size(), a_data, b_data, y.data(), &quantization_params);
192 
193       // Verify results.
194       for (size_t i = 0; i < batch_size(); i++) {
195         ASSERT_LE(uint32_t(y[i]), uint32_t(qmax()))
196           << "at element " << i << " / " << batch_size();
197         ASSERT_GE(uint32_t(y[i]), uint32_t(qmin()))
198           << "at element " << i << " / " << batch_size();
199         ASSERT_NEAR(float(int32_t(y[i])), y_fp[i], 0.6f)
200           << "at element " << i << " / " << batch_size();
201         ASSERT_EQ(uint32_t(y_ref[i]), uint32_t(y[i]))
202           << "at element " << i << " / " << batch_size();
203       }
204     }
205   }
206 
Test(xnn_qs8_vadd_minmax_ukernel_function vadd_minmax,xnn_init_qs8_add_minmax_params_fn init_params)207   void Test(xnn_qs8_vadd_minmax_ukernel_function vadd_minmax, xnn_init_qs8_add_minmax_params_fn init_params) const {
208     std::random_device random_device;
209     auto rng = std::mt19937(random_device());
210     auto i8rng = std::bind(
211       std::uniform_int_distribution<int32_t>(std::numeric_limits<int8_t>::min(), std::numeric_limits<int8_t>::max()), rng);
212 
213     std::vector<int8_t> a(batch_size() + XNN_EXTRA_BYTES / sizeof(int8_t));
214     std::vector<int8_t> b(batch_size() + XNN_EXTRA_BYTES / sizeof(int8_t));
215     std::vector<int8_t> y(batch_size() + (inplace_a() || inplace_b() ? XNN_EXTRA_BYTES / sizeof(int8_t) : 0));
216     std::vector<float> y_fp(batch_size());
217     std::vector<int8_t> y_ref(batch_size());
218     for (size_t iteration = 0; iteration < iterations(); iteration++) {
219       std::generate(a.begin(), a.end(), std::ref(i8rng));
220       std::generate(b.begin(), b.end(), std::ref(i8rng));
221       if (inplace_a() || inplace_b()) {
222         std::generate(y.begin(), y.end(), std::ref(i8rng));
223       } else {
224         std::fill(y.begin(), y.end(), 0xA5);
225       }
226       const int8_t* a_data = inplace_a() ? y.data() : a.data();
227       const int8_t* b_data = inplace_b() ? y.data() : b.data();
228 
229       // Prepare parameters.
230       xnn_qs8_add_minmax_params quantization_params;
231       init_params(
232         &quantization_params,
233         int8_t(a_zero_point() - 0x80), int8_t(b_zero_point() - 0x80), int8_t(y_zero_point() - 0x80),
234         a_scale() / y_scale(), b_scale() / y_scale(),
235         int8_t(qmin() - 0x80), int8_t(qmax() - 0x80));
236       xnn_qs8_add_minmax_params scalar_quantization_params;
237       xnn_init_qs8_add_minmax_scalar_params(
238         &scalar_quantization_params,
239         int8_t(a_zero_point() - 0x80), int8_t(b_zero_point() - 0x80), int8_t(y_zero_point() - 0x80),
240         a_scale() / y_scale(), b_scale() / y_scale(),
241         int8_t(qmin() - 0x80), int8_t(qmax() - 0x80));
242 
243       // Compute reference results.
244       for (size_t i = 0; i < batch_size(); i++) {
245         y_fp[i] = float(int32_t(y_zero_point() - 0x80)) +
246           float(int32_t(a_data[i]) - int32_t(a_zero_point() - 0x80)) * (a_scale() / y_scale()) +
247           float(int32_t(b_data[i]) - int32_t(b_zero_point() - 0x80)) * (b_scale() / y_scale());
248         y_fp[i] = std::min<float>(y_fp[i], float(int32_t(qmax() - 0x80)));
249         y_fp[i] = std::max<float>(y_fp[i], float(int32_t(qmin() - 0x80)));
250         y_ref[i] = xnn_qs8_quantize_add(a_data[i], b_data[i], scalar_quantization_params);
251       }
252 
253       // Call optimized micro-kernel.
254       vadd_minmax(batch_size(), a_data, b_data, y.data(), &quantization_params);
255 
256       // Verify results.
257       for (size_t i = 0; i < batch_size(); i++) {
258         ASSERT_LE(int32_t(y[i]), int32_t(qmax() - 0x80))
259           << "at element " << i << " / " << batch_size();
260         ASSERT_GE(int32_t(y[i]), int32_t(qmin() - 0x80))
261           << "at element " << i << " / " << batch_size();
262         ASSERT_EQ(int32_t(y_ref[i]), int32_t(y[i]))
263           << "at element " << i << " / " << batch_size();
264         ASSERT_NEAR(float(int32_t(y[i])), y_fp[i], 0.6f)
265           << "at element " << i << " / " << batch_size();
266       }
267     }
268   }
269 
270  private:
271   size_t batch_size_{1};
272   bool inplace_a_{false};
273   bool inplace_b_{false};
274   float a_scale_{0.75f};
275   float b_scale_{1.25f};
276   float y_scale_{0.96875f};
277   uint8_t a_zero_point_{121};
278   uint8_t b_zero_point_{127};
279   uint8_t y_zero_point_{133};
280   uint8_t qmin_{0};
281   uint8_t qmax_{255};
282   size_t iterations_{15};
283 };
284