Skip to content

Commit a0a9a2a

Browse files
cccclaikeyprocedure
authored andcommitted
Add op_amax support (pytorch#9955)
Summary: As title, add op_amax to support an internal model, add unit test in test_qnn_delegate.py Differential Revision: D72613814
1 parent 3134050 commit a0a9a2a

File tree

7 files changed

+124
-0
lines changed

7 files changed

+124
-0
lines changed

backends/qualcomm/_passes/layout_transform.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class LayoutTransform(ExportPass):
4747
layout_agnostic_ops = {
4848
exir_ops.edge.aten.abs.default,
4949
exir_ops.edge.aten.add.Tensor,
50+
exir_ops.edge.aten.amax.default,
5051
exir_ops.edge.aten.bitwise_or.Tensor,
5152
exir_ops.edge.aten.bmm.default,
5253
exir_ops.edge.aten.bitwise_and.Tensor,

backends/qualcomm/builders/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
op_abs,
1010
op_adaptive_avg_pool2d,
1111
op_add,
12+
op_amax,
1213
op_and,
1314
op_arange,
1415
op_argmin,
@@ -95,6 +96,7 @@
9596
op_abs,
9697
op_adaptive_avg_pool2d,
9798
op_add,
99+
op_amax,
98100
op_and,
99101
op_arange,
100102
op_argmin,
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Copyright (c) Qualcomm Innovation Center, Inc.
2+
# All rights reserved
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
from typing import cast, Dict, List
7+
8+
import executorch.backends.qualcomm.python.PyQnnWrapperAdaptor as PyQnnWrapper
9+
10+
import numpy as np
11+
12+
import torch
13+
from executorch.backends.qualcomm.utils.constants import QCOM_AXIS_ORDER, QCOM_DATA
14+
15+
from .node_visitor import NodeVisitor, register_node_visitor
16+
from .qnn_constants import OpAmax, QNN_OP_PACKAGE_NAME_QTI_AISW
17+
18+
19+
@register_node_visitor
20+
class AMax(NodeVisitor):
21+
target = ["aten.amax.default"]
22+
23+
def __init__(self, *args) -> None:
24+
super().__init__(*args)
25+
26+
def define_node(
27+
self,
28+
node: torch.fx.Node,
29+
nodes_to_wrappers: Dict[torch.fx.Node, PyQnnWrapper.TensorWrapper],
30+
) -> PyQnnWrapper.PyQnnOpWrapper:
31+
input_node = node.args[0]
32+
input_tensor = self.get_tensor(input_node, node)
33+
input_tensor_wrapper = self.define_tensor(
34+
input_node,
35+
node,
36+
input_tensor,
37+
PyQnnWrapper.Qnn_TensorType_t.QNN_TENSOR_TYPE_NATIVE,
38+
nodes_to_wrappers,
39+
)
40+
41+
# mean dims and keep dims
42+
mean_dims = cast(List[int], node.args[1])
43+
mean_dims = [
44+
mean_dim % len(input_node.meta["val"].shape) for mean_dim in mean_dims
45+
]
46+
if QCOM_AXIS_ORDER in node.meta:
47+
mean_dims = [
48+
node.meta[QCOM_AXIS_ORDER].index(mean_dim) for mean_dim in mean_dims
49+
]
50+
mean_dims_shape = [len(mean_dims)]
51+
52+
output_tensor = self.get_tensor(node, node)
53+
output_tensor_wrapper = self.define_tensor(
54+
node,
55+
node,
56+
output_tensor,
57+
PyQnnWrapper.Qnn_TensorType_t.QNN_TENSOR_TYPE_NATIVE,
58+
nodes_to_wrappers,
59+
)
60+
61+
reduce_max_op = PyQnnWrapper.PyQnnOpWrapper(
62+
node.name,
63+
QNN_OP_PACKAGE_NAME_QTI_AISW,
64+
OpAmax.op_name,
65+
)
66+
reduce_max_op.AddInputTensors([input_tensor_wrapper])
67+
reduce_max_op.AddOutputTensors([output_tensor_wrapper])
68+
reduce_max_op.AddTensorParam(
69+
OpAmax.param_axes,
70+
PyQnnWrapper.Qnn_DataType_t.QNN_DATATYPE_UINT_32,
71+
len(mean_dims_shape),
72+
mean_dims_shape,
73+
np.array(mean_dims, dtype=np.uint32),
74+
True,
75+
)
76+
if len(node.args) > 2:
77+
keep_dims = cast(bool, node.args[2])
78+
reduce_max_op.AddScalarParam(
79+
OpAmax.param_keep_dims,
80+
PyQnnWrapper.Qnn_DataType_t.QNN_DATATYPE_BOOL_8,
81+
{QCOM_DATA: keep_dims},
82+
)
83+
84+
return reduce_max_op

backends/qualcomm/builders/qnn_constants.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
# instead of replicating them here.
1515

1616

17+
@dataclass(init=False, frozen=True)
18+
class OpAmax:
19+
op_name: str = "ReduceMax"
20+
param_axes: str = "axes"
21+
param_keep_dims: str = "keep_dims"
22+
23+
1724
@dataclass(init=False, frozen=True)
1825
class OpBatchnorm:
1926
op_name: str = "Batchnorm"

backends/qualcomm/quantizer/annotators.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ def annotate_add(node: Node, quantization_config: QuantizationConfig) -> None:
182182
annotate_binary(node, quantization_config)
183183

184184

185+
@register_annotator([torch.ops.aten.amax.default])
186+
def annotate_amax(node: Node, quantization_config: QuantizationConfig) -> None:
187+
annotate_binary(node, quantization_config)
188+
189+
185190
@register_annotator([torch.ops.aten.argmin.default])
186191
def annotate_argmin(node: Node, quantization_config: QuantizationConfig) -> None:
187192
if _is_annotated([node]):

backends/qualcomm/tests/models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ def forward(self, x):
7272
return torch.any(x, dim=self.dim, keepdim=self.keepdim)
7373

7474

75+
class AMax(torch.nn.Module):
76+
def __init__(self, dim=None, keepdim=False):
77+
super().__init__()
78+
self.dim = dim
79+
self.keepdim = keepdim
80+
81+
def forward(self, x):
82+
return torch.amax(x, dim=self.dim, keepdim=self.keepdim)
83+
84+
7585
class Arange(torch.nn.Module):
7686
def __init__(self, start, end, step, dtype):
7787
super().__init__()

backends/qualcomm/tests/test_qnn_delegate.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ def test_qnn_backend_adaptive_avg_pool2d(self):
113113
sample_input = (torch.randn(1, 512, 7, 7),)
114114
self.lower_module_and_test_output(module, sample_input)
115115

116+
def test_qnn_backend_amax(self):
117+
modules = [AMax(dim=1, keepdim=False), AMax(dim=1, keepdim=True)] # noqa: F405
118+
sample_input = (torch.randn(4, 4),)
119+
for i, module in enumerate(modules):
120+
with self.subTest(i=i):
121+
self.lower_module_and_test_output(module, sample_input)
122+
116123
def test_qnn_backend_any(self):
117124
modules = [Any(), Any(dim=[0, 1]), Any(dim=1, keepdim=True)] # noqa: F405
118125
sample_input = (torch.randn(3, 3, 3) > 0,)
@@ -1111,6 +1118,14 @@ def test_qnn_backend_adaptive_avg_pool2d(self):
11111118
module = self.get_qdq_module(module, sample_input)
11121119
self.lower_module_and_test_output(module, sample_input)
11131120

1121+
def test_qnn_backend_amax(self):
1122+
modules = [AMax(dim=1, keepdim=False), AMax(dim=1, keepdim=True)] # noqa: F405
1123+
sample_input = (torch.randn(4, 4),)
1124+
for i, module in enumerate(modules):
1125+
with self.subTest(i=i):
1126+
module = self.get_qdq_module(module, sample_input)
1127+
self.lower_module_and_test_output(module, sample_input)
1128+
11141129
def test_qnn_backend_any(self):
11151130
modules = [Any(), Any(dim=[0, 1]), Any(dim=1, keepdim=True)] # noqa: F405
11161131
sample_input = (torch.randn(3, 3, 3) > 0,)

0 commit comments

Comments
 (0)