33# This source code is licensed under the BSD-style license found in the
44# LICENSE file in the root directory of this source tree.
55
6- import operator
7-
86import torch
97
108from executorch .backends .nxp .edge_passes .neutron_edge_pass import NeutronEdgePass
119from executorch .backends .nxp .neutron_partitioner import QDQClusterRecognizer
12- from executorch .backends .nxp .tests .ops_aliases import PermuteCopy
1310
14- # noinspection PyProtectedMember
15- from executorch .exir .dialects ._ops import ops as exir_ops
11+ from executorch .backends .nxp .ops_aliases import (
12+ AdaptiveAvgPool2D ,
13+ AddMM ,
14+ AvgPool2D ,
15+ Clone ,
16+ CloneDimOrder ,
17+ Convolution ,
18+ DequantizePerTensor ,
19+ GetItem ,
20+ HardTanh ,
21+ MaxPool2DWithIndices ,
22+ MM ,
23+ PermuteCopy ,
24+ QuantizePerTensor ,
25+ Relu ,
26+ Sigmoid ,
27+ SqueezeCopy ,
28+ Tanh ,
29+ UnsqueezeCopy ,
30+ ViewCopy ,
31+ )
1632from torch .fx import Node
1733from torch .fx .passes .infra .pass_base import PassResult
1834
19- # Operator aliases for better readability.
20- AddMM = exir_ops .edge .aten .addmm .default
21- AvgPool2D = exir_ops .edge .aten .avg_pool2d .default
22- MaxPool2D = exir_ops .edge .aten .max_pool2d_with_indices .default
23- Conv = exir_ops .edge .aten .convolution .default
24- Clone = exir_ops .edge .aten .clone .default
25- CloneDimOrder = exir_ops .edge .dim_order_ops ._clone_dim_order .default
26- Getitem = operator .getitem
27- HardTanh = exir_ops .edge .aten .hardtanh .default
28- MM = exir_ops .edge .aten .mm .default
29- Relu = exir_ops .edge .aten .relu .default
30- Sigmoid = exir_ops .edge .aten .sigmoid .default
31- SqueezeCopy = exir_ops .edge .aten .squeeze_copy .dims
32- Tanh = exir_ops .edge .aten .tanh .default
33- UnsqueezeCopy = exir_ops .edge .aten .unsqueeze_copy .default
34- ViewCopy = exir_ops .edge .aten .view_copy .default
35-
3635
3736def insert_qdq_pair_after_node (
3837 graph : torch .fx .Graph , anchor : torch .fx .Node , q_params : tuple
@@ -41,7 +40,7 @@ def insert_qdq_pair_after_node(
4140 with graph .inserting_after (anchor ):
4241 quantize_op = graph .create_node (
4342 op = "call_function" ,
44- target = exir_ops . edge . quantized_decomposed . quantize_per_tensor . default ,
43+ target = QuantizePerTensor ,
4544 args = (), # Will be added later.
4645 )
4746 quantize_op .meta = anchor .meta
@@ -50,7 +49,7 @@ def insert_qdq_pair_after_node(
5049 with graph .inserting_after (quantize_op ):
5150 dequantize_op = graph .create_node (
5251 op = "call_function" ,
53- target = exir_ops . edge . quantized_decomposed . dequantize_per_tensor . default ,
52+ target = DequantizePerTensor ,
5453 args = (quantize_op ,) + q_params ,
5554 )
5655 dequantize_op .meta = quantize_op .meta
@@ -65,17 +64,15 @@ def _is_dequantize(node_: Node) -> bool:
6564 return (
6665 hasattr (node_ , "op" )
6766 and node_ .op == "call_function"
68- and node_ .target
69- == exir_ops .edge .quantized_decomposed .dequantize_per_tensor .default
67+ and node_ .target == DequantizePerTensor
7068 )
7169
7270
7371def _is_quantize (node_ : Node ) -> bool :
7472 return (
7573 hasattr (node_ , "op" )
7674 and node_ .op == "call_function"
77- and node_ .target
78- == exir_ops .edge .quantized_decomposed .quantize_per_tensor .default
75+ and node_ .target == QuantizePerTensor
7976 )
8077
8178
@@ -117,7 +114,7 @@ class MoveLeadingAuxiliaryOperatorIntoSeparateQDQClusterPass(NeutronEdgePass):
117114 PermuteCopy ,
118115 ],
119116 ViewCopy : [Clone , CloneDimOrder ],
120- Conv : [
117+ Convolution : [
121118 ViewCopy , # For 1D conv
122119 ],
123120 # AvgPool1D is represented in edge as Unsqueeze -> AvgPool2D -> Squeeze. The reshaping nodes must be moved out
@@ -126,9 +123,15 @@ class MoveLeadingAuxiliaryOperatorIntoSeparateQDQClusterPass(NeutronEdgePass):
126123 ViewCopy ,
127124 UnsqueezeCopy ,
128125 ],
129- # MaxPool1D is represented in edge as Unsqueeze -> MaxPool2D -> Getitem -> Squeeze. The reshaping nodes must be moved out
126+ # MaxPool1D is represented in edge as Unsqueeze -> MaxPool2D -> GetItem -> Squeeze. The reshaping nodes must be moved out
130127 # of the cluster. Instead of [Un]squeeze, ViewCopy can be used as well.
131- MaxPool2D : [
128+ MaxPool2DWithIndices : [
129+ ViewCopy ,
130+ UnsqueezeCopy ,
131+ ],
132+ # AdaptiveAvgPool1D is represented in edge as Unsqueeze -> AdaptiveAvgPool2D -> Squeeze. The reshaping nodes
133+ # must be moved out of the cluster. Instead of [Un]squeeze, ViewCopy can be used as well.
134+ AdaptiveAvgPool2D : [
132135 ViewCopy ,
133136 UnsqueezeCopy ,
134137 ],
@@ -222,7 +225,7 @@ class MoveTrailingAuxiliaryOperatorIntoSeparateQDQClusterPass(NeutronEdgePass):
222225 Sigmoid ,
223226 Tanh ,
224227 ],
225- Conv : [
228+ Convolution : [
226229 HardTanh ,
227230 Relu ,
228231 Sigmoid ,
@@ -236,9 +239,15 @@ class MoveTrailingAuxiliaryOperatorIntoSeparateQDQClusterPass(NeutronEdgePass):
236239 ViewCopy ,
237240 SqueezeCopy ,
238241 ],
239- # MaxPool1D is represented in edge as Unsqueeze -> MaxPool2D -> Getitem -> Squeeze. The reshaping nodes must be moved out
242+ # MaxPool1D is represented in edge as Unsqueeze -> MaxPool2D -> GetItem -> Squeeze. The reshaping nodes must be moved out
240243 # of the cluster. Instead of [Un]squeeze, ViewCopy can be used as well.
241- Getitem : [
244+ GetItem : [
245+ ViewCopy ,
246+ SqueezeCopy ,
247+ ],
248+ # AdaptiveAvgPool1D is represented in edge as Unsqueeze -> AdaptiveAvgPool2D -> Squeeze. The reshaping nodes
249+ # must be moved out of the cluster. Instead of [Un]squeeze, ViewCopy can be used as well.
250+ AdaptiveAvgPool2D : [
242251 ViewCopy ,
243252 SqueezeCopy ,
244253 ],
@@ -278,7 +287,7 @@ def run(self, graph_module: torch.fx.GraphModule) -> PassResult:
278287 # satisfy the requirements of the `QDQClusterRecognizer`.
279288 actual_main_cluster_node = (
280289 main_cluster_node
281- if main_cluster_node .target != Getitem
290+ if main_cluster_node .target != GetItem
282291 else main_cluster_node .args [0 ]
283292 )
284293 cluster = QDQClusterRecognizer ().get_qdq_cluster (actual_main_cluster_node )
0 commit comments