fn
pack_padded_sequence
→PackedSequencepack_padded_sequence(input: Tensor, lengths: Tensor | list[int], batch_first: bool = False, enforce_sorted: bool = True)Pack a padded 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
inputTensorPadded batch. Default layout is
(T, B, *) with time on
axis 0; set batch_first=True for (B, T, *).lengthsTensor or list of intPer-sequence true lengths. Shape
(B,). When passed as a
tensor it must be a 1-D integer tensor.batch_firstbool= FalseWhether
input is laid out batch-first. Default False.enforce_sortedbool= TrueIf
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
PackedSequencePacked view of the batch.
Raises
ValueErrorWith
enforce_sorted=True and an unsorted lengths.Notes
Total packed length equals , the sum of true sequence lengths — strictly less than 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)