Skip to content

Commit ec3c880

Browse files
authored
Arm backend: Match comparison casts to PyTorch promotion (#24282)
Use torch.result_type to choose the comparison input dtype, then map integer results to a TOSA comparison dtype for the active profile. Check the profile and TOSA CAST pairs before inserting conversions. PyTorch rounds mixed integer/FP comparisons in the floating operand's dtype. Preserve that behavior at fp16, bf16, and fp32 boundaries while keeping integer-only int32 comparisons on the INT profile. Reject casts unsupported by TOSA during partitioning and lowering. cc @digantdesai @freddan80 @per @zingo @oscarandersson8218 @mansnils @Sebastian-Larsson @robell @rascani Signed-off-by: Erik Lundell <erik.lundell@arm.com>
1 parent 489590b commit ec3c880

7 files changed

Lines changed: 497 additions & 48 deletions

File tree

‎backends/arm/_passes/cast_int_comparison_inputs_pass.py‎

Lines changed: 105 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,24 @@
88
import torch
99

1010
from executorch.backends.arm._passes.arm_pass import ArmOpTargetedPass
11-
from executorch.backends.arm.tosa.specification import get_context_spec
11+
from executorch.backends.arm.tosa.cast_support import supported_tosa_casts
12+
from executorch.backends.arm.tosa.specification import (
13+
get_context_spec,
14+
TosaSpecification,
15+
)
1216
from executorch.exir.dialects._ops import ops as exir_ops
1317
from executorch.exir.pass_base import ExportPass
1418

1519

1620
class CastIntComparisonInputsPass(ArmOpTargetedPass):
17-
"""Cast integer comparison inputs to a lossless floating-point type."""
21+
"""Match supported comparison inputs to a dtype supported by the profile.
22+
23+
Use int32 for integer-only inputs when INT is supported. Otherwise, use
24+
PyTorch's floating promotion for supported integer/FP mixes, fp16 for
25+
int8-only inputs, and fp32 for other supported inputs. Reject unsupported
26+
inputs and casts.
27+
28+
"""
1829

1930
_passes_required_after: Set[Type[ExportPass]] = set()
2031

@@ -26,36 +37,104 @@ class CastIntComparisonInputsPass(ArmOpTargetedPass):
2637
exir_ops.edge.aten.le.Tensor,
2738
exir_ops.edge.aten.lt.Tensor,
2839
}
29-
castable_dtypes = {torch.int8, torch.int16}
3040

31-
def should_run_pass(self, graph_module: torch.fx.GraphModule) -> bool:
32-
tosa_spec = get_context_spec()
33-
return (
34-
tosa_spec.support_float()
35-
and not tosa_spec.support_integer()
36-
and super().should_run_pass(graph_module)
37-
)
41+
def _decide_cast_dtype(
42+
self, arg_data: list[torch.Tensor], tosa_spec: TosaSpecification
43+
) -> torch.dtype:
44+
"""Choose a TOSA comparison dtype from PyTorch's promoted input dtype.
45+
46+
Use ``torch.result_type`` on the tensors so zero-dimensional inputs
47+
follow PyTorch promotion. Integer results use int32 when INT is
48+
available; with FP only, int8 uses fp16 and int16 uses fp32. Floating
49+
results keep their promoted dtype. The selected dtype must support
50+
comparisons in the profile, and TOSA must support every required cast.
51+
52+
Args:
53+
arg_data (list[torch.Tensor]): Comparison input tensors.
54+
tosa_spec (TosaSpecification): Active TOSA profile and extensions.
55+
56+
Returns:
57+
torch.dtype: Common comparison input dtype.
58+
59+
Raises:
60+
ValueError: If the comparison dtype or a required cast is unsupported.
61+
62+
"""
63+
arg_dtypes = [arg.dtype for arg in arg_data]
64+
result_dtype = torch.result_type(arg_data[0], arg_data[1])
65+
int_dtypes = {torch.bool, torch.uint8, torch.int8, torch.int16, torch.int32}
66+
67+
if tosa_spec.support_integer() and result_dtype in int_dtypes:
68+
cast_dtype = torch.int32
69+
elif tosa_spec.support_float() and result_dtype == torch.int8:
70+
cast_dtype = torch.float16
71+
elif tosa_spec.support_float() and result_dtype == torch.int16:
72+
cast_dtype = torch.float32
73+
else:
74+
cast_dtype = result_dtype
75+
76+
comparison_dtypes = set()
77+
if tosa_spec.support_integer():
78+
comparison_dtypes.add(torch.int32)
79+
if tosa_spec.support_float():
80+
comparison_dtypes.update({torch.float16, torch.float32})
81+
if tosa_spec.support_extension("bf16"):
82+
comparison_dtypes.add(torch.bfloat16)
83+
84+
supported_casts = supported_tosa_casts(tosa_spec)
85+
if (
86+
cast_dtype not in comparison_dtypes
87+
or (
88+
result_dtype != cast_dtype
89+
and (result_dtype, cast_dtype) not in supported_casts
90+
)
91+
or any(
92+
dtype != cast_dtype and (dtype, cast_dtype) not in supported_casts
93+
for dtype in arg_dtypes
94+
)
95+
):
96+
raise ValueError(
97+
f"cannot cast comparison input dtypes {arg_dtypes} to a supported dtype."
98+
)
99+
return cast_dtype
38100

