|
| 1 | +# Copyright 2025 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 torch |
| 9 | +from executorch.backends.arm._passes.decompose_avg_pool2d import DecomposeAvgPool2d |
| 10 | +from executorch.backends.arm.test import common |
| 11 | +from executorch.backends.arm.test.tester.test_pipeline import PassPipeline |
| 12 | + |
| 13 | +input_t = Tuple[torch.Tensor] # Input x |
| 14 | + |
| 15 | + |
| 16 | +class AvgPool2dWithStride(torch.nn.Module): |
| 17 | + """ |
| 18 | + avg_pool2d model with explicit stride parameter |
| 19 | + """ |
| 20 | + |
| 21 | + def get_inputs(self) -> input_t: |
| 22 | + return (torch.rand(1, 3, 8, 8),) |
| 23 | + |
| 24 | + def forward(self, x): |
| 25 | + return torch.nn.functional.avg_pool2d(x, kernel_size=2, stride=2) |
| 26 | + |
| 27 | + |
| 28 | +class AvgPool2dWithoutStride(torch.nn.Module): |
| 29 | + """ |
| 30 | + avg_pool2d model without stride parameter (should default to kernel_size) |
| 31 | + """ |
| 32 | + |
| 33 | + def get_inputs(self) -> input_t: |
| 34 | + return (torch.rand(1, 3, 8, 8),) |
| 35 | + |
| 36 | + def forward(self, x): |
| 37 | + return torch.nn.functional.avg_pool2d(x, kernel_size=3) |
| 38 | + |
| 39 | + |
| 40 | +class AvgPool2dListKernel(torch.nn.Module): |
| 41 | + """ |
| 42 | + avg_pool2d model with list kernel_size and no stride |
| 43 | + """ |
| 44 | + |
| 45 | + def get_inputs(self) -> input_t: |
| 46 | + return (torch.rand(1, 3, 8, 8),) |
| 47 | + |
| 48 | + def forward(self, x): |
| 49 | + return torch.nn.functional.avg_pool2d(x, kernel_size=[2, 3]) |
| 50 | + |
| 51 | + |
| 52 | +modules = { |
| 53 | + "avg_pool2d_with_stride": AvgPool2dWithStride(), |
| 54 | + "avg_pool2d_without_stride": AvgPool2dWithoutStride(), |
| 55 | + "avg_pool2d_list_kernel": AvgPool2dListKernel(), |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +@common.parametrize("module", modules) |
| 60 | +def test_decompose_avg_pool2d_tosa_MI(module): |
| 61 | + """Test that DecomposeAvgPool2d pass works correctly with and without stride parameters.""" |
| 62 | + pipeline = PassPipeline[input_t]( |
| 63 | + module, |
| 64 | + module.get_inputs(), |
| 65 | + quantize=False, |
| 66 | + ops_before_pass={ |
| 67 | + "executorch_exir_dialects_edge__ops_aten_avg_pool2d_default": 1, |
| 68 | + }, |
| 69 | + ops_after_pass={ |
| 70 | + # After decomposition, we should still see avg_pool2d (transformed) |
| 71 | + "executorch_exir_dialects_edge__ops_aten_avg_pool2d_default": 1, |
| 72 | + }, |
| 73 | + pass_list=[DecomposeAvgPool2d], |
| 74 | + ) |
| 75 | + pipeline.run() |
0 commit comments