Skip to content

Commit 161d9f9

Browse files
authored
Arm backend: prefer TABLE for quantized LeakyReLU (pytorch#21739)
Arm backend: prefer TABLE for quantized LeakyReLU Change-Id: I65b16f97b043d9a406366f5fb770a80f092a1256 Signed-off-by: Rob Elliott <Robert.Elliott@arm.com> cc @digantdesai @freddan80 @per @zingo @oscarandersson8218 @mansnils @Sebastian-Larsson @rascani Signed-off-by: Rob Elliott <Robert.Elliott@arm.com>
1 parent 381232c commit 161d9f9

3 files changed

Lines changed: 74 additions & 4 deletions

File tree

‎backends/arm/_passes/replace_scalar_with_tensor_pass.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@
8484
exir_ops.edge.aten.remainder.Scalar,
8585
}
8686

87+
_preserve_quantized_leaky_relu_ops = {
88+
exir_ops.edge.aten.leaky_relu.default,
89+
torch.ops.aten.leaky_relu.default,
90+
}
91+
8792

8893
class ReplaceScalarWithTensorByProfilePass(ArmPass, ReplaceScalarWithTensorArgPass):
8994
"""Profile-aware scalar-to-tensor replacement pass for binary ops."""
@@ -128,6 +133,12 @@ def call_operator(self, op, args, kwargs, meta):
128133
input_qparams = meta.data.get("input_qparams", {})
129134
output_qparams = meta.data.get("output_qparams", {})
130135
if len(input_qparams) > 0 and len(output_qparams) > 0:
136+
if op in _preserve_quantized_leaky_relu_ops:
137+
# Route through ArmPass to avoid PyTorch fake/meta
138+
# execution rejecting int-like inputs with float
139+
# negative_slope for quantized leaky_relu.
140+
# Do not handle; forward unchanged.
141+
return ArmPass.call_operator(self, op, args, kwargs, meta)
131142
# Do not handle; forward unchanged.
132143
return ExportPass.call_operator(self, op, args, kwargs, meta)
133144

‎backends/arm/quantizer/arm_quantizer.py‎

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@
1313

1414
import functools
1515
import logging
16+
from contextlib import contextmanager
1617
from typing import Any, Callable, Dict, List, Optional
1718

