Skip to content

Commit 59caaf0

Browse files
Arm backend: Add full support for index_select (pytorch#16831)
- Decompose edge.aten.index_select via backend tosa.GATHER + shape ops - Broaden IndexSelectSupported to cover rank>=1 inputs with rank-1 int32 indices and TOSA-spec dtype constraints - Remove the index_select node visitor - Reorder DecomposeEmbeddingPass (generates index_select) before DecomposeIndexSelectToGatherPass - Add index_select tests for rank 1-4 and multiple dims Change-Id: I4abcd404f19a1811d0d87c16c861d3b6b05fdc94 Signed-off-by: Yufeng Shi <yufeng.shi@arm.com>
1 parent 7412efe commit 59caaf0

8 files changed

Lines changed: 345 additions & 233 deletions

File tree

‎backends/arm/_passes/__init__.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
from .decompose_glu_pass import DecomposeGluPass # noqa
5151
from .decompose_grouped_conv_pass import DecomposeGroupedConvPass # noqa
5252
from .decompose_groupnorm_pass import DecomposeGroupNormPass # noqa
53+
from .decompose_index_select_to_gather_pass import ( # noqa
54+
DecomposeIndexSelectToGatherPass,
55+
)
5356
from .decompose_int16_activation_conv_pass import ( # noqa
5457
DecomposeConvWithInt16ActivationPass,
5558
)

‎backends/arm/_passes/arm_pass_manager.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
DecomposeGluPass,
5757
DecomposeGroupedConvPass,
5858
DecomposeGroupNormPass,
59+
DecomposeIndexSelectToGatherPass,
5960
DecomposeIntPowPass,
6061
DecomposeLayerNormPass,
6162
DecomposeLeakyReLUPass,
@@ -289,6 +290,8 @@ def _tosa_pipeline(
289290
DecomposeAddSubAlphaPass(),
290291
DecomposeGroupedConvPass(),
291292
DecomposeUnfoldToGatherPass(),
293+
DecomposeEmbeddingPass(),
294+
DecomposeIndexSelectToGatherPass(),
292295
Conv1dUnsqueezePass(),
293296
]
294297
)
@@ -314,7 +317,6 @@ def _tosa_pipeline(
314317
RewriteIndexPutPass(),
315318
DecomposeRemainderPass(),
316319
DecomposeDivTensorModePass(),
317-
DecomposeEmbeddingPass(),
318320
FuseBatchNorm2dPass(exported_program),
319321
ConvertMmToBmmPass(),
320322
DecomposeGluPass(),

‎backends/arm/_passes/decompose_embedding_pass.py‎

Lines changed: 8 additions & 2 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.
@@ -10,6 +10,9 @@
1010

1111
import torch
1212
from executorch.backends.arm._passes import ArmPass
13+
from executorch.backends.arm._passes.decompose_index_select_to_gather_pass import (
14+
DecomposeIndexSelectToGatherPass,
15+
)
1316
from executorch.backends.arm._passes.fuse_view_copy_transform_pass import (
1417
FuseViewCopyTransformPass,
1518
)
@@ -35,7 +38,10 @@ class DecomposeEmbeddingPass(ArmPass):
3538
i = indices is expected to be int32 before this pass
3639
"""
3740

38-
_passes_required_after: Set[Type[ExportPass]] = {FuseViewCopyTransformPass}
41+
_passes_required_after: Set[Type[ExportPass]] = {
42+
DecomposeIndexSelectToGatherPass,
43+
FuseViewCopyTransformPass,
44+
}
3945

4046
aten_ops = (torch.ops.aten.embedding.default,)
4147
edge_ops = (exir_ops.edge.aten.embedding.default,)
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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 math
7+
from typing import Set, Type
8+
9+
import torch
10+
11+
from executorch.backends.arm._passes import ArmPass
12+
from executorch.backends.arm._passes.convert_expand_copy_to_repeat import (
13+
ConvertExpandCopyToRepeatPass,
14+
)
15+
from executorch.backends.arm._passes.convert_squeezes_to_view import (
16+
ConvertSqueezesToViewPass,
17+
)
18+
from executorch.exir.dialects._ops import ops as exir_ops
19+
from executorch.exir.pass_base import ExportPass
20+
21+
22+
def _get_index_select_decomposition(op):
23+
"""
24+
Return the operator overloads used to lower index_select via TOSA gather.
25+
26+
Raises:
27+
RuntimeError: If the provided operator is not supported by this pass.
28+
"""
29+
if op is exir_ops.edge.aten.index_select.default:
30+
return (
31+
exir_ops.edge.aten.view_copy.default,
32+
exir_ops.edge.aten.unsqueeze_copy.default,
33+
exir_ops.edge.aten.expand_copy.default,
34+
exir_ops.edge.dim_order_ops._to_dim_order_copy.default,
35+
exir_ops.backend.tosa.GATHER.default,
36+
)
37+
38+
raise RuntimeError(f"Can't get index_select decomposition for op {op}")
39+
40+
41+
class DecomposeIndexSelectToGatherPass(ArmPass):
42+
"""
43+
Decompose edge index_select into a single backend TOSA gather.
44+
45+
index_select(x, dim, index) semantics:
46+
- index is rank-1
47+
- output shape is x.shape with dim replaced by len(index)
48+
49+
Lowering strategy:
50+
- Normalize dim to [0, rank)
51+
- Flatten x into values [N, K, C] where:
52+
N = prod(x.shape[:dim]) (or 1)
53+
K = x.shape[dim]
54+
C = prod(x.shape[dim+1:]) (or 1)
55+
- Expand indices to [N, W] where W = len(index):
56+
index [W] -> [1, W] -> expand [N, W]
57+
- tosa.GATHER(values=[N,K,C], indices=[N,W]) -> [N,W,C]
58+
- Reshape back to original rank with dim replaced by W
59+
60+
Notes:
61+
- indices must be int32 (TOSA gather requirement).
62+
- bool input is cast to int8 for gather and cast back afterwards.
63+
"""
64+
65+
_passes_required_after: Set[Type[ExportPass]] = {
66+
ConvertExpandCopyToRepeatPass,
67+
ConvertSqueezesToViewPass,
68+
}
69+
70+
_TARGET_OPS = {
71+
exir_ops.edge.aten.index_select.default,
72+
}
73+
74+
def call_operator(self, op, args, kwargs, meta):
75+
if op not in self._TARGET_OPS:
76+
return super().call_operator(op, args, kwargs, meta)
77+
78+
x, dim, index = args
79+
80+
x_t, idx_t = x.data, index.data # FakeTensor
81+
x_shape, idx_shape = tuple(x_t.shape), tuple(idx_t.shape)
82+
x_rank, idx_rank = len(x_shape), len(idx_shape)
83+
84+
assert x_rank >= 1 and idx_rank == 1 and idx_t.dtype == torch.int32, (
85+
f"[{self.__class__.__name__}] unsupported index_select signature: "
86+
f"x_rank={x_rank}, index_rank={idx_rank}, index_dtype={idx_t.dtype} "
87+
"(expected x_rank>=1, index_rank==1, index_dtype=int32)."
88+
)
89+
90+
(
91+
view_op,
92+
unsqueeze_op,
93+
expand_op,
94+
to_copy_op,
95+
tosa_gather_op,
96+
) = _get_index_select_decomposition(op)
97+
98+
# Compute flattening factors
99+
dim_norm = dim % x_rank # Normalize dim
100+
before = list(x_shape[:dim_norm])
101+
after = list(x_shape[dim_norm + 1 :])
102+
103+
N = math.prod(before) if before else 1
104+
K = x_shape[dim_norm]
105+
C = math.prod(after) if after else 1
106+
W = idx_shape[0]
107+
108+
# Note:
109+
# bool index_select (and the bool<->int8 casts) are INT-profile only;
110+
# FP profile should reject these via the IndexSelectSupported check.
111+
# Optional: cast bool -> int8
112+
x_for_gather = x
113+
x_dtype = x_t.dtype
114+
if x_dtype == torch.bool:
115+
x_for_gather = super().call_operator(
116+
to_copy_op,
117+
(x,),
118+
{"dtype": torch.int8},
119+
meta,
120+
updated=True,
121+
)
122+
123+
# x_for_gather: [d0, d1, ..., d{r-1}] -> values_nkc: [N, K, C]
124+
values_nkc = super().call_operator(
125+
view_op,
126+
(x_for_gather, [N, K, C]),
127+
{},
128+
meta,
129+
updated=True,
130+
)
131+
132+
# index: [W] -> idx_1w: [1, W]
133+
idx_1w = super().call_operator(
134+
unsqueeze_op,
135+
(index, 0),
136+
{},
137+
meta,
138+
updated=True,
139+
)
140+
# idx_1w: [1, W] -> indices_nw: [N, W]
141+
indices_nw = super().call_operator(
142+
expand_op,
143+
(idx_1w, [N, W]),
144+
{},
145+
meta,
146+
updated=True,
147+
)
148+
149+
# TOSA gather:
150+
# values_nkc: [N, K, C]
151+
# indices_nw: [N, W]
152+
# gathered_nwc: [N, W, C]
153+
gathered_nwc = super().call_operator(
154+
tosa_gather_op,
155+
(values_nkc, indices_nw),
156+
{},
157+
meta,
158+
updated=True,
159+
)
160+
161+
# reshape back to original rank with dim replaced by W
162+
# gathered_nwc: [N, W, C] -> out: before + [W] + after
163+
out_shape = before + [W] + after
164+
out = super().call_operator(
165+
view_op,
166+
(gathered_nwc, out_shape),
167+
{},
168+
meta,
169+
updated=True,
170+
)
171+
172+
# Optional: cast int8 -> bool
173+
if x_dtype == torch.bool:
174+
out = super().call_operator(
175+
to_copy_op,
176+
(out,),
177+
{"dtype": torch.bool},
178+
meta,
179+
updated=True,
180+
)
181+
182+
return out
Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
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
"""Declare operator support for ``aten.index_select`` in TOSA.
66
7-
Accept int32 indices and restrict supported weight shapes to 2D or 3D with a
8-
unit batch dimension.
7+
This support check is intentionally broad to enable "full" index_select support
8+
via decomposition to backend TOSA gather.
99
10+
Constraints:
11+
- target: exir_ops.edge.aten.index_select.default
12+
- args: exactly (input, dim, index)
13+
- input rank must be >= 1 and dtype compatible with the active TOSA spec
14+
- index must be rank-1 and dtype int32
1015
"""
16+
1117
import torch
1218
import torch.fx as fx
19+
1320
from executorch.backends.arm.operator_support.tosa_supported_operators import (
1421
register_tosa_support_check,
1522
SupportedTOSAOperatorCheck,
@@ -32,29 +39,63 @@ class IndexSelectSupported(SupportedTOSAOperatorCheck):
3239
def is_node_tosa_supported(
3340
self, node: fx.Node, tosa_spec: TosaSpecification
3441
) -> bool: # type: ignore[override, misc]
35-
"""Return True if the node is supported by TOSA.
42+
if len(node.args) != 3:
43+
self.reporter.report_reject(
44+
node,
45+
f"{node.target}: expected 3 args (input, dim, index), "
46+
f"got {len(node.args)}.",
47+
)
48+
return False
49+
50+
x_arg, _, index_arg = node.args
3651

37-
Require int32 indices and limit weight shapes to 2D or 3D with a leading
38-
dimension of 1.
52+
x_val = x_arg.meta["val"] # type: ignore[union-attr]
53+
index_val = index_arg.meta["val"] # type: ignore[union-attr]
3954

40-
"""
41-
weights_shape = node.all_input_nodes[0].meta["val"].shape
42-
indices_val = node.all_input_nodes[1].meta["val"]
43-
indices_dtype = indices_val.dtype
55+
x_shape = tuple(x_val.shape)
56+
x_rank = len(x_shape)
4457

45-
if indices_dtype != torch.int32:
58+
if x_rank < 1:
4659
self.reporter.report_reject(
4760
node,
48-
f"Indices dtype {indices_val.dtype} is not supported in {node.target}.",
61+
f"{node.target}: input must be rank>=1, got shape={x_shape}.",
4962
)
5063
return False
5164

52-
if not (
53-
len(weights_shape) == 2
54-
or (len(weights_shape) == 3 and weights_shape[0] == 1)
55-
):
65+
# Indices must be int32 and rank-1.
66+
if not (index_val.dtype == torch.int32 and index_val.dim() == 1):
5667
self.reporter.report_reject(
57-
node, f"{node.target} with weights shape {weights_shape} not supported."
68+
node,
69+
f"{node.target}: index must be rank-1 int32, got dtype={index_val.dtype} "
70+
f"shape={tuple(index_val.shape)}.",
5871
)
5972
return False
73+
74+
# Dtype constraints
75+
values_dtype = x_val.dtype
76+
# ints (and bool via casts) require INT profile
77+
if values_dtype in (torch.bool, torch.int8, torch.int16, torch.int32):
78+
if not tosa_spec.support_integer():
79+
self.reporter.report_reject(
80+
node,
81+
f"{node.target}: dtype {values_dtype} requires INT profile.",
82+
)
83+
return False
84+
# fp16/fp32: either FP profile, or INT profile (via quantization)
85+
elif values_dtype in (torch.float16, torch.float32):
86+
if not (tosa_spec.support_float() or tosa_spec.support_integer()):
87+
self.reporter.report_reject(
88+
node,
89+
f"{node.target}: dtype {values_dtype} requires FP profile or "
90+
"INT profile (with quantization).",
91+
)
92+
return False
93+
else:
94+
self.reporter.report_reject(
95+
node,
96+
f"{node.target}: unsupported values dtype {values_dtype}; "
97+
"expected bool/int8/int16/int32/float16/float32.",
98+
)
99+
return False
100+
60101
return True

‎backends/arm/operators/__init__.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
op_floor,
3232
op_ge,
3333
op_gt,
34-
op_index_select,
3534
op_index_tensor,
3635
op_le,
3736
op_log,

0 commit comments

Comments
 (0)