Skip to content

Commit

Permalink
[dynamo] Fix test (pytorch#125107)
Browse files Browse the repository at this point in the history
Pull Request resolved: pytorch#125107
Approved by: https://github.com/jansel
ghstack dependencies: pytorch#125097
  • Loading branch information
anijain2305 authored and petrex committed May 3, 2024
1 parent 00172bb commit e65d4f7
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions test/dynamo/test_misc.py
Expand Up @@ -10158,21 +10158,36 @@ def test_linear_module_free(self):
def test_outside_linear_module_free(self):
# Compared to test_linear_module_free, the linear
# layer is not the code object that is directly compiled.
def model_inp_ctr():
fc = torch.nn.Linear(100, 100)

class Mod(torch.nn.Module):
def __init__(self):
super().__init__()
self.fc_ref = fc
# This test does not use _test_compile_model_free because of difficulty
# in handling variable fc.

def forward(self, x):
return fc(x[0])
fc = torch.nn.Linear(100, 100)

class Mod(torch.nn.Module):
def __init__(self):
super().__init__()
self.fc_ref = fc

def forward(self, x):
return fc(x[0])

# return fc to keep it alive in _test_compile_model_free
return Mod(), (torch.randn(100, 100), fc)
cleared = False

def finalize():
nonlocal cleared
cleared = True

self._test_compile_model_free(model_inp_ctr, lambda mod: mod.fc_ref)
def run():
mod = Mod()
inp = torch.randn(100, 100)
weakref.finalize(mod.fc_ref, finalize)
torch.compile(mod, backend="eager")(inp)

run()
del fc # This should delete all the references
gc.collect()
self.assertTrue(cleared)

@unittest.skipIf(sys.version_info >= (3, 12), "leaks in 3.12+")
def test_parameter_free(self):
Expand Down

0 comments on commit e65d4f7

Please sign in to comment.