1819
import torch
1920
from executorch.backends.arm._passes import ArmPassManager
2021
from executorch.backends.arm.common.annotation_meta import ArmAnnotationInfo
22+
from executorch.backends.arm.common.pipeline_config import (
23+
ArmPassPipelineConfig,
24+
LeakyReLULoweringConfig,
25+
)
2126
from executorch.backends.arm.constants import DISALLOW_TFA_META_KEY
2227
from executorch.backends.arm.ethosu import EthosUCompileSpec
2328
from executorch.backends.arm.quantizer.quantization_config import (
@@ -108,6 +113,34 @@
108113
logger = logging.getLogger(__name__)
109114

110115

116+
@contextmanager
117+
def _prefer_table_for_quantized_leaky_relu(
118+
compile_spec: ArmCompileSpec,
119+
):
120+
"""Temporarily prefer TABLE lowering while preparing a quantized graph.
121+
122+
The transform-for-annotation pipeline runs before qparams are attached. If
123+
LeakyReLU remains on the default DECOMPOSE setting there, quantized
124+
leaky_relu nodes are broken into primitive ops too early and can no longer
125+
reach InsertTableOpsPass later. Keep the compile-spec default unchanged for
126+
normal FP/TOSA lowering and only override this choice while quantizer
127+
preprocessing is running.
128+
129+
"""
130+
original_config = compile_spec._get_pass_pipeline_config()
131+
if original_config.leaky_relu is LeakyReLULoweringConfig.TABLE:
132+
yield
133+
return
134+
135+
quantized_config = ArmPassPipelineConfig.from_dict(original_config.to_dict())
136+
quantized_config.leaky_relu = LeakyReLULoweringConfig.TABLE
137+
compile_spec.set_pass_pipeline_config(quantized_config)
138+
try:
139+
yield
140+
finally:
141+
compile_spec.set_pass_pipeline_config(original_config)
142+
143+
111144
def _wrap_vgf_quantization_config(
112145
quantization_config: Optional[QuantizationConfig],
113146
) -> Optional[QuantizationConfig]:
@@ -1045,8 +1078,9 @@ def _set_disallow_tfa_for_nodes(self, model: GraphModule) -> None:
10451078
def transform_for_annotation(self, model: GraphModule) -> GraphModule:
10461079
self._set_disallow_tfa_for_nodes(model)
10471080

1048-
pass_manager = ArmPassManager(self.compile_spec)
1049-
return pass_manager.transform_for_annotation_pipeline(graph_module=model)
1081+
with _prefer_table_for_quantized_leaky_relu(self.compile_spec):
1082+
pass_manager = ArmPassManager(self.compile_spec)
1083+
return pass_manager.transform_for_annotation_pipeline(graph_module=model)
10501084

10511085
def annotate(self, model: GraphModule) -> GraphModule:
10521086
model = self._annotate_for_static_quantization_config(model)
@@ -1280,8 +1314,9 @@ def transform_for_annotation(self, model: GraphModule) -> GraphModule:
12801314

12811315
self._log_nonquantized_nodes(model)
12821316

1283-
pass_manager = ArmPassManager(self.compile_spec)
1284-
transformed_model = pass_manager.transform_for_annotation_pipeline(model)
1317+
with _prefer_table_for_quantized_leaky_relu(self.compile_spec):
1318+
pass_manager = ArmPassManager(self.compile_spec)
1319+
transformed_model = pass_manager.transform_for_annotation_pipeline(model)
12851320

12861321
# Remove the temporary annotations
12871322
return self._remove_annotations(transformed_model)

‎backends/arm/test/misc/test_pass_pipeline_config.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
QuantizeInfConfig,
1818
SoftmaxDecompositionConfig,
1919
)
20+
from executorch.backends.arm.quantizer import (
21+
get_symmetric_quantization_config,
22+
TOSAQuantizer,
23+
)
2024
from executorch.backends.arm.test import common
2125
from executorch.backends.arm.test.tester.arm_tester import ArmTester
2226
from executorch.backends.arm.tosa.compile_spec import TosaCompileSpec
@@ -127,6 +131,8 @@ def test_quant_inf_config_reaches_annotation_pipeline():
127131

128132

129133
def test_leaky_relu_config_serializes_roundtrip():
134+
assert ArmPassPipelineConfig().is_default()
135+
130136
config = ArmPassPipelineConfig(
131137
leaky_relu=LeakyReLULoweringConfig.DECOMPOSE,
132138
)
@@ -184,6 +190,24 @@ def test_leaky_relu_decompose_config_reaches_annotation_pipeline():
184190
assert torch.ops.aten.add.Tensor in targets
185191

186192

193+
def test_quantizer_transform_for_annotation_prefers_table_for_leaky_relu():
194+
compile_spec = TosaCompileSpec(
195+
TosaSpecification.create_from_string("TOSA-1.00+INT")
196+
)
197+
quantizer = TOSAQuantizer(compile_spec)
198+
quantizer.set_global(get_symmetric_quantization_config())
199+
exported = export(ModuleWithLeakyReLU(), (torch.randn(4),), strict=True)
200+
201+
transformed = quantizer.transform_for_annotation(exported.graph_module)
202+
targets = _call_function_targets(transformed)
203+
204+
assert torch.ops.aten.leaky_relu.default in targets
205+
assert (
206+
compile_spec._get_pass_pipeline_config().leaky_relu
207+
is LeakyReLULoweringConfig.DECOMPOSE
208+
)
209+
210+
187211
def test_leaky_relu_decompose_config_reaches_backend_pipeline():
188212
config = ArmPassPipelineConfig(
189213
leaky_relu=LeakyReLULoweringConfig.DECOMPOSE,

0 commit comments

Comments
 (0)