fn

pack_sequence

PackedSequence
pack_sequence(sequences: list[Tensor], enforce_sorted: bool = True)
source

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 Tensor
Per-example tensors. Leading axis is time; trailing axes are the feature shape and must match across entries.
enforce_sortedbool= True
If 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

PackedSequence

Packed view of the input list.

Raises

ValueError
If sequences is empty, or if it is not sorted while enforce_sorted=True.
NotImplementedError
If 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])