Skip to content

Commit

Permalink
Fix error messages thrown when the padding size is not valid (#50135)
Browse files Browse the repository at this point in the history
Summary:
Hi, I changed error messages so that they correspond to the actual implementation.
Acording to the implementation, half of kernel size is valid as padding size.

This is minor but an example that the padding size is exactly equal to the half of kernel size,

Input: 5 x 5
Kernel: 4 x 4
Stride: 4
Padding: 2
==> Output: 2 x 2

You don't get the error in the above case, like following:
```python
import torch
import torch.nn as nn

# no error
input = torch.randn(1, 1, 5, 5)
pool = nn.MaxPool2d(4, 4, padding=2)
print(pool(input).shape)
# >>> torch.Size([1, 1, 2, 2])
```

You get the error when you set the padding size larger then half of kernel size like:
```python
# it raises error
input = torch.randn(1, 1, 5, 5)
pool = nn.MaxPool2d(4, 4, padding=3)
print(pool(input).shape)
```

The error message is:
```
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-43-2b09d1c5d79a> in <module>()
      1 input = torch.randn(1, 1, 5, 5)
      2 pool = nn.MaxPool2d(4, 4, padding=3)
----> 3 print(pool(input).shape)

3 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in _max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode, return_indices)
    584         stride = torch.jit.annotate(List[int], [])
    585     return torch.max_pool2d(
--> 586         input, kernel_size, stride, padding, dilation, ceil_mode)
    587
    588 max_pool2d = boolean_dispatch(

RuntimeError: pad should be smaller than half of kernel size, but got padW = 3, padH = 3, kW = 4, kH = 4
```

Thanks in advance.

Pull Request resolved: #50135

Reviewed By: hl475

Differential Revision: D25815337

Pulled By: H-Huang

fbshipit-source-id: 98142296fa6e6849d2e1407d2c1d4e3c2f83076d
  • Loading branch information
reouno authored and facebook-github-bot committed Jan 7, 2021
1 parent 11cdb91 commit 968ad47
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions aten/src/ATen/native/Pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pool2d_shape_check(
TORCH_CHECK(input.numel() > 0 && (ndim == 3 || ndim == 4),
"non-empty 3D or 4D input tensor expected but got ndim: ", ndim);
TORCH_CHECK(kW/2 >= padW && kH/2 >= padH,
"pad should be smaller than half of kernel size, but got ",
"pad should be smaller than or equal to half of kernel size, but got ",
"padW = ", padW, ", padH = ", padH, ", kW = ", kW, ", kH = ", kH);

TORCH_CHECK(outputWidth >= 1 && outputHeight >= 1,
Expand Down Expand Up @@ -172,7 +172,7 @@ pool3d_shape_check(
}

TORCH_CHECK(kT/2 >= pT && kW/2 >= pW && kH/2 >= pH,
"pad should be smaller than half of kernel size, but got "
"pad should be smaller than or equal to half of kernel size, but got "
"kT: ", kT, " kW: ", kW, " kH: ", kH, " padT: ", pT, " padW: ", pW, " padH: ", pH);

TORCH_CHECK(otime >= 1 && owidth >= 1 && oheight >= 1,
Expand Down

0 comments on commit 968ad47

Please sign in to comment.