|
| 1 | +from typing import Generator |
| 2 | + |
| 3 | +import pytest |
| 4 | +import torch |
| 5 | + |
| 6 | +import flag_gems |
| 7 | + |
| 8 | +from . import attri_util as attr_utils |
| 9 | +from . import performance_utils as utils |
| 10 | + |
| 11 | + |
| 12 | +class AvgPool2dBenchmark(utils.GenericBenchmark): |
| 13 | + def get_input_iter(self, cur_dtype) -> Generator: |
| 14 | + shapes_4d = [ |
| 15 | + (4, 3, 224, 224), # Typical input image size |
| 16 | + (16, 64, 56, 56), # Early ResNet layer output |
| 17 | + (32, 128, 28, 28), # Mid ResNet layer output |
| 18 | + (64, 256, 14, 14), # Later ResNet layer output |
| 19 | + (128, 512, 7, 7), # Final ResNet layer output |
| 20 | + ] |
| 21 | + |
| 22 | + for shape in shapes_4d: |
| 23 | + yield from self.input_fn(shape, cur_dtype, self.device) |
| 24 | + |
| 25 | + |
| 26 | +def avg_pool2d_input_fn(shape, dtype, device): |
| 27 | + inp = utils.generate_tensor_input(shape, dtype, device) |
| 28 | + |
| 29 | + # Common case |
| 30 | + yield inp, { |
| 31 | + "kernel_size": 3, |
| 32 | + "stride": 2, |
| 33 | + "padding": 1, |
| 34 | + "ceil_mode": False, |
| 35 | + "count_include_pad": True, |
| 36 | + "divisor_override": None, |
| 37 | + } |
| 38 | + |
| 39 | + if utils.Config.bench_level == utils.BenchLevel.COMPREHENSIVE: |
| 40 | + # With count_include_pad=False |
| 41 | + yield inp, { |
| 42 | + "kernel_size": 3, |
| 43 | + "stride": 2, |
| 44 | + "padding": 1, |
| 45 | + "ceil_mode": False, |
| 46 | + "count_include_pad": False, |
| 47 | + "divisor_override": None, |
| 48 | + } |
| 49 | + |
| 50 | + # With ceil_mode |
| 51 | + yield inp, { |
| 52 | + "kernel_size": 3, |
| 53 | + "stride": 2, |
| 54 | + "padding": 1, |
| 55 | + "ceil_mode": True, |
| 56 | + "count_include_pad": True, |
| 57 | + "divisor_override": None, |
| 58 | + } |
| 59 | + |
| 60 | + # With divisor_override |
| 61 | + if shape[-2] >= 2 and shape[-1] >= 2: |
| 62 | + yield inp, { |
| 63 | + "kernel_size": 2, |
| 64 | + "stride": 1, |
| 65 | + "padding": 0, |
| 66 | + "ceil_mode": False, |
| 67 | + "count_include_pad": True, |
| 68 | + "divisor_override": 3, |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.avg_pool2d |
| 73 | +def test_avg_pool2d(): |
| 74 | + bench = AvgPool2dBenchmark( |
| 75 | + input_fn=avg_pool2d_input_fn, |
| 76 | + op_name="avg_pool2d", |
| 77 | + torch_op=torch.ops.aten.avg_pool2d, |
| 78 | + dtypes=attr_utils.FLOAT_DTYPES, |
| 79 | + ) |
| 80 | + bench.run() |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.skip(reason="Test case fails due to missing parameter self.") |
| 84 | +@pytest.mark.avg_pool2d_backward |
| 85 | +def test_avg_pool2d_backward(): |
| 86 | + if flag_gems.vendor_name == "mthreads": |
| 87 | + dtypes = [torch.float32] |
| 88 | + else: |
| 89 | + dtypes = (attr_utils.FLOAT_DTYPES,) |
| 90 | + |
| 91 | + bench = AvgPool2dBenchmark( |
| 92 | + input_fn=avg_pool2d_input_fn, |
| 93 | + op_name="avg_pool2d_backward", |
| 94 | + torch_op=torch.ops.aten.avg_pool2d_backward, |
| 95 | + dtypes=dtypes, |
| 96 | + is_backward=True, |
| 97 | + ) |
| 98 | + bench.run() |
0 commit comments