Skip to content
class

PackedSequence

extends_PackedSequenceFields
PackedSequence()

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
Concatenation of the active features at each time-step in descending-length order, shape (sum(batch_sizes), *feat).
batch_sizesTensor
1-D int tensor on the CPU giving the number of sequences still alive at each successive time-step. Strictly non-increasing.
sorted_indicesTensor or None
Permutation that took the original batch order to the packed (descending-length) order; None (default) if the input was already sorted.
unsorted_indicesTensor or None
Inverse of sorted_indices; used by pad_packed_sequence to restore the caller's original batch order on unpack. Derived from sorted_indices when omitted.

Raises

ValueError
If batch_sizes is not on the CPU, or sorted_indices is not a permutation.

Notes

Conceptually, with sequence lengths ℓ1≥⋯≥ℓB\ell_1 \geq \dots \geq \ell_B, the packed layout walks time-major:

data=[ x0(1),…,x0(B),  x1(1),…,x1(B1),  … ],\text{data} = \bigl[\,x^{(1)}_0, \dots, x^{(B)}_0,\; x^{(1)}_1, \dots, x^{(B_1)}_1,\; \dots\,\bigr],

where BtB_t (= batch_sizes[t]) is the count of sequences with length >t> t.

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

1
prop

is_metal

→bool
is_metal: bool

Whether data lives on the Metal device.

Instance methods

13
fn

byte

→Self
byte()

Cast data to uint8.

Raises

NotImplementedError
Always: Lucid has no uint8 dtype, exactly as Tensor.byte.
fn

cpu

→Self
cpu()

Move data and the index tensors to the CPU.

fn

double

→Self
double()

Cast data to float64.

fn

float

→Self
float()

Cast data to float32.

fn

half

→Self
half()

Cast data to float16.

fn

is_pinned

→bool
is_pinned()

Whether data is in pinned memory (always False on Apple Silicon).

fn

metal

→Self
metal()

Move data and the index tensors to the Metal device.

fn

pin_memory

→Self
pin_memory()

Pin data and the index tensors; a no-op on Apple Silicon's unified memory.

fn

short

→Self
short()

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

device(device, dtype, Tensor or None)= None
Target device, target dtype, or a tensor to match.
dtypedtype or None= None
Target dtype of data.
non_blockingbool= False
Passed through to lucid.Tensor.to.
copybool= False
Force data to be copied, as in lucid.Tensor.to.

Returns

PackedSequence

self if data needs no change, else a new packed sequence.