Skip to content

Commit f5d9599

Browse files
Arm backend: Centralize quantized guard in Arm passes (pytorch#17553)
Add an ArmPass helper to detect fully quantized meta and reuse it across decomposition passes to reduce duplication.
1 parent 138b69d commit f5d9599

15 files changed

Lines changed: 27 additions & 75 deletions

‎backends/arm/_passes/arm_pass.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ def allowed_to_transform(self, meta: NodeMetadata | dict[str, Any]) -> bool:
3535

3636
return not disallow_tfa
3737

38+
def _is_quantized_meta(self, meta: NodeMetadata | dict[str, Any]) -> bool:
39+
"""Return True when meta indicates fully quantized inputs and outputs."""
40+
if isinstance(meta, NodeMetadata):
41+
meta_dict = meta.data
42+
else:
43+
meta_dict = meta
44+
input_qparams = meta_dict.get("input_qparams", {})
45+
output_qparams = meta_dict.get("output_qparams", {})
46+
return bool(input_qparams) and bool(output_qparams)
47+
3848
@property
3949
@abstractmethod
4050
def _passes_required_after(self) -> Set[Type[ExportPass]]:

‎backends/arm/_passes/decompose_acosh_pass.py‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ def call_operator(self, op, args, kwargs, meta, updated=False):
4242
if op is not edge_acosh_op:
4343
return super().call_operator(op, args, kwargs, meta, updated)
4444

45-
is_quantized = (
46-
len(meta.data.get("input_qparams", {})) > 0
47-
and len(meta.data.get("output_qparams", {})) > 0
48-
)
49-
if is_quantized:
45+
if self._is_quantized_meta(meta):
5046
# If quantized, node should be replace by table op
5147
return super().call_operator(op, args, kwargs, meta, updated)
5248

‎backends/arm/_passes/decompose_asin_and_acos_pass.py‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,7 @@ def call_operator(self, op, args, kwargs, meta):
123123
if op not in (edge_asin_op + edge_acos_op):
124124
return super().call_operator(op, args, kwargs, meta)
125125

126-
is_quantized = (
127-
len(meta.data.get("input_qparams", {})) > 0
128-
and len(meta.data.get("output_qparams", {})) > 0
129-
)
130-
if is_quantized:
126+
if self._is_quantized_meta(meta):
131127
# If quantized, node should be replace by table op
132128
return super().call_operator(op, args, kwargs, meta)
133129

‎backends/arm/_passes/decompose_asinh_pass.py‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ def call_operator(self, op, args, kwargs, meta):
4141
if op not in edge_asinh_op:
4242
return super().call_operator(op, args, kwargs, meta)
4343

44-
is_quantized = (
45-
len(meta.data.get("input_qparams", {})) > 0
46-
and len(meta.data.get("output_qparams", {})) > 0
47-
)
48-
if is_quantized:
44+
if self._is_quantized_meta(meta):
4945
# If quantized, node should be replace by table op
5046
return super().call_operator(op, args, kwargs, meta)
5147

‎backends/arm/_passes/decompose_atan_pass.py‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 Arm Limited and/or its affiliates.
1+
# Copyright 2025-2026 Arm Limited and/or its affiliates.
22
#
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.
@@ -80,11 +80,7 @@ def call_operator(self, op, args, kwargs, meta):
8080
if op is not edge_atan:
8181
return super().call_operator(op, args, kwargs, meta, updated=False)
8282

83-
is_quantized = (
84-
len(meta.data.get("input_qparams", {})) > 0
85-
and len(meta.data.get("output_qparams", {})) > 0
86-
)
87-
if is_quantized:
83+
if self._is_quantized_meta(meta):
8884
# If quantized, node should be replace by table op
8985
return super().call_operator(op, args, kwargs, meta)
9086

‎backends/arm/_passes/decompose_atanh_pass.py‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ def call_operator(self, op, args, kwargs, meta):
5151
if op is not edge_atanh:
5252
return super().call_operator(op, args, kwargs, meta, updated=False)
5353

54-
is_quantized = (
55-
len(meta.data.get("input_qparams", {})) > 0
56-
and len(meta.data.get("output_qparams", {})) > 0
57-
)
58-
if is_quantized:
54+
if self._is_quantized_meta(meta):
5955
# If quantized, node should be replace by table op
6056
return super().call_operator(op, args, kwargs, meta)
6157

‎backends/arm/_passes/decompose_cosh_pass.py‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 Arm Limited and/or its affiliates.
1+
# Copyright 2025-2026 Arm Limited and/or its affiliates.
22
#
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.
@@ -39,11 +39,7 @@ def call_operator(self, op, args, kwargs, meta, updated=False):
3939
if op is not edge_cosh:
4040
return super().call_operator(op, args, kwargs, meta, updated)
4141

42-
is_quantized = (
43-
len(meta.data.get("input_qparams", {})) > 0
44-
and len(meta.data.get("output_qparams", {})) > 0
45-
)
46-
if is_quantized:
42+
if self._is_quantized_meta(meta):
4743
# If quantized, node should be replace by table op
4844
return super().call_operator(op, args, kwargs, meta)
4945

‎backends/arm/_passes/decompose_elu_pass.py‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ def call_operator(self, op, args, kwargs, meta):
6464
if op not in edge_elu_ops:
6565
return super().call_operator(op, args, kwargs, meta, updated=False)
6666

67-
is_quantized = (
68-
len(meta.data.get("input_qparams", {})) > 0
69-
and len(meta.data.get("output_qparams", {})) > 0
70-
)
71-
if is_quantized:
67+
if self._is_quantized_meta(meta):
7268
# If quantized, node should be replace by table op
7369
return super().call_operator(op, args, kwargs, meta)
7470

‎backends/arm/_passes/decompose_expm1_pass.py‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,7 @@ def call_operator(self, op, args, kwargs, meta):
9292
if op not in edge_expm1_ops:
9393
return super().call_operator(op, args, kwargs, meta, updated=False)
9494

95-
is_quantized = (
96-
len(meta.data.get("input_qparams", {})) > 0
97-
and len(meta.data.get("output_qparams", {})) > 0
98-
)
99-
if is_quantized:
95+
if self._is_quantized_meta(meta):
10096
# If quantized, node should be replace by table op
10197
return super().call_operator(op, args, kwargs, meta)
10298

‎backends/arm/_passes/decompose_gelu_pass.py‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,7 @@ class DecomposeGeluPass(ArmPass):
9494
def call_operator(self, op, args, kwargs, meta):
9595
if op not in torch_gelu + edge_gelu:
9696
return super().call_operator(op, args, kwargs, meta)
97-
is_quantized = (
98-
len(meta.data.get("input_qparams", {})) > 0
99-
and len(meta.data.get("output_qparams", {})) > 0
100-
)
101-
if is_quantized:
97+
if self._is_quantized_meta(meta):
10298
# If quantized, node should be replace by table op
10399
return super().call_operator(op, args, kwargs, meta)
104100

0 commit comments

Comments
 (0)