Skip to content

Commit fdae61d

Browse files
authored
Clean up FuseQuantDequantToRequantizePass to use get_arg (pytorch#21756)
Differential Revision: D115257095 Pull Request resolved: pytorch#21756
1 parent 187442b commit fdae61d

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

‎backends/cadence/aot/fuse_ops.py‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,12 @@ def _create_requantize_node(
662662
)
663663

664664
def _quant_params_match(self, node1: torch.fx.Node, node2: torch.fx.Node) -> bool:
665-
return node1.args[1:] == node2.args[1:]
665+
arg_names = ("scale", "zero_point", "quant_min", "quant_max", "dtype")
666+
arg_types = (float, int, int, int, torch.dtype)
667+
return all(
668+
get_arg(node1, name, arg_type) == get_arg(node2, name, arg_type)
669+
for name, arg_type in zip(arg_names, arg_types)
670+
)
666671

667672
def check_ok_to_fuse(
668673
self,
@@ -684,8 +689,11 @@ def get_fused_node(
684689
consumer: torch.fx.Node,
685690
graph_module: torch.fx.GraphModule,
686691
) -> torch.fx.Node:
687-
in_scale, in_zero_point = producer.args[1:3]
688-
in_tensor, out_scale, out_zero_point, _, _, out_dtype = consumer.args
692+
in_scale = get_arg(producer, "scale", float)
693+
in_zero_point = get_arg(producer, "zero_point", int)
694+
out_scale = get_arg(consumer, "scale", float)
695+
out_zero_point = get_arg(consumer, "zero_point", int)
696+
out_dtype = get_arg(consumer, "dtype", torch.dtype)
689697
if in_scale == out_scale and in_zero_point == out_zero_point:
690698
# If the quant params match, we can remove both dequantize-quantize ops.
691699
return cast(torch.fx.Node, consumer.args[0])
@@ -697,11 +705,11 @@ def get_fused_node(
697705
with graph_module.graph.inserting_before(consumer):
698706
requantize_node = self._create_requantize_node(
699707
in_tensor=cast(torch.fx.Node, consumer.args[0]),
700-
in_scale=cast(float, in_scale),
701-
in_zero_point=cast(int, in_zero_point),
702-
out_scale=cast(float, out_scale),
703-
out_zero_point=cast(int, out_zero_point),
704-
out_dtype=cast(torch.dtype, out_dtype),
708+
in_scale=in_scale,
709+
in_zero_point=in_zero_point,
710+
out_scale=out_scale,
711+
out_zero_point=out_zero_point,
712+
out_dtype=out_dtype,
705713
graph=graph_module.graph,
706714
)
707715
return requantize_node

0 commit comments

Comments
 (0)