|
| 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 Dict, Optional, Set, Tuple, Type |
| 7 | + |
| 8 | +import torch |
| 9 | + |
| 10 | +from executorch.backends.arm._passes import ArmPass |
| 11 | +from executorch.backends.arm.common.as_strided_utils import ( |
| 12 | + contiguous_strides, |
| 13 | + maybe_static_sequence, |
| 14 | + to_int, |
| 15 | + to_int_tuple, |
| 16 | +) |
| 17 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 18 | +from executorch.exir.pass_base import ExportPass |
| 19 | + |
| 20 | + |
| 21 | +class DecomposeAsStridedCopyPass(ArmPass): |
| 22 | + """ |
| 23 | + Replace contiguous `aten.as_strided_copy` with `aten.view_copy`. |
| 24 | +
|
| 25 | + The TOSA backend only supports the contiguous-as-strided case where the stride matches |
| 26 | + row-major layout and the storage offset is zero. In that scenario the operator is |
| 27 | + equivalent to a reshape with copy semantics and can be lowered via `view_copy`. |
| 28 | + """ |
| 29 | + |
| 30 | + _passes_required_after: Set[Type[ExportPass]] = set() |
| 31 | + |
| 32 | + _EDGE_OPS = (exir_ops.edge.aten.as_strided_copy.default,) |
| 33 | + _ATEN_OPS = (torch.ops.aten.as_strided_copy.default,) |
| 34 | + |
| 35 | + def _extract_args( |
| 36 | + self, args: Tuple[object, ...], kwargs: dict |
| 37 | + ) -> Optional[Tuple[Tuple[int, ...], Tuple[int, ...], int]]: |
| 38 | + """Return (size, stride, storage_offset) when they are statically known.""" |
| 39 | + if len(args) < 3: |
| 40 | + return None |
| 41 | + |
| 42 | + size_arg = args[1] |
| 43 | + stride_arg = args[2] |
| 44 | + offset_arg = ( |
| 45 | + kwargs.get("storage_offset") if "storage_offset" in kwargs else None |
| 46 | + ) |
| 47 | + if offset_arg is None and len(args) > 3: |
| 48 | + offset_arg = args[3] |
| 49 | + |
| 50 | + size_seq = maybe_static_sequence(size_arg) |
| 51 | + stride_seq = maybe_static_sequence(stride_arg) |
| 52 | + if size_seq is None or stride_seq is None: |
| 53 | + return None |
| 54 | + |
| 55 | + size_tuple = to_int_tuple(size_seq) |
| 56 | + stride_tuple = to_int_tuple(stride_seq) |
| 57 | + if size_tuple is None or stride_tuple is None: |
| 58 | + return None |
| 59 | + |
| 60 | + if len(size_tuple) != len(stride_tuple): |
| 61 | + return None |
| 62 | + |
| 63 | + if any(stride < 0 for stride in stride_tuple): |
| 64 | + return None |
| 65 | + |
| 66 | + if offset_arg is None: |
| 67 | + storage_offset = 0 |
| 68 | + else: |
| 69 | + parsed_offset = to_int(offset_arg) |
| 70 | + if parsed_offset is None: |
| 71 | + return None |
| 72 | + storage_offset = parsed_offset |
| 73 | + |
| 74 | + return size_tuple, stride_tuple, storage_offset |
| 75 | + |
| 76 | + def call_operator(self, op, args, kwargs, meta, updated: Optional[bool] = False): |
| 77 | + if op not in (*self._EDGE_OPS, *self._ATEN_OPS): |
| 78 | + return super().call_operator(op, args, kwargs, meta, updated) |
| 79 | + |
| 80 | + extracted = self._extract_args(args, kwargs) |
| 81 | + if extracted is None: |
| 82 | + return super().call_operator(op, args, kwargs, meta, updated) |
| 83 | + |
| 84 | + size_tuple, stride_tuple, storage_offset = extracted |
| 85 | + if storage_offset != 0: |
| 86 | + return super().call_operator(op, args, kwargs, meta, updated) |
| 87 | + |
| 88 | + expected_strides = contiguous_strides(size_tuple) |
| 89 | + |
| 90 | + def _stride_matches(idx: int, dim: int) -> bool: |
| 91 | + stride = stride_tuple[idx] |
| 92 | + expected = expected_strides[idx] |
| 93 | + if idx == len(size_tuple) - 1: |
| 94 | + return stride >= expected |
| 95 | + if dim == 1 or expected == 0: |
| 96 | + return True |
| 97 | + return stride == expected |
| 98 | + |
| 99 | + if any(not _stride_matches(i, dim) for i, dim in enumerate(size_tuple)): |
| 100 | + return super().call_operator(op, args, kwargs, meta, updated) |
| 101 | + |
| 102 | + view_args = (args[0], tuple(size_tuple)) |
| 103 | + view_kwargs: Dict[str, object] = {} |
| 104 | + |
| 105 | + view_op = ( |
| 106 | + exir_ops.edge.aten.view_copy.default |
| 107 | + if op in self._EDGE_OPS |
| 108 | + else torch.ops.aten.view_copy.default |
| 109 | + ) |
| 110 | + |
| 111 | + return super().call_operator( |
| 112 | + view_op, view_args, view_kwargs, meta, updated=True |
| 113 | + ) |
0 commit comments