|
| 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 |
0 commit comments