forked from google/XNNPACK
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathraddstoreexpminusmax-microkernel-tester.h
133 lines (115 loc) · 4.6 KB
/
raddstoreexpminusmax-microkernel-tester.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2019 Google LLC
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
#pragma once
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <limits>
#include <random>
#include <vector>
#include <gtest/gtest.h>
#include <fp16/fp16.h>
#include "xnnpack.h"
#include "xnnpack/microfnptr.h"
#include "xnnpack/microparams.h"
#include "replicable_random_device.h"
class RAddStoreExpMinusMaxMicrokernelTester {
public:
RAddStoreExpMinusMaxMicrokernelTester& elements(size_t elements) {
assert(elements != 0);
this->elements_ = elements;
return *this;
}
size_t elements() const {
return this->elements_;
}
RAddStoreExpMinusMaxMicrokernelTester& iterations(size_t iterations) {
this->iterations_ = iterations;
return *this;
}
size_t iterations() const {
return this->iterations_;
}
void Test(xnn_f16_raddstoreexpminusmax_ukernel_fn raddstoreexpminusmax, xnn_init_f16_expminus_params_fn init_params) const {
xnnpack::ReplicableRandomDevice rng;
// Choose such range that exph(x[i]) overflows, but exph(x[i] - x_max) doesn't.
// However, the range is still narrow enough that double-precision exp doesn't overflow.
std::uniform_real_distribution<float> f32dist(15.0f, 20.0f);
std::vector<xnn_float16> x(elements() + XNN_EXTRA_BYTES / sizeof(xnn_float16));
std::vector<xnn_float16> y(elements());
std::vector<float> y_ref(elements());
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(x.begin(), x.end(), [&]() { return f32dist(rng); });
std::fill(y.begin(), y.end(), std::nanf(""));
// Compute reference results.
float sum_ref = 0.0f;
float x_max_as_float = -std::numeric_limits<float>::infinity();
for (size_t i = 0; i < elements(); i++) {
x_max_as_float = std::max<float>(x_max_as_float, x[i]);
}
const xnn_float16 x_max_as_half = x_max_as_float;
for (size_t i = 0; i < elements(); i++) {
const float y_ref_value = exp(x[i] - x_max_as_float);
y_ref[i] = y_ref_value;
sum_ref += y_ref_value;
}
// Call optimized micro-kernel.
xnn_float16 sum = std::nanf("");
xnn_f16_expminus_params params;
if (init_params) {
init_params(¶ms);
}
raddstoreexpminusmax(elements() * sizeof(xnn_float16), x.data(), &x_max_as_half, y.data(), &sum, ¶ms);
// Verify results.
for (size_t i = 0; i < elements(); i++) {
EXPECT_NEAR(y_ref[i], y[i], std::abs(y_ref[i]) * 5.0e-3f)
<< "element " << i << " / " << elements() << ", x_max " << x_max_as_float;
}
ASSERT_NEAR(sum_ref, sum, std::abs(sum_ref) * 5.0e-3f)
<< "batch " << elements() << ", x_max " << x_max_as_float;
}
}
void Test(xnn_f32_raddstoreexpminusmax_ukernel_fn raddstoreexpminusmax, xnn_init_f32_expminus_params_fn init_params) const {
xnnpack::ReplicableRandomDevice rng;
// Choose such range that expf(x[i]) overflows, but expf(x[i] - x_max) doesn't.
// However, the range is still narrow enough that double-precision exp doesn't overflow.
std::uniform_real_distribution<float> f32dist(90.0f, 100.0f);
std::vector<float> x(elements() + XNN_EXTRA_BYTES / sizeof(float));
std::vector<float> y(elements());
std::vector<double> y_ref(elements());
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(x.begin(), x.end(), [&]() { return f32dist(rng); });
std::fill(y.begin(), y.end(), std::nanf(""));
// Compute reference results.
double sum_ref = 0.0f;
const float x_max = *std::max_element(x.begin(), x.begin() + elements());
for (size_t i = 0; i < elements(); i++) {
const double y_ref_value = exp(double(x[i]) - double(x_max));
y_ref[i] = y_ref_value;
sum_ref += y_ref_value;
}
// Call optimized micro-kernel.
float sum = std::nanf("");
xnn_f32_expminus_params params;
if (init_params) {
init_params(¶ms);
}
raddstoreexpminusmax(elements() * sizeof(float), x.data(), &x_max, y.data(), &sum, ¶ms);
// Verify results.
for (size_t i = 0; i < elements(); i++) {
EXPECT_NEAR(y_ref[i], double(y[i]), std::abs(y_ref[i]) * 1.0e-6)
<< "element " << i << " / " << elements() << ", x_max " << x_max;
}
ASSERT_NEAR(sum_ref, double(sum), std::abs(sum_ref) * 1.0e-6)
<< "batch " << elements() << ", x_max " << x_max;
}
}
private:
size_t elements_{1};
size_t iterations_{15};
};