mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
Pull Request resolved: https://github.com/pytorch/pytorch/pull/119921 Approved by: https://github.com/oulgen
51 lines
1.2 KiB
Python
51 lines
1.2 KiB
Python
# Owner(s): ["module: dynamo"]
|
|
|
|
import torch
|
|
import torch._dynamo
|
|
import torch._dynamo.test_case
|
|
from torch._dynamo.source import (
|
|
AttrSource,
|
|
GlobalSource,
|
|
is_from_local_source,
|
|
LocalSource,
|
|
)
|
|
|
|
|
|
class SourceTests(torch._dynamo.test_case.TestCase):
|
|
def test_is_local(self):
|
|
x_src = LocalSource("x")
|
|
y_src = GlobalSource("y")
|
|
|
|
attr_x_a = AttrSource(x_src, "a")
|
|
attr_y_b = AttrSource(y_src, "b")
|
|
|
|
self.assertTrue(is_from_local_source(attr_x_a))
|
|
self.assertEqual(is_from_local_source(attr_y_b), False)
|
|
|
|
def test_property_closure(self):
|
|
def external_property():
|
|
closed_value = 7
|
|
|
|
def internal_function(self):
|
|
return closed_value
|
|
|
|
return internal_function
|
|
|
|
class Elements:
|
|
myprop = property(external_property())
|
|
|
|
def func(elements):
|
|
if not elements.myprop:
|
|
return torch.tensor([1, 2, 3])
|
|
else:
|
|
return torch.tensor([4, 5, 6])
|
|
|
|
e = Elements()
|
|
a = func(e)
|
|
b = torch.compile(func, backend="eager", fullgraph=True)(e)
|
|
self.assertEqual(a, b)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
torch._dynamo.test_case.run_tests()
|