forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AveragePool3d.cu
730 lines (652 loc) · 24.2 KB
/
AveragePool3d.cu
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
#include <ATen/AccumulateType.h>
#include <ATen/native/Pool.h>
#include <ATen/cuda/CUDAContext.h>
#include <ATen/cuda/CUDAApplyUtils.cuh>
#include <ATen/cuda/detail/TensorInfo.cuh>
#include <ATen/cuda/detail/IndexUtils.cuh>
#include <ATen/cuda/detail/KernelUtils.h>
#include <THC/THCNumerics.cuh>
#include <c10/macros/Macros.h>
namespace at {
namespace native {
namespace {
__device__ inline int min(int a, int b) {
return a <= b ? a : b;
}
__device__ inline int max(int a, int b) {
return a >= b ? a : b;
}
template <typename scalar_t, typename accscalar_t>
__global__ void avg_pool3d_cuda_update_output(
PackedTensorAccessor<scalar_t, 4> input,
PackedTensorAccessor<scalar_t, 4> output,
int kT, int kH, int kW,
int dT, int dH, int dW,
int padT, int padH, int padW,
bool count_include_pad,
int offsetZ, int divisor_override)
{
int oCol = blockIdx.x * blockDim.x + threadIdx.x;
int oRow = blockIdx.y * blockDim.y + threadIdx.y;
int oFrame = (blockIdx.z + offsetZ) % output.size(1); // output frame/time
int slice = (blockIdx.z + offsetZ) / output.size(1); // output slice/feature
if (oRow < output.size(2) && oCol < output.size(3))
{
accscalar_t sum = 0.0;
int tstart = oFrame * dT - padT;
int hstart = oRow * dH - padH;
int wstart = oCol * dW - padW;
int tend = min(tstart + kT, input.size(1) + padT);
int hend = min(hstart + kH, input.size(2) + padH);
int wend = min(wstart + kW, input.size(3) + padW);
int pool_size = (tend - tstart) * (hend - hstart) * (wend - wstart);
tstart = max(tstart, 0);
hstart = max(hstart, 0);
wstart = max(wstart, 0);
tend = min(tend, input.size(1));
hend = min(hend, input.size(2));
wend = min(wend, input.size(3));
accscalar_t divide_factor;
if (divisor_override) {
divide_factor = static_cast<accscalar_t>(divisor_override);
} else {
if(count_include_pad) {
divide_factor = static_cast<accscalar_t>(pool_size);
} else {
divide_factor = static_cast<accscalar_t>((tend - tstart) * (hend - hstart) * (wend - wstart));
}
}
int ti, hi, wi;
for (ti = tstart; ti < tend; ++ti)
{
for (hi = hstart; hi < hend; ++hi)
{
for (wi = wstart; wi < wend; ++wi)
{
scalar_t val = input[slice][ti][hi][wi];
sum += val;
}
}
}
output[slice][oFrame][oRow][oCol] = ScalarConvert<accscalar_t, scalar_t>::to(sum / divide_factor);
}
}
// Inner-most loop size (kW) passed as template parameter for
// performance reasons.
//
template<int KERNEL_WIDTH, typename scalar_t, typename accscalar_t>
__global__ void avg_pool3d_cuda_update_output(
PackedTensorAccessor<scalar_t, 4> input,
PackedTensorAccessor<scalar_t, 4> output,
int kT, int kH,
int dT, int dH, int dW,
int padT, int padH, int padW,
bool count_include_pad,
int offsetZ, int divisor_override)
{
int oCol = blockIdx.x * blockDim.x + threadIdx.x;
int oRow = blockIdx.y * blockDim.y + threadIdx.y;
int oFrame = (blockIdx.z + offsetZ) % output.size(1); // output frame/time
int slice = (blockIdx.z + offsetZ) / output.size(1); // output slice/feature
if (oRow < output.size(2) && oCol < output.size(3))
{
accscalar_t sum = 0.0;
int tstart = oFrame * dT - padT;
int hstart = oRow * dH - padH;
int wstart = oCol * dW - padW;
int tend = min(tstart + kT, input.size(1) + padT);
int hend = min(hstart + kH, input.size(2) + padH);
int wend = min(wstart + KERNEL_WIDTH, input.size(3) + padW);
int pool_size = (tend - tstart) * (hend - hstart) * (wend - wstart);
tstart = max(tstart, 0);
hstart = max(hstart, 0);
wstart = max(wstart, 0);
tend = min(tend, input.size(1));
hend = min(hend, input.size(2));
wend = min(wend, input.size(3));
accscalar_t divide_factor;
if (divisor_override) {
divide_factor = static_cast<accscalar_t>(divisor_override);
} else {
if(count_include_pad) {
divide_factor = static_cast<accscalar_t>(pool_size);
} else {
divide_factor = static_cast<accscalar_t>((tend - tstart) * (hend - hstart) * (wend - wstart));
}
}
int ti, hi, wi;
for (ti = tstart; ti < tend; ++ti)
{
for (hi = hstart; hi < hend; ++hi)
{
for (wi = wstart; wi < wend; ++wi)
{
scalar_t val = input[slice][ti][hi][wi];
sum += val;
}
}
}
output[slice][oFrame][oRow][oCol] = ScalarConvert<accscalar_t, scalar_t>::to(sum / divide_factor);
}
}
template <typename scalar_t, typename accscalar_t>
__global__ void avg_pool3d_single_backward_out_frame_stride1(
PackedTensorAccessor<scalar_t, 4> gradOutput,
PackedTensorAccessor<scalar_t, 4> gradInput,
int kT, int kH, int kW,
accscalar_t normFactor,
int offsetZ)
{
int iCol = blockIdx.x * blockDim.x + threadIdx.x;
int iRow = blockIdx.y * blockDim.y + threadIdx.y;
int iFrame = (blockIdx.z + offsetZ) % gradInput.size(1); // input frame/time
int slice = (blockIdx.z + offsetZ) / gradInput.size(1); // input slice/feature
// guard against over-tiled threads
if (iRow < gradInput.size(2) && iCol < gradInput.size(3))
{
accscalar_t sum = 0.0;
scalar_t *gOut = &gradOutput[slice][max(0, iFrame - kT + 1)]
[max(0, iRow - kH + 1)][max(0, iCol - kW + 1)];
int frameOffset = 0;
for (int oFrame = max(0, iFrame - kT + 1);
oFrame < min(iFrame + 1, gradOutput.size(1));
++oFrame)
{
int rowOffset = frameOffset;
for (int oRow = max(0, iRow - kH + 1);
oRow < min(iRow + 1, gradOutput.size(2));
++oRow)
{
int colOffset = rowOffset;
for (int oCol = max(0, iCol - kW + 1);
oCol < min(iCol + 1, gradOutput.size(3));
++oCol)
{
sum += gOut[colOffset];
++colOffset;
}
rowOffset += gradOutput.size(3);
}
frameOffset += gradOutput.size(2) * gradOutput.size(3);
}
gradInput[slice][iFrame][iRow][iCol] = ScalarConvert<accscalar_t, scalar_t>::to(sum * normFactor);
}
}
template <typename scalar_t, typename accscalar_t>
__global__ void avg_pool3d_cuda_update_grad_input_atomic(
PackedTensorAccessor<scalar_t, 4> gradOutput,
PackedTensorAccessor<scalar_t, 4> gradInput,
int kT, int kH, int kW,
int dT, int dH, int dW,
int padT, int padH, int padW,
bool count_include_pad,
int offsetZ, int divisor_override)
{
int oCol = blockIdx.x * blockDim.x + threadIdx.x;
int oRow = blockIdx.y * blockDim.y + threadIdx.y;
int oFrame = (blockIdx.z + offsetZ) % gradOutput.size(1); // gradOutput frame/time
int slice = (blockIdx.z + offsetZ) / gradOutput.size(1); // gradOutput slice/feature
// guard against over-tiled threads
if (oRow < gradOutput.size(2) && oCol < gradOutput.size(3))
{
int tstart = oFrame * dT - padT;
int hstart = oRow * dH - padH;
int wstart = oCol * dW - padW;
int tend = min(tstart + kT, gradInput.size(1) + padT);
int hend = min(hstart + kH, gradInput.size(2) + padH);
int wend = min(wstart + kW, gradInput.size(3) + padW);
int pool_size = (tend - tstart) * (hend - hstart) * (wend - wstart);
tstart = max(tstart, 0);
hstart = max(hstart, 0);
wstart = max(wstart, 0);
tend = min(tend, gradInput.size(1));
hend = min(hend, gradInput.size(2));
wend = min(wend, gradInput.size(3));
accscalar_t divide_factor;
if (divisor_override) {
divide_factor = static_cast<accscalar_t>(divisor_override);
} else {
if(count_include_pad) {
divide_factor = static_cast<accscalar_t>(pool_size);
} else {
divide_factor = static_cast<accscalar_t>((tend - tstart) * (hend - hstart) * (wend - wstart));
}
}
scalar_t val = ScalarConvert<accscalar_t, scalar_t>::to(
ScalarConvert<scalar_t, accscalar_t>::to(gradOutput[slice][oFrame][oRow][oCol]) / divide_factor);
for (int iFrame = tstart; iFrame < tend; ++iFrame)
{
for (int iRow = hstart; iRow < hend; ++iRow)
{
for (int iCol = wstart; iCol < wend; ++iCol)
{
atomicAdd(&gradInput[slice][iFrame][iRow][iCol], val);
}
}
}
}
}
template <typename scalar_t, typename accscalar_t>
__global__ void avg_pool3d_cuda_update_grad_input(
PackedTensorAccessor<scalar_t, 4> gradOutput,
PackedTensorAccessor<scalar_t, 4> gradInput,
int kT, int kH, int kW,
int dT, int dH, int dW,
int padT, int padH, int padW,
bool count_include_pad, int offsetZ, int divisor_override)
{
int oCol = blockIdx.x * blockDim.x + threadIdx.x;
int oRow = blockIdx.y * blockDim.y + threadIdx.y;
int oFrame = (blockIdx.z + offsetZ) % gradOutput.size(1); // gradOutput frame/time
int slice = (blockIdx.z + offsetZ) / gradOutput.size(1); // gradOutput slice/feature
// guard against over-tiled threads
if (oRow < gradOutput.size(2) && oCol < gradOutput.size(3))
{
int tstart = oFrame * dT - padT;
int hstart = oRow * dH - padH;
int wstart = oCol * dW - padW;
int tend = min(tstart + kT, gradInput.size(1) + padT);
int hend = min(hstart + kH, gradInput.size(2) + padH);
int wend = min(wstart + kW, gradInput.size(3) + padW);
int pool_size = (tend - tstart) * (hend - hstart) * (wend - wstart);
tstart = max(tstart, 0);
hstart = max(hstart, 0);
wstart = max(wstart, 0);
tend = min(tend, gradInput.size(1));
hend = min(hend, gradInput.size(2));
wend = min(wend, gradInput.size(3));
accscalar_t divide_factor;
if (divisor_override) {
divide_factor = static_cast<accscalar_t>(divisor_override);
} else {
if(count_include_pad) {
divide_factor = static_cast<accscalar_t>(pool_size);
} else {
divide_factor = static_cast<accscalar_t>((tend - tstart) * (hend - hstart) * (wend - wstart));
}
}
scalar_t val = ScalarConvert<accscalar_t, scalar_t>::to(
ScalarConvert<scalar_t, accscalar_t>::to(gradOutput[slice][oFrame][oRow][oCol]) / divide_factor);
for (int iFrame = tstart; iFrame < tend; ++iFrame)
{
for (int iRow = hstart; iRow < hend; ++iRow)
{
for (int iCol = wstart; iCol < wend; ++iCol)
{
gradInput[slice][iFrame][iRow][iCol] = val;
}
}
}
}
}
#define LAUNCH_UPDATE_OUTPUT_KERNEL_WIDTH(KW) case KW: \
avg_pool3d_cuda_update_output<KW, scalar_t, accscalar_t> \
<<<grid, block, 0, at::cuda::getCurrentCUDAStream()>>>( \
work_input.packed_accessor<scalar_t, 4>(), \
work_output.packed_accessor<scalar_t, 4>(), \
kT, kH, \
dT, dH, dW, \
padT, padH, padW, \
count_include_pad, \
offsetZ, divisor); \
break
void avg_pool3d_out_cuda_template(
Tensor& output,
const Tensor& input,
IntArrayRef kernel_size,
IntArrayRef stride,
IntArrayRef padding,
bool ceil_mode,
bool count_include_pad,
c10::optional<int64_t> divisor_override)
{
TensorArg output_arg{ output, "output", 1 };
TensorArg input_arg{ input, "input", 2 };
checkAllSameGPU("avg_pool3d_out_cuda", {output_arg, input_arg});
// #20866, #22032: Guarantee this for the official C++ API?
TORCH_CHECK(kernel_size.size() == 1 || kernel_size.size() == 3,
"avg_pool3d: kernel_size must be a single int, or a tuple of three ints");
const int kT = safe_downcast<int, int64_t>(kernel_size[0]);
const int kH = kernel_size.size() == 1 ? kT : safe_downcast<int, int64_t>(kernel_size[1]);
const int kW = kernel_size.size() == 1 ? kT : safe_downcast<int, int64_t>(kernel_size[2]);
TORCH_CHECK(stride.empty() || stride.size() == 1 || stride.size() == 3,
"avg_pool3d: stride must be omitted, a single int, or a tuple of three ints");
const int dT = stride.empty() ? kT : safe_downcast<int, int64_t>(stride[0]);
const int dH = stride.empty() ? kH :
stride.size() == 1 ? dT : safe_downcast<int, int64_t>(stride[1]);
const int dW = stride.empty() ? kW :
stride.size() == 1 ? dT : safe_downcast<int, int64_t>(stride[2]);
TORCH_CHECK(padding.size() == 1 || padding.size() == 3,
"avg_pool3d: padding must be a single int, or a tuple of three ints");
const int padT = safe_downcast<int, int64_t>(padding[0]);
const int padH = padding.size() == 1 ? padT : safe_downcast<int, int64_t>(padding[1]);
const int padW = padding.size() == 1 ? padT : safe_downcast<int, int64_t>(padding[2]);
TORCH_CHECK((input.ndimension() == 4 || input.ndimension() == 5),
"non-empty 4D or 5D (batch mode) tensor expected for input");
// if divisor==0 then we will ignore it
int64_t divisor = 0;
if (divisor_override.has_value()) {
TORCH_CHECK(divisor_override.value() != 0, "divisor must be not zero");
divisor = divisor_override.value();
}
const int64_t nbatch = input.ndimension() == 5 ? input.size(-5) : 1;
const int64_t nslices = input.size(-4);
const int64_t itime = input.size(-3);
const int64_t iheight = input.size(-2);
const int64_t iwidth = input.size(-1);
const int64_t otime = pooling_output_shape<int64_t>(itime, kT, padT, dT, 1, ceil_mode);
const int64_t oheight = pooling_output_shape<int64_t>(iheight, kH, padH, dH, 1, ceil_mode);
const int64_t owidth = pooling_output_shape<int64_t>(iwidth, kW, padW, dW, 1, ceil_mode);
pool3d_shape_check(
input,
nslices,
kT, kH, kW,
dT, dH, dW,
padT, padH, padW,
1, 1, 1,
itime, iheight, iwidth,
otime, oheight, owidth,
/*check_input_size=*/ true);
if (input.ndimension() == 4) {
output.resize_({ nslices, otime, oheight, owidth});
}
else {
output.resize_({nbatch, nslices, otime, oheight, owidth});
}
Tensor work_input = input.contiguous();
Tensor work_output = output;
if (input.ndimension() == 5) {
// Collapse batch and feature dimensions.
work_input = work_input.reshape({nbatch * nslices, itime, iheight, iwidth});
work_output = work_output.reshape({nbatch * nslices, otime, oheight, owidth});
}
AT_DISPATCH_FLOATING_TYPES_AND_HALF(
input.scalar_type(),
"avg_pool3d_out_cuda",
[&] {
using accscalar_t = acc_type<scalar_t, true>;
int64_t totalZ = otime * nslices * nbatch;
int64_t offsetZ = 0;
dim3 block(32, 8);
while (totalZ > 0) {
dim3 grid(cuda::ATenCeilDiv(owidth, static_cast<int64_t>(block.x)),
cuda::ATenCeilDiv(oheight, static_cast<int64_t>(block.y)),
totalZ > 65535 ? 65535 : totalZ);
switch (kW) {
LAUNCH_UPDATE_OUTPUT_KERNEL_WIDTH(1);
LAUNCH_UPDATE_OUTPUT_KERNEL_WIDTH(2);
LAUNCH_UPDATE_OUTPUT_KERNEL_WIDTH(3);
LAUNCH_UPDATE_OUTPUT_KERNEL_WIDTH(4);
LAUNCH_UPDATE_OUTPUT_KERNEL_WIDTH(5);
LAUNCH_UPDATE_OUTPUT_KERNEL_WIDTH(6);
LAUNCH_UPDATE_OUTPUT_KERNEL_WIDTH(7);
default:
avg_pool3d_cuda_update_output<scalar_t, accscalar_t>
<<<grid, block, 0, at::cuda::getCurrentCUDAStream()>>>(
work_input.packed_accessor<scalar_t, 4>(),
work_output.packed_accessor<scalar_t, 4>(),
kT, kH, kW,
dT, dH, dW,
padT, padH, padW,
count_include_pad,
offsetZ, divisor);
break;
}
TORCH_CHECK(cudaGetLastError() == cudaSuccess,
"avg_pool3d_out_cuda failed with error code ",
cudaGetLastError());
totalZ -= 65535;
offsetZ += 65535;
}
}
);
}
#undef LAUNCH_UPDATE_OUTPUT_KERNEL_WIDTH
void avg_pool3d_backward_out_cuda_template(
Tensor& gradInput,
const Tensor& gradOutput,
const Tensor& input,
IntArrayRef kernel_size,
IntArrayRef stride,
IntArrayRef padding,
bool ceil_mode,
bool count_include_pad,
c10::optional<int64_t> divisor_override)
{
TensorArg gradInput_arg{ gradInput, "gradInput", 1 };
TensorArg gradOutput_arg{ gradOutput, "gradOutput", 2 };
TensorArg input_arg{ input, "input", 3 };
checkAllSameGPU("avg_pool3d_backward_out_cuda",
{gradInput_arg, gradOutput_arg, input_arg});
// #20866, #22032: Guarantee this for the official C++ API?
TORCH_CHECK(kernel_size.size() == 1 || kernel_size.size() == 3,
"avg_pool3d: kernel_size must be a single int, or a tuple of three ints");
const int kT = safe_downcast<int, int64_t>(kernel_size[0]);
const int kH = kernel_size.size() == 1 ? kT : safe_downcast<int, int64_t>(kernel_size[1]);
const int kW = kernel_size.size() == 1 ? kT : safe_downcast<int, int64_t>(kernel_size[2]);
TORCH_CHECK(stride.empty() || stride.size() == 1 || stride.size() == 3,
"avg_pool3d: stride must be omitted, a single int, or a tuple of three ints");
const int dT = stride.empty() ? kT : safe_downcast<int, int64_t>(stride[0]);
const int dH = stride.empty() ? kH :
stride.size() == 1 ? dT : safe_downcast<int, int64_t>(stride[1]);
const int dW = stride.empty() ? kW :
stride.size() == 1 ? dT : safe_downcast<int, int64_t>(stride[2]);
TORCH_CHECK(padding.size() == 1 || padding.size() == 3,
"avg_pool3d: padding must be a single int, or a tuple of three ints");
const int padT = safe_downcast<int, int64_t>(padding[0]);
const int padH = padding.size() == 1 ? padT : safe_downcast<int, int64_t>(padding[1]);
const int padW = padding.size() == 1 ? padT : safe_downcast<int, int64_t>(padding[2]);
TORCH_CHECK((input.ndimension() == 4 || input.ndimension() == 5),
"non-empty 4D or 5D (batch mode) tensor expected for input");
TORCH_CHECK((gradOutput.ndimension() == 4 || gradOutput.ndimension() == 5),
"non-empty 4D or 5D (batch mode) tensor expected for gradOutput");
// if divisor==0 then we will ignore it
int64_t divisor = 0;
if (divisor_override.has_value()) {
TORCH_CHECK(divisor_override.value() != 0, "divisor must be not zero");
divisor = divisor_override.value();
}
// Resize and initialize result tensor.
gradInput.resize_as_(input);
gradInput.zero_();
const int64_t nbatch = input.ndimension() == 5 ? input.size(-5) : 1;
const int64_t nslices = input.size(-4);
const int64_t itime = input.size(-3);
const int64_t iheight = input.size(-2);
const int64_t iwidth = input.size(-1);
const int64_t otime = gradOutput.size(-3);
const int64_t oheight = gradOutput.size(-2);
const int64_t owidth = gradOutput.size(-1);
/* XXX shape check behavior from TH */
const int64_t otime_for_shape_check = pooling_output_shape<int64_t>(itime, kT, padT, dT, 1, ceil_mode);
const int64_t oheight_for_shape_check = pooling_output_shape<int64_t>(iheight, kH, padH, dH, 1, ceil_mode);
const int64_t owidth_for_chape_check = pooling_output_shape<int64_t>(iwidth, kW, padW, dW, 1, ceil_mode);
const bool kernelsOverlap = (dT < kT) || (dH < kH) || (dW < kW);
avg_pool3d_backward_shape_check(
input,
gradOutput,
nslices,
kT, kH, kW,
dT, dH, dW,
padT, padH, padW,
itime, iheight, iwidth,
otime, oheight, owidth);
Tensor work_grad_input = gradInput;
Tensor work_grad_output = gradOutput.contiguous();
if (input.ndimension() == 5) {
// Collapse batch and feature dimensions.
work_grad_input = work_grad_input.reshape({nbatch * nslices, itime, iheight, iwidth});
work_grad_output = work_grad_output.reshape({nbatch * nslices, otime, oheight, owidth});
}
// Optimizing for stride 1 is probably only of limited value, but this
// specialization yields 3x speedup over the atomicAdd implementation.
// Padding must be 0, otherwise, pool size may change.
if (dT == 1 && dH == 1 && dW == 1 && padT == 0 && padH == 0 && padW == 0) {
AT_DISPATCH_FLOATING_TYPES_AND_HALF(input.scalar_type(),
"avg_pool3d_backward_out_frame_stride1",
[&] {
using accscalar_t = acc_type<scalar_t, true>;
int64_t totalZ = itime * nslices * nbatch;
int64_t offsetZ = 0;
dim3 block(32, 8);
accscalar_t divide_factor;
if (divisor) {
divide_factor = static_cast<accscalar_t>(divisor);
} else {
divide_factor = static_cast<accscalar_t>(kT * kH * kW);
}
while (totalZ > 0) {
dim3 grid(cuda::ATenCeilDiv(iwidth, static_cast<int64_t>(block.x)),
cuda::ATenCeilDiv(iheight, static_cast<int64_t>(block.y)),
totalZ > 65535 ? 65535 : totalZ);
avg_pool3d_single_backward_out_frame_stride1<scalar_t, accscalar_t>
<<<grid, block, 0, at::cuda::getCurrentCUDAStream()>>>(
work_grad_output.packed_accessor<scalar_t, 4>(),
work_grad_input.packed_accessor<scalar_t, 4>(),
kT, kH, kW,
1.0f/divide_factor,
offsetZ);
TORCH_CHECK(cudaGetLastError() == cudaSuccess,
"avg_pool3d_backward_out_frame failed with error code ",
cudaGetLastError());
totalZ -= 65535;
offsetZ += 65535;
}
}
);
}
else {
AT_DISPATCH_FLOATING_TYPES_AND_HALF(input.scalar_type(),
"avg_pool3d_backward_out_frame",
[&] {
using accscalar_t = acc_type<scalar_t, true>;
int64_t totalZ = otime * nslices * nbatch;
int64_t offsetZ = 0;
dim3 block(32, 8);
while (totalZ > 0) {
dim3 grid(cuda::ATenCeilDiv(owidth, static_cast<int64_t>(block.x)),
cuda::ATenCeilDiv(oheight, static_cast<int64_t>(block.y)),
totalZ > 65535 ? 65535 : totalZ);
if (kernelsOverlap) {
avg_pool3d_cuda_update_grad_input_atomic<scalar_t, accscalar_t>
<<<grid, block, 0, at::cuda::getCurrentCUDAStream()>>>(
work_grad_output.packed_accessor<scalar_t, 4>(),
work_grad_input.packed_accessor<scalar_t, 4>(),
kT, kH, kW,
dT, dH, dW,
padT, padH, padW,
count_include_pad,
offsetZ, divisor);
}
else {
avg_pool3d_cuda_update_grad_input<scalar_t, accscalar_t>
<<<grid, block, 0, at::cuda::getCurrentCUDAStream()>>>(
work_grad_output.packed_accessor<scalar_t, 4>(),
work_grad_input.packed_accessor<scalar_t, 4>(),
kT, kH, kW,
dT, dH, dW,
padT, padH, padW,
count_include_pad,
offsetZ, divisor);
}
TORCH_CHECK(cudaGetLastError() == cudaSuccess,
"avg_pool3d_backward_out_frame failed with error code ",
cudaGetLastError());
totalZ -= 65535;
offsetZ += 65535;
}
}
);
}
}
} // namespace
Tensor& avg_pool3d_out_cuda(
Tensor& output,
const Tensor& input,
IntArrayRef kernel_size,
IntArrayRef stride,
IntArrayRef padding,
bool ceil_mode,
bool count_include_pad,
c10::optional<int64_t> divisor_override)
{
avg_pool3d_out_cuda_template(
output,
input,
kernel_size,
stride,
padding,
ceil_mode,
count_include_pad,
divisor_override);
return output;
}
Tensor avg_pool3d_cuda(
const Tensor& input,
IntArrayRef kernel_size,
IntArrayRef stride,
IntArrayRef padding,
bool ceil_mode,
bool count_include_pad,
c10::optional<int64_t> divisor_override)
{
Tensor output = at::empty({0}, input.options());
avg_pool3d_out_cuda_template(
output,
input,
kernel_size,
stride,
padding,
ceil_mode,
count_include_pad,
divisor_override);
return output;
}
Tensor& avg_pool3d_backward_out_cuda(
Tensor& gradInput,
const Tensor& gradOutput_,
const Tensor& input,
IntArrayRef kernel_size,
IntArrayRef stride,
IntArrayRef padding,
bool ceil_mode,
bool count_include_pad,
c10::optional<int64_t> divisor_override)
{
avg_pool3d_backward_out_cuda_template(
gradInput,
gradOutput_,
input,
kernel_size,
stride,
padding,
ceil_mode,
count_include_pad,
divisor_override);
return gradInput;
}
Tensor avg_pool3d_backward_cuda(
const Tensor& gradOutput_,
const Tensor& input,
IntArrayRef kernel_size,
IntArrayRef stride,
IntArrayRef padding,
bool ceil_mode,
bool count_include_pad,
c10::optional<int64_t> divisor_override)
{
auto gradInput = at::zeros_like(input);
avg_pool3d_backward_out_cuda_template(
gradInput,
gradOutput_,
input,
kernel_size,
stride,
padding,
ceil_mode,
count_include_pad,
divisor_override);
return gradInput;
}
} // at::native
} // at