RuntimeError: cuda runtime error (59) : device-side assert triggered at /pytorch/aten/src/THC/generic/THCTensorMath.cu:26

I am new about torch text. I was using this repo https://github.com/prakashpandey9/Text-Classification-Pytorch for text classification (IMDB). It perfectly worked for IMDB, but when I changed train_data, test_data = datasets.IMDB.splits(TEXT, LABEL) to train_data, valid_data, test_data = datasets.SST.splits(TEXT, LABEL), it got this error:

/pytorch/aten/src/THCUNN/ClassNLLCriterion.cu:105: void cunn_ClassNLLCriterion_updateOutput_kernel(Dtype *, Dtype *, Dtype *, long *, Dtype *, int, int, int, int, long) [with Dtype = float, Acctype = float]: block: [0,0,0], thread: [0,0,0] Assertion t >= 0 && t < n_classes failed. /pytorch/aten/src/THCUNN/ClassNLLCriterion.cu:105: void cunn_ClassNLLCriterion_updateOutput_kernel(Dtype *, Dtype *, Dtype *, long *, Dtype *, int, int, int, int, long) [with Dtype = float, Acctype = float]: block: [0,0,0], thread: [11,0,0] Assertion t >= 0 && t < n_classes failed. /pytorch/aten/src/THCUNN/ClassNLLCriterion.cu:105: void cunn_ClassNLLCriterion_updateOutput_kernel(Dtype *, Dtype *, Dtype *, long *, Dtype *, int, int, int, int, long) [with Dtype = float, Acctype = float]: block: [0,0,0], thread: [19,0,0] Assertion t >= 0 && t < n_classes failed. /pytorch/aten/src/THCUNN/ClassNLLCriterion.cu:105: void cunn_ClassNLLCriterion_updateOutput_kernel(Dtype *, Dtype *, Dtype *, long *, Dtype *, int, int, int, int, long) [with Dtype = float, Acctype = float]: block: [0,0,0], thread: [20,0,0] Assertion t >= 0 && t < n_classes failed. /pytorch/aten/src/THCUNN/ClassNLLCriterion.cu:105: void cunn_ClassNLLCriterion_updateOutput_kernel(Dtype *, Dtype *, Dtype *, long *, Dtype *, int, int, int, int, long) [with Dtype = float, Acctype = float]: block: [0,0,0], thread: [26,0,0] Assertion t >= 0 && t < n_classes failed. Traceback (most recent call last): File "./main.py", line 91, in <module> train_loss, train_acc = train_model(model, train_iter, epoch) File "./main.py", line 44, in train_model loss.backward() File "....", line 118, in backward torch.autograd.backward(self, gradient, retain_graph, create_graph) File "....", line 93, in backward allow_unreachable=True) # allow_unreachable flag RuntimeError: cuda runtime error (59) : device-side assert triggered at /pytorch/aten/src/THC/generic/THCTensorMath.cu:26

I checked the size of prediction and targets, they are

torch.Size([32, 2])
torch.Size([32])

and the target values are
tensor([2, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 2, 1, 0, 0, 0, 0, 0, 1, 2, 2, 0, 1, 1, 0, 1, 2, 1, 1, 0, 1, 1], device='cuda:0')

this means the target has more than two classes? something like positive, negative, natural responses? how can I solve this error?

Hi,

After a quick look at the sst dataset in torchtext, it seems that the number of labels for SST is not the same as IMDB. You need to resize your output layer accordingly.

1 Like

Changing the output layer size solved the problem. Thank you very much, @albanD.