Did Conv2D shapes change between torch 1.4.0 and 1.6.0?

username: isnot
cooked: <p>I have some simple code that’s behaving drastically differently with torch 1.4.0 and 1.6.0: Conv2D filters are producing different shaped outputs. I can’t see anything in the release notes that would suggest that 2D convolution layers should be constructed differently between those torch releases. The following simple (and possibly buggy) code reproduces the issue:</p>
<pre><code class="lang-auto">#!/usr/bin/env python3
import torch
from torch import nn

c = 'circular'
class EmbeddingNetwork(nn.Module):
    def __init__(self):
        super().__init__()
        ksize = 5
        self.embedding = nn.Sequential(
            nn.Conv2d(10, 10, ksize, stride=1, padding=ksize-1, padding_mode=c),
            nn.ReLU())

    def forward(self, x):
        print("x.shape:", x.shape)
        print("embedding model:", self.embedding)
        result =  self.embedding(x)
        print("embedding(x).shape:", result.shape)
        return result

x = torch.zeros(1, 10, 26, 26)
embedding = EmbeddingNetwork()
embedding(x)
</code></pre>
<p>Running this with PyTorch 1.6.0, I get:</p>
<pre><code class="lang-auto">x.shape: torch.Size([1, 10, 26, 26])
embedding model: Sequential(
  (0): Conv2d(10, 10, kernel_size=(5, 5), stride=(1, 1), padding=(4, 4), padding_mode=circular)
  (1): ReLU()
)
embedding(x).shape: torch.Size([1, 10, 30, 30])
</code></pre>
<p>Running with PyTorch 1.4.0, I get:</p>
<pre><code class="lang-auto">x.shape: torch.Size([1, 10, 26, 26])
embedding model: Sequential(
  (0): Conv2d(10, 10, kernel_size=(5, 5), stride=(1, 1), padding=(4, 4), padding_mode=circular)
  (1): ReLU()
)
embedding(x).shape: torch.Size([1, 10, 26, 26])
</code></pre>

username: ptrblck
cooked: <p>I cannot install <code>1.6.0</code> currently to reproduce this issue due to the conda server issue, but at least in <code>1.7.0.dev20200728</code>, the output shape is <code>[1, 10, 30, 30]</code>. I’ll try to rerun it tomorrow.</p>

username: isnot
cooked: <p>Your 1.7.0dev result is as expected (torch 1.5.0 and above produce <code>[1, 10, 30, 30]</code>).</p>
<p>The following works for me for installing 1.4.0:</p>
<pre><code class="lang-auto">virtualenv -p python3 venv/
source venv/bin/activate
pip install torch==1.4.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
python /path/to/repro.py
</code></pre>

username: RaLo4
cooked: <p>just ran it with <code>PyTorch 1.6.0</code><br>
my output is:</p>
<pre><code class="lang-auto">x.shape: torch.Size([1, 10, 26, 26])
embedding model: Sequential(
  (0): Conv2d(10, 10, kernel_size=(5, 5), stride=(1, 1), padding=(4, 4), padding_mode=circular)
  (1): ReLU()
)
embedding(x).shape: torch.Size([1, 10, 30, 30])
</code></pre>

username: isnot
cooked: <p>Ooops, my original post was backwards. I see output shape [1, 10, 26, 26] from torch 1.4.0 and [1, 10, 30, 30] from 1.6.0. Sorry for the confusion! Will edit the original post now.</p>

username: RaLo4
cooked: <p>Ahh ok, that changes things.<br>
Mhhh seems like you are right.<br>
On my <code>pytorch 1.4.0</code> installation the above code also outputs:</p>
<pre><code class="lang-auto">x.shape: torch.Size([1, 10, 26, 26])
embedding model: Sequential(
  (0): Conv2d(10, 10, kernel_size=(5, 5), stride=(1, 1), padding=(4, 4), padding_mode=circular)
  (1): ReLU()
)
embedding(x).shape: torch.Size([1, 10, 26, 26])
</code></pre>

username: ptrblck
cooked: <p>There were a couple of issues (such as <a href="https://github.com/pytorch/pytorch/issues/29712">this one</a>) for older PyTorch versions with circular padding. Good to hear it’s working properly with the latest version. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>