Why does nn.Embedding layers expect LongTensor type input Tensors?

abercher: <p>I’m a bit confused about the types that different layers are expecting.</p>
<p>I discovered that a nn.Embedding layer expects its input to be of type LongTensor aka torch.int64. Then it outputs a tensor of type torch.float64.</p>
<p>But then if I want to use a nn.LSTM layer next, I need to change the type since nn.LSTM wants torch.float32 as input, and I use for this</p>
<pre><code>emb_vect = torch.tensor(emb_vect dtype=torch.float32)
</code></pre>
<p>and it works fine then. But I have two questions:</p>
<ol>
<li>Why have the constraints on type?</li>
<li>Does changing the type of a variable in a forward function have an effect on the backpropagation?</li>
</ol>
<p>Thanks a lot</p>

albanD: <p>Hi,</p>
<p>The embedding layer takes as input the index of the element in the embedding you want to select and return the corresponding embedding. The input is expected to be a LongTensor because it is an index and so must be an integer. The output is a float type, you can call <code>.float()</code> or <code>.double()</code> on the nn.Module to change between float32 and float64.<br>
The lstm takes float type for both input and output and can be switches between single and double precision with <code>.float()</code> and <code>.double()</code> as well.</p>
<p>Change of types are considered as any other operations by the autograd engine and so all the gradients will be computed properly. Note that there might be a loss of precision of going from double to float as you would expect.</p>

abercher: <p>Thanks a lot for your answer albanD.</p>
<p>In order not to loose precision, I decided to use what you suggested (I didn’t know it existed) with</p>
<pre><code>self.lstm = nn.LSTM(input_size=self.emb_dim,
                        hidden_size=n_lstm_units,
                        dropout=1-keep_prob).double()
</code></pre>
<p>instead of converting the output of the embedding layer. And it works fine. Thank you very much for your time and explanation!</p>

Bjorn_Lindqvist: <p>But why doesn’t it accept an IntTensor?</p>

albanD: <p>What is not accepting IntTensors?</p>

Bjorn_Lindqvist: <p>The Embedding layer. I want to save some memory by using IntTensors over LongTensors but the layer doesn’t accept IntTensors as input.</p>

albanD: <p>Hi,</p>
<p>Yes to avoid hard-to-debug issues on the user side, we only support Long Tensors that can always represent the indices that you will need.<br>
Is your index Tensor too large? You can try to convert it to long just before the embedding forward so that you only ever have a single batch in full precision at a time. That should fit in memory just fine no?</p>

Bjorn_Lindqvist: <p>How would supporting IntTensors cause bugs?</p>

albanD: <p>Hi,</p>
<p>It won’t cause errors in the code itself.<br>
But you can “easily” overflow an IntTensor. And this can be very hard to find out for a user.</p>

addisonklinke: <p><a class="mention" href="/u/alband">@albanD</a> I understand the concern of users accidentally overflowing an int32 tensor, however the requirement of int64 is causing issues for many people integrating with 3rd party inference engines like TensorRT and OpenVino. You can see the long history on this Github <a href="https://github.com/pytorch/pytorch/issues/145" rel="noopener nofollow ugc">issue</a> which has been open for almost 5 years!</p>
<p>I think providing full compatibility is a bigger concern than potential user errors, especially if int32 behavior was designed so that it’s only enabled on an explicit call like <code>torch.tensor(dtype=torch.int32)</code>. Then basic users will not find themselves in the overflow situation. Another option would be to put this under a different class name like <code>nn.SmallEmbedding</code> which again forces users to make an explicit choice where they are aware of the potential downsides.</p>
<p>Could the team please re-consider the priority of addressing this or provide a road map for community members to contribute this feature in a PR?</p>

albanD: <p>Hi,</p>
<p>Thanks for the feedback and details.<br>
I think we already have support on cpu and cuda for both long and int dtypes in embedding in the latest version of pytorch. Does that solve your issue?</p>

addisonklinke: <p>Excellent! I was on 1.7.1, but you are correct that 1.8.0 accepts int32 indices now. I will mention this in the Github issue thread so others are aware and it can be closed. FYI the release notes for 1.8.0 only mention this support for <code>nn.EmbeddingBag</code></p>

albanD: <aside class="quote no-group" data-username="addisonklinke" data-post="12" data-topic="21376">
<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/addisonklinke/48/20034_2.png" class="avatar"> addisonklinke:</div>
<blockquote>
<p>FYI the release notes for 1.8.0 only mention this support for <code>nn.EmbeddingBag</code></p>
</blockquote>
</aside>
<p>Ho my bad, thanks for pointing it out. Just updated the release notes! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>