Skip to content

Commit ae2958e

Browse files
NXP backend: Add support for AdaptiveAvgPool1d, fix AdaptiveAvgPool1d tests, add check for 4D pool ops inputs (pytorch#21740)
### Summary Adds support for AdaptiveAvgPool1d, fixes tests for AdaptiveAvgPool1d. Adds check for 4D pool ops inputs. Moves `ops-aliases.py` from `backends/nxp/tests` to `backends/nxp` ### Test plan Unit tests provided. cc @robert-kalmar
1 parent 041a6ec commit ae2958e

59 files changed

Lines changed: 343 additions & 274 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎backends/nxp/backend/edge_helper.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import torch
1010

11-
from executorch.backends.nxp.tests.ops_aliases import (
11+
from executorch.backends.nxp.ops_aliases import (
1212
AddTensor,
1313
Amax,
1414
Amin,

‎backends/nxp/backend/ir/converter/node_converters/ops_converters/adaptive_avg_pool_2d_converter.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import executorch.backends.nxp.backend.ir.lib.tflite.Padding as tflPadding
77
import torch
88

9+
from executorch.backends.nxp.backend.edge_helper import input_rank
910
from executorch.backends.nxp.backend.ir.converter.conversion import common
1011
from executorch.backends.nxp.backend.ir.converter.node_converter import (
1112
CustomDelegationOptions,
@@ -45,6 +46,10 @@ def _is_supported_in_IR(
4546
parameters_mapping: dict[str, Parameter],
4647
custom_delegation_options: CustomDelegationOptions,
4748
) -> bool:
49+
# The input must be 4D.
50+
if input_rank(node, 0) != 4:
51+
return False
52+
4853
input_size = node.args[0].meta["val"].shape
4954
output_size = node.args[1]
5055

‎backends/nxp/backend/ir/converter/node_converters/ops_converters/avg_pool_2d_converter.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import numpy as np
77
import torch
8+
9+
from executorch.backends.nxp.backend.edge_helper import input_rank
810
from executorch.backends.nxp.backend.ir.converter.conversion import (
911
aten_translator,
1012
common,
@@ -36,6 +38,10 @@ def _is_supported_in_IR(
3638
parameters_mapping: dict[str, Parameter],
3739
custom_delegation_options: CustomDelegationOptions,
3840
) -> bool:
41+
# The input must be 4D.
42+
if input_rank(node, 0) != 4:
43+
return False
44+
3945
n_args = len(node.args)
4046

4147
padding = node.args[3] if n_args >= 4 else [0, 0]

‎backends/nxp/backend/ir/converter/node_converters/ops_converters/max_pool2d_with_indices_converter.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import numpy as np
99
import torch
10-
from executorch.backends.nxp.backend.edge_helper import try_get_arg
10+
from executorch.backends.nxp.backend.edge_helper import input_rank, try_get_arg
1111
from executorch.backends.nxp.backend.ir.converter.conversion import (
1212
aten_translator,
1313
common,
@@ -42,6 +42,10 @@ def _is_supported_in_IR(
4242
parameters_mapping: dict[str, Parameter],
4343
custom_delegation_options: CustomDelegationOptions,
4444
) -> bool:
45+
# The input must be 4D.
46+
if input_rank(node, 0) != 4:
47+
return False
48+
4549
kernel_size, stride, padding, dilation, ceil_mode = (
4650
MaxPool2DWithIndicesConverter._get_node_args(node)
4751
)

‎backends/nxp/backend/node_format_inference.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
try_get_arg,
1616
)
1717
from executorch.backends.nxp.backend.edge_program_converter import functions_converters
18-
from executorch.backends.nxp.tests.ops_aliases import (
18+
from executorch.backends.nxp.ops_aliases import (
1919
AdaptiveAvgPool2D,
2020
Amax,
2121
Amin,

‎backends/nxp/edge_passes/move_auxiliary_operator_into_separate_qdq_cluster_pass.py‎

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,35 @@
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-
86
import torch
97

108
from executorch.backends.nxp.edge_passes.neutron_edge_pass import NeutronEdgePass
119
from 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+
)
1632
from torch.fx import Node
1733
from 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

3736
def 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

7371
def _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)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@
5656
SliceCopy = exir_ops.edge.aten.slice_copy.Tensor
5757
Softmax = exir_ops.edge.aten._softmax.default
5858
Squeeze = exir_ops.edge.aten.squeeze.default
59+
SqueezeCopy = exir_ops.edge.aten.squeeze_copy.dims
5960
SqueezeDim = exir_ops.edge.aten.squeeze.dim
6061
SqueezeDims = exir_ops.edge.aten.squeeze.dims
6162
SubTensor = exir_ops.edge.aten.sub.Tensor
6263
SumDimIntList = exir_ops.edge.aten.sum.dim_IntList
6364
Tanh = exir_ops.edge.aten.tanh.default
6465
Tanh_ = exir_ops.edge.aten.tanh_.default
6566
Unsqueeze = exir_ops.edge.aten.unsqueeze.default
67+
UnsqueezeCopy = exir_ops.edge.aten.unsqueeze_copy.default
6668
UpsampleBilinear2D = exir_ops.edge.aten.upsample_bilinear2d.vec
6769
UpsampleNearest2D = exir_ops.edge.aten.upsample_nearest2d.vec
6870
ViewCopy = exir_ops.edge.aten.view_copy.default

‎backends/nxp/quantizer/neutron_quantizer.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
from executorch.backends.nxp.quantizer.patterns import (
1414
AbsPattern,
1515
ActivationsConcatClusterPattern,
16-
AdaptiveAvgPoolPattern,
16+
AdaptiveAvgPool1DPattern,
17+
AdaptiveAvgPool2DPattern,
1718
AddmmPattern,
1819
AddTensorPattern,
1920
AmaxPattern,
@@ -267,7 +268,8 @@ def __init__(self, neutron_target_spec: NeutronTargetSpec, is_qat: bool = False)
267268
super().__init__(
268269
[
269270
OpQuantizer(AbsPattern(is_qat=is_qat), static_qconfig),
270-
OpQuantizer(AdaptiveAvgPoolPattern(is_qat=is_qat), static_qconfig),
271+
OpQuantizer(AdaptiveAvgPool1DPattern(is_qat=is_qat), static_qconfig),
272+
OpQuantizer(AdaptiveAvgPool2DPattern(is_qat=is_qat), static_qconfig),
271273
OpQuantizer(AddTensorPattern(is_qat=is_qat), static_qconfig),
272274
OpQuantizer(AddmmPattern(self, is_qat=is_qat), static_fc_qconfig),
273275
OpQuantizer(AmaxPattern(is_qat=is_qat), static_qconfig),

‎backends/nxp/quantizer/patterns.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,16 @@ def partition_types(self):
279279
return [torch.ops.aten.abs.default]
280280

281281

282-
class AdaptiveAvgPoolPattern(SharedSpecPattern):
282+
class AdaptiveAvgPool1DPattern(SharedSpecPattern):
283+
"""
284+
Quantizer for AdaptiveAvgPool1D operator.
285+
"""
286+
287+
def partition_types(self):
288+
return [torch.ops.aten.adaptive_avg_pool1d.default]
289+
290+
291+
class AdaptiveAvgPool2DPattern(SharedSpecPattern):
283292
"""
284293
Quantizer for AdaptiveAvgPool2D operator.
285294
"""

‎backends/nxp/tests/generic_tests/test_add_batch_size_for_3d_input_pool_2d_ops.py‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
from executorch.backends.nxp.aten_passes.neutron_aten_pass_manager import (
1616
NeutronAtenPassManager,
1717
)
18+
from executorch.backends.nxp.ops_aliases import (
19+
AdaptiveAvgPool2D,
20+
AvgPool2D,
21+
GetItem,
22+
MaxPool2DWithIndices,
23+
ViewCopy,
24+
)
1825
from executorch.backends.nxp.tests.dataset_creator import RandomDatasetCreator
1926
from executorch.backends.nxp.tests.executorch_pipeline import neutron_target_spec
2027
from executorch.backends.nxp.tests.executors import graph_contains_any_of_ops
@@ -28,13 +35,6 @@
2835
MaxPool2dModule,
2936
)
3037
from executorch.backends.nxp.tests.nsys_testing import lower_run_compare
31-
from executorch.backends.nxp.tests.ops_aliases import (
32-
AdaptiveAvgPool2D,
33-
AvgPool2D,
34-
GetItem,
35-
MaxPool2DWithIndices,
36-
ViewCopy,
37-
)
3838

3939

4040
@pytest.fixture(autouse=True)

0 commit comments

Comments
 (0)