fn
pack_sequence
→PackedSequencepack_sequence(sequences: list[Tensor], enforce_sorted: bool = True)One-shot pack-from-list — equivalent to pad_sequence then pack_padded_sequence.
Convenience wrapper for the common case where a list of per-example
tensors needs to become a PackedSequence without going
through an intermediate padded tensor. Each entry's leading axis
is treated as the time dimension; remaining axes carry the feature
shape (which must be identical across the list).
Parameters
sequenceslist of TensorPer-example tensors. Leading axis is time; trailing axes are
the feature shape and must match across entries.
enforce_sortedbool= TrueIf
True (the default), the caller must supply sequences in
non-increasing length order; mismatches raise
ValueError. False is not currently supported
and raises NotImplementedError — sort externally for
now.Returns
PackedSequencePacked view of the input list.
Raises
ValueErrorIf
sequences is empty, or if it is not sorted while
enforce_sorted=True.NotImplementedErrorIf
enforce_sorted=False is requested.Notes
Implemented as pad_sequence followed by
pack_padded_sequence. The intermediate padded tensor lives
only for the duration of the call.
Examples
>>> from lucid.nn.utils.rnn import pack_sequence
>>> packed = pack_sequence([long_seq, mid_seq, short_seq])