|
| 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 typing import Sequence, Set, Type |
| 7 | + |
| 8 | +from executorch.backends.arm._passes import ArmPass |
| 9 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 10 | +from executorch.exir.pass_base import ExportPass |
| 11 | + |
| 12 | + |
| 13 | +class RewriteHighRankSingletonPermutePass(ArmPass): |
| 14 | + """Rewrite high-rank permute via a lower-rank permute when singleton dims |
| 15 | + allow it. |
| 16 | +
|
| 17 | + For rank>4 tensors, some backends are fragile around direct high-rank |
| 18 | + TRANSPOSE. When singleton dimensions are present, we can rewrite: |
| 19 | +
|
| 20 | + permute(rank>4) -> view(remove singleton dims) -> permute(reduced rank) -> |
| 21 | + view(restore rank) |
| 22 | +
|
| 23 | + This keeps semantics unchanged while reducing the permute rank. |
| 24 | +
|
| 25 | + """ |
| 26 | + |
| 27 | + _passes_required_after: Set[Type[ExportPass]] = set() |
| 28 | + |
| 29 | + _PERMUTE_OPS = ( |
| 30 | + exir_ops.edge.aten.permute.default, |
| 31 | + exir_ops.edge.aten.permute_copy.default, |
| 32 | + ) |
| 33 | + |
| 34 | + @staticmethod |
| 35 | + def _extract_permutation(permutation_arg: object) -> tuple[int, ...] | None: |
| 36 | + if not isinstance(permutation_arg, (list, tuple)): |
| 37 | + return None |
| 38 | + if not all(isinstance(dim, int) for dim in permutation_arg): |
| 39 | + return None |
| 40 | + return tuple(permutation_arg) |
| 41 | + |
| 42 | + @staticmethod |
| 43 | + def _normalize_permutation( |
| 44 | + permutation: Sequence[int], rank: int |
| 45 | + ) -> tuple[int, ...]: |
| 46 | + return tuple(dim % rank for dim in permutation) |
| 47 | + |
| 48 | + def call_operator(self, op, args, kwargs, meta): |
| 49 | + if op not in self._PERMUTE_OPS: |
| 50 | + return super().call_operator(op, args, kwargs, meta) |
| 51 | + if len(args) < 2: |
| 52 | + return super().call_operator(op, args, kwargs, meta) |
| 53 | + if not hasattr(args[0], "data"): |
| 54 | + return super().call_operator(op, args, kwargs, meta) |
| 55 | + if "val" not in meta or not hasattr(meta["val"], "shape"): |
| 56 | + return super().call_operator(op, args, kwargs, meta) |
| 57 | + |
| 58 | + permutation = self._extract_permutation(args[1]) |
| 59 | + if permutation is None: |
| 60 | + return super().call_operator(op, args, kwargs, meta) |
| 61 | + |
| 62 | + input_shape = list(args[0].data.shape) |
| 63 | + output_shape = list(meta["val"].shape) |
| 64 | + rank = len(input_shape) |
| 65 | + if rank <= 4 or len(output_shape) != rank: |
| 66 | + return super().call_operator(op, args, kwargs, meta) |
| 67 | + |
| 68 | + normalized_permutation = self._normalize_permutation(permutation, rank) |
| 69 | + singleton_axes = [axis for axis, dim in enumerate(input_shape) if dim == 1] |
| 70 | + if not singleton_axes: |
| 71 | + return super().call_operator(op, args, kwargs, meta) |
| 72 | + |
| 73 | + non_singleton_axes = [ |
| 74 | + axis for axis in range(rank) if axis not in singleton_axes |
| 75 | + ] |
| 76 | + reduced_rank = len(non_singleton_axes) |
| 77 | + if reduced_rank > 4: |
| 78 | + return super().call_operator(op, args, kwargs, meta) |
| 79 | + |
| 80 | + axis_to_reduced_axis = { |
| 81 | + axis: idx for idx, axis in enumerate(non_singleton_axes) |
| 82 | + } |
| 83 | + reduced_permutation = tuple( |
| 84 | + axis_to_reduced_axis[axis] |
| 85 | + for axis in normalized_permutation |
| 86 | + if axis in axis_to_reduced_axis |
| 87 | + ) |
| 88 | + expected_axes = tuple(range(reduced_rank)) |
| 89 | + if tuple(sorted(reduced_permutation)) != expected_axes: |
| 90 | + return super().call_operator(op, args, kwargs, meta) |
| 91 | + |
| 92 | + reduced_input_shape = [input_shape[axis] for axis in non_singleton_axes] |
| 93 | + reduced_input = super().call_operator( |
| 94 | + exir_ops.edge.aten.view_copy.default, |
| 95 | + (args[0], reduced_input_shape), |
| 96 | + {}, |
| 97 | + meta, |
| 98 | + ) |
| 99 | + if reduced_permutation == expected_axes: |
| 100 | + reduced_output = reduced_input |
| 101 | + else: |
| 102 | + reduced_output = super().call_operator( |
| 103 | + op, |
| 104 | + (reduced_input, reduced_permutation), |
| 105 | + kwargs, |
| 106 | + meta, |
| 107 | + ) |
| 108 | + return super().call_operator( |
| 109 | + exir_ops.edge.aten.view_copy.default, |
| 110 | + (reduced_output, output_shape), |
| 111 | + {}, |
| 112 | + meta, |
| 113 | + ) |
0 commit comments