Skip to content

Commit 8e98a1a

Browse files
authored
Create avg_pool1d in ATen (#8880)
* Create avg_pool1d in ATen * Put function name into check1d method
1 parent 85f4d2b commit 8e98a1a

File tree

3 files changed

+72
-49
lines changed

3 files changed

+72
-49
lines changed

aten/src/ATen/native/Pooling.cpp

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
#include "ATen/ATen.h"
2-
#include "ATen/TensorUtils.h"
3-
#include "ATen/NativeFunctions.h"
42

5-
#include <sstream>
6-
#include <vector>
3+
#include "ATen/Error.h"
4+
#include "ATen/NativeFunctions.h"
5+
#include "ATen/TensorUtils.h"
76

7+
#include <tuple>
88

99
namespace at { namespace native {
1010

11-
static void check1d(const char* name, IntList x) {
12-
if (x.size() != 1) {
13-
std::ostringstream ss;
14-
ss << "max_pool1d() argument '" << name << "' should contain one int (got "
15-
<< x.size() << ")";
16-
throw std::runtime_error(ss.str());
17-
}
11+
static void check1d(
12+
const char* function_name,
13+
const char* argument_name,
14+
IntList x) {
15+
AT_CHECK(
16+
x.size() == 1,
17+
function_name, "() argument '", argument_name,
18+
"' should contain one int (got ", x.size(), ")");
1819
}
1920

2021
Tensor adaptive_avg_pool1d(const Tensor & self, IntList output_size) {
2122
checkDim("adaptive_avg_pool1d", TensorArg(self, "self", 1), 3);
22-
check1d("output_size", output_size);
23+
check1d("adaptive_avg_pool1d", "output_size", output_size);
2324

2425
auto output = at::adaptive_avg_pool2d(
2526
self.unsqueeze(2),
@@ -30,7 +31,7 @@ Tensor adaptive_avg_pool1d(const Tensor & self, IntList output_size) {
3031

3132
std::tuple<Tensor,Tensor> adaptive_max_pool1d(const Tensor & self, IntList output_size) {
3233
checkDim("adaptive_max_pool1d", TensorArg(self, "self", 1), 3);
33-
check1d("output_size", output_size);
34+
check1d("adaptive_max_pool1d", "output_size", output_size);
3435

3536
Tensor output, indices;
3637
std::tie(output, indices) = at::adaptive_max_pool2d(
@@ -48,10 +49,10 @@ std::tuple<Tensor,Tensor> max_pool1d(
4849
stride = kernel_size;
4950
}
5051
checkDim("max_pool1d", TensorArg(self, "self", 1), 3);
51-
check1d("kernel_size", kernel_size);
52-
check1d("stride", stride);
53-
check1d("padding", padding);
54-
check1d("dilation", dilation);
52+
check1d("max_pool1d", "kernel_size", kernel_size);
53+
check1d("max_pool1d", "stride", stride);
54+
check1d("max_pool1d", "padding", padding);
55+
check1d("max_pool1d", "dilation", dilation);
5556

5657
Tensor output, indices;
5758
std::tie(output, indices) = at::max_pool2d(
@@ -65,4 +66,30 @@ std::tuple<Tensor,Tensor> max_pool1d(
6566
return std::make_tuple(output.squeeze(2), indices.squeeze(2));
6667
}
6768

68-
}} // namespace at::native
69+
Tensor avg_pool1d(
70+
const Tensor& self,
71+
IntList kernel_size,
72+
IntList stride,
73+
IntList padding,
74+
bool ceil_mode,
75+
bool count_include_pad) {
76+
if (stride.empty()) {
77+
stride = kernel_size;
78+
}
79+
checkDim("avg_pool1d", TensorArg(self, "self", 1), 3);
80+
check1d("avg_pool1d", "kernel_size", kernel_size);
81+
check1d("avg_pool1d", "stride", stride);
82+
check1d("avg_pool1d", "padding", padding);
83+
84+
auto output = at::avg_pool2d(
85+
self.unsqueeze(2),
86+
{1, kernel_size[0]},
87+
{1, stride[0]},
88+
{0, padding[0]},
89+
ceil_mode,
90+
count_include_pad);
91+
92+
return output.squeeze(2);
93+
}
94+
} // namespace native
95+
} // namespace at

aten/src/ATen/native/native_functions.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
CPU: _acos_out_cpu
7676
CUDA: _acos_out_cuda
7777

78+
- func: avg_pool1d(Tensor self, IntList[1] kernel_size, IntList[1] stride={}, IntList[1] padding=0, bool ceil_mode=false, bool count_include_pad=true) -> Tensor
79+
variants: function
80+
7881
- func: adaptive_avg_pool1d(Tensor self, IntList[1] output_size) -> Tensor
7982
variants: function
8083

torch/nn/functional.py

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -212,40 +212,33 @@ def conv_tbc(input, weight, bias, pad=0):
212212

213213

214214
# Pooling
215-
def avg_pool1d(input, kernel_size, stride=None, padding=0,
216-
ceil_mode=False, count_include_pad=True):
217-
r"""Applies a 1D average pooling over an input signal composed of several
218-
input planes.
215+
avg_pool1d = _add_docstr(torch.avg_pool1d, r"""
216+
avg_pool1d(input, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True) -> Tensor
219217
220-
See :class:`~torch.nn.AvgPool1d` for details and output shape.
218+
Applies a 1D average pooling over an input signal composed of several
219+
input planes.
221220
222-
Args:
223-
input: input tensor of shape (:math:`minibatch \times in\_channels \times iW`)
224-
kernel_size: the size of the window. Can be a single number or a
225-
tuple `(kW,)`
226-
stride: the stride of the window. Can be a single number or a tuple
227-
`(sW,)`. Default: :attr:`kernel_size`
228-
padding: implicit zero paddings on both sides of the input. Can be a
229-
single number or a tuple `(padW,)`. Default: 0
230-
ceil_mode: when True, will use `ceil` instead of `floor` to compute the
231-
output shape. Default: ``False``
232-
count_include_pad: when True, will include the zero-padding in the
233-
averaging calculation. Default: ``True``
221+
See :class:`~torch.nn.AvgPool1d` for details and output shape.
234222
235-
Example::
236-
>>> # pool of square window of size=3, stride=2
237-
>>> input = torch.tensor([[[1,2,3,4,5,6,7]]])
238-
>>> F.avg_pool1d(input, kernel_size=3, stride=2)
239-
tensor([[[ 2., 4., 6.]]])
240-
"""
241-
if input.dim() != 3:
242-
raise ValueError('expected 3D input (got {} dimensions)'
243-
.format(input.dim()))
244-
kernel_size = _single(kernel_size) + (1,)
245-
stride = _single(stride) + (1,) if stride is not None else kernel_size
246-
padding = _single(padding) + (0,)
247-
return avg_pool2d(input.unsqueeze(3), kernel_size, stride, padding,
248-
ceil_mode, count_include_pad).squeeze(3)
223+
Args:
224+
input: input tensor of shape (:math:`minibatch \times in\_channels \times iW`)
225+
kernel_size: the size of the window. Can be a single number or a
226+
tuple `(kW,)`
227+
stride: the stride of the window. Can be a single number or a tuple
228+
`(sW,)`. Default: :attr:`kernel_size`
229+
padding: implicit zero paddings on both sides of the input. Can be a
230+
single number or a tuple `(padW,)`. Default: 0
231+
ceil_mode: when True, will use `ceil` instead of `floor` to compute the
232+
output shape. Default: ``False``
233+
count_include_pad: when True, will include the zero-padding in the
234+
averaging calculation. Default: ``True``
235+
236+
Example::
237+
>>> # pool of square window of size=3, stride=2
238+
>>> input = torch.tensor([[[1,2,3,4,5,6,7]]])
239+
>>> F.avg_pool1d(input, kernel_size=3, stride=2)
240+
tensor([[[ 2., 4., 6.]]])
241+
""")
249242

250243

251244
avg_pool2d = _add_docstr(torch._C._nn.avg_pool2d, r"""

0 commit comments

Comments
 (0)