Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Doc] Better doc for TensorDictModuleBase #1226

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions tensordict/nn/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,35 @@ class TensorDictModuleBase(nn.Module):

>>> tensordict_out = module.forward(tensordict_in)

Unlike :class:`~tensordict.nn.TensorDictModule`, `TensorDictModuleBase` is typically used via subclassing:
you can wrap any python function in a `TensorDictModuleBase` subclass, as long as the subclass forward reads and
writes tensordict (or related types) instances.

The `in_keys` and `out_keys` should be properly specified. For example, `out_keys` can be dynamically reduced using
:meth:`~tensordict.nn.TensorDictBase.select_out_keys`.

Examples:
>>> from tensordict import TensorDict
>>> from tensordict.nn import TensorDictModuleBase
>>> class Mod(TensorDictModuleBase):
... in_keys = ["a"] # can also be specified during __init__
... out_keys = ["b", "c"]
... def forward(self, tensordict):
... b = tensordict["a"].clone()
... c = b + 1
... return tensordict.replace({"b": b, "c": c})
>>> mod = Mod()
>>> td = mod(TensorDict(a=0))
>>> td["b"]
tensor(0)
>>> td["c"]
tensor(1)
>>> mod.select_out_keys("c")
>>> td = mod(TensorDict(a=0))
>>> td["c"]
tensor(1)
>>> assert "b" not in td

"""

def __new__(cls, *args, **kwargs):
Expand Down
Loading