"RuntimeError: CUDA error: device-side assert triggered" when training a question answering model

Sathsara_Rasantha: <p>Hi All,</p>
<p>I am trying to implement the BiDAF model for a Sinhala question answering dataset.<br>
This dataset is created by translating a portion of SQuAD into Sinhala language using Google translate.<br>
And I am using Google colab for this.<br>
I found a code which is implemented BiDAF for SQuAD (english) dataset using pytorch.<br>
And I modified it for my dataset.<br>
I changed some data pre-processing steps and used fastText word embeddings instead of GloVe word embeddings.</p>
<p>I got lot of issues on the way through.<br>
I solved many of them. But this one seems to be very difficult to understand because I am very new to pytorch.<br>
This is my final year research project as well.<br>
I kindly request someone to take a look at this issue and solve it for me. Any kind of help is highly appreciated. Thanks in advance.</p>
<p>This is the link to the Colab notebook : <a href="https://colab.research.google.com/drive/1zBn-jU_y-NbBOXR_eAi6j1lPxO-EhMSa?usp=sharing" rel="noopener nofollow ugc">https://colab.research.google.com/drive/1zBn-jU_y-NbBOXR_eAi6j1lPxO-EhMSa?usp=sharing</a></p>

Abhilash_Srivastava: <aside class="quote no-group" data-username="Sathsara_Rasantha" data-post="1" data-topic="107233">
<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/sathsara_rasantha/48/32726_2.png" class="avatar"> Sathsara_Rasantha:</div>
<blockquote>
<p>I kindly request someone to take a look at this issue and solve it for me.</p>
</blockquote>
</aside>
<p>You’ll need to be more specific than that. The more specific you are, the easier it will be for us to understand and suggest solutions.</p>
<p>From the outset, you can try disabling CUDA and first try to make it work end to end on a CPU (with a smaller dataset).</p>

Sathsara_Rasantha: <p><a class="mention" href="/u/abhilash_srivastava">@Abhilash_Srivastava</a> Thank you very much for replying.<br>
I added the notebook link for my code. And I thought it would help you guys.<br>
Anyway I will try what you suggested.</p>

Sathsara_Rasantha: <p><a class="mention" href="/u/abhilash_srivastava">@Abhilash_Srivastava</a> I tried running the code on cpu instead of gpu. Now I am getting a much more detailed trace of the error.</p>
<blockquote>
<h2>Starting training …<br>
Starting batch: 0</h2>
<p>IndexError                                Traceback (most recent call last)<br>
 in ()<br>
----&gt; 1 train(model, train_dataset)</p>
<p>2 frames<br>
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)<br>
2262                          .format(input.size(0), target.size(0)))<br>
2263     if dim == 2:<br>
 → 2264         ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)<br>
2265     elif dim == 4:<br>
2266         ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)</p>
<p>IndexError: Target 545 is out of bounds.</p>
</blockquote>
<p>Can you explain what this error supposed to mean ?</p>

ptrblck: <p>The error points towards an invalid target index. Assuming your max. target value is 545 it would mean you are dealing with 546 classes and the model output should have the shape <code>[batch_size, 546]</code> for a multi-class classification use case.<br>
This error is raised, if the size of <code>dim1</code> is smaller than the max. target index, so you would have to check the model output.</p>

Sathsara_Rasantha: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> Thank you very much. This explanation seems very helpful. I’ll look at the model output.</p>