Skip to content

Commit 33fad23

Browse files
Arm backend: Preserve Q/DQ in rescale pass (pytorch#22559)
### Cause InsertRescaleInt32Pass runs after Q/DQ folding and assumes that quantization metadata means the operator has been fully converted to an integer path. For partially quantized binary operators, however, FoldAndAnnotateQParamsPass intentionally preserves the Q/DQ boundaries so the operator continues to execute with floating-point inputs. It still records quantization metadata for later lowering. Consequently, the rescale pass misidentifies these operators as integer operations. For add, this can fail because only one input has quantization parameters; for mul, it can insert an invalid INT32 RESCALE on a dequantized floating-point input and alter the preserved graph boundary. ### Fix Skip INT32 rescale insertion when an operator still has a direct DQ input or Q output. These nodes indicate that Q/DQ folding deliberately preserved the quantization boundary, so the operator must not be rewritten as an INT32 operation. Fully folded quantized operators continue through the existing rescale path unchanged. ### Testing Added regression coverage for partially quantized add and mul. The tests verify that the original DQ input and Q output remain connected and that no TOSA RESCALE nodes are inserted. cc @digantdesai @freddan80 @per @zingo @oscarandersson8218 @mansnils @Sebastian-Larsson @robell @rascani Signed-off-by: Sangwon Ha <sangwon.ha@arm.com>
1 parent b4c53c6 commit 33fad23

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

‎backends/arm/_passes/insert_rescales_pass.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,12 @@ def call(self, graph_module: GraphModule) -> PassResult:
408408
if node.op != "call_function" or node.target not in self.included_targets:
409409
continue
410410

411+
has_preserved_qdq = any(
412+
input_node.target in DQ_OPS for input_node in node.all_input_nodes
413+
) or any(user.target in Q_OPS for user in node.users)
414+
if has_preserved_qdq:
415+
continue
416+
411417
if "input_qparams" not in node.meta or len(node.meta["input_qparams"]) == 0:
412418
continue
413419
input_qparams = node.meta["input_qparams"]

‎backends/arm/test/passes/test_insert_rescale_i32_pass.py‎

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
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.
55

6-
from typing import Tuple
6+
from typing import Callable, Tuple
77

8+
import pytest
89
import torch
910
from executorch.backends.arm._passes import (
1011
FoldAndAnnotateQParamsPass,
1112
InsertRescaleInt32Pass,
1213
)
14+
from executorch.backends.arm.common.annotation_meta import ArmAnnotationInfo
1315
from executorch.backends.arm.test.tester.test_pipeline import PassPipeline
16+
from executorch.exir.dialects._ops import ops as exir_ops
1417

1518

1619
class MultipleOpsModel(torch.nn.Module):
@@ -90,6 +93,55 @@ def test_insert_rescale_int32_tosa_INT_multiple_ops():
9093
_test_model_with_f32_data(MultipleOpsModel())
9194

9295

96+
@pytest.mark.parametrize(
97+
"binary_target",
98+
(exir_ops.edge.aten.add.Tensor, exir_ops.edge.aten.mul.Tensor),
99+
)
100+
def test_insert_rescale_int32_preserves_partial_qdq(
101+
binary_target: Callable[..., object],
102+
) -> None:
103+
graph = torch.fx.Graph()
104+
x = graph.placeholder("x")
105+
y = graph.placeholder("y")
106+
x_q = graph.call_function(
107+
exir_ops.edge.quantized_decomposed.quantize_per_tensor.default,
108+
(x, 0.5, 0, -128, 127, torch.int8),
109+
)
110+
x_dq = graph.call_function(
111+
exir_ops.edge.quantized_decomposed.dequantize_per_tensor.default,
112+
(x_q, 0.5, 0, -128, 127, torch.int8),
113+
)
114+
binary = graph.call_function(binary_target, (x_dq, y))
115+
binary.meta["custom"] = {
116+
ArmAnnotationInfo.CUSTOM_META_KEY: ArmAnnotationInfo(quantized=True)
117+
}
118+
output_q = graph.call_function(
119+
exir_ops.edge.quantized_decomposed.quantize_per_tensor.default,
120+
(binary, 0.5, 0, -128, 127, torch.int8),
121+
)
122+
output_dq = graph.call_function(
123+
exir_ops.edge.quantized_decomposed.dequantize_per_tensor.default,
124+
(output_q, 0.5, 0, -128, 127, torch.int8),
125+
)
126+
graph.output(output_dq)
127+
graph_module = torch.fx.GraphModule(torch.nn.Module(), graph)
128+
129+
fold_result = FoldAndAnnotateQParamsPass(preserve_partial_binary_tensor_qdq=True)(
130+
graph_module
131+
)
132+
assert fold_result is not None
133+
rescale_result = InsertRescaleInt32Pass()(fold_result.graph_module)
134+
assert rescale_result is not None
135+
136+
assert binary.args == (x_dq, y)
137+
assert output_q in binary.users
138+
assert not rescale_result.graph_module.graph.find_nodes(
139+
op="call_function",
140+
target=exir_ops.backend.tosa.RESCALE.default,
141+
sort=False,
142+
)
143+
144+
93145
def test_insert_rescale_int32_tosa_FP_dont_insert_rescales():
94146
module = MultipleOpsModel()
95147
input_t = Tuple[torch.Tensor, torch.Tensor]

0 commit comments

Comments
 (0)