forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unfold2d.cpp
306 lines (292 loc) · 9.63 KB
/
Unfold2d.cpp
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <ATen/Parallel.h>
#include <ATen/cpu/vec256/vec256.h>
#include <ATen/native/Unfold2d.h>
#include <ATen/native/cpu/Loops.h>
#include <cmath>
namespace at {
namespace native {
namespace {
template <typename scalar_t>
static inline void cadd(
scalar_t* z,
const scalar_t* x,
const scalar_t* y,
int64_t n) {
using Vec = vec256::Vec256<scalar_t>;
char* ptrs[] = {reinterpret_cast<char*>(z),
reinterpret_cast<char*>(const_cast<scalar_t*>(x)),
reinterpret_cast<char*>(const_cast<scalar_t*>(y))};
vectorized_loop(
ptrs,
n,
-1,
[](scalar_t x, scalar_t y) -> scalar_t { return x + y; },
[](Vec x, Vec y) -> Vec { return x + y; });
}
template <typename scalar_t>
static void unfolded2d_acc(
scalar_t* finput_data,
scalar_t* input_data,
int64_t kH,
int64_t kW,
int64_t dH,
int64_t dW,
int64_t padH,
int64_t padW,
int64_t n_input_plane,
int64_t input_height,
int64_t input_width,
int64_t output_height,
int64_t output_width) {
at::parallel_for(0, n_input_plane, 0, [&](int64_t start, int64_t end) {
for (auto nip = start; nip < end; nip++) {
int64_t kw, kh, y, x;
int64_t ix, iy;
for (kh = 0; kh < kH; kh++) {
for (kw = 0; kw < kW; kw++) {
scalar_t* src = finput_data +
nip * ((size_t)kH * kW * output_height * output_width) +
kh * ((size_t)kW * output_height * output_width) +
kw * ((size_t)output_height * output_width);
scalar_t* dst =
input_data + nip * ((size_t)input_height * input_width);
if (padW > 0 || padH > 0) {
int64_t lpad, rpad;
for (y = 0; y < output_height; y++) {
iy = (int64_t)y * dH - padH + kh;
if (iy < 0 || iy >= input_height) {
} else {
if (dW == 1) {
ix = 0 - padW + kw;
lpad = std::max<int64_t>(0, padW - kw);
rpad = std::max<int64_t>(0, padW - (kW - kw - 1));
scalar_t* dst_slice =
dst + (size_t)iy * input_width + ix + lpad;
cadd(
dst_slice,
dst_slice,
src + (size_t)y * output_width + lpad,
output_width - lpad - rpad);
} else {
for (x = 0; x < output_width; x++) {
ix = (int64_t)x * dW - padW + kw;
if (ix < 0 || ix >= input_width) {
} else {
scalar_t* dst_slice = dst + (size_t)iy * input_width + ix;
*dst_slice = *dst_slice + src[(size_t)y * output_width + x];
}
}
}
}
}
} else {
for (y = 0; y < output_height; y++) {
iy = (int64_t)y * dH + kh;
ix = 0 + kw;
if (dW == 1) {
scalar_t* dst_slice = dst + (size_t)iy * input_width + ix;
cadd(
dst_slice,
dst_slice,
src + (size_t)y * output_width,
output_width);
} else {
for (x = 0; x < output_width; x++) {
scalar_t* dst_slice =
dst + (size_t)iy * input_width + ix + x * dW;
*dst_slice = *dst_slice + src[(size_t)y * output_width + x];
}
}
}
}
}
}
}
});
}
/* note: due to write issues, this one cannot be parallelized as well as
* unfolded2d_copy */
void unfolded2d_acc_kernel(
Tensor& finput,
Tensor& input,
int64_t kH,
int64_t kW,
int64_t dH,
int64_t dW,
int64_t padH,
int64_t padW,
int64_t n_input_plane,
int64_t input_height,
int64_t input_width,
int64_t output_height,
int64_t output_width) {
// This function assumes that
// output_height*dH does not overflow a int64_t
// output_width*dW does not overflow a int64_t
AT_DISPATCH_FLOATING_TYPES_AND(
at::ScalarType::BFloat16, input.scalar_type(), "unfolded2d_acc", [&] {
scalar_t* finput_data = finput.data_ptr<scalar_t>();
scalar_t* input_data = input.data_ptr<scalar_t>();
unfolded2d_acc(
finput_data,
input_data,
kH,
kW,
dH,
dW,
padH,
padW,
n_input_plane,
input_height,
input_width,
output_height,
output_width);
});
}
template <typename scalar_t>
static void unfolded2d_copy(
scalar_t* input_data,
scalar_t* finput_data,
int64_t kH,
int64_t kW,
int64_t dH,
int64_t dW,
int64_t padH,
int64_t padW,
int64_t n_input_plane,
int64_t input_height,
int64_t input_width,
int64_t output_height,
int64_t output_width) {
at::parallel_for(
0, (int64_t)n_input_plane * kH * kW, 0, [&](int64_t start, int64_t end) {
for (auto k = start; k < end; k++) {
int64_t nip = k / (kH * kW);
int64_t rest = k % (kH * kW);
int64_t kh = rest / kW;
int64_t kw = rest % kW;
int64_t x, y;
int64_t ix, iy;
scalar_t* dst = finput_data +
nip * ((size_t)kH * kW * output_height * output_width) +
kh * ((size_t)kW * output_height * output_width) +
kw * ((size_t)output_height * output_width);
scalar_t* src =
input_data + nip * ((size_t)input_height * input_width);
if (padW > 0 || padH > 0) {
int64_t lpad, rpad;
for (y = 0; y < output_height; y++) {
iy = (int64_t)y * dH - padH + kh;
if (iy < 0 || iy >= input_height) {
memset(
dst + (size_t)y * output_width,
0,
sizeof(scalar_t) * output_width);
} else {
if (dW == 1) {
ix = 0 - padW + kw;
lpad = std::max<int64_t>(0, padW - kw);
rpad = std::max<int64_t>(0, padW - (kW - kw - 1));
if (output_width - rpad - lpad <= 0) {
memset(
dst + (size_t)y * output_width,
0,
sizeof(scalar_t) * output_width);
} else {
if (lpad > 0)
memset(
dst + (size_t)y * output_width,
0,
sizeof(scalar_t) * lpad);
memcpy(
dst + (size_t)y * output_width + lpad,
src + (size_t)iy * input_width + ix + lpad,
sizeof(scalar_t) * (output_width - rpad - lpad));
if (rpad > 0)
memset(
dst + (size_t)y * output_width + output_width - rpad,
0,
sizeof(scalar_t) * rpad);
}
} else {
for (x = 0; x < output_width; x++) {
ix = (int64_t)x * dW - padW + kw;
if (ix < 0 || ix >= input_width)
memset(
dst + (size_t)y * output_width + x,
0,
sizeof(scalar_t) * 1);
else
memcpy(
dst + (size_t)y * output_width + x,
src + (size_t)iy * input_width + ix,
sizeof(scalar_t) * (1));
}
}
}
}
} else {
for (y = 0; y < output_height; y++) {
iy = (int64_t)y * dH + kh;
ix = 0 + kw;
if (dW == 1)
memcpy(
dst + (size_t)y * output_width,
src + (size_t)iy * input_width + ix,
sizeof(scalar_t) * output_width);
else {
for (x = 0; x < output_width; x++)
memcpy(
dst + (size_t)y * output_width + x,
src + (size_t)iy * input_width + ix + (int64_t)x * dW,
sizeof(scalar_t) * (1));
}
}
}
}
});
}
void unfolded2d_copy_kernel(
Tensor& finput,
Tensor& input,
int64_t kH,
int64_t kW,
int64_t dH,
int64_t dW,
int64_t padH,
int64_t padW,
int64_t n_input_plane,
int64_t input_height,
int64_t input_width,
int64_t output_height,
int64_t output_width) {
// This function assumes that
// kH*kW does not overflow an int
// n_input_plane*kH*kW does not overflow a int64_t
// output_height*dH does not overflow a int64_t
// output_width*dW does not overflow a int64_t
AT_DISPATCH_ALL_TYPES_AND(
at::ScalarType::BFloat16, input.scalar_type(), "unfolded2d_copy", [&] {
scalar_t* input_data = input.data_ptr<scalar_t>();
scalar_t* finput_data = finput.data_ptr<scalar_t>();
unfolded2d_copy(
input_data,
finput_data,
kH,
kW,
dH,
dW,
padH,
padW,
n_input_plane,
input_height,
input_width,
output_height,
output_width);
});
}
} // namespace
REGISTER_DISPATCH(unfolded2d_copy_stub, &unfolded2d_copy_kernel);
REGISTER_DISPATCH(unfolded2d_acc_stub, &unfolded2d_acc_kernel);
} // namespace native
} // namespace at