|
| 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 collections.abc import Sequence |
| 7 | +from typing import cast |
| 8 | + |
| 9 | +from executorch.backends.transforms.aten_to_dialect_pass import ( |
| 10 | + AtenToDialectPass, |
| 11 | + DialectNodeSpec, |
| 12 | +) |
| 13 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 14 | +from torch.fx import Node |
| 15 | + |
| 16 | + |
| 17 | +def _get_arg(node: Node, index: int, name: str, default=None): |
| 18 | + if len(node.args) > index: |
| 19 | + return node.args[index] |
| 20 | + return node.kwargs.get(name, default) |
| 21 | + |
| 22 | + |
| 23 | +def _normalize_dim(dim: int, rank: int) -> int: |
| 24 | + return (dim + rank) % rank |
| 25 | + |
| 26 | + |
| 27 | +def _input_rank(node: Node) -> int: |
| 28 | + input_node = cast(Node, node.args[0]) |
| 29 | + return len(input_node.meta["val"].shape) |
| 30 | + |
| 31 | + |
| 32 | +def _rewrite_cat(node: Node, pass_: AtenToDialectPass) -> DialectNodeSpec: |
| 33 | + tensors = cast(Sequence[Node], node.args[0]) |
| 34 | + dim = _get_arg(node, 1, "dim", 0) |
| 35 | + first_tensor = tensors[0] |
| 36 | + axis = _normalize_dim(cast(int, dim), len(first_tensor.meta["val"].shape)) |
| 37 | + return DialectNodeSpec( |
| 38 | + exir_ops.backend.tosa.CONCAT.default, |
| 39 | + (tensors,), |
| 40 | + {"axis": axis}, |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +def _rewrite_view_copy(node: Node, pass_: AtenToDialectPass) -> DialectNodeSpec: |
| 45 | + return DialectNodeSpec( |
| 46 | + exir_ops.backend.tosa.RESHAPE.default, |
| 47 | + node.args, |
| 48 | + dict(node.kwargs), |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def _rewrite_repeat(node: Node, pass_: AtenToDialectPass) -> DialectNodeSpec: |
| 53 | + return DialectNodeSpec( |
| 54 | + exir_ops.backend.tosa.TILE.default, |
| 55 | + node.args, |
| 56 | + dict(node.kwargs), |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +def _rewrite_permute_copy(node: Node, pass_: AtenToDialectPass) -> DialectNodeSpec: |
| 61 | + permutation = list(cast(Sequence[int], _get_arg(node, 1, "dims"))) |
| 62 | + rank = _input_rank(node) |
| 63 | + permutation = [_normalize_dim(dim, rank) for dim in permutation] |
| 64 | + return DialectNodeSpec( |
| 65 | + exir_ops.backend.tosa.TRANSPOSE.default, |
| 66 | + (node.args[0], permutation), |
| 67 | + {}, |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +def _rewrite_flip(node: Node, pass_: AtenToDialectPass) -> DialectNodeSpec | None: |
| 72 | + dims = list(cast(Sequence[int], _get_arg(node, 1, "dims"))) |
| 73 | + if len(dims) != 1: |
| 74 | + return None |
| 75 | + |
| 76 | + return DialectNodeSpec( |
| 77 | + exir_ops.backend.tosa.REVERSE.default, |
| 78 | + (node.args[0],), |
| 79 | + {"axis": _normalize_dim(dims[0], _input_rank(node))}, |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +def rewrite_data_layout_operator( |
| 84 | + node: Node, pass_: AtenToDialectPass |
| 85 | +) -> DialectNodeSpec | None: |
| 86 | + match node.target: |
| 87 | + case exir_ops.edge.aten.cat.default: |
| 88 | + return _rewrite_cat(node, pass_) |
| 89 | + case exir_ops.edge.aten.view_copy.default: |
| 90 | + return _rewrite_view_copy(node, pass_) |
| 91 | + case exir_ops.edge.aten.repeat.default: |
| 92 | + return _rewrite_repeat(node, pass_) |
| 93 | + case exir_ops.edge.aten.permute_copy.default: |
| 94 | + return _rewrite_permute_copy(node, pass_) |
| 95 | + case exir_ops.edge.aten.flip.default: |
| 96 | + return _rewrite_flip(node, pass_) |
| 97 | + case _: |
| 98 | + return None |
0 commit comments