Skip to content

Commit 37264a0

Browse files
authored
Qualcomm AI Engine Direct - Adding QNN backend support for acosh, asinh, atanh, cosh, sinh core ATen ops (pytorch#20386)
### Summary Added support for the `acosh, asinh, atanh, cosh, sinh` hyperbolic functions core ATen ops via a decomposition pass and the following identities: ``` sinh(x) = 0.5 * (exp(x) - exp(-x)) cosh(x) = 0.5 * (exp(x) + exp(-x)) asinh(x) = log(x + sqrt(x*x + 1)) acosh(x) = log(x + sqrt(x*x - 1)) atanh(x) = 0.5 * log((1 + x) / (1 - x)) ``` Added support in a single pass for all hyperbolic variants because they are similar and the decomposed function nodes can be shared efficiently. Note that `tanh` is supported via an op builder directly as support was added previously. Also renamed the utility method `get_const_node` to `create_const_node` to better reflect its purpose. ### Test plan ``` python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNQuantizedOperator.test_qnn_backend_acosh --soc_model SM8750 --host aisw-vm15-labsd --device 545ee4aa --build_folder build-android python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNFloatingPointOperator.test_qnn_backend_acosh --soc_model SM8750 --host aisw-vm15-labsd --device 545ee4aa --build_folder build-android python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNQuantizedOperator.test_qnn_backend_asinh --soc_model SM8750 --host aisw-vm15-labsd --device 545ee4aa --build_folder build-android python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNFloatingPointOperator.test_qnn_backend_asinh --soc_model SM8750 --host aisw-vm15-labsd --device 545ee4aa --build_folder build-android python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNQuantizedOperator.test_qnn_backend_atanh --soc_model SM8750 --host aisw-vm15-labsd --device 545ee4aa --build_folder build-android python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNFloatingPointOperator.test_qnn_backend_atanh --soc_model SM8750 --host aisw-vm15-labsd --device 545ee4aa --build_folder build-android python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNQuantizedOperator.test_qnn_backend_cosh --soc_model SM8750 --host aisw-vm15-labsd --device 545ee4aa --build_folder build-android python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNFloatingPointOperator.test_qnn_backend_cosh --soc_model SM8750 --host aisw-vm15-labsd --device 545ee4aa --build_folder build-android python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNQuantizedOperator.test_qnn_backend_sinh --soc_model SM8750 --host aisw-vm15-labsd --device 545ee4aa --build_folder build-android python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNFloatingPointOperator.test_qnn_backend_sinh --soc_model SM8750 --host aisw-vm15-labsd --device 545ee4aa --build_folder build-android ```
1 parent ca95da1 commit 37264a0

13 files changed

Lines changed: 324 additions & 16 deletions

‎backends/qualcomm/_passes/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from .decompose_floor_divide import DecomposeFloorDivide
2828
from .decompose_glu import DecomposeGlu
2929
from .decompose_hardsigmoid import DecomposeHardsigmoid
30+
from .decompose_hyperbolic_variants import DecomposeHyperbolicVariants
3031
from .decompose_linalg_vector_norm import DecomposeLinalgVectorNorm
3132
from .decompose_log_variants import DecomposeLogVariants
3233
from .decompose_maxpool3d import DecomposeMaxPool3d
@@ -89,6 +90,7 @@
8990
DecomposeFill,
9091
DecomposeFloorDivide,
9192
DecomposeGlu,
93+
DecomposeHyperbolicVariants,
9294
DecomposeHardsigmoid,
9395
DecomposeLinalgVectorNorm,
9496
DecomposeLogVariants,

‎backends/qualcomm/_passes/decompose_acos.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from executorch.exir.dialects.edge._ops import EdgeOpOverload
1010
from executorch.exir.pass_base import ExportPass, PassResult
1111

12-
from .utils import copy_meta, get_const_node
12+
from .utils import copy_meta, create_const_node
1313

1414

1515
class DecomposeAcos(ExportPass):
@@ -52,7 +52,7 @@ def call(self, graph_module: torch.fx.GraphModule):
5252
)
5353

