Rate this Page
★ ★ ★ ★ ★

TensorDictBase.contiguous#

TensorDictBase.contiguous(*, canonical: bool = False, inplace: bool = False) → Self#

Returns a new tensordict of the same type with contiguous values (or self if values are already contiguous).

Parameters:
  • canonical (bool, optional) – if True, every dense tensor leaf whose strides do not match the canonical C-row-major strides for its shape is rematerialized into a freshly allocated contiguous tensor (using torch.contiguous_format), even if torch.Tensor.is_contiguous() returns True (e.g. because of size-1 dimensions). When False (the default), the historical behavior of torch.Tensor.contiguous() is preserved. Defaults to False.

  • inplace (bool, optional) – If True, this tensordict’s identity and key set are preserved; each non-contiguous leaf is replaced by its contiguous counterpart one at a time. Tensor.contiguous() returns self when a leaf is already contiguous, so for fully-contiguous tensordicts the operation is effectively a no-op aside from the key walk. Defaults to False.

Note

On a LazyStackedTensorDict, a PersistentTensorDict (h5, zarr) or a store, the result is a TensorDict. If the tensordicts of a lazy stack have no names, the result has no names either, even when the stack dimension has a name (from stack_dim_name or the names setter). densify() and clone() keep that name. To keep it here, name the dimension again with refine_names().

Examples

>>> import torch
>>> from tensordict import TensorDict, lazy_stack
>>> stack = lazy_stack(
...     [TensorDict(a=torch.zeros(3), batch_size=[3]) for _ in range(2)],
...     stack_dim_name="s",
... )
>>> stack.names, stack.contiguous().names
(['s', None], [None, None])
>>> stack.contiguous().refine_names("s", ...).names
['s', None]
>>> stack.densify().names
['s', None]

Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.