Skip to content

Commit 31f112e

Browse files
authored
Arm backend: Add hardsigmoid operator (#8091)
* Add filter_fn to ops_to_not_decompose, to filter out quantized candidate ops * Added aten.hardsigmoid as a table op for the BI case Signed-off-by: Tom Allsop <[email protected]>
1 parent 0be650e commit 31f112e

File tree

5 files changed

+165
-2
lines changed

5 files changed

+165
-2
lines changed

backends/arm/_passes/insert_table_ops.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class InsertTableOpsPass(ExportPass):
4242
exir_ops.edge.aten.rsqrt.default: torch.rsqrt,
4343
exir_ops.edge.aten.sigmoid.default: torch.sigmoid,
4444
exir_ops.edge.aten.tanh.default: torch.tanh,
45+
exir_ops.edge.aten.hardsigmoid.default: torch.nn.functional.hardsigmoid,
4546
}
4647

4748
def __init__(self, exported_program: ExportedProgram) -> None:

backends/arm/arm_partitioner.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,40 @@ def ops_to_not_decompose(
113113
self,
114114
ep: ExportedProgram,
115115
) -> Tuple[List[torch._ops.OpOverload], Optional[Callable[[torch.fx.Node], bool]]]:
116+
ops_to_not_decompose_if_quant_op = [
117+
torch.ops.aten.hardsigmoid.default,
118+
]
119+
120+
def filter_fn(node: torch.fx.Node) -> bool:
121+
# This function filters for operators to not decompose where:
122+
# - It's target is in ops_to_not_decompose_if_quant_op list.
123+
# - All it's inputs/outputs are quantize operators.
124+
dq = torch.ops.quantized_decomposed.dequantize_per_tensor.default
125+
q = torch.ops.quantized_decomposed.quantize_per_tensor.default
126+
127+
if node.target in ops_to_not_decompose_if_quant_op:
128+
# Assume we should not decompose the operator (it is quantized)
129+
should_not_decompose = True
130+
131+
input_nodes = node.all_input_nodes
132+
ouput_nodes = node.users
133+
134+
for inp in input_nodes:
135+
if inp.target != dq:
136+
should_not_decompose = False
137+
138+
for out in ouput_nodes:
139+
if out.target != q:
140+
should_not_decompose = False
141+
142+
return should_not_decompose
143+
144+
# Be default, do not decompose the operator
145+
return True
146+
116147
ops_to_not_decompose = [
117148
torch.ops.aten.linear.default,
118149
torch.ops.aten.upsample_nearest2d.vec,
119-
]
120-
return (ops_to_not_decompose, None)
150+
] + ops_to_not_decompose_if_quant_op
151+
152+
return (ops_to_not_decompose, filter_fn)

backends/arm/operator_support/tosa_supported_operators.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def is_node_supported(self, submodules, node: fx.Node) -> bool:
7979
exir_ops.edge.aten.clamp.default,
8080
exir_ops.edge.aten.bmm.default,
8181
exir_ops.edge.aten.permute_copy.default,
82+
exir_ops.edge.aten.hardsigmoid.default,
8283
exir_ops.edge.aten.hardtanh.default,
8384
exir_ops.edge.aten.convolution.default,
8485
exir_ops.edge.aten.div.Tensor,

backends/arm/quantizer/quantization_annotator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def _match_pattern(
132132
torch.ops.aten.sigmoid.default,
133133
torch.ops.aten.tanh.default,
134134
torch.ops.aten.sum.dim_IntList,
135+
torch.ops.aten.hardsigmoid.default,
135136
]
136137

