TensorDictBase.contiguous#
- TensorDictBase.contiguous(*, canonical: bool = False, inplace: bool = False) Self#
Returns a new tensordict with contiguous leaves.
Each leaf is replaced by
torch.Tensor.contiguous(): a leaf that is already contiguous is shared with the tensordict, the others are copied.- Keyword Arguments:
canonical (bool, optional) – if
True, a leaf whose strides are not the row-major strides of its shape is copied too, even iftorch.Tensor.is_contiguous()returnsTruefor it (as it can with dimensions of size 1). Defaults toFalse.inplace (bool, optional) – if
True, the tensordict keeps its identity and keys, each leaf that is not contiguous is replaced by a contiguous copy, andselfis returned. See In-place operations. Defaults toFalse.
Note
On a
LazyStackedTensorDict, aPersistentTensorDict(h5, zarr) or a store, the result is aTensorDict. If the tensordicts of a lazy stack have no names, the result has no names either, even when the stack dimension has a name (fromstack_dim_nameor thenamessetter).densify()andclone()keep that name. To keep it here, name the dimension again withrefine_names().Examples
>>> import torch >>> from tensordict import TensorDict, lazy_stack >>> td = TensorDict(a=torch.zeros(4, 3).T, batch_size=[3]) >>> td.is_contiguous(), td.contiguous().is_contiguous() (False, True)
On a lazy stack, the result loses the name of the stack dimension:
>>> 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.