forked from google/XNNPACK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel-shuffle.c
235 lines (205 loc) · 6.84 KB
/
channel-shuffle.c
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
// 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.
#include <assert.h>
#include <math.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <xnnpack.h>
#include <xnnpack/allocator.h>
#include <xnnpack/operator.h>
#include <xnnpack/log.h>
#include <xnnpack/params.h>
static enum xnn_status create_channel_shuffle_nc(
size_t groups,
size_t group_channels,
size_t input_stride,
size_t output_stride,
uint32_t flags,
enum xnn_operator_type operator_type,
xnn_operator_t* channel_shuffle_op_out)
{
xnn_operator_t channel_shuffle_op = NULL;
enum xnn_status status = xnn_status_uninitialized;
if (!xnn_params.initialized) {
xnn_log_error("failed to create Channel Shuffle operator: XNNPACK is not initialized");
goto error;
}
status = xnn_status_invalid_parameter;
if (groups <= 1) {
xnn_log_error(
"failed to create Channel Shuffle operator with %zu groups: at least two groups required", groups);
goto error;
}
if (group_channels == 0) {
xnn_log_error(
"failed to create Channel Shuffle operator with %zu group channels: number of group channels must be non-zero",
group_channels);
goto error;
}
const size_t channels = groups * group_channels;
if (input_stride < channels) {
xnn_log_error(
"failed to create Channel Shuffle operator with input element stride of %zu: "
"stride must be at least as large as the number of channels (%zux%zu)",
input_stride, groups, group_channels);
goto error;
}
if (output_stride < channels) {
xnn_log_error(
"failed to create Channel Shuffle operator with output element stride of %zu: "
"stride must be at least as large as the number of channels (%zux%zu)",
output_stride, groups, group_channels);
goto error;
}
status = xnn_status_out_of_memory;
channel_shuffle_op = xnn_allocate_zero_memory(sizeof(struct xnn_operator));
if (channel_shuffle_op == NULL) {
xnn_log_error("failed to allocate %zu bytes for Channel Shuffle operator descriptor", sizeof(struct xnn_operator));
goto error;
}
channel_shuffle_op->groups = groups;
channel_shuffle_op->group_channels = group_channels;
channel_shuffle_op->input_pixel_stride = input_stride;
channel_shuffle_op->output_pixel_stride = output_stride;
channel_shuffle_op->type = operator_type;
channel_shuffle_op->ukernel.type = xnn_ukernel_type_channel_shuffle;
channel_shuffle_op->state = xnn_run_state_invalid;
*channel_shuffle_op_out = channel_shuffle_op;
return xnn_status_success;
error:
xnn_delete_operator(channel_shuffle_op);
return status;
}
enum xnn_status xnn_create_channel_shuffle_nc_x8(
size_t groups,
size_t group_channels,
size_t input_stride,
size_t output_stride,
uint32_t flags,
xnn_operator_t* channel_shuffle_op_out)
{
return create_channel_shuffle_nc(
groups,
group_channels,
input_stride,
output_stride,
flags,
xnn_operator_type_channel_shuffle_x8,
channel_shuffle_op_out);
}
enum xnn_status xnn_create_channel_shuffle_nc_x32(
size_t groups,
size_t group_channels,
size_t input_stride,
size_t output_stride,
uint32_t flags,
xnn_operator_t* channel_shuffle_op_out)
{
return create_channel_shuffle_nc(
groups,
group_channels,
input_stride,
output_stride,
flags,
xnn_operator_type_channel_shuffle_x32,
channel_shuffle_op_out);
}
static enum xnn_status setup_channel_shuffle_nc(
xnn_operator_t channel_shuffle_op,
size_t batch_size,
const void* input,
void* output,
uint32_t log2_element_size,
const struct zip_parameters zip[restrict static 1])
{
channel_shuffle_op->state = xnn_run_state_invalid;
if (!xnn_params.initialized) {
xnn_log_error("failed to setup Channel Shuffle operator: XNNPACK is not initialized");
return xnn_status_uninitialized;
}
if (batch_size == 0) {
channel_shuffle_op->state = xnn_run_state_skip;
return xnn_status_success;
}
channel_shuffle_op->batch_size = batch_size;
channel_shuffle_op->input = input;
channel_shuffle_op->output = output;
const size_t groups = channel_shuffle_op->groups;
channel_shuffle_op->context.channel_shuffle = (struct channel_shuffle_context) {
.x = input,
.x_stride = channel_shuffle_op->input_pixel_stride << log2_element_size,
.y = output,
.y_stride = channel_shuffle_op->output_pixel_stride << log2_element_size,
.n = channel_shuffle_op->group_channels << log2_element_size,
.m = groups,
};
channel_shuffle_op->compute.type = xnn_parallelization_type_1d;
channel_shuffle_op->compute.range[0] = batch_size;
switch (groups) {
case 2:
channel_shuffle_op->compute.task_1d = (pthreadpool_task_1d_t) xnn_compute_channel_shuffle_fixed;
channel_shuffle_op->context.channel_shuffle.fixed_ukernel = zip->x2;
break;
case 3:
channel_shuffle_op->compute.task_1d = (pthreadpool_task_1d_t) xnn_compute_channel_shuffle_fixed;
channel_shuffle_op->context.channel_shuffle.fixed_ukernel = zip->x3;
break;
case 4:
channel_shuffle_op->compute.task_1d = (pthreadpool_task_1d_t) xnn_compute_channel_shuffle_fixed;
channel_shuffle_op->context.channel_shuffle.fixed_ukernel = zip->x4;
break;
default:
channel_shuffle_op->compute.task_1d = (pthreadpool_task_1d_t) xnn_compute_channel_shuffle_variable;
channel_shuffle_op->context.channel_shuffle.variable_ukernel = zip->xm;
break;
case 0:
case 1:
XNN_UNREACHABLE;
}
channel_shuffle_op->state = xnn_run_state_ready;
return xnn_status_success;
}
enum xnn_status xnn_setup_channel_shuffle_nc_x8(
xnn_operator_t channel_shuffle_op,
size_t batch_size,
const void* input,
void* output,
pthreadpool_t threadpool)
{
if (channel_shuffle_op->type != xnn_operator_type_channel_shuffle_x8) {
xnn_log_error("failed to setup Channel Shuffle (X8) operator: operator type mismatch");
return xnn_status_invalid_parameter;
}
return setup_channel_shuffle_nc(
channel_shuffle_op,
batch_size,
input,
output,
0 /* log2(sizeof(element)) = log2(sizeof(uint8_t)) */,
&xnn_params.x8.zip);
}
enum xnn_status xnn_setup_channel_shuffle_nc_x32(
xnn_operator_t channel_shuffle_op,
size_t batch_size,
const void* input,
void* output,
pthreadpool_t threadpool)
{
if (channel_shuffle_op->type != xnn_operator_type_channel_shuffle_x32) {
xnn_log_error("failed to setup Channel Shuffle (X32) operator: operator type mismatch");
return xnn_status_invalid_parameter;
}
return setup_channel_shuffle_nc(
channel_shuffle_op,
batch_size,
input,
output,
2 /* log2(sizeof(element)) = log2(sizeof(uint32_t)) */,
&xnn_params.x32.zip);
}