from_dataclass#
- tensordict.from_dataclass(obj: Any, *, dest_cls: Type | None = None, auto_batch_size: bool = False, batch_dims: int | None = None, batch_size: Size | None = None, frozen: bool = False, autocast: bool = False, nocast: bool = False, inplace: bool = False, shadow: bool = False, tensor_only: bool = False, device: device | None = None) Any#
Converts a dataclass instance to a tensorclass instance, or a dataclass type to a tensorclass type.
Use it to move code from
@dataclassto a tensorclass. For a type, the result is a new tensorclass named<name>_tcwith the same fields, or the type itself, made a tensorclass, ifinplace=True. A type that is already a tensorclass is returned as it is. For an instance, the result is an instance of a new<name>_tctensorclass, or ofdest_cls, built from the fields ofobj.TensorDict.from_dataclassconverts a dataclass instance to aTensorDictinstead.To write the class as a tensorclass, a dataclass of tensors with
to(device),pin_memory(), slicing and stacking for all its fields, see TensorClass: a dataclass of tensors. Coming from a hand-written dataclass shows what replaces each method of a hand-written dataclass of tensors.- Parameters:
obj (Any) – the dataclass instance or type to convert.
- Keyword Arguments:
dest_cls (type, optional) – for an instance, the tensorclass to build. When it is given, the class flags below have no effect. Not used for a type. Defaults to
None, which creates a new tensorclass.auto_batch_size (bool, optional) – for an instance, if
True, sets the batch size to the largest leading shape that all the entries share. Exclusive withbatch_size. Defaults toFalse.batch_dims (int, optional) – with
auto_batch_size, the maximum number of batch dimensions. Ignored withoutauto_batch_size, with aFutureWarningwhen it would change the result. Starting with TensorDict 0.17, the default will change to turning onauto_batch_sizewhenbatch_dimsis given, asfrom_dictdoes. Defaults toNone, which allows as many as the entries share.batch_size (Size or list of int, optional) – for an instance, the batch size of the result: the leading dimensions that all its entries share. Defaults to
None, which gives an empty batch size.frozen (bool, optional) – the
frozenclass flag: ifTrue, assigning a field raisesdataclasses.FrozenInstanceError. Defaults toFalse.autocast (bool, optional) – the
autocastclass flag: ifTrue, a value set to a field is converted to the annotated type. Defaults toFalse.nocast (bool, optional) – the
nocastclass flag: ifTrue, tensor-compatible values (int,float,np.ndarray) are stored as they are, not cast to tensors. Defaults toFalse.inplace (bool, optional) – for a type, if
True, turnsobjitself into a tensorclass instead of creating a new class. Not used for an instance. Defaults toFalse.shadow (bool, optional) – the
shadowclass flag: ifTrue, fields can have the name of a tensorclass member (batch_size,sum, …) and hide it. Defaults toFalse.tensor_only (bool, optional) – the
tensor_onlyclass flag: ifTrue, every field annotation must be a tensor type or a tensor collection, and reading a field is faster. Defaults toFalse.device (device, str or int, optional) – for an instance, the device of the result. Defaults to
None, which leaves each leaf where it is.
The class flags are described in
TensorClass. Pass at most one ofautocast,nocastandtensor_only, as fortensorclass(). When the flags are applied (not withdest_cls, and not for a type that is already a tensorclass),autocastwithnocast, or, for a type,tensor_onlywith either of them, emits aFutureWarningtoday. Starting with TensorDict 0.17, the default will change to raising theTypeErrorthattensorclass()raises.- Returns:
A tensorclass type if
objis a type, a tensorclass instance otherwise.- Raises:
TypeError – if
objis not a dataclass instance or type, or if, for an instance,tensor_onlyis combined withautocastornocast, orauto_batch_size=Truewithbatch_size.
Examples
>>> from dataclasses import dataclass >>> import torch >>> from tensordict.tensorclass import from_dataclass >>> >>> @dataclass ... class X: ... a: int ... b: torch.Tensor ... >>> x = X(0, 0) >>> x2 = from_dataclass(x) >>> print(x2) X_tc(batch_size=[], fields={a: i64[], b: i64[]}) >>> X2 = from_dataclass(X, autocast=True) >>> print(X2(a=0, b=0)) X_tc(batch_size=[], fields={a: int, b: i64[]})