blob: 129a645bcec6f4e90c4ff7c91b06e9aabd960cb5 [file] [log] [blame]
Marat Dukhanc07cb7f2019-11-14 15:32:05 -08001// Copyright 2019 Google LLC
2//
3// This source code is licensed under the BSD-style license found in the
4// LICENSE file in the root directory of this source tree.
5
6#pragma once
7
8#include <gtest/gtest.h>
9
10#include <algorithm>
11#include <cassert>
12#include <cstddef>
13#include <cstdlib>
14#include <functional>
15#include <random>
16#include <vector>
17
18#include <xnnpack.h>
19#include <xnnpack/params-init.h>
20#include <xnnpack/params.h>
21
22
23class VBinOpCMicrokernelTester {
24 public:
25 enum class OpType {
26 AddC,
Marat Dukhan77ca6302019-12-06 12:48:15 -080027 DivC,
28 RDivC,
Marat Dukhan403b7d42019-12-05 12:49:11 -080029 MaxC,
30 MinC,
Marat Dukhanc07cb7f2019-11-14 15:32:05 -080031 MulC,
32 SubC,
33 RSubC,
34 };
35
36 enum class Variant {
37 Native,
38 Scalar,
39 };
40
41 inline VBinOpCMicrokernelTester& batch_size(size_t batch_size) {
42 assert(batch_size != 0);
43 this->batch_size_ = batch_size;
44 return *this;
45 }
46
47 inline size_t batch_size() const {
48 return this->batch_size_;
49 }
50
51 inline VBinOpCMicrokernelTester& inplace(bool inplace) {
52 this->inplace_ = inplace;
53 return *this;
54 }
55
56 inline bool inplace() const {
57 return this->inplace_;
58 }
59
60 inline VBinOpCMicrokernelTester& qmin(uint8_t qmin) {
61 this->qmin_ = qmin;
62 return *this;
63 }
64
65 inline uint8_t qmin() const {
66 return this->qmin_;
67 }
68
69 inline VBinOpCMicrokernelTester& qmax(uint8_t qmax) {
70 this->qmax_ = qmax;
71 return *this;
72 }
73
74 inline uint8_t qmax() const {
75 return this->qmax_;
76 }
77
78 inline VBinOpCMicrokernelTester& iterations(size_t iterations) {
79 this->iterations_ = iterations;
80 return *this;
81 }
82
83 inline size_t iterations() const {
84 return this->iterations_;
85 }
86
Marat Dukhan1e782c42019-11-21 17:02:40 -080087 void Test(xnn_f32_vbinary_ukernel_function vbinaryc, OpType op_type, Variant variant = Variant::Native) const {
Marat Dukhanc07cb7f2019-11-14 15:32:05 -080088 std::random_device random_device;
89 auto rng = std::mt19937(random_device());
90 auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), rng);
91
92 std::vector<float> a(batch_size() + XNN_EXTRA_BYTES / sizeof(float));
93 const float b = f32rng();
94 std::vector<float> y(batch_size() + (inplace() ? XNN_EXTRA_BYTES / sizeof(float) : 0));
95 std::vector<float> y_ref(batch_size());
96 for (size_t iteration = 0; iteration < iterations(); iteration++) {
97 std::generate(a.begin(), a.end(), std::ref(f32rng));
98 if (inplace()) {
99 std::generate(y.begin(), y.end(), std::ref(f32rng));
100 } else {
101 std::fill(y.begin(), y.end(), nanf(""));
102 }
103 const float* a_data = inplace() ? y.data() : a.data();
104
105 // Compute reference results.
106 for (size_t i = 0; i < batch_size(); i++) {
107 switch (op_type) {
108 case OpType::AddC:
109 y_ref[i] = a_data[i] + b;
110 break;
Marat Dukhan77ca6302019-12-06 12:48:15 -0800111 case OpType::DivC:
112 y_ref[i] = a_data[i] / b;
113 break;
114 case OpType::RDivC:
115 y_ref[i] = b / a_data[i];
116 break;
Marat Dukhan403b7d42019-12-05 12:49:11 -0800117 case OpType::MaxC:
118 y_ref[i] = std::max<float>(a_data[i], b);
119 break;
120 case OpType::MinC:
121 y_ref[i] = std::min<float>(a_data[i], b);
122 break;
Marat Dukhanc07cb7f2019-11-14 15:32:05 -0800123 case OpType::MulC:
124 y_ref[i] = a_data[i] * b;
125 break;
126 case OpType::SubC:
127 y_ref[i] = a_data[i] - b;
128 break;
129 case OpType::RSubC:
130 y_ref[i] = b - a_data[i];
131 break;
132 }
133 }
134 const float accumulated_min = *std::min_element(y_ref.cbegin(), y_ref.cend());
135 const float accumulated_max = *std::max_element(y_ref.cbegin(), y_ref.cend());
136 const float accumulated_range = accumulated_max - accumulated_min;
137 const float y_max = accumulated_range > 0.0f ?
138 (accumulated_max - accumulated_range / 255.0f * float(255 - qmax())) :
139 +std::numeric_limits<float>::infinity();
140 const float y_min = accumulated_range > 0.0f ?
141 (accumulated_min + accumulated_range / 255.0f * float(qmin())) :
142 -std::numeric_limits<float>::infinity();
143 for (size_t i = 0; i < batch_size(); i++) {
144 y_ref[i] = std::max<float>(std::min<float>(y_ref[i], y_max), y_min);
145 }
146
147 // Prepare output parameters.
148 xnn_f32_output_params output_params = { };
149 switch (variant) {
150 case Variant::Native:
151 output_params = xnn_init_f32_output_params(y_min, y_max);
152 break;
153 case Variant::Scalar:
154 output_params = xnn_init_scalar_f32_output_params(y_min, y_max);
155 break;
156 }
157
158 // Call optimized micro-kernel.
Marat Dukhan1e782c42019-11-21 17:02:40 -0800159 vbinaryc(batch_size() * sizeof(float), a_data, &b, y.data(), &output_params);
Marat Dukhanc07cb7f2019-11-14 15:32:05 -0800160
161 // Verify results.
162 for (size_t i = 0; i < batch_size(); i++) {
163 ASSERT_NEAR(y[i], y_ref[i], std::abs(y_ref[i]) * 1.0e-6f)
164 << "at " << i << " / " << batch_size();
165 }
166 }
167 }
168
169 private:
170 size_t batch_size_{1};
171 bool inplace_{false};
172 uint8_t qmin_{0};
173 uint8_t qmax_{255};
174 size_t iterations_{15};
175};