VmapModule¶
- class torchrl.modules.VmapModule(*args, **kwargs)[source]¶
A TensorDictModule wrapper to vmap over the input.
It is intended to be used with modules that accept data with one less batch dimension than the one provided. By using this wrapper, one can hide a batch dimension and satisfy the wrapped module.
- Parameters:
module (TensorDictModuleBase) – the module to vmap over.
vmap_dim (int, optional) – the vmap input and output dim. If none is provided, the last dimension of the tensordict is assumed.
Note
Since vmap requires to have control over the batch size of the input this module does not support dispatched arguments
Example
>>> lam = TensorDictModule(lambda x: x[0], in_keys=["x"], out_keys=["y"]) >>> sample_in = torch.ones((10,3,2)) >>> sample_in_td = TensorDict({"x":sample_in}, batch_size=[10]) >>> lam(sample_in) >>> vm = VmapModule(lam, 0) >>> vm(sample_in_td) >>> assert (sample_in_td["x"][:, 0] == sample_in_td["y"]).all()
- forward(tensordict)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.