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

username: Niki
cooked: <p>I am new about torch text. I was using this repo <a href="https://github.com/prakashpandey9/Text-Classification-Pytorch" rel="nofollow noopener">https://github.com/prakashpandey9/Text-Classification-Pytorch</a> for text classification (IMDB). It perfectly worked for IMDB, but when I changed <code>train_data, test_data = datasets.IMDB.splits(TEXT, LABEL)</code> to <code>train_data, valid_data, test_data = datasets.SST.splits(TEXT, LABEL)</code>, it got this error:</p>
<p><code>/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 </code> t &gt;= 0 &amp;&amp; t &lt; n_classes <code>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</code> t &gt;= 0 &amp;&amp; t &lt; n_classes <code>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</code> t &gt;= 0 &amp;&amp; t &lt; n_classes <code>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</code> t &gt;= 0 &amp;&amp; t &lt; n_classes <code>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</code> t &gt;= 0 &amp;&amp; t &lt; n_classes <code> failed. Traceback (most recent call last): File "./main.py", line 91, in &lt;module&gt; 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</code></p>
<p>I checked the size of prediction and targets, they are</p>
<pre><code class="lang-auto">torch.Size([32, 2])
torch.Size([32])
</code></pre>
<p>and the target values are<br>
<code>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')</code></p>
<p>this means the target has more than two classes? something like positive, negative, natural responses?  how can I solve this error?</p>

username: albanD
cooked: <p>Hi,</p>
<p>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.</p>

username: Niki
cooked: <p>Changing the output layer size solved the problem. Thank you very much, <a class="mention" href="/u/alband">@albanD</a>.</p>