Skip to content

Commit

Permalink
Remove reference cycle (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
anijain2305 authored May 10, 2022
1 parent 243222e commit 7aeb1d4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions torchdynamo/convert_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def transform(instructions, code_options):
)
tracer.run()
output = tracer.output
output.cleanup()
assert output.output_instructions
instructions[:] = output.output_instructions
code_options.update(output.code_options)
Expand Down
9 changes: 9 additions & 0 deletions torchdynamo/output_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,12 @@ def add_output_instructions(self, prefix: List[Instruction]):

def install_global(self, name, value):
self.cleanups.append(CleanupHook.create(self.root_globals, name, value))

def cleanup(self):
# There is a reference cycle between tracer and OutputGraph, causing
# some of the tensor objects to be held alive for longer than necessary.
self.root_tx = None

# Cleanup graphargs
for graph_arg in self.graphargs:
graph_arg.erase()
3 changes: 3 additions & 0 deletions torchdynamo/variables/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ def get_examples(self):
def __len__(self):
return 1

def erase(self):
self.example = None


class VariableBuilder:
"""Wrap a python value in a VariableTracker() instance"""
Expand Down

0 comments on commit 7aeb1d4

Please sign in to comment.