Is there any case that 'cuda' mode doesn't work?

username: malofleur
cooked: <p>I design my lstm model like this:</p>
<pre><code class="lang-auto">device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
class LSTMpred(nn.Module):
    def __init__(self, input_size, hidden_dim):
        super(LSTMpred, self).__init__()
        self.input_dim = input_size
        self.hidden_dim = hidden_dim
        self.lstm = nn.LSTM(input_size, hidden_dim)
        self.hidden2out = nn.Linear(hidden_dim, 1)
        self.hidden = self.initHidden()

    def initHidden(self):
        return (Variable(torch.zeros(1, 1, self.hidden_dim)),
                Variable(torch.zeros(1, 1, self.hidden_dim)))

    def forward(self, *input):
        x = input[0]
        lstm_out, self.hidden = self.lstm(
            x.view(len(x), 1, -1), self.hidden
        )
        outdat = self.hidden2out(lstm_out.view(len(x), -1))
        return outdat
</code></pre>
<p>I use <code>model = LSTMModel.LSTMpred(1,40).to(device)</code> to initialize my model, but when I try to train it, it’s just stuck on the code <code>modelout = model(indata)</code> and the whole program gets existed after a few seconds saying <code>Process finished with exit code -1073741819 (0xC0000005)</code>.<br>
However, when I initialize <code>device</code> to <code>cpu</code>, like <code>device = torch.device('cpu')</code> it starts to work<br>
I’m confused about it, why does it happen? I would appreciate your help.</p>

username: smth
cooked: <p>it’s possible that CUDA itself is not correctly working on your machine?<br>
Does any other program in CUDA mode work correctly?</p>

username: malofleur
cooked: <p>Yep, if I use the model provided by pytorch itself, using MNIST dataset, and set as ‘CUDA’, it works.</p>

username: smth
cooked: <p>One thing I do see is <code>model = LSTMModel.LSTMpred(1,40).to(device)</code> does not move the <code>self.hidden</code> over to GPU, because it’s not registered as a parameter or a buffer.</p>
<p>I think you want to move <code>self.initHidden()</code> into the <code>forward</code> function, rather being in the constructor.</p>
<p>Something like:</p>
<pre><code class="lang-auto">def forward(self, *input):
    hidden = self.initHidden()
</code></pre>
<p>The reason <code>self.hidden</code> doesn’t get moved to GPU is because it’s not an instance of <code>nn.Parameter</code>, or it’s not been declared as a buffer via <a href="https://pytorch.org/docs/stable/nn.html?highlight=register_buffer#torch.nn.Module.register_buffer" rel="nofollow noopener">https://pytorch.org/docs/stable/nn.html?highlight=register_buffer#torch.nn.Module.register_buffer</a> so PyTorch doesn’t know when you do <code>.to('cuda')</code> to move this onto GPU.</p>

username: malofleur
cooked: <p>Still doesn’t work <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=12" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20"><br>
I try to debug to find the problem, and it stuck on the following code(filename: torch/nn/_functions/rnn.py):</p>
<pre><code class="lang-auto">output, hy, cy, reserve, new_weight_buf = torch._cudnn_rnn(
            input, weight_arr, weight_stride0,
            flat_weight,
            hx, cx,
            mode, hidden_size, num_layers,
            batch_first, dropout, train, bool(bidirectional),
            list(batch_sizes.data) if variable_length else (),
            dropout_ts)
</code></pre>
<p>these are the parameters used above<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/3/31ecbf181b57901b1eb3b17f6b9e9a6fe19af733.png" data-download-href="https://discuss.pytorch.org/uploads/default/31ecbf181b57901b1eb3b17f6b9e9a6fe19af733" title="paras"><img src="https://discuss.pytorch.org/uploads/default/original/2X/3/31ecbf181b57901b1eb3b17f6b9e9a6fe19af733.png" alt="paras" data-base62-sha1="77ECCPVG4gpZZ4JI685NIXHmmQz" width="690" height="244" data-dominant-color="404345"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">paras</span><span class="informations">1205×427 16.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
can you figure out the reason <img src="https://discuss.pytorch.org/images/emoji/apple/confounded.png?v=12" title=":confounded:" class="emoji" alt=":confounded:" loading="lazy" width="20" height="20"></p>

username: _ike
cooked: <p>Were you figure that? I have met the same question.</p>