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

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

Code to reproduce:

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)

Traceback call:

TypeError                                 Traceback (most recent call last)
<ipython-input-15-0514770d2b3f> in <module>
----> 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:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

<ipython-input-13-d067faa2b8ec> in forward(self, x)
    109     def forward(self, x):
    110         x = self.conv1_1(x)
--> 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:
--> 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) -> Tensor:
--> 102         return F.relu(input, inplace=self.inplace)
    103 
    104     def extra_repr(self) -> 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:
--> 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'

I’m not sure why is ‘inplace’ is coming in the error, it is just an argument in nn.ReLU , it is very unexpected.

Any fixes?

I cannot reproduce the issue in a recent nightly or 1.7.0 so unsure where the error might be coming from since your usage looks correct.
Are you sure you are using this model definition or could you be mixing up model definitions?

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

Correction: till yesterday there was no issue when I tried with torch 1.10 and today the same error is replicating.

Reinstalling the libraries didn’t work.

So, I replaced, nn.ReLU with nn.functional.relu_ and it seems to working fine without the error. But what would be the repercussions of making this change?

Also, I don’t think I will able to replace nn.functional.relu_ everywhere as I’m referencing self.relu = nn.ReLU(inplace=True) in many places and calling them later.

Could you post a fully executable code snippet as well as the output of python -m torch.utils.collect_env as I’m not able to reproduce the issue, please?

Hello, so I have fixed the error, it was this issue:

I didn’t notice it before as it wasn’t occurring in the reproduceable code that I shared.

Thanks!