|
| 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 Tuple |
| 7 | + |
| 8 | +import executorch.backends.arm.tosa.dialect # noqa: F401 |
| 9 | +import torch |
| 10 | +from executorch.backends.arm._passes import FuseConsecutiveSlicesPass, RewriteSlicePass |
| 11 | +from executorch.backends.arm.test.tester.test_pipeline import PassPipeline |
| 12 | +from executorch.backends.arm.tosa.mapping import TosaSpecialDtype |
| 13 | +from executorch.backends.arm.tosa.specification import ( |
| 14 | + TosaLoweringContext, |
| 15 | + TosaSpecification, |
| 16 | +) |
| 17 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 18 | + |
| 19 | +input_t = Tuple[torch.Tensor] |
| 20 | +TOSA_FP_SPEC = TosaSpecification.create_from_string("TOSA-1.0+FP") |
| 21 | + |
| 22 | + |
| 23 | +class ConsecutiveSlice(torch.nn.Module): |
| 24 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 25 | + y = torch.ops.aten.slice.Tensor(x, 1, 1, 7, 1) |
| 26 | + return torch.ops.aten.slice.Tensor(y, 2, 2, 5, 1) |
| 27 | + |
| 28 | + |
| 29 | +def _const_shape(graph: torch.fx.Graph, shape: list[int]) -> torch.fx.Node: |
| 30 | + node = graph.create_node( |
| 31 | + "call_function", |
| 32 | + exir_ops.backend.tosa.CONST_SHAPE.default, |
| 33 | + args=(shape,), |
| 34 | + ) |
| 35 | + node.meta = { |
| 36 | + "val": shape, |
| 37 | + TosaSpecialDtype.meta_key(): TosaSpecialDtype.SHAPE, |
| 38 | + } |
| 39 | + return node |
| 40 | + |
| 41 | + |
| 42 | +def _slice( |
| 43 | + graph: torch.fx.Graph, |
| 44 | + input_node: torch.fx.Node, |
| 45 | + start: list[int], |
| 46 | + size: list[int], |
| 47 | + output_shape: tuple[int, ...], |
| 48 | +) -> torch.fx.Node: |
| 49 | + slice_node = graph.create_node( |
| 50 | + "call_function", |
| 51 | + exir_ops.backend.tosa.SLICE.default, |
| 52 | + args=(input_node, _const_shape(graph, start), _const_shape(graph, size)), |
| 53 | + ) |
| 54 | + slice_node.meta["val"] = torch.ones(output_shape) |
| 55 | + return slice_node |
| 56 | + |
| 57 | + |
| 58 | +def _slice_nodes(graph_module: torch.fx.GraphModule) -> list[torch.fx.Node]: |
| 59 | + return [ |
| 60 | + node |
| 61 | + for node in graph_module.graph.nodes |
| 62 | + if node.op == "call_function" |
| 63 | + and node.target == exir_ops.backend.tosa.SLICE.default |
| 64 | + ] |
| 65 | + |
| 66 | + |
| 67 | +def _shape_arg_value(node: torch.fx.Node, arg_index: int) -> list[int]: |
| 68 | + shape_node = node.args[arg_index] |
| 69 | + assert isinstance(shape_node, torch.fx.Node) |
| 70 | + assert shape_node.target == exir_ops.backend.tosa.CONST_SHAPE.default |
| 71 | + return list(shape_node.args[0]) # type: ignore[arg-type] |
| 72 | + |
| 73 | + |
| 74 | +def test_fuse_consecutive_slices_combines_static_starts() -> None: |
| 75 | + graph = torch.fx.Graph() |
| 76 | + x = graph.placeholder("x") |
| 77 | + x.meta["val"] = torch.ones((1, 8, 10)) |
| 78 | + first_slice = _slice(graph, x, [0, 1, 0], [1, 6, 10], (1, 6, 10)) |
| 79 | + second_slice = _slice(graph, first_slice, [0, 2, 3], [1, 2, 4], (1, 2, 4)) |
| 80 | + graph.output(second_slice) |
| 81 | + graph_module = torch.fx.GraphModule({}, graph) |
| 82 | + |
| 83 | + with TosaLoweringContext(TOSA_FP_SPEC): |
| 84 | + result = FuseConsecutiveSlicesPass().call(graph_module) |
| 85 | + |
| 86 | + slice_nodes = _slice_nodes(result.graph_module) |
| 87 | + |
| 88 | + assert result.modified |
| 89 | + assert len(slice_nodes) == 1 |
| 90 | + assert slice_nodes[0].args[0] is x |
| 91 | + assert _shape_arg_value(slice_nodes[0], 1) == [0, 3, 3] |
| 92 | + assert _shape_arg_value(slice_nodes[0], 2) == [1, 2, 4] |
| 93 | + |
| 94 | + |
| 95 | +def test_fuse_consecutive_slices_tosa_FP_pipeline() -> None: |
| 96 | + pipeline = PassPipeline[input_t]( |
| 97 | + ConsecutiveSlice(), |
| 98 | + (torch.randn(1, 8, 10),), |
| 99 | + ops_before_pass={ |
| 100 | + "executorch_exir_dialects_edge__ops_aten_slice_copy_Tensor": 2 |
| 101 | + }, |
| 102 | + ops_after_pass={"backend__ops_tosa_SLICE_default": 1}, |
| 103 | + ops_not_after_pass=[ |
| 104 | + "executorch_exir_dialects_edge__ops_aten_slice_copy_Tensor" |
| 105 | + ], |
| 106 | + pass_list=[RewriteSlicePass, FuseConsecutiveSlicesPass], |
| 107 | + ) |
| 108 | + pipeline.pop_stage(-1) |
| 109 | + |
| 110 | + pipeline.run() |
0 commit comments