TensorDictBase.to_module#
- TensorDictBase.to_module(module: Module, *, inplace: bool | None = None, return_swap: bool = True, swap_dest=None, use_state_dict: bool = False, non_blocking: bool = False, preserve_module_state: bool = True, memo=None)#
Writes the content of the tensordict into the parameters and buffers of a module, recursively.
The keys of the tensordict must match the names of the module attributes, as
from_module()gives them. A nested key is a submodule:("layers", "0", "weight")ismodule.layers[0].weight.to_modulecan also be used as a context manager, to populate a module with other parameters for the duration of awithblock (see the second example). When the block exits, the original parameters are written back, also if the block raises an exception.- Parameters:
module (nn.Module) – a module to write the parameters into.
- Keyword Arguments:
inplace (bool, optional) – if
True, copies the values into the existing parameters and buffers of the module, which keep their identity. Otherwise the leaves of the tensordict replace them. Defaults toNone, which behaves likeFalse. Cannot be passed withuse_state_dict=True.return_swap (bool, optional) – if
True, returns the parameters and buffers that the module held before the call. Defaults toTrue.swap_dest (TensorDictBase, optional) – with
return_swap, a tensordict to write the old values into and to return. Defaults toNone, which returns a new tensordict.use_state_dict (bool, optional) – if
True, loads the values through the state-dict API, so that the module’s state-dict hooks run. Defaults toFalse.non_blocking (bool, optional) – if
True, copies between devices can run asynchronously with respect to the host. Defaults toFalse.preserve_module_state (bool, optional) –
if
True, existingParameterand buffer registrations are preserved when writing tensor leaves tomodule: parameters remain parameters with their originalrequires_gradvalue, and buffers remain registered buffers. Leaves that are not parameters but require gradients, or that are batched byvmap(), are registered as they are, so that gradients still reach them. IfFalse, a leaf that is not aParameterreplaces the attribute as it is, which can deregister an existing parameter (the historical behavior). Defaults toTrue.Changed in version 0.15:
preserve_module_state=None, deprecated in 0.14, is no longer accepted.memo – no longer accepted: passing it raises a
RuntimeError.
- Returns:
A tensordict of the values that the module held before the call (or
swap_dest) ifreturn_swapisTrue,Noneotherwise.
Examples
>>> import torch >>> from torch import nn >>> from tensordict import TensorDict >>> module = nn.Linear(3, 4) >>> params = TensorDict.from_module(module) >>> zeros = params.data.clone().zero_() >>> old_params = zeros.to_module(module) >>> bool((module.weight == 0).all()) True >>> _ = old_params.to_module(module) # restores the original values >>> bool((module.weight != 0).any()) True
Use the tensordict as a context manager to make a functional call. When the block exits, the original parameters are written back:
>>> with zeros.to_module(module): ... y = module(torch.ones(2, 3)) # runs with zeroed parameters >>> bool((y == 0).all()) True >>> bool((module.weight != 0).any()) True
The original parameters are written back if the block raises an exception too:
>>> try: ... with zeros.to_module(module): ... raise ValueError("stop") ... except ValueError: ... pass >>> bool((module.weight != 0).any()) True
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.