blob: 75091e8dd70db0a6ddb834c3ba8ff0115962c710 [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 Dukhan403b7d42019-12-05 12:49:11 -080027 MaxC,
28 MinC,
Marat Dukhanc07cb7f2019-11-14 15:32:05 -080029 MulC,
30 SubC,
31 RSubC,
32 };
33
34 enum class Variant {
35 Native,
36 Scalar,
37 };
38
39 inline VBinOpCMicrokernelTester& batch_size(size_t batch_size) {
40 assert(batch_size != 0);
41 this->batch_size_ = batch_size;
42 return *this;
43 }
44
45 inline size_t batch_size() const {
46 return this->batch_size_;
47 }
48
49 inline VBinOpCMicrokernelTester& inplace(bool inplace) {
50 this->inplace_ = inplace;
51 return *this;
52 }
53
54 inline bool inplace() const {
55 return this->inplace_;
56 }
57
58 inline VBinOpCMicrokernelTester& qmin(uint8_t qmin) {
59 this->qmin_ = qmin;
60 return *this;
61 }
62
63 inline uint8_t qmin() const {
64 return this->qmin_;
65 }
66
67 inline VBinOpCMicrokernelTester& qmax(uint8_t qmax) {
68 this->qmax_ = qmax;
69 return *this;
70 }
71
72 inline uint8_t qmax() const {
73 return this->qmax_;
74 }
75
76 inline VBinOpCMicrokernelTester& iterations(size_t iterations) {
77 this->iterations_ = iterations;
78 return *this;
79 }
80
81 inline size_t iterations() const {
82 return this->iterations_;
83 }
84
Marat Dukhan1e782c42019-11-21 17:02:40 -080085 void Test(xnn_f32_vbinary_ukernel_function vbinaryc, OpType op_type, Variant variant = Variant::Native) const {
Marat Dukhanc07cb7f2019-11-14 15:32:05 -080086 std::random_device random_device;
87 auto rng = std::mt19937(random_device());
88 auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), rng);
89
90 std::vector<float> a(batch_size() + XNN_EXTRA_BYTES / sizeof(float));
91 const float b = f32rng();
92 std::vector<float> y(batch_size() + (inplace() ? XNN_EXTRA_BYTES / sizeof(float) : 0));
93 std::vector<float> y_ref(batch_size());
94 for (size_t iteration = 0; iteration < iterations(); iteration++) {
95 std::generate(a.begin(), a.end(), std::ref(f32rng));
96 if (inplace()) {
97 std::generate(y.begin(), y.end(), std::ref(f32rng));
98 } else {
99 std::fill(y.begin(), y.end(), nanf(""));
100 }
101 const float* a_data = inplace() ? y.data() : a.data();
102
103 // Compute reference results.
104 for (size_t i = 0; i < batch_size(); i++) {
105 switch (op_type) {
106 case OpType::AddC:
107 y_ref[i] = a_data[i] + b;
108 break;
Marat Dukhan403b7d42019-12-05 12:49:11 -0800109 case OpType::MaxC:
110 y_ref[i] = std::max<float>(a_data[i], b);
111 break;
112 case OpType::MinC:
113 y_ref[i] = std::min<float>(a_data[i], b);
114 break;
Marat Dukhanc07cb7f2019-11-14 15:32:05 -0800115 case OpType::MulC:
116 y_ref[i] = a_data[i] * b;
117 break;
118 case OpType::SubC:
119 y_ref[i] = a_data[i] - b;
120 break;
121 case OpType::RSubC:
122 y_ref[i] = b - a_data[i];
123 break;
124 }
125 }
126 const float accumulated_min = *std::min_element(y_ref.cbegin(), y_ref.cend());
127 const float accumulated_max = *std::max_element(y_ref.cbegin(), y_ref.cend());
128 const float accumulated_range = accumulated_max - accumulated_min;
129 const float y_max = accumulated_range > 0.0f ?
130 (accumulated_max - accumulated_range / 255.0f * float(255 - qmax())) :
131 +std::numeric_limits<float>::infinity();
132 const float y_min = accumulated_range > 0.0f ?
133 (accumulated_min + accumulated_range / 255.0f * float(qmin())) :
134 -std::numeric_limits<float>::infinity();
135 for (size_t i = 0; i < batch_size(); i++) {
136 y_ref[i] = std::max<float>(std::min<float>(y_ref[i], y_max), y_min);
137 }
138
139 // Prepare output parameters.
140 xnn_f32_output_params output_params = { };
141 switch (variant) {
142 case Variant::Native:
143 output_params = xnn_init_f32_output_params(y_min, y_max);
144 break;
145 case Variant::Scalar:
146 output_params = xnn_init_scalar_f32_output_params(y_min, y_max);
147 break;
148 }
149
150 // Call optimized micro-kernel.
Marat Dukhan1e782c42019-11-21 17:02:40 -0800151 vbinaryc(batch_size() * sizeof(float), a_data, &b, y.data(), &output_params);
Marat Dukhanc07cb7f2019-11-14 15:32:05 -0800152
153 // Verify results.
154 for (size_t i = 0; i < batch_size(); i++) {
155 ASSERT_NEAR(y[i], y_ref[i], std::abs(y_ref[i]) * 1.0e-6f)
156 << "at " << i << " / " << batch_size();
157 }
158 }
159 }
160
161 private:
162 size_t batch_size_{1};
163 bool inplace_{false};
164 uint8_t qmin_{0};
165 uint8_t qmax_{255};
166 size_t iterations_{15};
167};