set_composite_lp_aggregate#
- class tensordict.nn.set_composite_lp_aggregate(mode: bool = True, *, thread_local: bool = False)#
Bases:
_DecoratorContextManagerSets whether a
CompositeDistributionsums its log-probabilities in one tensor.When
composite_lp_aggregate()returnsTrue, the log-probabilities and entropies of aCompositeDistributionare summed into one tensor with the shape of the root tensordict. When it returnsFalse(the default), they are returned as a TensorDict with one<name>_log_prob(or<name>_entropy) entry per sampled key. The value also decides the keys where probabilistic modules write their log-probabilities: see the log-probability key table ofProbabilisticTensorDictModule, and Modules: TensorDictModule and tensordict.nn.The
COMPOSITE_LP_AGGREGATEenvironment variable sets the value too. It works as a context manager and as a decorator, andset()andunset()change it outside of a block.- Parameters:
mode (bool, optional) – the value that
composite_lp_aggregate()returns inside the block. Defaults toTrue.- Keyword Arguments:
thread_local (bool, optional) – if
True, the mode applies to the calling thread only, and blocks that other threads enter do not change it. Inside the block, blocks without this flag that the same thread enters stay in that thread too. Threads started inside the block see the process-wide mode. IfFalse, the mode applies to the whole process. Defaults toFalse.
Note
Threads. The mode is process-wide by default, and is meant to be set once for the whole process, for example with
set()before you build the modules: it decides the keys of the log-probabilities, which the threads that write them and the threads that read them must agree on. Awithblock in one thread also changes the value in other threads at that time, unless it passesthread_local=True. A thread started inside athread_localblock reads the process-wide value, not the block’s.Examples
>>> import torch >>> from tensordict import TensorDict >>> from tensordict.nn import CompositeDistribution, set_composite_lp_aggregate >>> from torch import distributions as d >>> _ = torch.manual_seed(0) >>> params = TensorDict({ ... "cont": {"loc": torch.randn(3, 4), "scale": torch.rand(3, 4)}, ... ("nested", "disc"): {"logits": torch.randn(3, 10)} ... }, [3]) >>> dist = CompositeDistribution(params, ... distribution_map={"cont": d.Normal, ("nested", "disc"): d.Categorical}) >>> sample = dist.sample((4,)) >>> with set_composite_lp_aggregate(False): ... lp = dist.log_prob(sample) ... print(lp) TensorDict(batch_size=[4,3], fields={ cont_log_prob: f32[4], nested: {disc_log_prob: f32[]}}) >>> with set_composite_lp_aggregate(True): ... lp = dist.log_prob(sample) ... print(lp) tensor([[-2.0886, -1.2155, -0.0414], [-2.8973, -5.5165, 2.4402], [-0.2806, -1.2799, 3.1733], [-3.0407, -4.3593, 0.5763]])