Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
[ghstack-poisoned]
  • Loading branch information
vmoens committed Dec 2, 2024
1 parent 7217e7a commit e0c29ce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tensordict/nn/probabilistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,21 @@ def get_dist(
include_sum=self.include_sum,
)

@property
def default_interaction_type(self):
"""Returns the `default_interaction_type` of the module using an iterative heuristic.
This property iterates over all modules in reverse order, attempting to retrieve the
`default_interaction_type` attribute from any child module. The first non-None value
encountered is returned. If no such value is found, a default `interaction_type()` is returned.
"""
for m in reversed(self.module):
interaction = getattr(m, "default_interaction_type", None)
if interaction is not None:
return interaction
return interaction_type()

def log_prob(
self,
tensordict,
Expand Down
7 changes: 7 additions & 0 deletions test/test_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ def test_stateful_probabilistic_deprec(self, lazy, it, out_keys):
)

tensordict_module = ProbabilisticTensorDictSequential(net, prob_module)
assert tensordict_module.default_interaction_type is not None

td = TensorDict({"in": torch.randn(3, 3)}, [3])
with set_interaction_type(it):
Expand Down Expand Up @@ -450,6 +451,7 @@ def test_stateful_probabilistic_kwargs(self, lazy, it, out_keys, max_dist):
)

tensordict_module = ProbabilisticTensorDictSequential(net, prob_module)
assert tensordict_module.default_interaction_type is not None

td = TensorDict({"in": torch.randn(3, 3)}, [3])
with set_interaction_type(it):
Expand Down Expand Up @@ -513,6 +515,7 @@ def test_stateful_probabilistic(self, lazy, it, out_keys):
tensordict_module = ProbabilisticTensorDictSequential(
net, normal_params, prob_module
)
assert tensordict_module.default_interaction_type is not None

td = TensorDict({"in": torch.randn(3, 3)}, [3])
with set_interaction_type(it):
Expand Down Expand Up @@ -962,6 +965,7 @@ def test_stateful_probabilistic_deprec(self, lazy):
tdmodule = ProbabilisticTensorDictSequential(
tdmodule1, dummy_tdmodule, tdmodule2, prob_module
)
assert tdmodule.default_interaction_type is not None

assert hasattr(tdmodule, "__setitem__")
assert len(tdmodule) == 4
Expand Down Expand Up @@ -1002,6 +1006,7 @@ def test_probtdseq(self, return_log_prob, td_out):
default_interaction_type="random",
),
)
assert mod.default_interaction_type is not None
inp = TensorDict({"a": 0.0, "b": 1.0})
inp_clone = inp.clone()
if td_out:
Expand Down Expand Up @@ -1073,6 +1078,7 @@ def test_probtdseq_multdist(self, include_sum, aggregate_probabilities, inplace)
inplace=inplace,
return_composite=True,
)
assert tdm.default_interaction_type is not None
dist: CompositeDistribution = tdm.get_dist(TensorDict(x=torch.randn(10, 3)))
s = dist.sample()
assert dist.aggregate_probabilities is aggregate_probabilities
Expand Down Expand Up @@ -1129,6 +1135,7 @@ def test_probtdseq_intermediate_dist(
inplace=inplace,
return_composite=True,
)
assert tdm.default_interaction_type is not None
dist: CompositeDistribution = tdm.get_dist(TensorDict(x=torch.randn(10, 3)))
assert isinstance(dist, CompositeDistribution)

Expand Down

0 comments on commit e0c29ce

Please sign in to comment.