<h2 dir="auto">Issue description</h2>
<p dir="auto">I just upgraded my Pytorch version from 0.3.0 to v0.4.0, and I'm running into the following issue.</p>
<p dir="auto">I want to freeze the parameters of a layer (i.e. set <code class="notranslate">requires_grad to False</code> for that layer) and train. This worked fine of v0.3.0 but now, I get the following error at the line <code class="notranslate">loss.backward()</code>:<br>
<code class="notranslate">RuntimeError: backward_input can only be called in training mode</code></p>
<p dir="auto">Based on what I found online, I added <code class="notranslate">model.train()</code> to the code, but now I get the following error on <code class="notranslate">loss.backward()</code>:<br>
<code class="notranslate">RuntimeError: inconsistent range for TensorList output</code></p>
<p dir="auto">When I comment out the layer freezing, things work fine. It seems that model.train() assumes <code class="notranslate">requires.grad=True</code> for all the parameters. How can I make training work properly with freezing?</p>
<p dir="auto">Giving the code example below.</p>
<h2 dir="auto">Code example</h2>
<p dir="auto">The following code ran fine on v0.3.0</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="for name, param in model.named_parameters():
    if name == 'encoder.embedding.weight':
        param.requires_grad = False
loss = model.forward()
loss.backward()"><pre class="notranslate"><code class="notranslate">for name, param in model.named_parameters():
    if name == 'encoder.embedding.weight':
        param.requires_grad = False
loss = model.forward()
loss.backward()
</code></pre></div>
<p dir="auto">Running it on v0.4.0 yields to following error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="  File &quot;/mnt/fs_default/Conversational-Agents/model.py&quot;, line 86, in train_batch
    loss.backward()
  File &quot;/usr/local/lib/python3.5/dist-packages/torch/tensor.py&quot;, line 93, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File &quot;/usr/local/lib/python3.5/dist-packages/torch/autograd/__init__.py&quot;, line 89, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: backward_input can only be called in training mode"><pre class="notranslate"><code class="notranslate">  File "/mnt/fs_default/Conversational-Agents/model.py", line 86, in train_batch
    loss.backward()
  File "/usr/local/lib/python3.5/dist-packages/torch/tensor.py", line 93, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "/usr/local/lib/python3.5/dist-packages/torch/autograd/__init__.py", line 89, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: backward_input can only be called in training mode
</code></pre></div>
<p dir="auto">So then I added <code class="notranslate">model.train()</code> to my code as follows:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="for name, param in model.named_parameters():
    if name == 'encoder.embedding.weight':
        param.requires_grad = False
model.train()
loss = model.forward()
loss.backward()"><pre class="notranslate"><code class="notranslate">for name, param in model.named_parameters():
    if name == 'encoder.embedding.weight':
        param.requires_grad = False
model.train()
loss = model.forward()
loss.backward()
</code></pre></div>
<p dir="auto">but now I get the error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="  File &quot;/mnt/fs_default/kaselby/Conversational-Agents/model.py&quot;, line 88, in train_batch
    loss.backward()
  File &quot;/usr/local/lib/python3.5/dist-packages/torch/tensor.py&quot;, line 93, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File &quot;/usr/local/lib/python3.5/dist-packages/torch/autograd/__init__.py&quot;, line 89, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: inconsistent range for TensorList output"><pre class="notranslate"><code class="notranslate">  File "/mnt/fs_default/kaselby/Conversational-Agents/model.py", line 88, in train_batch
    loss.backward()
  File "/usr/local/lib/python3.5/dist-packages/torch/tensor.py", line 93, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "/usr/local/lib/python3.5/dist-packages/torch/autograd/__init__.py", line 89, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: inconsistent range for TensorList output
</code></pre></div>
<p dir="auto">Is this a bug? If not, can someone tell me how to make layer freezing work properly with model.train()?</p>
<h2 dir="auto">System Info</h2>
<p dir="auto">PyTorch version: 0.4.0<br>
Is debug build: No<br>
CUDA used to build PyTorch: 9.0.176</p>
<p dir="auto">OS: Ubuntu 16.04.3 LTS<br>
GCC version: (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609<br>
CMake version: version 3.5.1</p>
<p dir="auto">Python version: 3.5<br>
Is CUDA available: Yes<br>
CUDA runtime version: 9.0.176<br>
GPU models and configuration:<br>
GPU 0: Tesla P100-PCIE-16GB<br>
GPU 1: Tesla P100-PCIE-16GB<br>
GPU 2: Tesla P100-PCIE-16GB<br>
GPU 3: Tesla P100-PCIE-16GB</p>
<p dir="auto">Nvidia driver version: 384.111<br>
cuDNN version: Could not collect</p>
<p dir="auto">Versions of relevant libraries:<br>
[pip3] msgpack-numpy (0.4.1)<br>
[pip3] numpy (1.13.3)<br>
[pip3] torch (0.4.0)<br>
[pip3] torchtext (0.2.3)<br>
[pip3] torchvision (0.1.9)<br>
[conda] Could not collect</p>
<h2 dir="auto">Issue description</h2>
<p dir="auto">The general situation is that we have a pretrained Language Model, and during a first phase we only want to train the new embedding layer we added before fine-tuning the whole thing. This worked fine in 0.3 but now sends an error message during back propagation. Minimal reproduction is to just create a simple model with a linear layer and an LSTM, freeze this second layer (by applying require_grads=False to its parameters) and try to compute a back-propagation.</p>
<h2 dir="auto">Code example</h2>
<p dir="auto">See <a href="https://github.com/sgugger/Deep-Learning/blob/master/Bug%20with%20frozen%20LSTM%20layer.ipynb">here</a></p>
<h2 dir="auto">System Info</h2>
<p dir="auto">PyTorch version: 0.4.0<br>
Is debug build: No<br>
CUDA used to build PyTorch: 9.0                                                                                                                                                                                                                 OS: Microsoft Windows 10 Home<br>
GCC version: Could not collect<br>
CMake version: Could not collect                                                                                                                                                                                                                Python version: 3.6<br>
Is CUDA available: Yes<br>
CUDA runtime version: 9.1.85<br>
GPU models and configuration: Could not collect<br>
Nvidia driver version: Could not collect<br>
cuDNN version: Could not collect                                                                                                                                                                                                                Versions of relevant libraries:<br>
[pip] Could not collect<br>
[conda] Could not collect</p>
<p dir="auto">Pytorch was installed with conda, the bug also appears on my linux instances.</p>
<p dir="auto">Thanks for your help!</p>