Why did this CUDA error occured in PyTorch?
On the process in building a RNN model, I met with the below error. The following are part of my code:
class RNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.embedding = nn.Embedding(emb_num, emb_size)
        self.dropout1 = nn.Dropout(dropout_rate) 
        self.LSTM = nn.LSTM(50, 128, 1, bidirectional = True)
        self.dropout2 = nn.Dropout(dropout_rate)
        self.full_connect = nn.Linear(256 , 5) #  biLSTM state * 2
    def forward(self, x):
        x = self.embedding(x)
        x = x.permute(1,0,2)
        x = self.dropout1(x)
        _, (hn, cn) = self.LSTM(x) 
        out = self.dropout2(hn)
        #print(out.shape)
        out = torch.cat([out[i, :, :] for i in range(2)], 1)
        out = out.squeeze()
        out = self.full_connect(out) 
        return out

def train():
    optimizer = torch.optim.Adam(model.parameters(), lr = learning_rate, weight_decay = 0.001)
    Loss = nn.CrossEntropyLoss()

    for epoch in range(epochs):
        model.train()
        max_acc = 0
        print('epoch:{}'.format(epoch + 1))
        for i, data in enumerate(trainloader, 0):
            X_train, y_train = data
            optimizer.zero_grad()
            X_train = X_train.long().to(device)
            y_train = y_train.long().to(device)
            output = model(X_train)
            loss = Loss(output, y_train)
            loss.backward()
            optimizer.step()
        print('loss:{:3f}'.format(loss))
        model.eval()
        acc = valid(validloader)
        print('epoch:{} acc:{}'.format(epoch+1, acc))
        if epoch + 1 == 50:
            torch.save(model.state_dict(), 'epoch50.pt')
        if acc &gt; max_acc:
            max_acc = acc
            torch.save(model.state_dict(), 'max_acc model.pt')
    torch.save(model.state_dict(), 'final model.pt')

def valid(dataloader):
    correct = 0
    total = 0
    with torch.no_grad():
        for i, data in enumerate(dataloader, 0):
            X_train, y_train = data
            #optimizer.zero_grad()
            X_train = X_train.long().to(device)
            y_train = y_train.long().to(device)
            output = model(X_train)
            #loss = Loss(output, y_train)
            #loss.backward()
            #optimizer.step()
            correct += (torch.argmax(output, dim = 1) == y_train).sum().item()
            total += y_train.shape[0]
    return correct / total

In the above code, I created a devset to test the model in training. But after 4 epoch or more, this error occured:
Traceback (most recent call last):
  File &quot;c:\Users\hhhh\Desktop\NLP-beginner\task2\task2.py&quot;, line 287, in &lt;module&gt;
    train()
  File &quot;c:\Users\hhhh\Desktop\NLP-beginner\task2\task2.py&quot;, line 185, in train
    acc = valid(validloader)
  File &quot;c:\Users\hhhh\Desktop\NLP-beginner\task2\task2.py&quot;, line 207, in valid
    correct += (torch.argmax(output, dim = 1) == y_train).sum().item()
RuntimeError: CUDA error: unspecified launch failure

I have tried to switch to cpu device to train the model, yet the training speed is to slow even for 1 epoch. Is it because my computer configuration that not enough to run this?

To check whether your system has CUDA or not:
from torch.cuda import is_available


def main():
    use_cuda = not args.no_cuda and is_available()
    dev = device(&quot;cuda&quot; if use_cuda else &quot;cpu&quot;)
    model = RNN().to(device=dev)
    # Call train and test methods below


if __name__ == '__main__':
    main()


