forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdaptiveAveragePooling.cpp
155 lines (131 loc) · 5.21 KB
/
AdaptiveAveragePooling.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
#define TORCH_ASSERT_ONLY_METHOD_OPERATORS
#include <ATen/core/Tensor.h>
#include <ATen/native/AdaptivePooling.h>
#include <ATen/native/xnnpack/Engine.h>
#include <c10/util/irange.h>
#ifndef AT_PER_OPERATOR_HEADERS
#include <ATen/Functions.h>
#include <ATen/NativeFunctions.h>
#else
#include <ATen/ops/_adaptive_avg_pool2d.h>
#include <ATen/ops/_adaptive_avg_pool2d_backward_native.h>
#include <ATen/ops/_adaptive_avg_pool2d_native.h>
#include <ATen/ops/adaptive_avg_pool2d_native.h>
#include <ATen/ops/empty.h>
#include <ATen/ops/mkldnn_adaptive_avg_pool2d.h>
#endif
namespace at::native {
namespace {
void adaptive_avg_pool2d_out_cpu_template(
at::Tensor& output,
at::Tensor const& input,
IntArrayRef output_size)
{
TORCH_CHECK(output_size.size() == 2, "adaptive_avg_pool2d: output_size must be 2");
int64_t ndim = input.dim();
TORCH_CHECK((ndim == 3 || ndim == 4),
"adaptive_avg_pool2d(): Expected 3D or 4D tensor, but got ", input.sizes());
for (const auto i : {-2, -1}) {
TORCH_CHECK(input.size(i) > 0,
"adaptive_avg_pool2d(): Expected input to have non-zero size for non-batch dimensions, "
"but input has sizes ", input.sizes(), " with dimension ", i + ndim, " being "
"empty");
}
TORCH_CHECK(input.dtype() == output.dtype(),
"expected dtype ", input.dtype(), " for `output` but got dtype ", output.dtype());
int64_t channels = input.size(-3);
int64_t output_height = output_size[0];
int64_t output_width = output_size[1];
if (ndim == 3) {
output.resize_({channels, output_height, output_width});
} else {
int64_t nbatch = input.size(0);
output.resize_({nbatch, channels, output_height, output_width}, input.suggest_memory_format());
}
if (output.numel() == 0) {
return;
}
adaptive_avg_pool2d_kernel(kCPU, output, input, output_size);
}
Tensor& adaptive_avg_pool2d_backward_out_cpu_template(
Tensor& grad_input,
const Tensor& grad_output,
const Tensor& input)
{
int64_t ndim = grad_output.ndimension();
for (const auto i : c10::irange(1, ndim)) {
TORCH_CHECK(grad_output.size(i) > 0,
"adaptive_avg_pool2d_backward(): Expected grad_output to have non-zero size for non-batch dimensions, "
"but grad_output has sizes ", grad_output.sizes(), " with dimension ", i, " being "
"empty");
}
TORCH_CHECK((ndim == 3 || ndim == 4),
"adaptive_avg_pool2d_backward(): Expected 3D or 4D tensor, but got ", input.sizes());
TORCH_CHECK(input.dtype() == grad_output.dtype(),
"expected dtype ", input.dtype(), " for `grad_output` but got dtype ", grad_output.dtype());
TORCH_CHECK(input.dtype() == grad_input.dtype(),
"expected dtype ", input.dtype(), " for `grad_input` but got dtype ", grad_input.dtype());
grad_input.resize_(input.sizes(), input.suggest_memory_format());
grad_input.zero_();
adaptive_avg_pool2d_backward_kernel(kCPU, grad_input, grad_output);
return grad_input;
}
} // namespace
Tensor& adaptive_avg_pool2d_out_cpu(const Tensor& input,
IntArrayRef output_size,
Tensor& output)
{
adaptive_avg_pool2d_out_cpu_template(
output, input, output_size);
return output;
}
Tensor adaptive_avg_pool2d_cpu(
at::Tensor const& input,
IntArrayRef output_size)
{
auto output = at::empty({0}, input.options());
adaptive_avg_pool2d_out_cpu_template(
output, input, output_size);
return output;
}
Tensor adaptive_avg_pool2d_symint(at::Tensor const& input, SymIntArrayRef output_size) {
TORCH_CHECK(output_size.size() == 2, "adaptive_avg_pool2d: output_size must be 2");
TORCH_CHECK(
(output_size[0] >= 0 && output_size[1] >= 0),
"adaptive_avg_pool2d: elements of output_size must be greater than or equal to 0 ",
"but received {", output_size[0], ", ", output_size[1], "}");
if (input.is_mkldnn()) {
return at::mkldnn_adaptive_avg_pool2d(input, C10_AS_INTARRAYREF_SLOW(output_size));
}
if (!input.is_quantized() && output_size[0] == 1 && output_size[1] == 1 && !input.is_xpu()) {
// in this case, adaptive pooling is just computing mean over hw
// dimensions, which can be done more efficiently
#if defined(C10_MOBILE) && defined(USE_XNNPACK)
if (xnnpack::use_global_average_pool(input)) {
return xnnpack::global_average_pool(input);
}
#endif
Tensor out = input.mean({-1, -2}, /* keepdim = */ true);
if (input.suggest_memory_format() == at::MemoryFormat::ChannelsLast) {
// assert ndim == 4, since ndim = 3 doesn't give channels_last
const auto n = input.sym_size(0);
const auto c = input.sym_size(1);
out.as_strided__symint({n, c, 1, 1}, {c, 1, c, c});
}
return out;
} else {
return _adaptive_avg_pool2d_symint(input, output_size);
}
}
Tensor adaptive_avg_pool2d_backward_cpu(
const Tensor& grad_output,
const Tensor& input)
{
auto grad_input = at::empty({0}, input.options());
adaptive_avg_pool2d_backward_out_cpu_template(
grad_input, grad_output, input);
return grad_input;
}
DEFINE_DISPATCH(adaptive_avg_pool2d_kernel);
DEFINE_DISPATCH(adaptive_avg_pool2d_backward_kernel);
} // namespace at::native