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+
68import torch
79
810from executorch .backends .nxp .edge_passes .neutron_edge_pass import NeutronEdgePass
911from executorch .backends .nxp .neutron_partitioner import QDQClusterRecognizer
12+ from executorch .backends .nxp .tests .ops_aliases import PermuteCopy
1013
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- )
14+ # noinspection PyProtectedMember
15+ from executorch .exir .dialects ._ops import ops as exir_ops
3216from torch .fx import Node
3317from torch .fx .passes .infra .pass_base import PassResult
3418
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+
3536
3637def insert_qdq_pair_after_node (
3738 graph : torch .fx .Graph , anchor : torch .fx .Node , q_params : tuple
@@ -40,7 +41,7 @@ def insert_qdq_pair_after_node(
4041 with graph .inserting_after (anchor ):
4142 quantize_op = graph .create_node (
4243 op = "call_function" ,
43- target = QuantizePerTensor ,
44+ target = exir_ops . edge . quantized_decomposed . quantize_per_tensor . default ,
4445 args = (), # Will be added later.
4546 )
4647 quantize_op .meta = anchor .meta
@@ -49,7 +50,7 @@ def insert_qdq_pair_after_node(
4950 with graph .inserting_after (quantize_op ):
5051 dequantize_op = graph .create_node (
5152 op = "call_function" ,
52- target = DequantizePerTensor ,
53+ target = exir_ops . edge . quantized_decomposed . dequantize_per_tensor . default ,
5354 args = (quantize_op ,) + q_params ,
5455 )
5556 dequantize_op .meta = quantize_op .meta
@@ -64,15 +65,17 @@ def _is_dequantize(node_: Node) -> bool:
6465 return (
6566 hasattr (node_ , "op" )
6667 and node_ .op == "call_function"
67- and node_ .target == DequantizePerTensor
68+ and node_ .target
69+ == exir_ops .edge .quantized_decomposed .dequantize_per_tensor .default
6870 )
6971
7072
7173def _is_quantize (node_ : Node ) -> bool :
7274 return (
7375 hasattr (node_ , "op" )
7476 and node_ .op == "call_function"
75- and node_ .target == QuantizePerTensor
77+ and node_ .target
78+ == exir_ops .edge .quantized_decomposed .quantize_per_tensor .default
7679 )
7780
7881
@@ -114,7 +117,7 @@ class MoveLeadingAuxiliaryOperatorIntoSeparateQDQClusterPass(NeutronEdgePass):
114117 PermuteCopy ,
115118 ],
116119 ViewCopy : [Clone , CloneDimOrder ],
117- Convolution : [
120+ Conv : [
118121 ViewCopy , # For 1D conv
119122 ],
120123 # AvgPool1D is represented in edge as Unsqueeze -> AvgPool2D -> Squeeze. The reshaping nodes must be moved out
@@ -123,15 +126,9 @@ class MoveLeadingAuxiliaryOperatorIntoSeparateQDQClusterPass(NeutronEdgePass):
123126 ViewCopy ,
124127 UnsqueezeCopy ,
125128 ],
126- # MaxPool1D is represented in edge as Unsqueeze -> MaxPool2D -> GetItem -> Squeeze. The reshaping nodes must be moved out
129+ # MaxPool1D is represented in edge as Unsqueeze -> MaxPool2D -> Getitem -> Squeeze. The reshaping nodes must be moved out
127130 # of the cluster. Instead of [Un]squeeze, ViewCopy can be used as well.
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 : [
131+ MaxPool2D : [
135132 ViewCopy ,
136133 UnsqueezeCopy ,
137134 ],
@@ -225,7 +222,7 @@ class MoveTrailingAuxiliaryOperatorIntoSeparateQDQClusterPass(NeutronEdgePass):
225222 Sigmoid ,
226223 Tanh ,
227224 ],
228- Convolution : [
225+ Conv : [
229226 HardTanh ,
230227 Relu ,
231228 Sigmoid ,
@@ -239,15 +236,9 @@ class MoveTrailingAuxiliaryOperatorIntoSeparateQDQClusterPass(NeutronEdgePass):
239236 ViewCopy ,
240237 SqueezeCopy ,
241238 ],
242- # MaxPool1D is represented in edge as Unsqueeze -> MaxPool2D -> GetItem -> Squeeze. The reshaping nodes must be moved out
239+ # MaxPool1D is represented in edge as Unsqueeze -> MaxPool2D -> Getitem -> Squeeze. The reshaping nodes must be moved out
243240 # of the cluster. Instead of [Un]squeeze, ViewCopy can be used as well.
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 : [
241+ Getitem : [
251242 ViewCopy ,
252243 SqueezeCopy ,
253244 ],
@@ -287,7 +278,7 @@ def run(self, graph_module: torch.fx.GraphModule) -> PassResult:
287278 # satisfy the requirements of the `QDQClusterRecognizer`.
288279 actual_main_cluster_node = (
289280 main_cluster_node
290- if main_cluster_node .target != GetItem
281+ if main_cluster_node .target != Getitem
291282 else main_cluster_node .args [0 ]
292283 )
293284 cluster = QDQClusterRecognizer ().get_qdq_cluster (actual_main_cluster_node )
0 commit comments