forked from google/XNNPACK
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchannel-shuffle-operator-tester.h
210 lines (175 loc) · 6.42 KB
/
channel-shuffle-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
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
// 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 <limits>
#include <memory>
#include <random>
#include <vector>
#include <gtest/gtest.h>
#include "xnnpack.h"
#include "replicable_random_device.h"
class ChannelShuffleOperatorTester {
public:
ChannelShuffleOperatorTester& groups(size_t groups) {
assert(groups != 0);
this->groups_ = groups;
return *this;
}
size_t groups() const {
return this->groups_;
}
ChannelShuffleOperatorTester& group_channels(size_t group_channels) {
assert(group_channels != 0);
this->group_channels_ = group_channels;
return *this;
}
size_t group_channels() const {
return this->group_channels_;
}
size_t channels() const {
return groups() * group_channels();
}
ChannelShuffleOperatorTester& input_stride(size_t input_stride) {
assert(input_stride != 0);
this->input_stride_ = input_stride;
return *this;
}
size_t input_stride() const {
if (this->input_stride_ == 0) {
return channels();
} else {
assert(this->input_stride_ >= channels());
return this->input_stride_;
}
}
ChannelShuffleOperatorTester& output_stride(size_t output_stride) {
assert(output_stride != 0);
this->output_stride_ = output_stride;
return *this;
}
size_t output_stride() const {
if (this->output_stride_ == 0) {
return channels();
} else {
assert(this->output_stride_ >= channels());
return this->output_stride_;
}
}
ChannelShuffleOperatorTester& batch_size(size_t batch_size) {
assert(batch_size != 0);
this->batch_size_ = batch_size;
return *this;
}
size_t batch_size() const {
return this->batch_size_;
}
ChannelShuffleOperatorTester& iterations(size_t iterations) {
this->iterations_ = iterations;
return *this;
}
size_t iterations() const {
return this->iterations_;
}
void TestX8() const {
xnnpack::ReplicableRandomDevice rng;
std::uniform_int_distribution<int32_t> u8dist(
std::numeric_limits<uint8_t>::min(), std::numeric_limits<uint8_t>::max());
std::vector<uint8_t> input(XNN_EXTRA_BYTES / sizeof(uint8_t) + (batch_size() - 1) * input_stride() + channels());
std::vector<uint8_t> output((batch_size() - 1) * output_stride() + channels());
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(input.begin(), input.end(), [&]() { return u8dist(rng); });
std::fill(output.begin(), output.end(), UINT8_C(0xA5));
// Create, setup, run, and destroy Channel Shuffle operator.
ASSERT_EQ(xnn_status_success, xnn_initialize(nullptr /* allocator */));
xnn_operator_t channel_shuffle_op = nullptr;
ASSERT_EQ(xnn_status_success,
xnn_create_channel_shuffle_nc_x8(
groups(), group_channels(),
input_stride(), output_stride(),
0, &channel_shuffle_op));
ASSERT_NE(nullptr, channel_shuffle_op);
// Smart pointer to automatically delete channel_shuffle_op.
std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_channel_shuffle_op(channel_shuffle_op, xnn_delete_operator);
ASSERT_EQ(xnn_status_success,
xnn_reshape_channel_shuffle_nc_x8(
channel_shuffle_op,
batch_size(),
/*threadpool=*/nullptr));
ASSERT_EQ(xnn_status_success,
xnn_setup_channel_shuffle_nc_x8(
channel_shuffle_op,
input.data(), output.data()));
ASSERT_EQ(xnn_status_success,
xnn_run_operator(channel_shuffle_op, /*threadpool=*/nullptr));
// Verify results.
for (size_t i = 0; i < batch_size(); i++) {
for (size_t g = 0; g < groups(); g++) {
for (size_t c = 0; c < group_channels(); c++) {
ASSERT_EQ(int32_t(input[i * input_stride() + g * group_channels() + c]),
int32_t(output[i * output_stride() + c * groups() + g]))
<< "batch index " << i << ", group " << g << ", channel " << c;
}
}
}
}
}
void TestX32() const {
xnnpack::ReplicableRandomDevice rng;
std::uniform_int_distribution<uint32_t> u32dist;
std::vector<uint32_t> input(XNN_EXTRA_BYTES / sizeof(uint32_t) + (batch_size() - 1) * input_stride() + channels());
std::vector<uint32_t> output((batch_size() - 1) * output_stride() + channels());
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(input.begin(), input.end(), [&]() { return u32dist(rng); });
std::fill(output.begin(), output.end(), UINT32_C(0xDEADBEAF));
// Create, setup, run, and destroy Channel Shuffle operator.
ASSERT_EQ(xnn_status_success, xnn_initialize(nullptr /* allocator */));
xnn_operator_t channel_shuffle_op = nullptr;
ASSERT_EQ(xnn_status_success,
xnn_create_channel_shuffle_nc_x32(
groups(), group_channels(),
input_stride(), output_stride(),
0, &channel_shuffle_op));
ASSERT_NE(nullptr, channel_shuffle_op);
// Smart pointer to automatically delete channel_shuffle_op.
std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_channel_shuffle_op(channel_shuffle_op, xnn_delete_operator);
ASSERT_EQ(xnn_status_success,
xnn_reshape_channel_shuffle_nc_x32(
channel_shuffle_op,
batch_size(),
/*threadpool=*/nullptr));
ASSERT_EQ(xnn_status_success,
xnn_setup_channel_shuffle_nc_x32(
channel_shuffle_op,
input.data(), output.data()));
ASSERT_EQ(xnn_status_success,
xnn_run_operator(channel_shuffle_op, /*threadpool=*/nullptr));
// Verify results.
for (size_t i = 0; i < batch_size(); i++) {
for (size_t g = 0; g < groups(); g++) {
for (size_t c = 0; c < group_channels(); c++) {
ASSERT_EQ(input[i * input_stride() + g * group_channels() + c],
output[i * output_stride() + c * groups() + g])
<< "batch index " << i << ", group " << g << ", channel " << c;
}
}
}
}
}
private:
size_t groups_{1};
size_t group_channels_{1};
size_t batch_size_{1};
size_t input_stride_{0};
size_t output_stride_{0};
size_t iterations_{15};
};