forked from google/XNNPACK
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpackb-microkernel-tester.h
246 lines (212 loc) · 8.32 KB
/
packb-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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright 2022 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 <numeric>
#include <vector>
#include <gtest/gtest.h>
#include "xnnpack/aligned-allocator.h"
#include "xnnpack/math.h"
#include "xnnpack/microfnptr.h"
// Reference bias packing function for f32.
static void f32_packb_reference(
size_t groups,
size_t channels,
size_t kernel_tile,
size_t channel_tile,
size_t channel_subtile,
size_t channel_round,
const float* weights,
const float* bias,
float* out,
size_t per_tile_extra_bytes,
size_t per_subtile_extra_bytes) {
assert(groups > 0);
// Group loop.
do {
// Channel tile loop.
size_t c = round_up_po2(channels, channel_round);
size_t tiled_c = round_down_po2(c, channel_tile);
size_t cr_block_start = 0;
for (; cr_block_start < tiled_c; cr_block_start += channel_tile) {
const size_t cr_block_size = min(channels - cr_block_start, channel_tile);
if (bias != nullptr) {
for (size_t i = 0; i < cr_block_size; i++) {
*out++ = bias[cr_block_start + i];
}
} else {
size_t i = cr_block_size;
do {
*out++ = 0.0f;
} while (--i != 0);
}
out += channel_tile - cr_block_size;
out += kernel_tile * channel_tile;
out += per_tile_extra_bytes;
}
// Channel subtile loop.
for (; cr_block_start < c; cr_block_start += channel_subtile) {
const size_t cr_block_size = min(channels - cr_block_start, channel_subtile);
if (bias != nullptr) {
for (size_t i = 0; i < cr_block_size; i++) {
*out++ = bias[cr_block_start + i];
}
} else {
size_t i = cr_block_size;
do {
*out++ = 0.0f;
} while (--i != 0);
}
out += channel_subtile - cr_block_size;
out += kernel_tile * channel_subtile;
out += per_subtile_extra_bytes;
}
if (bias != nullptr) {
bias += channels;
}
} while (--groups > 0);
}
class PackBMicrokernelTester {
public:
PackBMicrokernelTester& groups(size_t groups) {
this->groups_ = groups;
return *this;
}
size_t groups() const {
return this->groups_;
}
PackBMicrokernelTester& channel_tile(size_t channel_tile) {
this->channel_tile_ = channel_tile;
return *this;
}
size_t channel_tile() const {
return this->channel_tile_;
}
PackBMicrokernelTester& channel_subtile(size_t channel_subtile) {
this->channel_subtile_ = channel_subtile;
return *this;
}
size_t channel_subtile() const {
return this->channel_subtile_;
}
PackBMicrokernelTester& channel_round(size_t channel_round) {
this->channel_round_ = channel_round;
return *this;
}
size_t channel_round() const {
return this->channel_round_;
}
PackBMicrokernelTester& channels(size_t channels) {
assert(channels != 0);
this->channels_ = channels;
return *this;
}
size_t channels() const {
return this->channels_;
}
size_t packed_channels() const {
return round_up(channels(), channel_subtile());
}
PackBMicrokernelTester& kernel_tile(size_t kernel_tile) {
this->kernel_tile_ = kernel_tile;
return *this;
}
size_t kernel_tile() const {
return this->kernel_tile_;
}
void Test(xnn_x32_packb_gemm_ukernel_fn packb) const {
std::vector<uint32_t> weights(groups() * channels() * kernel_tile());
std::vector<uint32_t> bias(groups() * channels());
std::vector<uint32_t, AlignedAllocator<uint32_t, 64>> packed_w(
groups() * (packed_channels() * kernel_tile() + packed_channels()));
std::vector<uint32_t> packed_w_ref(groups() * (packed_channels() * kernel_tile() + packed_channels()));
std::fill(weights.begin(), weights.end(), 0xDEADBEEF);
std::iota(bias.begin(), bias.end(), UINT32_C(0x80000000));
std::fill(packed_w.begin(), packed_w.end(), UINT32_C(0x12345678));
std::fill(packed_w_ref.begin(), packed_w_ref.end(), UINT32_C(0xDEADBEEF));
// Compute reference results.
f32_packb_reference(
groups(), channels(), kernel_tile(), channel_tile(), channel_subtile(), channel_round(),
reinterpret_cast<const float*>(weights.data()), reinterpret_cast<const float*>(bias.data()),
reinterpret_cast<float*>(packed_w_ref.data()), /*per_tile_extra_bytes=*/0, /*per_subtile_extra_bytes=*/0);
// Call optimized micro-kernel.
packb(
groups(), channels(), bias.data(), packed_w.data(),
/*channel_tile_stride=*/sizeof(float) * (kernel_tile() * channel_tile() + channel_tile()),
/*channel_subtile_stride=*/sizeof(float) * (kernel_tile() * channel_subtile() + channel_subtile()),
nullptr);
// Verify results.
for (size_t i = 0; i < packed_w.size(); i++) {
if (packed_w_ref[i] != UINT32_C(0xDEADBEEF)) { // Allow weights and padding to differ.
EXPECT_EQ(packed_w[i], packed_w_ref[i]) << "at position " << i << " / " << packed_w.size() << ", channels "
<< channels() << ", kernel tile " << kernel_tile() << ", groups "
<< groups();
} else {
// These are weights, and should be unmodified.
EXPECT_EQ(packed_w[i], 0x12345678) << "at position " << i << " / " << packed_w.size() << ", channels "
<< channels() << ", kernel tile " << kernel_tile() << ", groups "
<< groups();
}
}
}
void Test(xnn_x32_zerob_gemm_ukernel_fn zerob) const {
std::vector<uint32_t> weights(groups() * channels() * kernel_tile());
std::vector<uint32_t, AlignedAllocator<uint32_t, 64>> packed_w(
groups() * (packed_channels() * kernel_tile() + packed_channels()));
std::vector<uint32_t> packed_w_ref(groups() * (packed_channels() * kernel_tile() + packed_channels()));
std::fill(weights.begin(), weights.end(), 0xDEADBEEF);
std::fill(packed_w.begin(), packed_w.end(), UINT32_C(0x12345678));
std::fill(packed_w_ref.begin(), packed_w_ref.end(), UINT32_C(0xDEADBEEF));
// Compute reference results.
f32_packb_reference(
groups(), channels(), kernel_tile(), channel_tile(), channel_subtile(), channel_round(),
reinterpret_cast<const float*>(weights.data()), nullptr,
reinterpret_cast<float*>(packed_w_ref.data()), /*per_tile_extra_bytes=*/0, /*per_subtile_extra_bytes=*/0);
// Call optimized micro-kernel.
zerob(
groups(), channels(), packed_w.data(),
/*channel_tile_stride=*/sizeof(float) * (kernel_tile() * channel_tile() + channel_tile()),
/*channel_subtile_stride=*/sizeof(float) * (kernel_tile() * channel_subtile() + channel_subtile()),
nullptr);
// Verify results.
for (size_t i = 0; i < packed_w.size(); i++) {
if (packed_w_ref[i] != UINT32_C(0xDEADBEEF)) { // Allow weights and padding to differ.
EXPECT_EQ(packed_w[i], packed_w_ref[i]) << "at position " << i << " / " << packed_w.size() << ", channels "
<< channels() << ", kernel tile " << kernel_tile();
// Bias should be zero.
EXPECT_EQ(packed_w[i], 0.0f) << "at position " << i << " / " << packed_w.size() << ", channels " << channels()
<< ", kernel tile " << kernel_tile();
} else {
// These are weights, and should be unmodified.
EXPECT_EQ(packed_w[i], 0x12345678) << "at position " << i << " / " << packed_w.size() << ", channels "
<< channels() << ", kernel tile " << kernel_tile();
}
}
}
struct Kernel {
explicit Kernel(xnn_x32_packb_gemm_ukernel_fn packb) {
dispatch = [packb](const PackBMicrokernelTester& tester) { tester.Test(packb); };
}
explicit Kernel(xnn_x32_zerob_gemm_ukernel_fn zerob) {
dispatch = [zerob](const PackBMicrokernelTester& tester) { tester.Test(zerob); };
}
std::function<void(const PackBMicrokernelTester)> dispatch;
};
void Test(const Kernel& kernel) const {
kernel.dispatch(*this);
}
private:
size_t groups_{1};
size_t channels_{1};
size_t channel_tile_{1};
size_t channel_subtile_{1};
size_t channel_round_{1};
size_t kernel_tile_{1};
};