forked from google/XNNPACK
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzip-microkernel-tester.h
166 lines (136 loc) · 4.36 KB
/
zip-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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright (c) Facebook, Inc. and its affiliates.
// All rights reserved.
//
// 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 <cstddef>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <limits>
#include <random>
#include <vector>
#include <gtest/gtest.h>
#include "xnnpack/microfnptr.h"
#include "replicable_random_device.h"
class ZipMicrokernelTester {
public:
ZipMicrokernelTester& n(size_t n) {
assert(n != 0);
this->n_ = n;
return *this;
}
size_t n() const {
return this->n_;
}
ZipMicrokernelTester& g(size_t g) {
assert(g != 0);
this->g_ = g;
return *this;
}
size_t g() const {
return this->g_;
}
ZipMicrokernelTester& iterations(size_t iterations) {
this->iterations_ = iterations;
return *this;
}
size_t iterations() const {
return this->iterations_;
}
void Test(xnn_x8_zipc_ukernel_fn zip) const {
xnnpack::ReplicableRandomDevice rng;
auto u8rng = [&rng]() {
return std::uniform_int_distribution<uint32_t>(
0, std::numeric_limits<uint8_t>::max())(rng);
};
std::vector<uint8_t> x(n() * g());
std::vector<uint8_t> x_ref(g() * n());
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(x.begin(), x.end(), std::ref(u8rng));
std::fill(x_ref.begin(), x_ref.end(), 0xA5);
// Call optimized micro-kernel.
zip(n() * sizeof(uint8_t), x.data(), x_ref.data());
// Verify results.
for (size_t i = 0; i < n(); i++) {
for (size_t j = 0; j < g(); j++) {
EXPECT_EQ(uint32_t(x_ref[i * g() + j]), uint32_t(x[j * n() + i]))
<< "at element " << i << ", group " << j;
}
}
}
}
void Test(xnn_x8_zipv_ukernel_fn zip) const {
xnnpack::ReplicableRandomDevice rng;
auto u8rng = [&rng]() {
return std::uniform_int_distribution<uint32_t>(
0, std::numeric_limits<uint8_t>::max())(rng);
};
std::vector<uint8_t> x(n() * g());
std::vector<uint8_t> x_ref(g() * n());
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(x.begin(), x.end(), std::ref(u8rng));
std::fill(x_ref.begin(), x_ref.end(), 0xA5);
// Call optimized micro-kernel.
zip(n() * sizeof(uint8_t), g(), x.data(), x_ref.data());
// Verify results.
for (size_t i = 0; i < n(); i++) {
for (size_t j = 0; j < g(); j++) {
EXPECT_EQ(uint32_t(x_ref[i * g() + j]), uint32_t(x[j * n() + i]))
<< "at element " << i << ", group " << j;
}
}
}
}
void Test(xnn_x32_zipc_ukernel_fn zip) const {
xnnpack::ReplicableRandomDevice rng;
auto u32rng = [&rng]() {
return std::uniform_int_distribution<uint32_t>()(rng);
};
std::vector<uint32_t> x(n() * g());
std::vector<uint32_t> x_ref(g() * n());
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(x.begin(), x.end(), std::ref(u32rng));
std::fill(x_ref.begin(), x_ref.end(), 0xA55A5AA5);
// Call optimized micro-kernel.
zip(n() * sizeof(uint32_t), x.data(), x_ref.data());
// Verify results.
for (size_t i = 0; i < n(); i++) {
for (size_t j = 0; j < g(); j++) {
EXPECT_EQ(x_ref[i * g() + j], x[j * n() + i])
<< "at element " << i << ", group " << j;
}
}
}
}
void Test(xnn_x32_zipv_ukernel_fn zip) const {
xnnpack::ReplicableRandomDevice rng;
auto u32rng = [&rng]() {
return std::uniform_int_distribution<uint32_t>()(rng);
};
std::vector<uint32_t> x(n() * g());
std::vector<uint32_t> x_ref(g() * n());
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(x.begin(), x.end(), std::ref(u32rng));
std::fill(x_ref.begin(), x_ref.end(), 0xA55A5AA5);
// Call optimized micro-kernel.
zip(n() * sizeof(uint32_t), g(), x.data(), x_ref.data());
// Verify results.
for (size_t i = 0; i < n(); i++) {
for (size_t j = 0; j < g(); j++) {
EXPECT_EQ(x_ref[i * g() + j], x[j * n() + i])
<< "at element " << i << ", group " << j;
}
}
}
}
private:
size_t n_{1};
size_t g_{1};
size_t iterations_{3};
};