Unable to set model to training mode

Amith_Adiraju: <p>I’m trying to do a very simple FC Neural network. I have 2 GPUs on my machine, I’ve followed this tutorial to make the code use 2 GPUs: <a href="https://pytorch.org/tutorials/beginner/blitz/data_parallel_tutorial.html" rel="noopener nofollow ugc">Data Parallel</a></p>
<p>For some reason, I’m unable to train the model, the error I keep getting is</p>
<blockquote>
<p>RuntimeError: cudnn RNN backward can only be called in training mode</p>
</blockquote>
<p>The solution seemed very trivial, to set the model to training mode before <code>forward</code> call, but that doesn’t fix the issue at all. I tried many different ways to actually set the model to train mode, but none worked.</p>
<p>Here’s my code:</p>
<p><code>Class</code></p>
<pre><code class="lang-auto">class ABC(nn.Module):
    
    def __init__(self, inp_dim_size, hid_dim_size, out_size):
        
        super(ABC, self).__init__()
        
        self.inp_dim_size = inp_dim_size
        
        self.hid_dim_size  = hid_dim_size
        
        self.out_size = out_size
        
        self.seq_layer = nn.Sequential(
                
            nn.Linear( self.inp_dim_size, self.hid_dim_size  ),
            
            nn.ELU(),
            
            nn.Dropout(0.4),
            nn.Linear(self.hid_dim_size, self.hid_dim_size // 2 ),
            
            nn.ELU(),
            
            nn.Dropout(0.4),
            nn.Linear(self.hid_dim_size // 2, self.hid_dim_size // 2),
            
            nn.ELU(),
            
            nn.Dropout(0.3),
            
            nn.Linear(self.hid_dim_size // 2, self.out_size)
        
        )
        
        
        
    def forward(self, X_batch):
        
        output_scores = self.seq_layer(X_batch)
        
        return output_scores
</code></pre>
<p><code>Train code</code>:</p>
<pre><code class="lang-auto">for epoch in range(num_epochs):  # loop over the dataset multiple times

        rl, ns = 0.0, 0
        for i, data in enumerate(trainloader, 0):
            # get the inputs; data is a list of [inputs, labels]
            inputs, labels = data
            
            inputs = inputs.to(device)
            scores = labels.to(device)
            
            br, _, _ = scores.shape

            # zero the parameter gradients
            optimizer.zero_grad()

            # forward + backward + optimize
            predictions = model(inputs)
            loss = criterion(predictions,scores.view(br, 1))
            loss.backward(retain_graph = True)
            optimizer.step()

            ...



model = ABC(103, 51, 1)


if torch.cuda.device_count() &gt; 1:
    
    model = nn.DataParallel(model)
    
    model.to(device)
    
    model = model.train()
    
else:
    
    model = model.to(device)
    
    model = model.train()
</code></pre>
<p>It’s been pretty frustrating trying to solve seemingly easy issue without any results. Any inputs will be highly appreciated. TIA !</p>

ptrblck: <p>Are you sure you are running this code, as you don’t use any RNNs, which the error message points to?</p>

Amith_Adiraju: <p>I didn’t get the last part of your message.</p>

ptrblck: <p>The error message:</p>
<aside class="quote no-group" data-username="Amith_Adiraju" data-post="1" data-topic="53969">
<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/amith_adiraju/48/13801_2.png" class="avatar"> Amith_Adiraju:</div>
<blockquote>
<p>RuntimeError: cudnn RNN backward can only be called in training mode</p>
</blockquote>
</aside>
<p>claims you are running an <code>RNN</code> module in training mode and try to call <code>backward()</code> somewhere.<br>
However, your <code>ABC</code> model doesn’t use any RNNs, so it seems you missed some code parts or the error is thrown from some other code parts.<br>
If you are using a Jupyter notebook, make sure to restart the notebook.</p>

Amith_Adiraju: <p>ok, will make sure and let you know. Thanks</p>

Amith_Adiraju: <p>I think I figured out the issue. The data I was passing to my <code>ABC</code> network was generated through a saved RNN file, I forgot to use .detach() on that data, so while using my network, the input data was using require_grad=True, which it shouldn’t. Using .detach() fixed the issue. Thanks for suggestions <a class="mention" href="/u/ptrblck">@ptrblck</a>.</p>

ptrblck: <p>Good to hear you’ve figured it out! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>