39101
def call_operator(self, op, args, kwargs, meta):
40102
if op not in self.target_ops:
41103
return super().call_operator(op, args, kwargs, meta)
42104

43-
if not all(arg.data.dtype in self.castable_dtypes for arg in args):
44-
return super().call_operator(op, args, kwargs, meta)
105+
arg_dtypes = [arg.data.dtype for arg in args]
106+
tosa_spec = get_context_spec()
45107

46-
cast_dtype = (
47-
torch.float16
48-
if all(arg.data.dtype == torch.int8 for arg in args)
49-
else torch.float32
50-
)
51-
casted_args = []
108+
# Handle cases with bool inputs
109+
if torch.bool in arg_dtypes:
110+
if all(dtype == torch.bool for dtype in arg_dtypes) and op in {
111+
exir_ops.edge.aten.eq.Tensor,
112+
exir_ops.edge.aten.ne.Tensor,
113+
}:
114+
return super().call_operator(op, args, kwargs, meta)
115+
116+
if not tosa_spec.support_integer() or any(
117+
dtype
118+
not in {torch.int8, torch.int16, torch.int32, torch.bool, torch.uint8}
119+
for dtype in arg_dtypes
120+
):
121+
raise ValueError(
122+
f"{op} cannot cast {arg_dtypes} to a {tosa_spec} comparison dtype."
123+
)
124+
125+
cast_dtype = self._decide_cast_dtype([arg.data for arg in args], tosa_spec)
126+
127+
new_args = []
52128
for arg in args:
53-
casted_args.append(
54-
super().call_operator(
55-
exir_ops.edge.dim_order_ops._to_dim_order_copy.default,
56-
(arg,),
57-
{"dtype": cast_dtype},
58-
meta,
129+
if arg.data.dtype == cast_dtype:
130+
new_args.append(arg)
131+
else:
132+
new_args.append(
133+
super().call_operator(
134+
exir_ops.edge.dim_order_ops._to_dim_order_copy.default,
135+
(arg,),
136+
{"dtype": cast_dtype},
137+
meta,
138+
)
59139
)
60-
)
61-
return super().call_operator(op, tuple(casted_args), kwargs, meta)
140+
return super().call_operator(op, tuple(new_args), kwargs, meta)

‎backends/arm/operator_support/tosa_supported_operators.py‎

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,10 @@ def _is_quantized_constant(node: torch.fx.Node) -> bool:
249249
def _floating_profile_negative_checks(
250250
tosa_spec: TosaSpecification, reporter: WhyNoPartitionReporter
251251
) -> list[OperatorSupportBase]:
252-
checks: list[OperatorSupportBase] = [CheckMixedFloatingInputs(reporter)]
253-
if not tosa_spec.support_integer():
254-
checks.append(CheckFPComparisonInputs(reporter))
255-
return checks
252+
return [
253+
CheckMixedFloatingInputs(reporter),
254+
CheckFPComparisonInputs(reporter, tosa_spec.support_integer()),
255+
]
256256

257257

258258
def is_quantized(node: torch.fx.Node) -> bool:
@@ -1581,7 +1581,7 @@ def is_node_supported(
15811581

15821582

15831583
class CheckFPComparisonInputs(OperatorSupportBase):
1584-
"""Reject unsupported comparison inputs under the FP profile."""
1584+
"""Reject comparison inputs without a supported TOSA promotion."""
15851585

15861586
comparison_ops = {
15871587
exir_ops.edge.aten.eq.Tensor,
@@ -1602,10 +1602,14 @@ class CheckFPComparisonInputs(OperatorSupportBase):
16021602
exir_ops.edge.aten.isnan.default,
16031603
}
16041604
supported_dtypes = {torch.float16, torch.float32, torch.bfloat16}
1605-
castable_comparison_dtypes = {torch.int8, torch.int16}
1605+
castable_comparison_dtypes = {torch.int8, torch.int16, torch.int32}
1606+
integer_dtypes = {torch.bool, torch.int8, torch.int16, torch.int32}
16061607

1607-
def __init__(self, reporter: WhyNoPartitionReporter) -> None:
1608+
def __init__(
1609+
self, reporter: WhyNoPartitionReporter, int_supported: bool = False
1610+
) -> None:
16081611
self.reporter = reporter
1612+
self.int_supported = int_supported
16091613
super().__init__()
16101614

16111615
def is_node_supported(
@@ -1628,8 +1632,24 @@ def is_node_supported(
16281632
} and all(dtype == torch.bool for dtype in input_dtypes):
16291633
return True
16301634

1631-
if node.target in self.comparison_ops and all(
1632-
dtype in self.castable_comparison_dtypes for dtype in input_dtypes
1635+
if (
1636+
self.int_supported
1637+
and node.target in self.comparison_ops
1638+
and all(dtype in self.integer_dtypes for dtype in input_dtypes)
1639+
):
1640+
return True
1641+
1642+
if (
1643+
node.target in self.comparison_ops
1644+
and all(
1645+
dtype in self.supported_dtypes | self.castable_comparison_dtypes
1646+
for dtype in input_dtypes
1647+
)
1648+
and (
1649+
self.int_supported
1650+
or torch.int32 not in input_dtypes
1651+
or any(dtype in self.supported_dtypes for dtype in input_dtypes)
1652+
)
16331653
):
16341654
return True
16351655

@@ -1638,7 +1658,7 @@ def is_node_supported(
16381658
)
16391659
self.reporter.report_reject(
16401660
node,
1641-
f"FP profile does not support {unsupported_dtype} comparison inputs.",
1661+
f"Comparison input {unsupported_dtype} has no supported promotion for this profile.",
16421662
)
16431663
return False
16441664

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def test_where_bf16_requires_extension(x_dtype, y_dtype, bf16_enabled) -> None:
8383
)
8484
@pytest.mark.parametrize(
8585
"dtype",
86-
(torch.bool, torch.uint8, torch.int32, torch.int64),
86+
(torch.bool, torch.uint8, torch.int64),
8787
)
8888
def test_fp_comparison_input_support(target, dtype) -> None:
8989
graph = torch.fx.Graph()
@@ -100,7 +100,13 @@ def test_fp_comparison_input_support(target, dtype) -> None:
100100

101101
@pytest.mark.parametrize(
102102
"dtype",
103-
(torch.float16, torch.float32, torch.bfloat16, torch.int8, torch.int16),
103+
(
104+
torch.float16,
105+
torch.float32,
106+
torch.bfloat16,
107+
torch.int8,
108+
torch.int16,
109+
),
104110
)
105111
def test_fp_comparison_accepts_supported_inputs(dtype) -> None:
106112
graph = torch.fx.Graph()
@@ -112,6 +118,44 @@ def test_fp_comparison_accepts_supported_inputs(dtype) -> None:
112118
assert _fp_comparison_checker().is_node_supported({}, node)
113119

114120

121+
@pytest.mark.parametrize(
122+
("dtypes", "supported"),
123+
(
124+
((torch.int32, torch.float32), True),
125+
((torch.float32, torch.int32), True),
126+
((torch.int32, torch.float16), True),
127+
((torch.int32, torch.bfloat16), True),
128+
((torch.int16, torch.float32), True),
129+
((torch.int16, torch.float16), True),
130+
((torch.int16, torch.bfloat16), True),
131+
((torch.int8, torch.float16), True),
132+
((torch.uint8, torch.float32), False),
133+
),
134+
)
135+
@pytest.mark.parametrize("int_supported", (False, True))
136+
def test_fp_comparison_mixed_inputs(dtypes, supported, int_supported) -> None:
137+
graph = torch.fx.Graph()
138+
x = _placeholder(graph, "x", (3, 4), dtypes[0])
139+
y = _placeholder(graph, "y", (3, 4), dtypes[1])
140+
node = graph.call_function(exir_ops.edge.aten.eq.Tensor, (x, y))
141+
node.meta["val"] = _fake_tensor((3, 4), torch.bool)
142+
143+
checker = CheckFPComparisonInputs(WhyNoPartitionReporter(), int_supported)
144+
assert checker.is_node_supported({}, node) == supported
145+
146+
147+
@pytest.mark.parametrize("int_supported", (False, True))
148+
def test_int32_comparison_requires_int_profile(int_supported) -> None:
149+
graph = torch.fx.Graph()
150+
x = _placeholder(graph, "x", (3, 4), torch.int32)
151+
y = _placeholder(graph, "y", (3, 4), torch.int32)
152+
node = graph.call_function(exir_ops.edge.aten.eq.Tensor, (x, y))
153+
node.meta["val"] = _fake_tensor((3, 4), torch.bool)
154+
155+
checker = CheckFPComparisonInputs(WhyNoPartitionReporter(), int_supported)
156+
assert checker.is_node_supported({}, node) == int_supported
157+
158+
115159
def test_rejects_argmax_without_int32_cast_user() -> None:
116160
graph = torch.fx.Graph()
117161
x = _placeholder(graph, "x", (3, 4))

0 commit comments

Comments
 (0)