Transformer Embedding - IndexError: index out of range in self

username: maxrivera
cooked: <p>I’m training a transformer model from scratch, and was able to run a “mini” train and dev set on the CPU without any errors, but when I run the training loop on the larger dataset, I encounter this error (also includes debug print statements):</p>
<pre><code class="lang-auto">-- Inside Encoder Init --
d_vocab:  5493
d_embedding:  128
-- Inside Decoder Init --
d_vocab:  3194
d_embedding:  128
-- Inside Encoder Forward --
embedding in:  torch.Size([64, 60])
embedding out:  torch.Size([64, 60, 128])
-- Inside Decoder Forward --
embedding in:  torch.Size([64, 60])
Traceback (most recent call last):
  File "~/transformer_training.py", line 176, in &lt;module&gt;
    out = model(src,trg, mask)
  File "~/myenv2/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1186, in _call_impl
    return forward_call(*input, **kwargs)
  File "~/transformer.py", line 237, in forward
    x = self.decoder(x, trg, mask)
  File "~/myenv2/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1186, in _call_impl
    return forward_call(*input, **kwargs)
  File "~/transformer.py", line 205, in forward
    x = self.embedding(x)
  File "~/myenv2/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1186, in _call_impl
    return forward_call(*input, **kwargs)
  File "~/transformer.py", line 20, in forward
    x = self.embedding(x)
  File "~/myenv2/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1186, in _call_impl
    return forward_call(*input, **kwargs)
  File "~/myenv2/lib/python3.9/site-packages/torch/nn/modules/sparse.py", line 159, in forward
    return F.embedding(
  File "~/myenv2/lib/python3.9/site-packages/torch/nn/functional.py", line 2197, in embedding
    return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
IndexError: index out of range in self
</code></pre>
<p>NOTE: This does not occur when I use a smaller train and dev set for debug purposes (terminal output):</p>
<pre><code class="lang-auto">-- Inside Encoder Init --
d_vocab:  1435
d_embedding:  128
-- Inside Decoder Init --
d_vocab:  1446
d_embedding:  128
-- Inside Encoder Forward --
embedding in:  torch.Size([64, 60])
embedding out:  torch.Size([64, 60, 128])
-- Inside Decoder Forward --
embedding in:  torch.Size([64, 60])
embedding out:  torch.Size([64, 60, 128])
</code></pre>

username: basingse
cooked: <p>Hello again,</p>
<p>In error trace of yours (error in decoder stage)</p>
<pre><code class="lang-bash">  File "~/transformer.py", line 20, in forward
    x = self.embedding(x)
</code></pre>
<p>can you add <code>print(torch.max(x))</code> before the line <code>x = self.embedding(x)</code></p>
<p>I guess the error is because of <code>x</code> contains <code>id</code> that is <code>&gt;=3194</code>. If the value is greater than <code>3194</code>, then pytorch will raise the error mentioned in the stack trace.</p>
<p>It means that while you are converting tokens to ids, you are assigning the value greater than 3194.</p>
<p>One way to debug this is checking the max value for the batch before sending to model. Once the value is greater than or equal to 3194, then in that batch you find out for which sample via</p>
<pre data-code-wrap="python3"><code class="lang-nohighlight"># x-&gt; (batch_size, seq_len)
torch.max(x, dim=0)
</code></pre>
<p>and finding the index where 3194 comes. With this you know the index of the sample, now you can traceback to ids and then tokens and then to the original sentence as well.</p>
<p>Hope it helps.</p>