Conv2d error with `padding='same'` and `padding_mode='reflect'`


I’ve got an error in a convolution using the options padding='same' and padding_mode='reflect'.

Given a kernel of size 3, stride=1, and dilation=1, I was expecting those two convolutions to be equivalent:

conv1 = torch.nn.Conv2d(2, 2, 3, padding = 'same', padding_mode = 'reflect')

conv2 = torch.nn.Conv2d(2, 2, 3, padding = 1, padding_mode = 'reflect')

but the former one raise an error, while the latter work as intended:

>>> a = torch.randn(1,2,4,4)
>>> conv = torch.nn.Conv2d(2, 2, 3, padding = 'same', padding_mode = 'reflect')
>>> conv(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/", line 399, in forward
    return self._conv_forward(input, self.weight, self.bias)
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/", line 392, in _conv_forward
    return F.conv2d(F.pad(input, self._reversed_padding_repeated_twice, mode=self.padding_mode),
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/", line 4012, in _pad
    assert len(pad) == 4, "4D tensors expect 4 values for padding"

Concerning the mode same the documentation for Conv2D only states:

padding='same' pads the input so the output has the shape as the input. However, this mode doesn’t support any stride values other than 1.

Could you explain me why this error is triggered? Am I missing something in the documentation?

Best regards,

Your code works for me in 1.11.0.dev20211101+cu113. Which PyTorch version are you using?
If an older one, could you update to the latest nightly binary and check if this issue might have been already fixed?

Thanks for your comment. I did use an older pytorch, version 1.9.0a0+df837d0 from the NVIDIA container image for PyTorch release 21.03.

I just pulled the last nvidia docker container (PyTorch Release 21.11) with pytorch version 1.11.0a0+b6df043. The problem is now solved, the previous code snippet is working.

It’s not 1st time I’ve got those kind of issues due to out-dated container or due to container with not stable version of pytorch (which seem to be usual with nvidia pytorch containers, i don’t know why). Maybe I should switch to the pytorch images from Docker Hub.

Thanks for your help,

We update the submodules usually monthly (pytorch, torchvision, torchtext etc.) to provide the latest features as well as fixes, and to send it through our QA process so that issues can be found quickly before the next stable release is done.
If you prefer a stable release, you might indeed want to use the docker hub containers.

1 Like