5454
if is_edge and pi_half_node is None:
55-
pi_half_node = get_const_node(
55+
pi_half_node = create_const_node(
5656
graph, graph_module, "_pi_half_constant", pi_half, node
5757
)
5858

‎backends/qualcomm/_passes/decompose_addmm.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from executorch.exir.dialects.edge._ops import EdgeOpOverload
1010
from executorch.exir.pass_base import ExportPass, PassResult
1111

12-
from .utils import copy_meta, get_const_node
12+
from .utils import copy_meta, create_const_node
1313

1414

1515
class DecomposeAddmm(ExportPass):
@@ -70,7 +70,7 @@ def call(self, graph_module: torch.fx.GraphModule):
7070
mm_node.meta = copy_meta(meta)
7171

7272
if alpha != 1:
73-
alpha_node = get_const_node(
73+
alpha_node = create_const_node(
7474
graph,
7575
graph_module,
7676
f"{node.name}_alpha",
@@ -86,7 +86,7 @@ def call(self, graph_module: torch.fx.GraphModule):
8686
mm_result = mm_node
8787

8888
if beta != 1:
89-
beta_const = get_const_node(
89+
beta_const = create_const_node(
9090
graph,
9191
graph_module,
9292
f"{node.name}_beta",

‎backends/qualcomm/_passes/decompose_atan2.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from executorch.exir.dialects.edge._ops import EdgeOpOverload
1010
from executorch.exir.pass_base import ExportPass, PassResult
1111

12-
from .utils import copy_meta, create_node, get_const_node
12+
from .utils import copy_meta, create_const_node, create_node
1313

1414

1515
class DecomposeAtan2(ExportPass):
@@ -68,7 +68,7 @@ def _get_constants(self, graph, graph_module, node, is_edge, const_cache):
6868

6969
def make_const(name, val):
7070
if name not in const_cache:
71-
const_cache[name] = get_const_node(
71+
const_cache[name] = create_const_node(
7272
graph, graph_module, name, val, node
7373
)
7474
return const_cache[name]
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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+
7+
import torch
8+
from executorch.exir.dialects._ops import ops as exir_ops
9+
from executorch.exir.pass_base import ExportPass, PassResult
10+
11+
from .utils import create_const_node, create_node
12+
13+
14+
class DecomposeHyperbolicVariants(ExportPass):
15+
"""
16+
Decompose hyperbolic functions into supported primitives:
17+
sinh(x) = 0.5 * (exp(x) - exp(-x))
18+
cosh(x) = 0.5 * (exp(x) + exp(-x))
19+
asinh(x) = log(x + sqrt(x*x + 1))
20+
acosh(x) = log(x + sqrt(x*x - 1))
21+
atanh(x) = 0.5 * log((1 + x) / (1 - x))
22+
"""
23+
24+
_EDGE_OPS = {
25+
exir_ops.edge.aten.sinh.default,
26+
exir_ops.edge.aten.cosh.default,
27+
exir_ops.edge.aten.asinh.default,
28+
exir_ops.edge.aten.acosh.default,
29+
exir_ops.edge.aten.atanh.default,
30+
}
31+
32+
def __init__(self):
33+
super(DecomposeHyperbolicVariants, self).__init__()
34+
self._dispatcher = {
35+
# ATen dialect
36+
torch.ops.aten.sinh.default: self._decompose_sinh,
37+
torch.ops.aten.cosh.default: self._decompose_cosh,
38+
torch.ops.aten.asinh.default: self._decompose_asinh,
39+
torch.ops.aten.acosh.default: self._decompose_acosh,
40+
torch.ops.aten.atanh.default: self._decompose_atanh,
41+
# Edge dialect
42+
exir_ops.edge.aten.sinh.default: self._decompose_sinh,
43+
exir_ops.edge.aten.cosh.default: self._decompose_cosh,
44+
exir_ops.edge.aten.asinh.default: self._decompose_asinh,
45+
exir_ops.edge.aten.acosh.default: self._decompose_acosh,
46+
exir_ops.edge.aten.atanh.default: self._decompose_atanh,
47+
}
48+
49+
def _get_ops(self, is_edge):
50+
if is_edge:
51+
return {
52+
"exp": exir_ops.edge.aten.exp.default,
53+
"neg": exir_ops.edge.aten.neg.default,
54+
"add": exir_ops.edge.aten.add.Tensor,
55+
"sub": exir_ops.edge.aten.sub.Tensor,
56+
"mul": exir_ops.edge.aten.mul.Tensor,
57+
"div": exir_ops.edge.aten.div.Tensor,
58+
"log": exir_ops.edge.aten.log.default,
59+
"sqrt": exir_ops.edge.aten.sqrt.default,
60+
}
61+
return {
62+
"exp": torch.ops.aten.exp.default,
63+
"neg": torch.ops.aten.neg.default,
64+
"add": torch.ops.aten.add.Tensor,
65+
"sub": torch.ops.aten.sub.Tensor,
66+
"mul": torch.ops.aten.mul.Tensor,
67+
"div": torch.ops.aten.div.Tensor,
68+
"log": torch.ops.aten.log.default,
69+
"sqrt": torch.ops.aten.sqrt.default,
70+
}
71+
72+
def _decompose_exp_symmetry(
73+
self, node, graph, graph_module, const_cache, combine_op_key
74+
):
75+
"""Shared helper for sinh and cosh: (exp(x) ± exp(-x)) / 2."""
76+
is_edge = node.target in self._EDGE_OPS
77+
ops = self._get_ops(is_edge)
78+
meta = node.meta
79+
input_node = node.args[0]
80+
81+
if is_edge:
82+
half_name = "_half_constant"
83+
if half_name not in const_cache:
84+
const_cache[half_name] = create_const_node(
85+
graph, graph_module, half_name, 0.5, node
86+
)
87+
half_arg = const_cache[half_name]
88+
else:
89+
half_arg = 0.5
90+
91+
with graph.inserting_before(node):
92+
exp_pos = create_node(graph, ops["exp"], (input_node,), meta)
93+
neg_x = create_node(graph, ops["neg"], (input_node,), meta)
94+
exp_neg = create_node(graph, ops["exp"], (neg_x,), meta)
95+
combine = create_node(graph, ops[combine_op_key], (exp_pos, exp_neg), meta)
96+
result = create_node(graph, ops["mul"], (combine, half_arg), meta)
97+
98+
for user in node.users.copy():
99+
user.replace_input_with(node, result)
100+
101+
def _decompose_sinh(self, node, graph, graph_module, const_cache):
102+
self._decompose_exp_symmetry(node, graph, graph_module, const_cache, "sub")
103+
104+
def _decompose_cosh(self, node, graph, graph_module, const_cache):
105+
self._decompose_exp_symmetry(node, graph, graph_module, const_cache, "add")
106+
107+
def _decompose_asinh(self, node, graph, graph_module, const_cache):
108+
is_edge = node.target in self._EDGE_OPS
109+
ops = self._get_ops(is_edge)
110+
meta = node.meta
111+
input_node = node.args[0]
112+
113+
if is_edge:
114+
one_name = "_one_constant"
115+
if one_name not in const_cache:
116+
const_cache[one_name] = create_const_node(
117+
graph, graph_module, one_name, 1.0, node
118+
)
119+
one_arg = const_cache[one_name]
120+
else:
121+
one_arg = 1.0
122+
123+
with graph.inserting_before(node):
124+
x_sq = create_node(graph, ops["mul"], (input_node, input_node), meta)
125+
x_sq_plus_1 = create_node(graph, ops["add"], (x_sq, one_arg), meta)
126+
sqrt_node = create_node(graph, ops["sqrt"], (x_sq_plus_1,), meta)
127+
sum_node = create_node(graph, ops["add"], (input_node, sqrt_node), meta)
128+
result = create_node(graph, ops["log"], (sum_node,), meta)
129+
130+
for user in node.users.copy():
131+
user.replace_input_with(node, result)
132+
133+
def _decompose_acosh(self, node, graph, graph_module, const_cache):
134+
is_edge = node.target in self._EDGE_OPS
135+
ops = self._get_ops(is_edge)
136+
meta = node.meta
137+
input_node = node.args[0]
138+
139+
if is_edge:
140+
one_name = "_one_constant"
141+
if one_name not in const_cache:
142+
const_cache[one_name] = create_const_node(
143+
graph, graph_module, one_name, 1.0, node
144+
)
145+
one_arg = const_cache[one_name]
146+
else:
147+
one_arg = 1.0
148+
149+
with graph.inserting_before(node):
150+
x_sq = create_node(graph, ops["mul"], (input_node, input_node), meta)
151+
x_sq_minus_1 = create_node(graph, ops["sub"], (x_sq, one_arg), meta)
152+
sqrt_node = create_node(graph, ops["sqrt"], (x_sq_minus_1,), meta)
153+
sum_node = create_node(graph, ops["add"], (input_node, sqrt_node), meta)
154+
result = create_node(graph, ops["log"], (sum_node,), meta)
155+
156+
for user in node.users.copy():
157+
user.replace_input_with(node, result)
158+
159+
def _decompose_atanh(self, node, graph, graph_module, const_cache):
160+
is_edge = node.target in self._EDGE_OPS
161+
ops = self._get_ops(is_edge)
162+
meta = node.meta
163+
input_node = node.args[0]
164+
165+
if is_edge:
166+
one_name = "_one_constant"
167+
if one_name not in const_cache:
168+
const_cache[one_name] = create_const_node(
169+
graph, graph_module, one_name, 1.0, node
170+
)
171+
one_arg = const_cache[one_name]
172+
half_name = "_half_constant"
173+
if half_name not in const_cache:
174+
const_cache[half_name] = create_const_node(
175+
graph, graph_module, half_name, 0.5, node
176+
)
177+
half_arg = const_cache[half_name]
178+
else:
179+
one_arg = 1.0
180+
half_arg = 0.5
181+
182+
with graph.inserting_before(node):
183+
one_plus_x = create_node(graph, ops["add"], (one_arg, input_node), meta)
184+
one_minus_x = create_node(graph, ops["sub"], (one_arg, input_node), meta)
185+
ratio = create_node(graph, ops["div"], (one_plus_x, one_minus_x), meta)
186+
log_node = create_node(graph, ops["log"], (ratio,), meta)
187+
result = create_node(graph, ops["mul"], (half_arg, log_node), meta)
188+
189+
for user in node.users.copy():
190+
user.replace_input_with(node, result)
191+
192+
def call(self, graph_module: torch.fx.GraphModule) -> PassResult:
193+
graph = graph_module.graph
194+
const_cache = {}
195+
196+
for node in list(graph.nodes):
197+
if node.target in self._dispatcher:
198+
self._dispatcher[node.target](node, graph, graph_module, const_cache)
199+
200+
graph.eliminate_dead_code()
201+
graph_module.recompile()
202+
return PassResult(graph_module, True)

‎backends/qualcomm/_passes/decompose_log_variants.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from executorch.exir.dialects._ops import ops as exir_ops
1212
from executorch.exir.pass_base import ExportPass, PassResult
1313

14-
from .utils import copy_meta, get_const_node
14+
from .utils import copy_meta, create_const_node
1515

1616

1717
class DecomposeLogVariants(ExportPass):
@@ -50,7 +50,7 @@ def _decompose_log_n(self, node, graph, graph_module, const_cache, n):
5050
div_op = exir_ops.edge.aten.div.Tensor
5151
attr_name = f"_log_base_{n}_constant"
5252
if attr_name not in const_cache:
53-
const_cache[attr_name] = get_const_node(
53+
const_cache[attr_name] = create_const_node(
5454
graph, graph_module, attr_name, math.log(n), node
5555
)
5656
div_arg = const_cache[attr_name]
@@ -81,7 +81,7 @@ def _decompose_log_p(self, node, graph, graph_module, const_cache, p):
8181
log_op = exir_ops.edge.aten.log.default
8282
attr_name = f"_log1p_addend_{p}_constant"
8383
if attr_name not in const_cache:
84-
const_cache[attr_name] = get_const_node(
84+
const_cache[attr_name] = create_const_node(
8585
graph, graph_module, attr_name, p, node
8686
)
8787
add_arg = const_cache[attr_name]

‎backends/qualcomm/_passes/decompose_remainder.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from executorch.exir.pass_base import ExportPass, PassResult
1111
from torchao.quantization.pt2e.utils import get_new_attr_name_with_prefix
1212

13-
from .utils import copy_meta, get_const_node
13+
from .utils import copy_meta, create_const_node
1414

1515

1616
class DecomposeRemainder(ExportPass):
@@ -69,7 +69,7 @@ def call(self, graph_module: torch.fx.GraphModule):
6969
attr_name = get_new_attr_name_with_prefix("_remainder_const_")(
7070
graph_module
7171
)
72-
const_cache[x_arg] = get_const_node(
72+
const_cache[x_arg] = create_const_node(
7373
graph, graph_module, attr_name, x_arg, node
7474
)
7575
x_node = const_cache[x_arg]
@@ -82,7 +82,7 @@ def call(self, graph_module: torch.fx.GraphModule):
8282
attr_name = get_new_attr_name_with_prefix("_remainder_const_")(
8383
graph_module
8484
)
85-
const_cache[y_arg] = get_const_node(
85+
const_cache[y_arg] = create_const_node(
8686
graph, graph_module, attr_name, y_arg, node
8787
)
8888
y_node = const_cache[y_arg]

‎backends/qualcomm/_passes/decompose_var.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from executorch.exir.pass_base import ExportPass, PassResult
1111
from torchao.quantization.pt2e.utils import get_new_attr_name_with_prefix
1212

13-
from .utils import copy_meta, get_const_node
13+
from .utils import copy_meta, create_const_node
1414

1515

1616
class DecomposeVar(ExportPass):
@@ -155,7 +155,7 @@ def call(self, graph_module: torch.fx.GraphModule):
155155
attr_name = get_new_attr_name_with_prefix(
156156
"_var_scale_const_"
157157
)(graph_module)
158-
const_cache[cache_key] = get_const_node(
158+
const_cache[cache_key] = create_const_node(
159159
graph, graph_module, attr_name, scale, node
160160
)
161161
scale_node = const_cache[cache_key]

‎backends/qualcomm/_passes/qnn_pass_manager.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
DecomposeFill,
3333
DecomposeFloorDivide,
3434
DecomposeGlu,
35+
DecomposeHyperbolicVariants,
3536
DecomposeLinalgVectorNorm,
3637
DecomposeLogVariants,
3738
DecomposeMaxPool3d,
@@ -130,6 +131,7 @@ def get_default_pass_activations(cls):
130131
(DecomposeCDist, True),
131132
(DecomposeDivMode, True),
132133
(DecomposeFill, True),
134+
(DecomposeHyperbolicVariants, True),
133135
(DecomposeLogVariants, True),
134136
(DecomposeMaxPool3d, True),
135137
(DecomposeMinMaxDim, True),
@@ -182,6 +184,7 @@ def get_annotation_passes(cls):
182184
DecomposeExpM1,
183185
DecomposeFill,
184186
DecomposeGlu,
187+
DecomposeHyperbolicVariants,
185188
DecomposeRemainder,
186189
DecomposeSelectScatter,
187190
DecomposeLinalgVectorNorm,
@@ -285,6 +288,7 @@ def get_passes_dependency_for_capture_program(cls):
285288
DecomposeCDist: [RemoveRedundancy],
286289
DecomposeDivMode: [RemoveRedundancy],
287290
DecomposeFill: [RemoveRedundancy],
291+
DecomposeHyperbolicVariants: [RemoveRedundancy],
288292
DecomposeLinalgVectorNorm: [RemoveRedundancy],
289293
DecomposeLogVariants: [RemoveRedundancy],
290294
DecomposeMaxPool3d: [RemoveRedundancy],

‎backends/qualcomm/_passes/utils.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def append_qdq(
343343
return dq_node
344344

345345

346-
def get_const_node(
346+
def create_const_node(
347347
graph: torch.fx.Graph,
348348
graph_module: torch.fx.GraphModule,
349349
attr_name: str,

0 commit comments

Comments
 (0)