Skip to content
This repository was archived by the owner on Feb 7, 2023. It is now read-only.

Commit 7f7a30c

Browse files
authored
Correct Implemenation of DeadCodeElimination (#485)
1 parent d1b90ce commit 7f7a30c

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

onnx_coreml/_transformers.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -797,20 +797,28 @@ def __call__(self, graph): # type: (Graph) -> Graph
797797
output_names = set([str(output_[0]) for output_ in graph.outputs])
798798

799799
nodes_to_be_removed = []
800-
use_set = set()
800+
uses = {}
801801

802-
for node in graph.nodes:
803-
for _input in node.inputs:
804-
use_set.add(_input)
802+
for _output in output_names:
803+
uses[_output] = uses.get(_output, 0) + 1
805804

806805
for node in graph.nodes:
806+
for _input in node.inputs:
807+
uses[_input] = uses.get(_input, 0) + 1
808+
809+
for node in reversed(graph.nodes):
807810
output_used = False
808811
for _output in node.outputs:
809-
if _output in output_names or _output in use_set:
812+
if _output in uses:
810813
output_used = True
811814
break
815+
812816
if not output_used:
813817
# Remove current node
818+
for _input in node.inputs:
819+
uses[_input] -= 1
820+
if uses[_input] == 0:
821+
del uses[_input]
814822
nodes_to_be_removed.append(node.name)
815823
for parent in node.parents:
816824
parent.children.remove(node)
@@ -821,7 +829,7 @@ def __call__(self, graph): # type: (Graph) -> Graph
821829
transformed_nodes.append(node)
822830

823831
for _input in input_names:
824-
if _input not in use_set:
832+
if _input not in uses:
825833
for i in range(len(graph.inputs)):
826834
if graph.inputs[i][0] is _input:
827835
graph.inputs.remove(graph.inputs[i])

0 commit comments

Comments
 (0)