Compact representation of a batch of variable-length sequences.
A PackedSequence interleaves all surviving time-steps of every
sequence in the batch into a single flat tensor, dropping the
padding entries entirely. RNN cells consume this form to avoid
wasting compute on padded positions.
A tuple subclass of (data, batch_sizes, sorted_indices, unsorted_indices), so it unpacks and indexes like one. Build it
with pack_padded_sequence / pack_sequence, or by hand
from data and batch_sizes alone.
Parameters
dataTensor(sum(batch_sizes), *feat).batch_ sizesTensorsorted_ indicesTensor or NoneNone (default) if the input was
already sorted.unsorted_ indicesTensor or Nonesorted_indices; used by
pad_packed_sequence to restore the caller's original
batch order on unpack. Derived from sorted_indices when
omitted.Raises
ValueErrorbatch_sizes is not on the CPU, or sorted_indices is
not a permutation.Notes
Conceptually, with sequence lengths , the packed layout walks time-major:
where (= batch_sizes[t]) is the count of sequences
with length .
The conversion methods (to, float, cpu, ...)
move or cast data and carry the index tensors to data's
device; batch_sizes always stays on the CPU.
Examples
>>> import lucid
>>> from lucid.nn.utils.rnn import PackedSequence
>>> from lucid.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
>>> # padded (B=3, T=4, F=5), sorted-by-length [4, 3, 2]
>>> x_padded = lucid.randn(3, 4, 5)
>>> packed = pack_padded_sequence(x_padded, lengths=[4, 3, 2], batch_first=True)
>>> packed.data.shape # 4 + 3 + 2 real time-steps
(9, 5)
>>> packed.batch_sizes.tolist() # sequences alive at each step
[3, 3, 2, 1]
>>> # ... feed to RNN ...
>>> unpacked, lengths = pad_packed_sequence(packed, batch_first=True)
>>> unpacked.shape, lengths.tolist()
((3, 4, 5), [4, 3, 2])
>>> # hand-built: the inverse permutation is derived
>>> p = PackedSequence(lucid.zeros(3, 2), lucid.tensor([2, 1]), lucid.tensor([1, 2, 0]))
>>> p.unsorted_indices.tolist()
[2, 0, 1]Used by 4
Properties
1Instance methods
13Cast data to uint8.
Raises
NotImplementedErroruint8 dtype, exactly as Tensor.byte.Cast data to int8.
Move data and the index tensors to the CPU.
Cast data to float64.
Cast data to float32.
Cast data to float16.
Cast data to int32.
Whether data is in pinned memory (always False on Apple Silicon).
Cast data to int64.
Move data and the index tensors to the Metal device.
Pin data and the index tensors; a no-op on Apple Silicon's unified memory.
Cast data to int16.
to(device: DeviceLike | DTypeLike | Tensor = None, dtype: DTypeLike = None, non_blocking: bool = False, copy: bool = False)Move or cast data; the index tensors follow it to its device.
Takes what lucid.Tensor.to takes. dtype applies to
data only — the index tensors stay integer and batch_sizes
stays on the CPU.
Parameters
dtypedtype or None= Nonedata.non_ blockingbool= Falselucid.Tensor.to.copybool= Falsedata to be copied, as in lucid.Tensor.to.Returns
PackedSequenceself if data needs no change, else a new packed sequence.