symbolic_trace#
- tensordict.prototype.symbolic_trace(td_module: TensorDictModuleBase) TDGraphModule#
Traces a TensorDictModule or a TensorDictSequential into an FX graph.
This function is a prototype: its API can change without a deprecation period. See FX tracing (prototype) for a guide.
It supports a
TensorDictModulethat wraps anModulethattorch.fx.symbolic_trace()can trace, and aTensorDictSequentialof such modules. A module that wraps a plain function or a lambda raises aRuntimeError(“Unsupported type … passed for root!”). Other module types, such asProbabilisticTensorDictModule, raise aTypeError.- Parameters:
td_module (TensorDictModuleBase) – the module to trace.
- Returns:
a
TDGraphModulewith the same inputs and outputs astd_module.
Examples
>>> import torch >>> from torch import nn >>> from tensordict import TensorDict >>> from tensordict.nn import TensorDictModule, TensorDictSequential >>> from tensordict.prototype.fx import symbolic_trace >>> seq = TensorDictSequential( ... TensorDictModule(nn.Linear(3, 4), in_keys=["x"], out_keys=["hidden"]), ... TensorDictModule(nn.ReLU(), in_keys=["hidden"], out_keys=["y"]), ... ) >>> graph_module = symbolic_trace(seq) >>> td = graph_module(TensorDict(x=torch.ones(2, 3), batch_size=[2])) >>> sorted(td.keys()) ['hidden', 'x', 'y'] >>> expected = seq(TensorDict(x=torch.ones(2, 3), batch_size=[2]))["y"] >>> torch.equal(td["y"], expected) True