Skip to content

Commit 7aeb1d4

Browse files
authored
Remove reference cycle (#223)
1 parent 243222e commit 7aeb1d4

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

torchdynamo/convert_frame.py

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def transform(instructions, code_options):
149149
)
150150
tracer.run()
151151
output = tracer.output
152+
output.cleanup()
152153
assert output.output_instructions
153154
instructions[:] = output.output_instructions
154155
code_options.update(output.code_options)

torchdynamo/output_graph.py

+9
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,12 @@ def add_output_instructions(self, prefix: List[Instruction]):
365365

366366
def install_global(self, name, value):
367367
self.cleanups.append(CleanupHook.create(self.root_globals, name, value))
368+
369+
def cleanup(self):
370+
# There is a reference cycle between tracer and OutputGraph, causing
371+
# some of the tensor objects to be held alive for longer than necessary.
372+
self.root_tx = None
373+
374+
# Cleanup graphargs
375+
for graph_arg in self.graphargs:
376+
graph_arg.erase()

torchdynamo/variables/builder.py

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ def get_examples(self):
7070
def __len__(self):
7171
return 1
7272

73+
def erase(self):
74+
self.example = None
75+
7376

7477
class VariableBuilder:
7578
"""Wrap a python value in a VariableTracker() instance"""

0 commit comments

Comments
 (0)