forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdaptiveMaxPooling3d.cpp
438 lines (393 loc) · 12.5 KB
/
AdaptiveMaxPooling3d.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
#include <ATen/ATen.h>
#include <ATen/NativeFunctions.h>
#include <ATen/Parallel.h>
#include <tuple>
namespace at {
namespace meta {
TORCH_META_FUNC(adaptive_max_pool3d) (const Tensor& input, IntArrayRef output_size) {
for (int64_t i = 0; i < input.ndimension(); i++) {
TORCH_CHECK(
input.size(i) > 0,
"adaptive_max_pool3d: expected input to have non-empty spatial dimensions, "
"but input has sizes ",
input.sizes(),
" with dimension ",
i,
" being "
"empty");
}
TORCH_CHECK(
(input.ndimension() == 4 || input.ndimension() == 5),
"non-empty 4D or 5D (batch mode) tensor expected for input");
TORCH_CHECK(
output_size.size() == 3,
"adaptive_max_pool3d: internal error: output_size.size() must be 3");
int dimD = 0;
int64_t sizeB = 1;
int64_t sizeD = 0;
if (input.ndimension() == 5) {
sizeB = input.size(0);
dimD++;
}
/* sizes */
sizeD = input.size(dimD);
int64_t osizeT = output_size[0];
int64_t osizeH = output_size[1];
int64_t osizeW = output_size[2];
/* resize output */
if (input.ndimension() == 4) {
set_output(0, {sizeD, osizeT, osizeH, osizeW}, input.options());
/* indices will contain max input locations for each output point */
set_output(1, {sizeD, osizeT, osizeH, osizeW}, input.options().dtype(kLong));
} else {
set_output(0, {sizeB, sizeD, osizeT, osizeH, osizeW}, input.options());
/* indices will contain max input locations for each output point */
set_output(1, {sizeB, sizeD, osizeT, osizeH, osizeW}, input.options().dtype(kLong));
}
}
TORCH_META_FUNC(adaptive_max_pool3d_backward)
(const Tensor& gradOutput, const Tensor& input, const Tensor& indices) {
set_output(0, input.sizes(), input.options());
}
} // namespace meta
namespace native {
namespace {
inline int start_index(int a, int b, int c) {
return (int)std::floor((float)(a * c) / b);
}
inline int end_index(int a, int b, int c) {
return (int)std::ceil((float)((a + 1) * c) / b);
}
// #define START_IND(a,b,c) a * c / b
// #define END_IND(a,b,c) (a + 1) * c / b + ((a + 1) * c % b > 0)?1:0
// 5d tensor B x D x T x H x W
template <typename scalar_t>
static void adaptive_max_pool3d_single_out_frame(
scalar_t *input_p,
scalar_t *output_p,
int64_t *ind_p,
int64_t sizeD,
int64_t isizeT,
int64_t isizeH,
int64_t isizeW,
int64_t osizeT,
int64_t osizeH,
int64_t osizeW,
int64_t istrideD,
int64_t istrideT,
int64_t istrideH,
int64_t istrideW)
{
at::parallel_for(0, sizeD, 0, [&](int64_t start, int64_t end) {
for (auto d = start; d < end; d++)
{
/* loop over output */
int64_t ot, oh, ow;
for(ot = 0; ot < osizeT; ot++)
{
int64_t istartT = start_index(ot, osizeT, isizeT);
int64_t iendT = end_index(ot, osizeT, isizeT);
int64_t kT = iendT - istartT;
for(oh = 0; oh < osizeH; oh++)
{
int64_t istartH = start_index(oh, osizeH, isizeH);
int64_t iendH = end_index(oh, osizeH, isizeH);
int64_t kH = iendH - istartH;
for(ow = 0; ow < osizeW; ow++)
{
int64_t istartW = start_index(ow, osizeW, isizeW);
int64_t iendW = end_index(ow, osizeW, isizeW);
int64_t kW = iendW - istartW;
/* local pointers */
scalar_t *ip = input_p + d*istrideD + istartT *istrideT + istartH*istrideH + istartW*istrideW;
scalar_t *op = output_p + d*osizeT*osizeH*osizeW + ot*osizeH*osizeW + oh*osizeW + ow;
int64_t *indp = ind_p + d*osizeT*osizeH*osizeW + ot*osizeH*osizeW + oh*osizeW + ow;
/* compute local max: */
int64_t it = 0, ih = 0, iw = 0;
int64_t maxindex = (it+istartT)*isizeH*isizeW + (ih+istartH)*isizeW + (iw+istartW);
scalar_t maxval = -std::numeric_limits<scalar_t>::infinity();
for(it = 0; it < kT; it++)
{
for(ih = 0; ih < kH; ih++)
{
for(iw = 0; iw < kW; iw++)
{
scalar_t val = *(ip + it*istrideT + ih*istrideH + iw*istrideW);
if ((val > maxval) || std::isnan(val))
{
maxval = val;
maxindex = (it+istartT)*isizeH*isizeW + (ih+istartH)*isizeW + (iw+istartW);
}
}
}
}
/* set output to local max */
*op = maxval;
/* store location of max */
*indp = maxindex;
}
}
}
}
});
}
template <typename scalar_t>
static void adaptive_max_pool3d_out_frame(
scalar_t *input_data,
scalar_t *output_data,
int64_t *indices_data,
int64_t sizeB,
int64_t sizeD,
int64_t isizeT,
int64_t isizeH,
int64_t isizeW,
int64_t osizeT,
int64_t osizeH,
int64_t osizeW,
int64_t istrideB,
int64_t istrideD,
int64_t istrideT,
int64_t istrideH,
int64_t istrideW)
{
at::parallel_for(0, sizeB, 0, [&](int64_t start, int64_t end) {
for (auto b = start; b < end; b++)
{
adaptive_max_pool3d_single_out_frame<scalar_t>(input_data+b*istrideB, output_data+b*sizeD*osizeT*osizeH*osizeW,
indices_data+b*sizeD*osizeT*osizeH*osizeW,
sizeD,
isizeT, isizeH, isizeW,
osizeT, osizeH, osizeW,
istrideD, istrideT,
istrideH, istrideW);
}
});
}
template <typename scalar_t>
static void adaptive_max_pool3d_backward_single_out_frame(
scalar_t *gradInput_p,
scalar_t *gradOutput_p,
int64_t *ind_p,
int64_t sizeD,
int64_t isizeT,
int64_t isizeH,
int64_t isizeW,
int64_t osizeT,
int64_t osizeH,
int64_t osizeW)
{
at::parallel_for(0, sizeD, 0, [&](int64_t start, int64_t end) {
for (auto d = start; d < end; d++)
{
scalar_t *gradInput_p_d = gradInput_p + d*isizeT*isizeH*isizeW;
scalar_t *gradOutput_p_d = gradOutput_p + d*osizeT*osizeH*osizeW;
int64_t *ind_p_d = ind_p + d*osizeT*osizeH*osizeW;
/* calculate max points */
int64_t ot, oh, ow;
for(ot = 0; ot < osizeT; ot++)
{
for(oh = 0; oh < osizeH; oh++)
{
for(ow = 0; ow < osizeW; ow++)
{
/* retrieve position of max */
int64_t maxp = ind_p_d[ot*osizeH*osizeW + oh*osizeW + ow];
/* update gradient */
gradInput_p_d[maxp] += gradOutput_p_d[ot*osizeH*osizeW + oh*osizeW + ow];
}
}
}
}
});
}
template <typename scalar_t>
static void adaptive_max_pool3d_backward_out_frame(
scalar_t *gradInput_data,
scalar_t *gradOutput_data,
int64_t *indices_data,
int64_t sizeB,
int64_t sizeD,
int64_t isizeT,
int64_t isizeH,
int64_t isizeW,
int64_t osizeT,
int64_t osizeH,
int64_t osizeW)
{
at::parallel_for(0, sizeB, 0, [&](int64_t start, int64_t end) {
for (auto b = start; b < end; b++)
{
adaptive_max_pool3d_backward_single_out_frame<scalar_t>(gradInput_data+b*sizeD*isizeT*isizeH*isizeW, gradOutput_data+b*sizeD*osizeT*osizeH*osizeW,
indices_data+b*sizeD*osizeT*osizeH*osizeW,
sizeD,
isizeT, isizeH, isizeW,
osizeT, osizeH, osizeW);
}
});
}
} // namespace
TORCH_IMPL_FUNC(adaptive_max_pool3d_out_cpu)
(const Tensor& input, IntArrayRef output_size, const Tensor& output, const Tensor& indices) {
int dimD = 0;
int dimT = 1;
int dimH = 2;
int dimW = 3;
int64_t sizeB = 1;
int64_t sizeD = 0;
int64_t isizeT = 0;
int64_t isizeH = 0;
int64_t isizeW = 0;
int64_t istrideB = 0;
int64_t istrideD = 0;
int64_t istrideT = 0;
int64_t istrideH = 0;
int64_t istrideW = 0;
if (input.ndimension() == 5) {
istrideB = input.stride(0);
sizeB = input.size(0);
dimD++;
dimT++;
dimH++;
dimW++;
}
/* sizes */
sizeD = input.size(dimD);
isizeT = input.size(dimT);
isizeH = input.size(dimH);
isizeW = input.size(dimW);
/* strides */
istrideD = input.stride(dimD);
istrideT = input.stride(dimT);
istrideH = input.stride(dimH);
istrideW = input.stride(dimW);
int64_t osizeT = output_size[0];
int64_t osizeH = output_size[1];
int64_t osizeW = output_size[2];
if (input.ndimension() == 4) {
AT_DISPATCH_FLOATING_TYPES(
input.scalar_type(), "adaptive_max_pool3d_cpu", [&] {
auto input_data = input.data_ptr<scalar_t>();
auto output_data = output.data_ptr<scalar_t>();
auto indices_data = indices.data_ptr<int64_t>();
adaptive_max_pool3d_single_out_frame<scalar_t>(
input_data,
output_data,
indices_data,
sizeD,
isizeT,
isizeH,
isizeW,
osizeT,
osizeH,
osizeW,
istrideD,
istrideT,
istrideH,
istrideW);
});
} else {
AT_DISPATCH_FLOATING_TYPES(
input.scalar_type(), "adaptive_max_pool3d_cpu", [&] {
auto input_data = input.data_ptr<scalar_t>();
auto output_data = output.data_ptr<scalar_t>();
auto indices_data = indices.data_ptr<int64_t>();
adaptive_max_pool3d_out_frame<scalar_t>(
input_data,
output_data,
indices_data,
sizeB,
sizeD,
isizeT,
isizeH,
isizeW,
osizeT,
osizeH,
osizeW,
istrideB,
istrideD,
istrideT,
istrideH,
istrideW);
});
}
}
TORCH_IMPL_FUNC(adaptive_max_pool3d_backward_out_cpu)
(const Tensor& gradOutput,
const Tensor& input,
const Tensor& indices,
const Tensor& gradInput) {
int dimD = 0;
int dimT = 1;
int dimH = 2;
int dimW = 3;
int64_t sizeB = 1;
int64_t sizeD;
int64_t isizeT;
int64_t isizeH;
int64_t isizeW;
int64_t osizeT;
int64_t osizeH;
int64_t osizeW;
/* get contiguous gradOutput */
auto gradOutput_ = gradOutput.contiguous();
/* resize */
gradInput.zero_();
if (input.ndimension() == 5) {
sizeB = input.size(0);
dimD++;
dimT++;
dimH++;
dimW++;
}
/* sizes */
sizeD = input.size(dimD);
isizeT = input.size(dimT);
isizeH = input.size(dimH);
isizeW = input.size(dimW);
osizeT = gradOutput_.size(dimT);
osizeH = gradOutput_.size(dimH);
osizeW = gradOutput_.size(dimW);
/* backprop */
if (input.ndimension() == 4) {
AT_DISPATCH_FLOATING_TYPES(
input.scalar_type(), "adaptive_max_pool3d_backward", [&] {
/* get raw pointers */
scalar_t* gradInput_data = gradInput.data_ptr<scalar_t>();
scalar_t* gradOutput_data = gradOutput_.data_ptr<scalar_t>();
int64_t* indices_data = indices.data_ptr<int64_t>();
adaptive_max_pool3d_backward_single_out_frame<scalar_t>(
gradInput_data,
gradOutput_data,
indices_data,
sizeD,
isizeT,
isizeH,
isizeW,
osizeT,
osizeH,
osizeW);
});
} else {
AT_DISPATCH_FLOATING_TYPES(
input.scalar_type(), "adaptive_max_pool3d_backward", [&] {
/* get raw pointers */
scalar_t* gradInput_data = gradInput.data_ptr<scalar_t>();
scalar_t* gradOutput_data = gradOutput_.data_ptr<scalar_t>();
int64_t* indices_data = indices.data_ptr<int64_t>();
adaptive_max_pool3d_backward_out_frame<scalar_t>(
gradInput_data,
gradOutput_data,
indices_data,
sizeB,
sizeD,
isizeT,
isizeH,
isizeW,
osizeT,
osizeH,
osizeW);
});
}
}
} // at::native
} // at