Skip to content

Commit fd0c103

Browse files
authored
Remove reference cycle - with exceptions (#228)
* Remove reference cycle - with exceptions * Fix for InliningInstructionTranslator
1 parent 7acc153 commit fd0c103

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

torchdynamo/convert_frame.py

-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ def transform(instructions, code_options):
152152
)
153153
tracer.run()
154154
output = tracer.output
155-
output.cleanup()
156155
assert output.output_instructions
157156
instructions[:] = output.output_instructions
158157
code_options.update(output.code_options)

torchdynamo/output_graph.py

+8
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ def restore_graphstate(self, state):
104104
# FX deepcopy doesn't work for a partially created graph, so just remove new nodes
105105
for node in reversed(list(self.graph.nodes)):
106106
if node not in graph_nodes:
107+
# Erasing node alone does not remove the meta information
108+
# So, remove the help tensor explicitly
109+
if "example_value" in node.meta:
110+
del node.meta["example_value"]
107111
self.graph.erase_node(node)
108112

109113
def count_calls(self):
@@ -374,3 +378,7 @@ def cleanup(self):
374378
# Cleanup graphargs
375379
for graph_arg in self.graphargs:
376380
graph_arg.erase()
381+
382+
for node in self.graph.nodes:
383+
if "example_value" in node.meta:
384+
del node.meta["example_value"]

torchdynamo/symbolic_convert.py

+8
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,14 @@ def run(self):
314314
f"{self.lineno} {typestr(e)}\n"
315315
)
316316
raise
317+
finally:
318+
# Cleanup the outputGraph to delete the held tensors. We perform the
319+
# cleanup only for InstructionTranslator and not
320+
# InliningInstructionTranslator. The InliningInstructionTranslator
321+
# mutates the output object and is restored to original state if
322+
# there was an exception.
323+
if isinstance(self, InstructionTranslator):
324+
self.output.cleanup()
317325

318326
def push(self, val):
319327
assert val is None or isinstance(

0 commit comments

Comments
 (0)