Skip to content

Commit

Permalink
Delete duplicate quant nodes in QAT (#48751)
Browse files Browse the repository at this point in the history
  • Loading branch information
yghstill authored Dec 8, 2022
1 parent 2a31c9d commit a85dedf
Showing 1 changed file with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2907,13 +2907,31 @@ def apply(self, graph):
graph, IrGraph
), 'graph must be the instance of IrGraph.'
fake_quant_dequant_ops = []
remove_fake_quant_ops = []
observer_out_node_names = []
for op in graph.all_op_nodes():
# collect observer node
if op.name() == "moving_average_abs_max_scale":
observer_out_node_names.append(op.output("Out")[0])

for op in graph.all_op_nodes():
if (
op.name() in _fake_quant_dequant_op_list
or op.name() == "moving_average_abs_max_scale"
):
fake_quant_dequant_ops.append(op)
var_name = op.input("X")[0]
if var_name in observer_out_node_names:
remove_fake_quant_ops.append(op)
else:
fake_quant_dequant_ops.append(op)

for _op in remove_fake_quant_ops:
x_node = graph._find_node_by_name(_op.inputs, _op.input("X")[0])
out_node = graph._find_node_by_name(
_op.outputs, _op.output("Out")[0]
)
for next_op_node in out_node.outputs:
graph.update_input_link(out_node, x_node, next_op_node)

for _op in fake_quant_dequant_ops:
self._replace_op(graph, _op)
Expand Down

0 comments on commit a85dedf

Please sign in to comment.