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

username: Azerus
cooked: <p>Hello,</p>
<p>I’ve got an error in a convolution using the options <code>padding='same'</code> and <code>padding_mode='reflect'</code>.</p>
<p>Given a kernel of size 3, stride=1, and dilation=1, I was expecting those two convolutions to be equivalent:</p>
<pre><code class="lang-auto">conv1 = torch.nn.Conv2d(2, 2, 3, padding = 'same', padding_mode = 'reflect')

conv2 = torch.nn.Conv2d(2, 2, 3, padding = 1, padding_mode = 'reflect')
</code></pre>
<p>but the former one raise an error, while the latter work as intended:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; a = torch.randn(1,2,4,4)
&gt;&gt;&gt; conv = torch.nn.Conv2d(2, 2, 3, padding = 'same', padding_mode = 'reflect')
&gt;&gt;&gt; conv(a)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 399, in forward
    return self._conv_forward(input, self.weight, self.bias)
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/conv.py", 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/functional.py", line 4012, in _pad
    assert len(pad) == 4, "4D tensors expect 4 values for padding"
</code></pre>
<p>Concerning the mode <code>same</code> the documentation for <code>Conv2D</code> only states:</p>
<blockquote>
<p><code>padding='same'</code> pads the input so the output has the shape as the input. However, this mode doesn’t support any stride values other than 1.</p>
</blockquote>
<p>Could you explain me why this error is triggered? Am I missing something in the documentation?</p>
<p>Best regards,<br>
Thomas</p>

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

username: Azerus
cooked: <p>Thanks for your comment. I did use an older pytorch, version 1.9.0a0+df837d0 from the <a href="https://docs.nvidia.com/deeplearning/frameworks/pytorch-release-notes/rel_21-03.html#rel_21-03" rel="noopener nofollow ugc">NVIDIA container image for PyTorch release 21.03</a>.</p>
<p>I just pulled the last nvidia docker container (<a href="https://docs.nvidia.com/deeplearning/frameworks/pytorch-release-notes/rel_21-11.html#rel_21-11" rel="noopener nofollow ugc">PyTorch Release 21.11</a>)  with pytorch version 1.11.0a0+b6df043. The problem is now solved, the previous code snippet is working.</p>
<p>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 <a href="https://hub.docker.com/r/pytorch/pytorch/tags" class="inline-onebox" rel="noopener nofollow ugc">Docker Hub</a>.</p>
<p>Thanks for your help,<br>
Thomas</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="Azerus" data-post="3" data-topic="139291">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/azerus/48/33745_2.png" class="avatar"> Azerus:</div>
<blockquote>
<p>which seem to be usual with nvidia pytorch containers, i don’t know why</p>
</blockquote>
</aside>
<p>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.<br>
If you prefer a stable release, you might indeed want to use the docker hub containers.</p>