fn

pack_padded_sequence

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

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. Shape (B,). When passed as a tensor it must be a 1-D integer tensor.
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. 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.

Raises

ValueError
With enforce_sorted=True and an unsorted lengths.

Notes

Total packed length equals bb\sum_b \ell_b, the sum of true sequence lengths — strictly less than TBT \cdot B whenever any sequence is shorter than the max.

Examples

>>> from lucid.nn.utils.rnn import pack_padded_sequence
>>> packed = pack_padded_sequence(x, lengths=[5, 3, 2], batch_first=False)