RuntimeError: CUDA error: device-side assert triggered - "Index out of bounds" failed

EdinMahmutovic: <p>So I have some long code, but I’ll give a snippet of the code that gives problems. I have build a DQN learning agent and I am training it on a Tesla V100 GPU.<br>
This is the line of code where the problem occurs:</p>
<pre><code class="lang-auto">q_eval = self.Q_eval.forward(state_batch, state_seq_batch, tensor_batch_index)
q_eval = q_eval[batch_index, action_batch]
</code></pre>
<p>and the error message is:</p>
<p><strong>/pytorch/aten/src/ATen/native/cuda/IndexKernel.cu:60: lambda <a></a>-&gt;auto::operator()(int)-&gt;auto: block: [0,0,0], thread: [70,0,0] Assertion <code>index &gt;= -sizes[i] &amp;&amp; index &lt; sizes[i] &amp;&amp; "index out of bounds"</code> failed.</strong><br>
<strong>/pytorch/aten/src/ATen/native/cuda/IndexKernel.cu:60: lambda <a></a>-&gt;auto::operator()(int)-&gt;auto: block: [0,0,0], thread: [71,0,0] Assertion <code>index &gt;= -sizes[i] &amp;&amp; index &lt; sizes[i] &amp;&amp; "index out of bounds"</code> failed.</strong><br>
<strong>/pytorch/aten/src/ATen/native/cuda/IndexKernel.cu:60: lambda <a></a>-&gt;auto::operator()(int)-&gt;auto: block: [0,0,0], thread: [72,0,0] Assertion <code>index &gt;= -sizes[i] &amp;&amp; index &lt; sizes[i] &amp;&amp; "index out of bounds"</code> failed.</strong><br>
<strong>/pytorch/aten/src/ATen/native/cuda/IndexKernel.cu:60: lambda <a></a>-&gt;auto::operator()(int)-&gt;auto: block: [0,0,0], thread: [73,0,0] Assertion <code>index &gt;= -sizes[i] &amp;&amp; index &lt; sizes[i] &amp;&amp; "index out of bounds"</code> failed.</strong><br>
<strong>/pytorch/aten/src/ATen/native/cuda/IndexKernel.cu:60: lambda <a></a>-&gt;auto::operator()(int)-&gt;auto: block: [0,0,0], thread: [74,0,0] Assertion <code>index &gt;= -sizes[i] &amp;&amp; index &lt; sizes[i] &amp;&amp; "index out of bounds"</code> failed.</strong><br>
<strong>Traceback (most recent call last):</strong></p>
<p><strong>File “main.py”, line 268, in </strong><br>
<strong>agent.learn()</strong><br>
<strong>File “/zhome/8b/b/126923/Scripts/Python/Bachelor_Project/agents.py”, line 208, in learn</strong><br>
<strong>q_eval = q_eval[batch_index, action_batch]</strong><br>
<strong>RuntimeError: CUDA error: device-side assert triggered</strong></p>
<p>I wondered it had to something with the indexing so I have printed out the shape of q_eval and the values of batch_index and action_batch:</p>
<p><strong>q_eval shape:: torch.Size([32, 200])</strong><br>
<strong>batch_index = [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23</strong><br>
<strong>24 25 26 27 28 29 30 31]</strong><br>
<strong>action_batch = [ 19.  48.  67.  19.  30.  11.  18.  16.  67.  69.   7.   1.  26.  19.</strong><br>
<strong>43.  38.  61.  34.  21.   0.  11.  41.  11.  20.  42.   8.  17.  45.</strong><br>
<strong>24.  18.  43. 110.]</strong></p>
<p>I don’t see any values where the indexing goes “out out bounds”…<br>
And the worst part about all of this, is that it occurs after 1 hour +/- 5 minutes of training. Meaning I have to wait for quite some time to see if the problem has be dissolved. Another note is, that I cannot recreate the indexing error on a CPU.</p>

ptrblck: <p>Did you print the problematic batch, which creates the error or just a random one?<br>
In the latter case you could add an <code>assert</code> statement checking for the invalid indices and print them, in case it’s triggered. This could narrow down the faulty batch and you could dig into the code to figure out, how this invalid index is created.</p>

EdinMahmutovic: <p>That is the problematic batch. I printed out every batch to see whether any changes are happening right before the problematic batch or if any unusual happens. But thanks for the tip!</p>
<p>But nothing unusual is happening. The Q_eval is constantly 32 x 200 and both index is constantly a range between 0 to 31 and action batch is always between 0 and 199. Though I have no idea why the error occurs.</p>
<p>And to comment on your idea. it doesn’t really speed up the process of debugging if I only print the faulty batch. This still occurs after around 1 hour which is what bothers me. But nothing changes in the code after 1 hour which makes this question to an open debate…</p>

ptrblck: <p>If that’s the problematic batch, I assume you can reproduce the error by feeding these values to the mentioned operations?</p>

EdinMahmutovic: <p>That sounded like a great idea.</p>
<p>I fixed the action batch to the fixed array that is given in the description. But it does not reproduce the error instantly… It still fails after 1 hour of training, e.g, there’s no problem in the first 50k iterations… Can the error be something else than an indexing error even though the error message says otherwise?</p>

ptrblck: <p>No, the error message would give you the failing operation.<br>
However, the stack trace might point to the wrong line of code, due to the asynchronous behavior.<br>
You could rerun the code with:</p>
<pre><code class="lang-auto">CUDA_LAUNCH_BLOCKING=1 python script.py args
</code></pre>
<p>to get the proper stack trace with the offending operation.</p>

EdinMahmutovic: <p>Thank you! I didn’t know that command existed. It pointed to another line of code at which I quickly found the error.</p>

winston-wen: <p>Your advice help me find the exact location of error, which is really life-saving!<br>
In my case, I created a boolean indexer like the following snippet, and I forgot to check the case that the indexer can be all False!</p>
<pre><code class="lang-auto">valid = distmat &lt;= 10
tp = pred[valid, :]
blabla
</code></pre>

Meet_Vora: <p>Facing the similar issue when using facebook/bart.<br>
However, I am unable to fix it with the solutions mentioned here. I tried replicating the code with CPU inference, but the issue seems to persist.</p>

ptrblck: <p>Try to narrow down which indexing operation fails first to be able to fix the indices.</p>