TypeError: forward() got an unexpected keyword argument 'inplace'

username: Xanthan
cooked: <p>I want to make a model (which takes batches of images as input) in pytorch and there seems to be this unexpected issue. Pytorch version: 1.7.1 Python version: 3.8.12</p>
<p>Code to reproduce:</p>
<pre><code class="lang-auto">class ABC(nn.Module):
  def __init__(self):
    super(ABC, self).__init__()
    self.out_channels = 64

    self.conv1_1 = nn.Conv2d(in_channels = 3, out_channels = self.out_channels, kernel_size=3, padding=1)
    self.conv1_2 = nn.Conv2d(in_channels = self.out_channels, out_channels = self.out_channels, kernel_size=3, padding=1)
    self.relu = nn.ReLU(inplace=True)
 
  def forward(self, x):
    x = self.conv1_1(x)
    x = self.relu(x)
    x = self.conv1_2(x)
    x = self.relu(x)
    return x

model = ABC()
output = model(images)
</code></pre>
<p>Traceback call:</p>
<pre><code class="lang-auto">TypeError                                 Traceback (most recent call last)
&lt;ipython-input-15-0514770d2b3f&gt; in &lt;module&gt;
----&gt; 1 model(images)

D:\Anaconda\envs\ffconvolution\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--&gt; 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

&lt;ipython-input-13-d067faa2b8ec&gt; in forward(self, x)
    109     def forward(self, x):
    110         x = self.conv1_1(x)
--&gt; 111         x = self.relu(x)
    112         x = self.conv1_2(x)
    113         x = self.relu(x)

D:\Anaconda\envs\ffconvolution\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--&gt; 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

D:\Anaconda\envs\ffconvolution\lib\site-packages\torch\nn\modules\activation.py in forward(self, input)
    100 
    101     def forward(self, input: Tensor) -&gt; Tensor:
--&gt; 102         return F.relu(input, inplace=self.inplace)
    103 
    104     def extra_repr(self) -&gt; str:

D:\Anaconda\envs\ffconvolution\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--&gt; 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

TypeError: forward() got an unexpected keyword argument 'inplace'
</code></pre>
<p>I’m not sure why is ‘inplace’ is coming in the error, it is just an argument in nn.ReLU , it is very unexpected.</p>
<p>Any fixes?</p>

username: ptrblck
cooked: <p>I cannot reproduce the issue in a recent nightly or <code>1.7.0</code> so unsure where the error might be coming from since your usage looks correct.<br>
Are you sure you are using this model definition or could you be mixing up model definitions?</p>

username: Xanthan
cooked: <p>No, I’m not mixing up model definitions. On my end, this piece of code is able to reproduce the issue. I think there is a problem in torch library, as this error doesn’t come in pytorch version 1.10 and python 3.9.6 . I think I will try delete and make a new virtual environment again and see if it still reproduces the same problem</p>
<p>Correction: till yesterday there was no issue when I tried with torch 1.10 and today the same error is replicating.</p>

username: Xanthan
cooked: <p>Reinstalling the libraries didn’t work.</p>
<p>So, I replaced, <code>nn.ReLU</code> with <code>nn.functional.relu_</code> and it seems to working fine without the error. But what would be the repercussions of making this change?</p>
<p>Also, I don’t think I will able to replace <code>nn.functional.relu_</code> everywhere as I’m referencing <code>self.relu =  nn.ReLU(inplace=True)</code>  in many places and calling them later.</p>

username: ptrblck
cooked: <p>Could you post a fully executable code snippet as well as the output of <code>python -m torch.utils.collect_env</code> as I’m not able to reproduce the issue, please?</p>

username: Xanthan
cooked: <p>Hello, so I have fixed the error, it was this issue:</p>
<aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="144770">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>model definition or could you be mixing up model definitions</p>
</blockquote>
</aside>
<p>I didn’t notice it before as it wasn’t occurring in the reproduceable code that I shared.</p>
<p>Thanks!</p>