forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReflectionPad.cpp
543 lines (476 loc) · 15.4 KB
/
ReflectionPad.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#include <ATen/ATen.h>
#include <ATen/NativeFunctions.h>
namespace at {
namespace native {
namespace {
template <typename scalar_t>
static void reflection_pad1d_out_frame(
scalar_t *input_p, scalar_t *output_p,
int64_t nplane,
int64_t input_w, int64_t output_w,
int64_t pad_l) {
int64_t i_start_x = std::max(int64_t(0), -pad_l);
int64_t o_start_x = std::max(int64_t(0), pad_l);
int64_t k, ip_x;
#pragma omp parallel for private(k, ip_x)
for (k = 0; k < nplane; k++) {
for (int64_t j = 0; j < output_w; j++) {
if (j < pad_l) {
ip_x = pad_l * 2 - j;
} else if (j >= pad_l && j < input_w + pad_l) {
ip_x = j;
} else {
ip_x = (input_w + pad_l - 1) * 2 - j;
}
ip_x = ip_x - o_start_x + i_start_x;
scalar_t *dest_p = output_p + k*output_w + j;
scalar_t *src_p = input_p + k*input_w + ip_x;
*dest_p = *src_p;
}
}
}
template <typename scalar_t>
inline void reflection_pad1d_out_loop(
scalar_t *input_p, scalar_t *output_p,
int64_t nbatch, int64_t nplane,
int64_t input_w, int64_t output_w,
int64_t pad_l) {
int64_t p;
#pragma omp parallel for private(p)
for (p = 0; p < nbatch; p++) {
reflection_pad1d_out_frame<scalar_t>(
input_p + p * nplane * input_w,
output_p + p * nplane * output_w,
nplane,
input_w, output_w,
pad_l);
}
}
void reflection_pad1d_out_template(
Tensor& output, const Tensor& input_, IntArrayRef padding) {
int64_t dim_plane = 0;
int64_t dim_w = 1;
int64_t nbatch = 1;
AT_CHECK(input_.numel() > 0 &&
(input_.ndimension() == 2 || input_.ndimension() == 3), "non-empty 2D "
"or 3D (batch mode) tensor expected for input, but got: ", input_);
if (input_.ndimension() == 3) {
nbatch = input_.size(0);
dim_w++;
dim_plane++;
}
/* sizes */
auto pad_l = padding[0];
auto pad_r = padding[1];
int64_t nplane = input_.size(dim_plane);
int64_t input_w = input_.size(dim_w);
int64_t output_w = input_w + pad_l + pad_r;
AT_CHECK(pad_l < input_w && pad_r < input_w, "Argument #4: Padding size "
"should be less than the corresponding input dimension, but got: padding (",
pad_l, ", ", pad_r, ") at dimension ", dim_w, " of input ", input_.sizes());
AT_CHECK(output_w >= 1 , 2,
"input (W: ", input_w, ")is too small. Calculated output W: ", output_w);
/* get contiguous input */
Tensor input = input_.contiguous();
/* resize output */
if (input.ndimension() == 2) {
output.resize_({nplane, output_w});
AT_DISPATCH_FLOATING_TYPES(input.type(), "reflection_pad1d", [&] {
reflection_pad1d_out_frame<scalar_t>(
input.data<scalar_t>(), output.data<scalar_t>(),
nplane,
input_w, output_w,
pad_l);
});
} else {
output.resize_({nbatch, nplane, output_w});
AT_DISPATCH_FLOATING_TYPES(input.type(), "reflection_pad1d", [&] {
reflection_pad1d_out_loop<scalar_t>(
input.data<scalar_t>(), output.data<scalar_t>(),
nbatch, nplane,
input_w, output_w,
pad_l);
});
}
}
template <typename scalar_t>
static void reflection_pad1d_backward_out_frame(
scalar_t * grad_input, scalar_t * grad_output,
int64_t nplane,
int64_t input_w, int64_t output_w,
int64_t pad_l) {
int64_t i_start_x = std::max(int64_t(0), -pad_l);
int64_t o_start_x = std::max(int64_t(0), pad_l);
int64_t k, ip_x;
#pragma omp parallel for private(k, ip_x)
for (k = 0; k < nplane; k++) {
for (int64_t j = 0; j < output_w; j++) {
if (j < pad_l) {
ip_x = pad_l * 2 - j;
} else if (j >= pad_l && j < input_w + pad_l) {
ip_x = j;
} else {
ip_x = (input_w + pad_l - 1) * 2 - j;
}
ip_x = ip_x - o_start_x + i_start_x;
scalar_t *src_p = grad_output + k*output_w + j;
scalar_t *dest_p = grad_input + k*input_w + ip_x;
*dest_p += *src_p;
}
}
}
template <typename scalar_t>
inline void reflection_pad1d_backward_out_loop(
scalar_t *grad_input, scalar_t *grad_output,
int64_t nbatch, int64_t nplane,
int64_t input_w, int64_t output_w,
int64_t pad_l) {
int64_t p;
#pragma omp parallel for private(p)
for (p = 0; p < nbatch; p++) {
reflection_pad1d_backward_out_frame<scalar_t>(
grad_input + p * nplane * input_w,
grad_output + p * nplane * output_w,
nplane,
input_w, output_w,
pad_l);
}
}
void reflection_pad1d_backward_out_template(
Tensor& grad_input, const Tensor& grad_output_, const Tensor& input,
IntArrayRef padding) {
int64_t dim_plane = 0;
int64_t dim_w = 1;
int64_t nbatch = 1;
if (input.ndimension() == 3) {
nbatch = input.size(0);
dim_w++;
dim_plane++;
}
/* sizes */
auto pad_l = padding[0];
auto pad_r = padding[1];
int64_t nplane = input.size(dim_plane);
int64_t input_w = input.size(dim_w);
int64_t output_w = input_w + pad_l + pad_r;
AT_CHECK(output_w == grad_output_.size(dim_w), "grad_output width unexpected."
" Expected: ", output_w, ", Got: ", grad_output_.size(dim_w));
/* get contiguous grad_output */
Tensor grad_output = grad_output_.contiguous();
/* backprop */
if (input.ndimension() == 2) {
AT_DISPATCH_FLOATING_TYPES(
grad_input.type(), "reflection_pad1d_backward", [&] {
reflection_pad1d_backward_out_frame(
grad_input.data<scalar_t>(), grad_output.data<scalar_t>(),
nplane,
input_w, output_w,
pad_l);
}
);
} else {
AT_DISPATCH_FLOATING_TYPES(
grad_input.type(), "reflection_pad1d_backward", [&] {
reflection_pad1d_backward_out_loop(
grad_input.data<scalar_t>(),
grad_output.data<scalar_t>(),
nbatch, nplane,
input_w, output_w,
pad_l);
}
);
}
}
template <typename scalar_t>
static void reflection_pad2d_out_frame(
scalar_t * input_p, scalar_t * output_p,
int64_t nplane,
int64_t input_w, int64_t input_h,
int64_t output_w, int64_t output_h,
int64_t pad_l, int64_t pad_t) {
auto i_start_x = std::max(int64_t(0), -pad_l);
auto i_start_y = std::max(int64_t(0), -pad_t);
auto o_start_x = std::max(int64_t(0), pad_l);
auto o_start_y = std::max(int64_t(0), pad_t);
int64_t k, ip_x, ip_y;
#pragma omp parallel for private(k, ip_x, ip_y)
for (k = 0; k < nplane; k++) {
for (int64_t i = 0; i < output_h; i++) {
for (int64_t j = 0; j < output_w; j++) {
if (j < pad_l) {
ip_x = pad_l * 2 - j;
} else if (j >= pad_l && j < input_w + pad_l) {
ip_x = j;
} else {
ip_x = (input_w + pad_l - 1) * 2 - j;
}
ip_x = ip_x - o_start_x + i_start_x;
if (i < pad_t) {
ip_y = pad_t * 2 - i;
} else if (i >= pad_t && i < input_h + pad_t) {
ip_y = i;
} else {
ip_y = (input_h + pad_t - 1) * 2 - i;
}
ip_y = ip_y - o_start_y + i_start_y;
scalar_t *dest_p = output_p + k*output_w*output_h + i * output_w + j;
scalar_t *src_p = input_p + k*input_w*input_h + ip_y * input_w + ip_x;
*dest_p = *src_p;
}
}
}
}
template <typename scalar_t>
inline void reflection_pad2d_out_loop(
scalar_t * input_p, scalar_t * output_p,
int64_t nbatch, int64_t nplane,
int64_t input_w, int64_t input_h,
int64_t output_w, int64_t output_h,
int64_t pad_l, int64_t pad_t) {
int64_t p;
#pragma omp parallel for private(p)
for (p = 0; p < nbatch; p++) {
reflection_pad2d_out_frame(
input_p + p * nplane * input_w * input_h,
output_p + p * nplane * output_w * output_h,
nplane,
input_w, input_h, output_w, output_h,
pad_l, pad_t);
}
}
void reflection_pad2d_out_template(
Tensor &output, const Tensor &input_, IntArrayRef padding) {
int dim_w = 2;
int dim_h = 1;
int dim_slices = 0;
int64_t nbatch = 1;
AT_CHECK(input_.numel() > 0 &&
(input_.ndimension() == 3 || input_.ndimension() == 4), "non-empty 3D or "
"4D (batch mode) tensor expected for input, but got: ", input_);
if (input_.ndimension() == 4) {
nbatch = input_.size(0);
dim_w++;
dim_h++;
dim_slices++;
}
/* sizes */
int64_t pad_l = padding[0];
int64_t pad_r = padding[1];
int64_t pad_t = padding[2];
int64_t pad_b = padding[3];
int64_t nplane = input_.size(dim_slices);
int64_t input_h = input_.size(dim_h);
int64_t input_w = input_.size(dim_w);
int64_t output_h = input_h + pad_t + pad_b;
int64_t output_w = input_w + pad_l + pad_r;
AT_CHECK(pad_l < input_w && pad_r < input_w,
"Argument #4: Padding size should be less than the corresponding "
"input dimension, but got: padding (", pad_l, ", ", pad_r,
") at dimension ", dim_w, " of input ", input_.ndimension());
AT_CHECK(pad_t < input_h && pad_b < input_h,
"Argument #6: Padding size should be less than the corresponding "
"input dimension, but got: padding (", pad_t, ", ", pad_b,
") at dimension ", dim_h, " of input ", input_.ndimension());
AT_CHECK(output_w >= 1 || output_h >= 1,
"input (H: ", input_h, ", W: ", input_w, ")is too small. Calculated "
"output H: ", output_h, " W: ", output_w);
/* get contiguous input */
Tensor input = input_.contiguous();
if (input.ndimension() == 3) {
/* resize output */
output.resize_({nplane, output_h, output_w});
AT_DISPATCH_FLOATING_TYPES(input.type(), "reflection_pad2d", [&] {
reflection_pad2d_out_frame(
input.data<scalar_t>(), output.data<scalar_t>(),
nplane,
input_w, input_h, output_w, output_h,
pad_l, pad_t);
});
} else {
/* resize output */
output.resize_({nbatch, nplane, output_h, output_w});
AT_DISPATCH_FLOATING_TYPES(input.type(), "reflection_pad2d", [&] {
reflection_pad2d_out_loop(
input.data<scalar_t>(), output.data<scalar_t>(),
nbatch, nplane,
input_w, input_h, output_w, output_h,
pad_l, pad_t);
});
}
}
template <typename scalar_t>
static void reflection_pad2d_backward_out_frame(
scalar_t *grad_input, scalar_t *grad_output,
int64_t nplane,
int64_t input_w, int64_t input_h,
int64_t output_w, int64_t output_h,
int64_t pad_l, int64_t pad_t) {
auto i_start_x = std::max(int64_t(0), -pad_l);
auto i_start_y = std::max(int64_t(0), -pad_t);
auto o_start_x = std::max(int64_t(0), pad_l);
auto o_start_y = std::max(int64_t(0), pad_t);
int64_t k, ip_x, ip_y;
#pragma omp parallel for private(k, ip_x, ip_y)
for (k = 0; k < nplane; k++) {
for (int64_t i = 0; i < output_h; i++) {
for (int64_t j = 0; j < output_w; j++) {
if (j < pad_l) {
ip_x = pad_l * 2 - j;
} else if (j >= pad_l && j < input_w + pad_l) {
ip_x = j;
} else {
ip_x = (input_w + pad_l - 1) * 2 - j;
}
ip_x = ip_x - o_start_x + i_start_x;
if (i < pad_t) {
ip_y = pad_t * 2 - i;
} else if (i >= pad_t && i < input_h + pad_t) {
ip_y = i;
} else {
ip_y = (input_h + pad_t - 1) * 2 - i;
}
ip_y = ip_y - o_start_y + i_start_y;
scalar_t *src_p =
grad_output + k * output_w * output_h + i * output_w + j;
scalar_t *dest_p =
grad_input + k * input_w * input_h + ip_y * input_w + ip_x;
*dest_p += *src_p;
}
}
}
}
template <typename scalar_t>
inline void reflection_pad2d_backward_out_loop(
scalar_t *grad_input, scalar_t *grad_output,
int64_t nbatch, int64_t nplane,
int64_t input_w, int64_t input_h,
int64_t output_w, int64_t output_h,
int64_t pad_l, int64_t pad_t) {
int64_t p;
#pragma omp parallel for private(p)
for (p = 0; p < nbatch; p++) {
reflection_pad2d_backward_out_frame(
grad_input + p * nplane * input_h * input_w,
grad_output + p * nplane * output_h * output_w,
nplane,
input_w, input_h, output_w, output_h,
pad_l, pad_t);
}
}
void reflection_pad2d_backward_out_template(
Tensor &grad_input, const Tensor &grad_output_,
const Tensor &input, IntArrayRef padding) {
int dim_w = 2;
int dim_h = 1;
int dim_plane = 0;
int64_t nbatch = 1;
if (input.ndimension() == 4) {
nbatch = input.size(0);
dim_w++;
dim_h++;
dim_plane++;
}
/* sizes */
int64_t pad_l = padding[0];
int64_t pad_r = padding[1];
int64_t pad_t = padding[2];
int64_t pad_b = padding[3];
int64_t nplane = input.size(dim_plane);
int64_t input_h = input.size(dim_h);
int64_t input_w = input.size(dim_w);
int64_t output_h = input_h + pad_t + pad_b;
int64_t output_w = input_w + pad_l + pad_r;
AT_CHECK(output_w == grad_output_.size(dim_w),
"gradOutput width unexpected. Expected: ", output_w, ", Got: ",
grad_output_.size(dim_w));
AT_CHECK(output_h == grad_output_.size(dim_h),
"gradOutput height unexpected. Expected: ", output_h, ", Got: ",
grad_output_.size(dim_h));
/* get contiguous gradOutput */
Tensor grad_output = grad_output_.contiguous();
/* backprop */
if (input.ndimension() == 3) {
AT_DISPATCH_FLOATING_TYPES(
grad_output.type(), "reflection_pad2d_backward", [&] {
reflection_pad2d_backward_out_frame(
grad_input.data<scalar_t>(), grad_output.data<scalar_t>(),
nplane,
input_w, input_h, output_w, output_h,
pad_l, pad_t);
}
);
} else {
AT_DISPATCH_FLOATING_TYPES(
grad_output.type(), "reflection_pad2d_backward", [&] {
reflection_pad2d_backward_out_loop(
grad_input.data<scalar_t>(), grad_output.data<scalar_t>(),
nbatch, nplane,
input_w, input_h, output_w, output_h,
pad_l, pad_t);
}
);
}
}
} // namespace
Tensor& reflection_pad1d_out_cpu(
Tensor& output, const Tensor& input, IntArrayRef padding) {
reflection_pad1d_out_template(output, input, padding);
return output;
}
Tensor reflection_pad1d_cpu(const Tensor& input, IntArrayRef padding) {
auto output = at::empty({0}, input.options());
reflection_pad1d_out_template(output, input, padding);
return output;
}
Tensor& reflection_pad1d_backward_out_cpu(
Tensor& grad_input,
const Tensor& grad_output,
const Tensor& input,
IntArrayRef padding) {
grad_input.resize_as_(input);
grad_input.zero_();
reflection_pad1d_backward_out_template(
grad_input, grad_output, input, padding);
return grad_input;
}
Tensor reflection_pad1d_backward_cpu(
const Tensor& grad_output,
const Tensor& input,
IntArrayRef padding) {
auto grad_input = at::zeros_like(input);
reflection_pad1d_backward_out_template(
grad_input, grad_output, input, padding);
return grad_input;
}
Tensor& reflection_pad2d_out_cpu(
Tensor& output, const Tensor& input, IntArrayRef padding) {
reflection_pad2d_out_template(output, input, padding);
return output;
}
Tensor reflection_pad2d_cpu(const Tensor& input, IntArrayRef padding) {
auto output = at::empty({0}, input.options());
reflection_pad2d_out_template(output, input, padding);
return output;
}
Tensor& reflection_pad2d_backward_out_cpu(
Tensor& grad_input,
const Tensor& grad_output,
const Tensor& input,
IntArrayRef padding) {
grad_input.resize_as_(input);
grad_input.zero_();
reflection_pad2d_backward_out_template(
grad_input, grad_output, input, padding);
return grad_input;
}
Tensor reflection_pad2d_backward_cpu(
const Tensor& grad_output,
const Tensor& input,
IntArrayRef padding) {
auto grad_input = at::zeros_like(input);
reflection_pad2d_backward_out_template(
grad_input, grad_output, input, padding);
return grad_input;
}
} // namespace native
} // namespace at