Accessing `.data` in PackedSequence when `batch_first=True`

TylerS: <p>When creating a variable-length PackedSequence with <code>batch_first=True</code>, accessing the <code>.data</code> attribute returns the sequences out of order, as if <code>batch_first=False</code>.</p>
<p>I don’t really understand the reasoning behind having the sequence be the first dimension by default (it seems less intuitive given how pytorch otherwise deals with batches), but I’m assuming it is for performance reasons. Even then, given that the <code>.data</code> attribute is public-facing, I feel like it should be returned in the same order as it was given. Then, for those of us writing modules that use padded and packed sequences, we can more naturally deal with this input without hacking together even more re-ordering code; requiring <code>pack_padded_sequence()</code> and <code>pack_sequence()</code> to receive sequences in decreasing length is enough of a hassle, but that’s another topic.</p>
<p>I wasn’t sure if this behavior was intended or not, so I’m posting here rather than making a bug report. But is this behavior correct? If so, why, and how does pytorch recommend dealing with this issue?</p>
<h3>Code to Reproduce Behavior:</h3>
<pre><code class="lang-python">import torch
from torch.nn.utils.rnn import pad_sequence, pack_padded_sequence

batch_first_seqs = [ \
    torch.rand((3, 2)),
    torch.rand((2, 2)),
    torch.rand((1, 2))]
lengths = torch.LongTensor([3, 2, 1])
padded_seqs = pad_sequence(batch_first_seqs, batch_first=True)
packed_seqs = pack_padded_sequence(padded_seqs, lengths=lengths, batch_first=True)

print(batch_first_seqs)
print(padded_seqs)
print(packed_seqs)
print(torch.cat(batch_first_seqs) == packed_seqs.data)
</code></pre>
<p>Output:</p>
<pre><code class="lang-python">[tensor([[0.7967, 0.5329],
        [0.6376, 0.3543],
        [0.6514, 0.8007]]), tensor([[0.1709, 0.1577],
        [0.5007, 0.8083]]), tensor([[0.3345, 0.7590]])]

tensor([[[0.7967, 0.5329],
         [0.6376, 0.3543],
         [0.6514, 0.8007]],

        [[0.1709, 0.1577],
         [0.5007, 0.8083],
         [0.0000, 0.0000]],

        [[0.3345, 0.7590],
         [0.0000, 0.0000],
         [0.0000, 0.0000]]])

PackedSequence(data=tensor([[0.7967, 0.5329],
        [0.1709, 0.1577],
        [0.3345, 0.7590],
        [0.6376, 0.3543],
        [0.5007, 0.8083],
        [0.6514, 0.8007]]), batch_sizes=tensor([3, 2, 1]))

tensor([[1, 1],
        [0, 0],
        [0, 0],
        [0, 0],
        [1, 1],
        [0, 0]], dtype=torch.uint8)
</code></pre>

albanD: <p>Hi,</p>
<p>The <code>.data</code> field is kept for backward compatibility but should not be used at all.<br>
Why do you need it? You should replace all use of it with either <code>.detach()</code> to break the graph or <code>with torch.no_grad()</code> to perform ops that are not tracked by the autograd engine.</p>

TylerS: <p>Thanks for responding. I was using this as a bit of a hack to remove the padding from a padded sequence. Basically, I wanted to to let a function parameter specify whether an input was padded or not, then break up that padded sequence into a list of variable-length sequences for use in a stateful layer (like an LSTM, or a GRU). And of course, I was wanting to keep the batch dimension as the first dimension.</p>
<p>Knowing that the <code>.data</code> field shouldn’t be used at all, it makes sense that this isn’t the pytorch-intended way of doing this. In fact, it seems likely that the library would rather this not be done at all. It seems unavoidable when writing custom state-based layers, so I suppose I’ll just write my own utility functions to help clean things up.</p>
<p>Here’s hoping 1.0 cleans up this part of the library!</p>

Messiah: <p>Hi <a class="mention" href="/u/alband">@albanD</a>,</p>
<p>Can you please elaborate as to why we should not use the <code>.data</code> attribute at all? I have encountered a scenario where using this attribute is the only way I can effectively pass data through my network, similar to <a href="https://discuss.pytorch.org/t/how-to-process-variable-length-sequence-of-images-with-cnn/82422/8" class="inline-onebox">How to process variable length sequence of images with CNN - #8 by 3nomis</a>. In that discussion, it seems like we can use <code>.data</code> as a workaround for certain scenarios where it is needed. Is that approach actually valid? Do we actually need to use <code>.detach()</code> for each of these references to <code>.data</code>? I would much appreciate any further insights you may have regarding this.</p>

ptrblck: <p><a class="mention" href="/u/alband">@albanD</a> made most likely the same mistake as I did <a href="https://discuss.pytorch.org/t/how-to-process-variable-length-sequence-of-images-with-cnn/82422/10">here</a> and confused the <code>PackedSequence.data</code> attribute with the deprecated <code>tensor.data</code> one.</p>