Skip to content

[Backend Tester] Add adaptive maxpool tests #13242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions backends/test/suite/operators/test_adaptive_avgpool1d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

# pyre-unsafe


import torch
from executorch.backends.test.suite.flow import TestFlow

from executorch.backends.test.suite.operators import (
dtype_test,
operator_test,
OperatorTest,
)


class Model(torch.nn.Module):
def __init__(
self,
output_size=5,
):
super().__init__()
self.adaptive_avgpool = torch.nn.AdaptiveAvgPool1d(
output_size=output_size,
)

def forward(self, x):
return self.adaptive_avgpool(x)


@operator_test
class AdaptiveAvgPool1d(OperatorTest):
@dtype_test
def test_adaptive_avgpool1d_dtype(self, flow: TestFlow, dtype) -> None:
# Input shape: (batch_size, channels, length)
self._test_op(
Model().to(dtype),
((torch.rand(1, 8, 100) * 10).to(dtype),),
flow,
)

def test_adaptive_avgpool1d_output_size(self, flow: TestFlow) -> None:
# Test with different output sizes
self._test_op(
Model(output_size=1),
(torch.randn(1, 8, 100),),
flow,
)
self._test_op(
Model(output_size=10),
(torch.randn(1, 8, 100),),
flow,
)
self._test_op(
Model(output_size=50),
(torch.randn(1, 8, 100),),
flow,
)

def test_adaptive_avgpool1d_batch_sizes(self, flow: TestFlow) -> None:
# Test with batch inputs
self._test_op(
Model(),
(torch.randn(2, 8, 100),),
flow,
)
self._test_op(
Model(),
(torch.randn(8, 8, 100),),
flow,
)
self._test_op(
Model(),
(torch.randn(16, 8, 100),),
flow,
)

def test_adaptive_avgpool1d_input_sizes(self, flow: TestFlow) -> None:
# Test with different input sizes
self._test_op(
Model(),
(torch.randn(1, 4, 100),),
flow,
)
self._test_op(
Model(),
(torch.randn(1, 16, 100),),
flow,
)
self._test_op(
Model(),
(torch.randn(1, 8, 50),),
flow,
)
self._test_op(
Model(),
(torch.randn(1, 8, 200),),
flow,
)
112 changes: 112 additions & 0 deletions backends/test/suite/operators/test_adaptive_avgpool2d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

# pyre-unsafe


import torch
from executorch.backends.test.suite.flow import TestFlow

from executorch.backends.test.suite.operators import (
dtype_test,
operator_test,
OperatorTest,
)


class Model(torch.nn.Module):
def __init__(
self,
output_size=(5, 5),
):
super().__init__()
self.adaptive_avgpool = torch.nn.AdaptiveAvgPool2d(
output_size=output_size,
)

def forward(self, x):
return self.adaptive_avgpool(x)


@operator_test
class AdaptiveAvgPool2d(OperatorTest):
@dtype_test
def test_adaptive_avgpool2d_dtype(self, flow: TestFlow, dtype) -> None:
# Input shape: (batch_size, channels, height, width)
self._test_op(
Model().to(dtype),
((torch.rand(1, 8, 20, 20) * 10).to(dtype),),
flow,
)

def test_adaptive_avgpool2d_output_size(self, flow: TestFlow) -> None:
# Test with different output sizes
self._test_op(
Model(output_size=1),
(torch.randn(1, 8, 20, 20),),
flow,
)
self._test_op(
Model(output_size=(1, 1)),
(torch.randn(1, 8, 20, 20),),
flow,
)
self._test_op(
Model(output_size=(10, 10)),
(torch.randn(1, 8, 20, 20),),
flow,
)
self._test_op(
Model(output_size=(5, 10)),
(torch.randn(1, 8, 20, 20),),
flow,
)

def test_adaptive_avgpool2d_batch_sizes(self, flow: TestFlow) -> None:
# Test with batch inputs
self._test_op(
Model(),
(torch.randn(2, 8, 20, 20),),
flow,
)
self._test_op(
Model(),
(torch.randn(8, 8, 20, 20),),
flow,
)
self._test_op(
Model(),
(torch.randn(16, 8, 20, 20),),
flow,
)

def test_adaptive_avgpool2d_input_sizes(self, flow: TestFlow) -> None:
# Test with different input sizes
self._test_op(
Model(),
(torch.randn(1, 4, 20, 20),),
flow,
)
self._test_op(
Model(),
(torch.randn(1, 16, 20, 20),),
flow,
)
self._test_op(
Model(),
(torch.randn(1, 8, 10, 10),),
flow,
)
self._test_op(
Model(),
(torch.randn(1, 8, 30, 30),),
flow,
)
self._test_op(
Model(),
(torch.randn(1, 8, 15, 25),),
flow,
)
112 changes: 112 additions & 0 deletions backends/test/suite/operators/test_adaptive_avgpool3d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

# pyre-unsafe


import torch
from executorch.backends.test.suite.flow import TestFlow

from executorch.backends.test.suite.operators import (
dtype_test,
operator_test,
OperatorTest,
)


class Model(torch.nn.Module):
def __init__(
self,
output_size=(4, 4, 4),
):
super().__init__()
self.adaptive_avgpool = torch.nn.AdaptiveAvgPool3d(
output_size=output_size,
)

def forward(self, x):
return self.adaptive_avgpool(x)


@operator_test
class AdaptiveAvgPool3d(OperatorTest):
@dtype_test
def test_adaptive_avgpool3d_dtype(self, flow: TestFlow, dtype) -> None:
# Input shape: (batch_size, channels, depth, height, width)
self._test_op(
Model().to(dtype),
((torch.rand(1, 4, 8, 8, 8) * 10).to(dtype),),
flow,
)

def test_adaptive_avgpool3d_output_size(self, flow: TestFlow) -> None:
# Test with different output sizes
self._test_op(
Model(output_size=1),
(torch.randn(1, 4, 8, 8, 8),),
flow,
)
self._test_op(
Model(output_size=(1, 1, 1)),
(torch.randn(1, 4, 8, 8, 8),),
flow,
)
self._test_op(
Model(output_size=(6, 6, 6)),
(torch.randn(1, 4, 8, 8, 8),),
flow,
)
self._test_op(
Model(output_size=(2, 4, 6)),
(torch.randn(1, 4, 8, 8, 8),),
flow,
)

def test_adaptive_avgpool3d_batch_sizes(self, flow: TestFlow) -> None:
# Test with batch inputs
self._test_op(
Model(),
(torch.randn(2, 4, 8, 8, 8),),
flow,
)
self._test_op(
Model(),
(torch.randn(8, 4, 8, 8, 8),),
flow,
)
self._test_op(
Model(),
(torch.randn(16, 4, 8, 8, 8),),
flow,
)

def test_adaptive_avgpool3d_input_sizes(self, flow: TestFlow) -> None:
# Test with different input sizes
self._test_op(
Model(),
(torch.randn(1, 2, 8, 8, 8),),
flow,
)
self._test_op(
Model(),
(torch.randn(1, 8, 8, 8, 8),),
flow,
)
self._test_op(
Model(),
(torch.randn(1, 4, 6, 6, 6),),
flow,
)
self._test_op(
Model(),
(torch.randn(1, 4, 10, 10, 10),),
flow,
)
self._test_op(
Model(),
(torch.randn(1, 4, 7, 9, 11),),
flow,
)
Loading
Loading