forked from google/XNNPACK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbankers-rounding-operator-tester.h
142 lines (117 loc) · 4.04 KB
/
bankers-rounding-operator-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
134
135
136
137
138
139
140
141
142
// Copyright 2020 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 <gtest/gtest.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstddef>
#include <cstdlib>
#include <functional>
#include <random>
#include <vector>
#include <xnnpack.h>
class BankersRoundingOperatorTester {
public:
inline BankersRoundingOperatorTester& channels(size_t channels) {
assert(channels != 0);
this->channels_ = channels;
return *this;
}
inline size_t channels() const {
return this->channels_;
}
inline BankersRoundingOperatorTester& input_stride(size_t input_stride) {
assert(input_stride != 0);
this->input_stride_ = input_stride;
return *this;
}
inline size_t input_stride() const {
if (this->input_stride_ == 0) {
return this->channels_;
} else {
assert(this->input_stride_ >= this->channels_);
return this->input_stride_;
}
}
inline BankersRoundingOperatorTester& output_stride(size_t output_stride) {
assert(output_stride != 0);
this->output_stride_ = output_stride;
return *this;
}
inline size_t output_stride() const {
if (this->output_stride_ == 0) {
return this->channels_;
} else {
assert(this->output_stride_ >= this->channels_);
return this->output_stride_;
}
}
inline BankersRoundingOperatorTester& batch_size(size_t batch_size) {
assert(batch_size != 0);
this->batch_size_ = batch_size;
return *this;
}
inline size_t batch_size() const {
return this->batch_size_;
}
inline BankersRoundingOperatorTester& iterations(size_t iterations) {
this->iterations_ = iterations;
return *this;
}
inline size_t iterations() const {
return this->iterations_;
}
void TestF32() const {
std::random_device random_device;
auto rng = std::mt19937(random_device());
auto f32rng = std::bind(std::uniform_real_distribution<float>(-1.0f, 1.0f), rng);
std::vector<float> input(XNN_EXTRA_BYTES / sizeof(float) +
(batch_size() - 1) * input_stride() + channels());
std::vector<float> output((batch_size() - 1) * output_stride() + channels());
std::vector<float> output_ref(batch_size() * channels());
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(input.begin(), input.end(), std::ref(f32rng));
std::fill(output.begin(), output.end(), std::nanf(""));
// Compute reference results.
for (size_t i = 0; i < batch_size(); i++) {
for (size_t c = 0; c < channels(); c++) {
output_ref[i * channels() + c] = std::nearbyint(input[i * input_stride() + c]);
}
}
// Create, setup, run, and destroy Bankers' Rounding operator.
ASSERT_EQ(xnn_status_success, xnn_initialize(nullptr /* allocator */));
xnn_operator_t rounding_op = nullptr;
ASSERT_EQ(xnn_status_success,
xnn_create_bankers_rounding_nc_f32(
channels(), input_stride(), output_stride(),
0, &rounding_op));
ASSERT_NE(nullptr, rounding_op);
// Smart pointer to automatically delete rounding_op.
std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_rounding_op(rounding_op, xnn_delete_operator);
ASSERT_EQ(xnn_status_success,
xnn_setup_bankers_rounding_nc_f32(
rounding_op,
batch_size(),
input.data(), output.data(),
nullptr /* thread pool */));
ASSERT_EQ(xnn_status_success,
xnn_run_operator(rounding_op, nullptr /* thread pool */));
// Verify results.
for (size_t i = 0; i < batch_size(); i++) {
for (size_t c = 0; c < channels(); c++) {
ASSERT_EQ(output_ref[i * channels() + c], output[i * output_stride() + c])
<< "at batch " << i << " / " << batch_size() << ", channel " << c << " / " << channels();
}
}
}
}
private:
size_t batch_size_{1};
size_t channels_{1};
size_t input_stride_{0};
size_t output_stride_{0};
size_t iterations_{15};
};