Skip to content
fn

pack_padded_sequence

→PackedSequence
pack_padded_sequence(input: Tensor, lengths: Tensor | list[int], batch_first: bool = False, enforce_sorted: bool = True)

Pack a padded (T,B,∗)(T, B, *) batch into a PackedSequence.

Strips out the padding cells so downstream RNN kernels iterate only over genuine time-steps. Reduces both compute and (with masked losses) accidental gradient flow through pad positions.

Parameters

inputTensor
Padded batch. Default layout is (T, B, *) with time on axis 0; set batch_first=True for (B, T, *).
lengthsTensor or list of int
Per-sequence true lengths, 1-D with one entry per batch element, each in [1, T]. A tensor may live on any device; non-integer values truncate toward zero.
batch_firstbool= False
Whether input is laid out batch-first. Default False.
enforce_sortedbool= True
If True (the default), assume the caller already supplied sequences in descending-length order — cheap but raises ValueError on violation, and the result carries no permutation (sorted_indices / unsorted_indices are None). Set to False to have the function sort internally; the sort permutation is stored on the returned PackedSequence so pad_packed_sequence can undo it.

Returns

PackedSequence

Packed view of the batch. data and the index tensors live on input's device; batch_sizes is CPU int64.

Raises

ValueError
If input has fewer than two axes or no elements; if lengths is not 1-D, does not have one entry per batch element, or has an entry <= 0 or longer than T; or with enforce_sorted=True and an unsorted lengths.

Notes

Total packed length equals ∑bℓb\sum_b \ell_b, the sum of true sequence lengths — strictly less than T⋅BT \cdot B whenever any sequence is shorter than the max.

Examples

>>> import lucid
>>> from lucid.nn.utils.rnn import pack_padded_sequence
>>> x = lucid.randn(5, 3, 8)                 # (T=5, B=3, F=8)
>>> packed = pack_padded_sequence(x, lengths=[5, 3, 2], batch_first=False)
>>> packed.data.shape                        # 5 + 3 + 2 real time-steps
(10, 8)

Used by 1