Skip to content
Merged
Show file tree
Hide file tree
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
27 changes: 26 additions & 1 deletion test/test_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
SoftmaxTransform,
StickBreakingTransform,
identity_transform)
from torch.distributions.utils import _finfo, probs_to_logits, softmax
from torch.distributions.utils import _finfo, probs_to_logits, softmax, lazy_property

TEST_NUMPY = True
try:
Expand Down Expand Up @@ -690,6 +690,31 @@ def test_enumerate_support_type(self):
except NotImplementedError:
pass

def test_lazy_property_grad(self):
x = torch.randn(1, requires_grad=True)

class Dummy(object):
@lazy_property
def y(self):
return x + 1

def test():
x.grad = None
Dummy().y.backward()
self.assertEqual(x.grad, torch.ones(1))

test()
with torch.no_grad():
test()

This comment was marked as off-topic.


mean = torch.randn(2)
cov = torch.eye(2, requires_grad=True)
distn = MultivariateNormal(mean, cov)
with torch.no_grad():
distn.scale_tril
distn.scale_tril.sum().backward()
self.assertIsNotNone(cov.grad)

def test_has_examples(self):
distributions_with_examples = set(e.Dist for e in EXAMPLES)
for Dist in globals().values():
Expand Down
3 changes: 2 additions & 1 deletion torch/distributions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ def __init__(self, wrapped):
def __get__(self, instance, obj_type=None):
if instance is None:
return self
value = self.wrapped(instance)
with torch.enable_grad():

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

value = self.wrapped(instance)
setattr(instance, self.wrapped.__name__, value)
return value