137138
_one_to_one_shared_input_qspec = [
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Copyright 2025 Arm Limited and/or its affiliates.
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 unittest
8+
9+
from typing import Tuple
10+
11+
import pytest
12+
import torch
13+
14+
from executorch.backends.arm.test import common, conftest
15+
from executorch.backends.arm.test.tester.arm_tester import ArmTester
16+
from executorch.exir.backend.compile_spec_schema import CompileSpec
17+
from parameterized import parameterized
18+
19+
20+
test_data_suite = [
21+
# (test_name, test_data)
22+
("zeros", torch.zeros(1, 10, 10, 10)),
23+
("ones", torch.ones(10, 10, 10)),
24+
("rand", torch.rand(10, 10) - 0.5),
25+
("randn_pos", torch.randn(10) + 10),
26+
("randn_neg", torch.randn(10) - 10),
27+
("ramp", torch.arange(-16, 16, 0.2)),
28+
]
29+
30+
31+
class TestHardsigmoid(unittest.TestCase):
32+
class Hardsigmoid(torch.nn.Module):
33+
def __init__(self):
34+
super().__init__()
35+
self.hardsigmoid = torch.nn.Hardsigmoid()
36+
37+
def forward(self, x):
38+
return self.hardsigmoid(x)
39+
40+
def _test_hardsigmoid_tosa_MI_pipeline(
41+
self, module: torch.nn.Module, test_data: Tuple[torch.tensor]
42+
):
43+
(
44+
ArmTester(
45+
module,
46+
example_inputs=test_data,
47+
compile_spec=common.get_tosa_compile_spec("TOSA-0.80+MI"),
48+
)
49+
.export()
50+
.check(["torch.ops.aten.hardsigmoid.default"])
51+
.check_not(["torch.ops.quantized_decomposed"])
52+
.to_edge_transform_and_lower()
53+
.check_not(["executorch_exir_dialects_edge__ops_aten_clamp_default"])
54+
.check_count({"torch.ops.higher_order.executorch_call_delegate": 1})
55+
.to_executorch()
56+
.run_method_and_compare_outputs(inputs=test_data)
57+
)
58+
59+
def _test_hardsigmoid_tosa_BI_pipeline(
60+
self, module: torch.nn.Module, test_data: Tuple
61+
):
62+
(
63+
ArmTester(
64+
module,
65+
example_inputs=test_data,
66+
compile_spec=common.get_tosa_compile_spec("TOSA-0.80+BI"),
67+
)
68+
.quantize()
69+
.export()
70+
.check(["torch.ops.aten.hardsigmoid.default"])
71+
.check(["torch.ops.quantized_decomposed"])
72+
.to_edge_transform_and_lower()
73+
.check_not(["executorch_exir_dialects_edge__ops_aten_clamp_default"])
74+
.check_count({"torch.ops.higher_order.executorch_call_delegate": 1})
75+
.to_executorch()
76+
.run_method_and_compare_outputs(inputs=test_data)
77+
)
78+
79+
def _test_hardsigmoid_tosa_ethos_BI_pipeline(
80+
self,
81+
compile_spec: list[CompileSpec],
82+
module: torch.nn.Module,
83+
test_data: Tuple[torch.tensor],
84+
):
85+
tester = (
86+
ArmTester(
87+
module,
88+
example_inputs=test_data,
89+
compile_spec=compile_spec,
90+
)
91+
.quantize()
92+
.export()
93+
.check_count({"torch.ops.aten.hardsigmoid.default": 1})
94+
.check(["torch.ops.quantized_decomposed"])
95+
.to_edge_transform_and_lower()
96+
.check_not(["executorch_exir_dialects_edge__ops_aten_clamp_default"])
97+
.check_count({"torch.ops.higher_order.executorch_call_delegate": 1})
98+
.to_executorch()
99+
.serialize()
100+
)
101+
if conftest.is_option_enabled("corstone_fvp"):
102+
tester.run_method_and_compare_outputs(qtol=1, inputs=test_data)
103+
104+
@parameterized.expand(test_data_suite)
105+
def test_hardsigmoid_tosa_MI(
106+
self,
107+
test_name: str,
108+
test_data: torch.Tensor,
109+
):
110+
self._test_hardsigmoid_tosa_MI_pipeline(self.Hardsigmoid(), (test_data,))
111+
112+
@parameterized.expand(test_data_suite)
113+
def test_hardsigmoid_tosa_BI(self, test_name: str, test_data: torch.Tensor):
114+
self._test_hardsigmoid_tosa_BI_pipeline(self.Hardsigmoid(), (test_data,))
115+
116+
@parameterized.expand(test_data_suite)
117+
@pytest.mark.corstone_fvp
118+
def test_hardsigmoid_tosa_u55_BI(self, test_name: str, test_data: torch.Tensor):
119+
self._test_hardsigmoid_tosa_ethos_BI_pipeline(
120+
common.get_u55_compile_spec(), self.Hardsigmoid(), (test_data,)
121+
)
122+
123+
@parameterized.expand(test_data_suite)
124+
@pytest.mark.corstone_fvp
125+
def test_hardsigmoid_tosa_u85_BI(self, test_name: str, test_data: torch.Tensor):
126+
self._test_hardsigmoid_tosa_ethos_BI_pipeline(
127+
common.get_u85_compile_spec(), self.Hardsigmoid(), (test_data,)
128+
)

0 commit comments

Comments
 (0)