Skip to content

Commit

Permalink
Fix max_pool2d with ceil_mode bug
Browse files Browse the repository at this point in the history
ghstack-source-id: 61d608ceac4abd4cd329fb92dc61ea4c2757cd00
Pull Request resolved: #46558
  • Loading branch information
heitorschueroff committed Oct 21, 2020
1 parent 8328630 commit 3d7e824
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
5 changes: 3 additions & 2 deletions aten/src/ATen/native/Pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ static inline T pooling_output_shape_pad_lr(
T outputSize = div_rtn<T>(
inputSize + pad_l + pad_r - dilation * (kernelSize - 1) - 1 +
(ceil_mode ? stride - 1 : 0), stride) + 1;
if (pad_l) {
if (ceil_mode) {
// ensure that the last pooling starts inside the image
// needed to avoid problems in ceil mode
if ((outputSize - 1) * stride >= inputSize + pad_l)
if ((outputSize - 1) * stride >= inputSize + pad_l + pad_r) {
--outputSize;
}
}
return outputSize;
}
Expand Down
6 changes: 3 additions & 3 deletions test/quantization/test_quantized_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ def pool_output_shape(input_size, kernel_size, padding, stride,
output_size = (
(input_size + 2 * padding - dilation * (kernel_size - 1) - 1
+ (stride - 1 if ceiling_mode else 0)) // stride + 1)
if (padding > 0 and
((output_size - 1) * stride >= input_size + padding)):
output_size += 1
if (ceiling_mode and
((output_size - 1) * stride >= input_size + 2 * padding)):
output_size -= 1
return output_size

"""
Expand Down
9 changes: 9 additions & 0 deletions test/test_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -10484,6 +10484,15 @@ def test(nonlinearity, *args, **kwargs):
test('threshold', 3, 2)
test('threshold', 3, 2, inplace=True)

def test_pooling_shape(self, device):
def check(expected_out_shape, sizes, *args, **kwargs):
t = torch.randn(sizes, device=device)
self.assertEqual(torch.nn.functional.max_pool3d(t, *args, **kwargs).shape, expected_out_shape)

check((1, 1, 3, 4), (1, 1, 6, 7), kernel_size=1, stride=2, padding=0, ceil_mode=True)
check((1, 2, 3, 3), (1, 3, 4, 5), kernel_size=2, stride=2, padding=1, ceil_mode=False)
check((1, 3, 3, 4), (1, 3, 4, 5), kernel_size=2, stride=2, padding=1, ceil_mode=True)

@onlyOnCPUAndCUDA # TODO: fix on XLA
def test_adaptive_avg_pool2d_output_size_one(self, device):
def helper(size, memory_format):
Expand Down

0 comments on commit 3d7e824

Please sign in to comment.