Skip to content

Commit 0240bc8

Browse files
authored
Arm backend: Add TOSA elementwise ternerary node visitors (pytorch#21242)
SELECT operator added. Aten -> Tosa pass updated. Signed-off-by: Saoirse Stewart <saoirse.stewart@arm.com>
1 parent ae8aa04 commit 0240bc8

6 files changed

Lines changed: 265 additions & 1 deletion

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2026 Arm Limited and/or its affiliates.
2+
#
3+
# This source code is licensed under the BSD-style license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
from executorch.backends.transforms.aten_to_dialect_pass import (
7+
AtenToDialectPass,
8+
DialectNodeSpec,
9+
)
10+
from executorch.exir.dialects._ops import ops as exir_ops
11+
from torch.fx import Node
12+
13+
14+
def rewrite_ternary_operator(
15+
node: Node, pass_: AtenToDialectPass
16+
) -> DialectNodeSpec | None:
17+
match node.target:
18+
case exir_ops.edge.aten.where.self:
19+
return DialectNodeSpec(
20+
exir_ops.backend.tosa.SELECT.default,
21+
node.args,
22+
dict(node.kwargs),
23+
)
24+
case _:
25+
return None

‎backends/arm/_passes/exir_to_tosa_pass.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
rewrite_rfft2,
1919
rewrite_unary_operator,
2020
)
21+
from executorch.backends.arm._passes.aten_to_tosa_ternary import (
22+
rewrite_ternary_operator,
23+
)
2124
from executorch.backends.transforms.aten_to_dialect_pass import (
2225
AtenToDialectPass,
2326
DialectNodeSpec,
@@ -123,6 +126,15 @@ def _get_activation_replacement(
123126
return get_activation_replacement(node, pass_)
124127

125128

129+
@register_dialect_substitutions(
130+
exir_ops.edge.aten.where.self,
131+
)
132+
def _get_ternary_replacement(
133+
node: Node, pass_: AtenToDialectPass
134+
) -> DialectNodeSpec | None:
135+
return rewrite_ternary_operator(node, pass_)
136+
137+
126138
@register_dialect_substitutions(
127139
exir_ops.edge.aten.cat.default,
128140
exir_ops.edge.aten.flip.default,

‎backends/arm/operators/op_where.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
@register_node_visitor
2525
class WhereVisitor(NodeVisitor):
26-
target = "aten.where.self"
26+
target = "tosa.SELECT.default"
2727

2828
def __init__(self, *args):
2929
super().__init__(*args)
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# Copyright 2026 Arm Limited and/or its affiliates.
2+
#
3+
# This source code is licensed under the BSD-style license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
import executorch.backends.arm.tosa.dialect # noqa: F401
7+
import pytest
8+
import torch
9+
from executorch.backends.arm.tosa.dialect.lib import TosaValueError
10+
from executorch.backends.arm.tosa.dialect.ops_registration import (
11+
get_registered_tosa_ops,
12+
)
13+
from executorch.backends.arm.tosa.specification import (
14+
TosaLoweringContext,
15+
TosaSpecification,
16+
)
17+
from executorch.exir.dialects._ops import ops as exir_ops
18+
from torch._subclasses.fake_tensor import FakeTensorMode
19+
20+
21+
def _to_fake(mode: FakeTensorMode, *values):
22+
return [
23+
mode.from_tensor(value) if isinstance(value, torch.Tensor) else value
24+
for value in values
25+
]
26+
27+
28+
@pytest.mark.parametrize(
29+
(
30+
"spec",
31+
"condition",
32+
"input1",
33+
"input2",
34+
"expected_shape",
35+
"expected_dtype",
36+
),
37+
[
38+
pytest.param(
39+
"TOSA-1.1+INT",
40+
torch.randint(0, 2, (2, 1, 3), dtype=torch.bool),
41+
torch.randint(-8, 8, (1, 4, 3), dtype=torch.int32),
42+
torch.randint(-8, 8, (1, 4, 3), dtype=torch.int32),
43+
(2, 4, 3),
44+
torch.int32,
45+
id="int32",
46+
),
47+
pytest.param(
48+
"TOSA-1.1+FP",
49+
torch.randint(0, 2, (2, 1, 3), dtype=torch.bool),
50+
torch.randn((1, 4, 3), dtype=torch.float32),
51+
torch.randn((1, 4, 3), dtype=torch.float32),
52+
(2, 4, 3),
53+
torch.float32,
54+
id="fp32",
55+
),
56+
pytest.param(
57+
"TOSA-1.1+FP",
58+
torch.randint(0, 2, (2, 1, 3), dtype=torch.bool),
59+
torch.randint(0, 2, (1, 4, 3), dtype=torch.bool),
60+
torch.randint(0, 2, (1, 4, 3), dtype=torch.bool),
61+
(2, 4, 3),
62+
torch.bool,
63+
id="bool",
64+
),
65+
],
66+
)
67+
def test_tosa_select(
68+
spec: str,
69+
condition: torch.Tensor,
70+
input1: torch.Tensor,
71+
input2: torch.Tensor,
72+
expected_shape: tuple[int, ...],
73+
expected_dtype: torch.dtype,
74+
) -> None:
75+
with TosaLoweringContext(
76+
TosaSpecification.create_from_string(spec)
77+
), FakeTensorMode() as mode:
78+
output = exir_ops.backend.tosa.SELECT.default(
79+
*_to_fake(mode, condition, input1, input2)
80+
)
81+
82+
assert output.dtype == expected_dtype
83+
assert tuple(output.shape) == expected_shape
84+
85+
86+
@pytest.mark.parametrize("spec", ["TOSA-1.1+INT", "TOSA-1.1+FP"])
87+
def test_tosa_select_registered_for_all_profiles(spec: str) -> None:
88+
with TosaLoweringContext(TosaSpecification.create_from_string(spec)):
89+
registered_ops = get_registered_tosa_ops()
90+
91+
assert exir_ops.backend.tosa.SELECT.default in registered_ops
92+
93+
94+
def test_tosa_select_accepts_bfloat16_with_bf16_extension() -> None:
95+
condition = torch.randint(0, 2, (2, 3), dtype=torch.bool)
96+
input1 = torch.randn((2, 3), dtype=torch.bfloat16)
97+
input2 = torch.randn((2, 3), dtype=torch.bfloat16)
98+
99+
with TosaLoweringContext(
100+
TosaSpecification.create_from_string("TOSA-1.1+FP+bf16")
101+
), FakeTensorMode() as mode:
102+
output = exir_ops.backend.tosa.SELECT.default(
103+
*_to_fake(mode, condition, input1, input2)
104+
)
105+
106+
assert output.dtype == torch.bfloat16
107+
assert tuple(output.shape) == tuple(input1.shape)
108+
109+
110+
def test_tosa_select_rejects_non_bool_condition() -> None:
111+
condition = torch.ones((2, 3), dtype=torch.int32)
112+
input1 = torch.randn((2, 3), dtype=torch.float32)
113+
input2 = torch.randn((2, 3), dtype=torch.float32)
114+
115+
with TosaLoweringContext(
116+
TosaSpecification.create_from_string("TOSA-1.1+FP")
117+
), FakeTensorMode() as mode:
118+
with pytest.raises(TosaValueError, match="requires bool condition"):
119+
exir_ops.backend.tosa.SELECT.default(
120+
*_to_fake(mode, condition, input1, input2)
121+
)
122+
123+
124+
def test_tosa_select_rejects_mismatched_value_dtypes() -> None:
125+
condition = torch.randint(0, 2, (2, 3), dtype=torch.bool)
126+
input1 = torch.randn((2, 3), dtype=torch.float32)
127+
input2 = torch.randn((2, 3), dtype=torch.float16)
128+
129+
with TosaLoweringContext(
130+
TosaSpecification.create_from_string("TOSA-1.1+FP")
131+
), FakeTensorMode() as mode:
132+
with pytest.raises(TosaValueError, match="Expected matching dtypes"):
133+
exir_ops.backend.tosa.SELECT.default(
134+
*_to_fake(mode, condition, input1, input2)
135+
)
136+
137+
138+
def test_tosa_select_rejects_bfloat16_without_bf16_extension() -> None:
139+
condition = torch.randint(0, 2, (2, 3), dtype=torch.bool)
140+
input1 = torch.randn((2, 3), dtype=torch.bfloat16)
141+
input2 = torch.randn((2, 3), dtype=torch.bfloat16)
142+
143+
with TosaLoweringContext(
144+
TosaSpecification.create_from_string("TOSA-1.1+FP")
145+
), FakeTensorMode() as mode:
146+
with pytest.raises(TosaValueError, match="doesn't support"):
147+
exir_ops.backend.tosa.SELECT.default(
148+
*_to_fake(mode, condition, input1, input2)
149+
)

‎backends/arm/tosa/dialect/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
scatter,
2929
shape_ops,
3030
table,
31+
ternary_elementwise,
3132
transpose_conv2d,
3233
unary_elementwise,
3334
)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Copyright 2026 Arm Limited and/or its affiliates.
2+
#
3+
# This source code is licensed under the BSD-style license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
import torch
7+
from executorch.backends.arm.tosa.dialect.lib import TosaValueError
8+
from executorch.backends.arm.tosa.dialect.ops._common import (
9+
broadcast_shape,
10+
require_same_dtype,
11+
)
12+
from executorch.backends.arm.tosa.dialect.ops_registration import register_fake_tosa_op
13+
from executorch.backends.arm.tosa.specification import (
14+
get_context_spec,
15+
TosaSpecification,
16+
)
17+
18+
19+
def _raise_unsupported_dtype(dtype: torch.dtype, op: str) -> None:
20+
raise TosaValueError(f"Unsupported dtype {dtype} for {op}", op=op)
21+
22+
23+
def _raise_unsupported_profile(dtype: torch.dtype, op: str) -> None:
24+
raise TosaValueError(
25+
f"TOSA spec {get_context_spec()} doesn't support {dtype} for {op}",
26+
op=op,
27+
)
28+
29+
30+
def _validate_condition_dtype(dtype: torch.dtype, op: str) -> None:
31+
if dtype != torch.bool:
32+
raise TosaValueError(f"{op} requires bool condition but got {dtype}", op=op)
33+
34+
35+
def _validate_select_value_dtype(dtype: torch.dtype) -> None:
36+
tosa_spec = get_context_spec()
37+
38+
if dtype == torch.bool:
39+
return
40+
41+
if dtype in {torch.int8, torch.int16, torch.int32}:
42+
if not tosa_spec.support_integer():
43+
_raise_unsupported_profile(dtype, "SELECT")
44+
return
45+
46+
if dtype in {torch.float16, torch.float32}:
47+
if not tosa_spec.support_float():
48+
_raise_unsupported_profile(dtype, "SELECT")
49+
return
50+
51+
if dtype == torch.bfloat16:
52+
if not (tosa_spec.support_float() and tosa_spec.support_extension("bf16")):
53+
_raise_unsupported_profile(dtype, "SELECT")
54+
return
55+
56+
_raise_unsupported_dtype(dtype, "SELECT")
57+
58+
59+
@register_fake_tosa_op(
60+
"SELECT(Tensor condition, Tensor input1, Tensor input2) -> Tensor",
61+
TosaSpecification.all_versions_and_profiles(),
62+
)
63+
def SELECT(
64+
condition: torch.Tensor,
65+
input1: torch.Tensor,
66+
input2: torch.Tensor,
67+
) -> torch.Tensor:
68+
_validate_condition_dtype(condition.dtype, "SELECT")
69+
require_same_dtype(input1, input2, "SELECT")
70+
_validate_select_value_dtype(input1.dtype)
71+
output_shape = broadcast_shape(condition, input1, "SELECT")
72+
output_shape = broadcast_shape(
73+
torch.empty(output_shape, dtype=input1.dtype),
74+
input2,
75+
"SELECT",
76+
)
77+
return torch.empty(output_shape, dtype=input1.dtype)

0 commit comments

Comments
 (0)