Pytorch cudnn RNN backward can only be called in training mode

Lewis_Liu: <p>The Error is ’ cudnn RNN backward can only be called in training mode.’</p>
<p>I’m doing a DDPG with LSTM network. Part of the code is shown blow.<br>
It works fine on CPU but when run on GPU, it reports the error.<br>
But! As you can see that I’ve ready set the critic network to train mode, why it still reports error literally four lines after that??</p>
<pre><code class="lang-auto">    self.target_critic.eval()
    self.critic.eval()

    target_actions=self.target_actor.forward(new_state)
    critic_value_=self.target_critic.forward(new_state,target_actions)
    critic_value=self.critic.forward(state,action)

    target=[]
    for j in range(self.batch_size):
        target.append(reward[j]+self.gamma*critic_value_[j]*done[j])
    target=T.tensor(target).to(self.critic.device)
    target=target.view(self.batch_size,1)
    
    self.critic.train()
    self.critic.optimizer.zero_grad()
    critic_loss=F.mse_loss(target,critic_value)
    critic_loss.backward()
    self.critic.optimizer.step()
</code></pre>
<p>I checked the .training boolean value and confirmed that all my networks are in training mode. Yet it still reports the issue</p>

emcp: <p>When you call .eval() on a model… it’s telling the model it is not going to be training anymore… as I understand anyway.</p>
<p>I am not sure you can switch from train mode to eval mode like your code is demonstrating… and I’d follow the lead of most examples and separate the two loops entirely into separate files</p>

Lewis_Liu: <p>That’s bad…so based on what you said, it’s impossible to use RNN in such application(DDPG)? I couldn’t come up with an alternative…</p>

emcp: <p>I would look at a basic RNN example before you conclude that…</p>
<p>im no expert, just chiming in regarding .eval()</p>
<p><a href="https://pytorch.org/tutorials/intermediate/char_rnn_classification_tutorial.html" class="onebox" target="_blank" rel="nofollow noopener">https://pytorch.org/tutorials/intermediate/char_rnn_classification_tutorial.html</a></p>

ptrblck: <p>You could call either</p>
<ul>
<li>call <code>.train()</code> on the rnn module after using <code>model.eval()</code>
</li>
<li>call <code>.eval()</code> only on the necessary modules</li>
<li>or disable cudnn via <code>torch.backends.cudnn.enabled = False</code>
</li>
</ul>

Lewis_Liu: <p>But I did call .train() before the backward line. Doesn’t it count?</p>

Sowmya_Krishnan: <p>Hi all</p>
<p>I’m facing the exact same issue in PyTorch when training a model with reinforcement learning. It works fine in CPU, but throws this error when training with GPU.</p>
<blockquote>
<p>cudnn RNN backward can only be called in training mode</p>
</blockquote>
<p>I have also set my model back to train() mode after calling eval() mode once. I have also tried setting torch.backends.cudnn.enabled=False. The error still remains. Is there any update on this issue? What is the solution?</p>

ptrblck: <p>Disabling cudnn should work, so could you recheck it?<br>
The suggested solutions are given in the <a href="https://discuss.pytorch.org/t/pytorch-cudnn-rnn-backward-can-only-be-called-in-training-mode/80080/6">previous post</a>, which all point towards either leaving the cudnn RNN in training mode or disabling cudnn (for this layer).</p>

Sowmya_Krishnan: <p>I have rechecked it - disabling cudnn did not work for me. However, leaving the model in train() mode rather than switching between train() and eval() did the trick for me. This trick had to be applied only when training the model in GPU - in CPU the switch from eval() to train() works perfectly fine. Thanks for the suggestion!<br>
One question though - I tried disabling cudnn just before I call the backward() function. Is that a reason for it not working? Should I disable it at the point where the training process begins?</p>

ptrblck: <aside class="quote no-group" data-username="Sowmya_Krishnan" data-post="10" data-topic="80080">
<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/sowmya_krishnan/48/30188_2.png" class="avatar"> Sowmya_Krishnan:</div>
<blockquote>
<p>One question though - I tried disabling cudnn just before I call the backward() function. Is that a reason for it not working? Should I disable it at the point where the training process begins?</p>
</blockquote>
</aside>
<p>Yes, that’s the reason, as you should disable it for the forward and backward pass for this layer.</p>