xref: /aosp_15_r20/external/executorch/backends/xnnpack/test/ops/avgpool2d.py (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1# Copyright (c) Meta Platforms, Inc. and affiliates.
2# All rights reserved.
3#
4# This source code is licensed under the BSD-style license found in the
5# LICENSE file in the root directory of this source tree.
6
7import unittest
8
9import torch
10from executorch.backends.xnnpack.test.tester import Tester
11
12
13class TestAvgPool2d(unittest.TestCase):
14    class AvgPool2d(torch.nn.Module):
15        def __init__(
16            self, count_include_pad=False, ceil_mode=False, divisor_override=None
17        ):
18            super().__init__()
19            self.avgPool = torch.nn.AvgPool2d(
20                kernel_size=(2, 2),
21                padding=(1, 1),
22                stride=(2, 2),
23                count_include_pad=count_include_pad,
24                ceil_mode=ceil_mode,
25                divisor_override=divisor_override,
26            )
27
28        def forward(self, x):
29            return self.avgPool(x)
30
31    def _test_argpool2d(self, inputs):
32        (
33            Tester(self.AvgPool2d(), inputs)
34            .export()
35            .check_count({"torch.ops.aten.avg_pool2d.default": 1})
36            .to_edge_transform_and_lower()
37            .check_count({"torch.ops.higher_order.executorch_call_delegate": 1})
38            .check_not(["executorch_exir_dialects_edge__ops_aten_avg_pool2d_default"])
39            .to_executorch()
40            .serialize()
41            .run_method_and_compare_outputs()
42        )
43
44    def test_fp16_avgpool2d(self):
45        inputs = (torch.randn(1, 1, 10, 10).to(torch.float16),)
46        self._test_argpool2d(inputs)
47
48    def test_fp32_avgpool2d(self):
49        inputs = (torch.randn(1, 1, 10, 10),)
50        self._test_argpool2d(inputs)
51
52    def test_fp32_avgpool2d_ceil_mode_unsupported(self):
53        """
54        The XNNPACK backend does not support ceil mode.
55        """
56        inputs = (torch.randn(1, 1, 10, 10),)
57        (
58            Tester(self.AvgPool2d(ceil_mode=True), inputs)
59            .export()
60            .check_count({"torch.ops.aten.avg_pool2d.default": 1})
61            .to_edge_transform_and_lower()
62            .check_not(["torch.ops.higher_order.executorch_call_delegate"])
63        )
64
65    def test_fp32_avgpool2d_count_include_pad_unsupported(self):
66        """
67        The XNNPACK backend does not support count_include_pad=True.
68        """
69        inputs = (torch.randn(1, 1, 10, 10),)
70        (
71            Tester(self.AvgPool2d(count_include_pad=True), inputs)
72            .export()
73            .check_count({"torch.ops.aten.avg_pool2d.default": 1})
74            .to_edge_transform_and_lower()
75            .check_not(["torch.ops.higher_order.executorch_call_delegate"])
76        )
77
78    def test_fp32_avgpool2d_divisor_override(self):
79        """
80        The XNNPACK backend does not support divisor overrides not equal to the pooling region.
81        """
82        inputs = (torch.randn(1, 1, 10, 10),)
83        (
84            Tester(self.AvgPool2d(divisor_override=5), inputs)
85            .export()
86            .check_count({"torch.ops.aten.avg_pool2d.default": 1})
87            .to_edge_transform_and_lower()
88            .check_not(["torch.ops.higher_order.executorch_call_delegate"])
89        )
90