|
| 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 cast, Dict, Protocol, Tuple |
| 7 | + |
| 8 | +import torch |
| 9 | +from executorch.backends.arm._passes.remove_getitem_pass import RemoveGetItemPass |
| 10 | +from executorch.backends.arm._passes.rewrite_max_pool2d_pass import RewriteMaxPool2dPass |
| 11 | +from executorch.backends.arm.test import common |
| 12 | +from executorch.backends.arm.test.tester.test_pipeline import PassPipeline |
| 13 | + |
| 14 | +input_t = Tuple[torch.Tensor] |
| 15 | + |
| 16 | + |
| 17 | +class ModuleWithInputs(Protocol): |
| 18 | + def get_inputs(self) -> input_t: ... |
| 19 | + |
| 20 | + |
| 21 | +class MaxPool2dWithStride(torch.nn.Module): |
| 22 | + def get_inputs(self) -> input_t: |
| 23 | + return (torch.rand(1, 3, 8, 8),) |
| 24 | + |
| 25 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 26 | + return torch.nn.functional.max_pool2d(x, kernel_size=2, stride=2) |
| 27 | + |
| 28 | + |
| 29 | +class MaxPool2dWithoutStride(torch.nn.Module): |
| 30 | + def get_inputs(self) -> input_t: |
| 31 | + return (torch.rand(1, 3, 8, 8),) |
| 32 | + |
| 33 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 34 | + return torch.nn.functional.max_pool2d(x, kernel_size=3) |
| 35 | + |
| 36 | + |
| 37 | +class MaxPool2dListKernel(torch.nn.Module): |
| 38 | + def get_inputs(self) -> input_t: |
| 39 | + return (torch.rand(1, 3, 8, 8),) |
| 40 | + |
| 41 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 42 | + return torch.nn.functional.max_pool2d(x, kernel_size=[2, 3]) |
| 43 | + |
| 44 | + |
| 45 | +modules: Dict[str, ModuleWithInputs] = { |
| 46 | + "max_pool2d_with_stride": MaxPool2dWithStride(), |
| 47 | + "max_pool2d_without_stride": MaxPool2dWithoutStride(), |
| 48 | + "max_pool2d_list_kernel": MaxPool2dListKernel(), |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +@common.parametrize("module", modules) |
| 53 | +def test_rewrite_max_pool2d_tosa(module: ModuleWithInputs) -> None: |
| 54 | + nn_module = cast(torch.nn.Module, module) |
| 55 | + pipeline = PassPipeline[input_t]( |
| 56 | + nn_module, |
| 57 | + module.get_inputs(), |
| 58 | + ops_before_pass={ |
| 59 | + "executorch_exir_dialects_edge__ops_aten_max_pool2d_with_indices_default": 1, |
| 60 | + }, |
| 61 | + ops_after_pass={ |
| 62 | + "executorch_exir_dialects_backend__ops_tosa_MAX_POOL2D_default": 1, |
| 63 | + }, |
| 64 | + pass_list=[RemoveGetItemPass, RewriteMaxPool2dPass], |
| 65 | + ) |
| 66 | + pipeline.pop_stage( |
| 67 | + "run_method_and_compare_outputs" |
| 68 | + ) # Cannnot run aten graph with tosa dialect ops |
| 69 | + pipeline.run() |
0 commit comments