PackedSequence — PyTorch 2.7 documentation (original) (raw)

class torch.nn.utils.rnn.PackedSequence(data, batch_sizes=None, sorted_indices=None, unsorted_indices=None)[source][source]

Holds the data and list of batch_sizes of a packed sequence.

All RNN modules accept packed sequences as inputs.

Note

Instances of this class should never be created manually. They are meant to be instantiated by functions like pack_padded_sequence().

Batch sizes represent the number elements at each sequence step in the batch, not the varying sequence lengths passed topack_padded_sequence(). For instance, given data abc and xthe PackedSequence would contain data axbc withbatch_sizes=[2,1,1].

Variables

Return type

Self

Note

data can be on arbitrary device and of arbitrary dtype.sorted_indices and unsorted_indices must be torch.int64tensors on the same device as data.

However, batch_sizes should always be a CPU torch.int64 tensor.

This invariant is maintained throughout PackedSequence class, and all functions that construct a PackedSequence in PyTorch (i.e., they only pass in tensors conforming to this constraint).

batch_sizes_: Tensor_

Alias for field number 1

count(value, /)

Return number of occurrences of value.

data_: Tensor_

Alias for field number 0

index(value, start=0, stop=9223372036854775807, /)

Return first index of value.

Raises ValueError if the value is not present.

property is_cuda_: bool_

Return true if self.data stored on a gpu.

is_pinned()[source][source]

Return true if self.data stored on in pinned memory.

Return type

bool

sorted_indices_: Optional[Tensor]_

Alias for field number 2

to(dtype: dtype, non_blocking: bool = ..., copy: bool = ...) → Self[source][source]

to(device: Optional[Union[str, device, int]] = ..., dtype: Optional[dtype] = ..., non_blocking: bool = ..., copy: bool = ...) → Self

to(other: Tensor, non_blocking: bool = ..., copy: bool = ...) → Self

Perform dtype and/or device conversion on self.data.

It has similar signature as torch.Tensor.to(), except optional arguments like non_blocking and copy should be passed as kwargs, not args, or they will not apply to the index tensors.

Note

If the self.data Tensor already has the correct torch.dtypeand torch.device, then self is returned. Otherwise, returns a copy with the desired configuration.

unsorted_indices_: Optional[Tensor]_

Alias for field number 3