Skip to content

Commit afa009a

Browse files
authored
[Backend Tester] Add adaptive avgpool tests (#13241)
Add tests for adaptive avgpooling operators. This is done in the context of #12898.
1 parent cffc3f3 commit afa009a

File tree

3 files changed

+326
-0
lines changed

3 files changed

+326
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
7+
# pyre-unsafe
8+
9+
10+
import torch
11+
from executorch.backends.test.suite.flow import TestFlow
12+
13+
from executorch.backends.test.suite.operators import (
14+
dtype_test,
15+
operator_test,
16+
OperatorTest,
17+
)
18+
19+
20+
class Model(torch.nn.Module):
21+
def __init__(
22+
self,
23+
output_size=5,
24+
):
25+
super().__init__()
26+
self.adaptive_avgpool = torch.nn.AdaptiveAvgPool1d(
27+
output_size=output_size,
28+
)
29+
30+
def forward(self, x):
31+
return self.adaptive_avgpool(x)
32+
33+
34+
@operator_test
35+
class AdaptiveAvgPool1d(OperatorTest):
36+
@dtype_test
37+
def test_adaptive_avgpool1d_dtype(self, flow: TestFlow, dtype) -> None:
38+
# Input shape: (batch_size, channels, length)
39+
self._test_op(
40+
Model().to(dtype),
41+
((torch.rand(1, 8, 100) * 10).to(dtype),),
42+
flow,
43+
)
44+
45+
def test_adaptive_avgpool1d_output_size(self, flow: TestFlow) -> None:
46+
# Test with different output sizes
47+
self._test_op(
48+
Model(output_size=1),
49+
(torch.randn(1, 8, 100),),
50+
flow,
51+
)
52+
self._test_op(
53+
Model(output_size=10),
54+
(torch.randn(1, 8, 100),),
55+
flow,
56+
)
57+
self._test_op(
58+
Model(output_size=50),
59+
(torch.randn(1, 8, 100),),
60+
flow,
61+
)
62+
63+
def test_adaptive_avgpool1d_batch_sizes(self, flow: TestFlow) -> None:
64+
# Test with batch inputs
65+
self._test_op(
66+
Model(),
67+
(torch.randn(2, 8, 100),),
68+
flow,
69+
)
70+
self._test_op(
71+
Model(),
72+
(torch.randn(8, 8, 100),),
73+
flow,
74+
)
75+
self._test_op(
76+
Model(),
77+
(torch.randn(16, 8, 100),),
78+
flow,
79+
)
80+
81+
def test_adaptive_avgpool1d_input_sizes(self, flow: TestFlow) -> None:
82+
# Test with different input sizes
83+
self._test_op(
84+
Model(),
85+
(torch.randn(1, 4, 100),),
86+
flow,
87+
)
88+
self._test_op(
89+
Model(),
90+
(torch.randn(1, 16, 100),),
91+
flow,
92+
)
93+
self._test_op(
94+
Model(),
95+
(torch.randn(1, 8, 50),),
96+
flow,
97+
)
98+
self._test_op(
99+
Model(),
100+
(torch.randn(1, 8, 200),),
101+
flow,
102+
)
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
7+
# pyre-unsafe
8+
9+
10+
import torch
11+
from executorch.backends.test.suite.flow import TestFlow
12+
13+
from executorch.backends.test.suite.operators import (
14+
dtype_test,
15+
operator_test,
16+
OperatorTest,
17+
)
18+
19+
20+
class Model(torch.nn.Module):
21+
def __init__(
22+
self,
23+
output_size=(5, 5),
24+
):
25+
super().__init__()
26+
self.adaptive_avgpool = torch.nn.AdaptiveAvgPool2d(
27+
output_size=output_size,
28+
)
29+
30+
def forward(self, x):
31+
return self.adaptive_avgpool(x)
32+
33+
34+
@operator_test
35+
class AdaptiveAvgPool2d(OperatorTest):
36+
@dtype_test
37+
def test_adaptive_avgpool2d_dtype(self, flow: TestFlow, dtype) -> None:
38+
# Input shape: (batch_size, channels, height, width)
39+
self._test_op(
40+
Model().to(dtype),
41+
((torch.rand(1, 8, 20, 20) * 10).to(dtype),),
42+
flow,
43+
)
44+
45+
def test_adaptive_avgpool2d_output_size(self, flow: TestFlow) -> None:
46+
# Test with different output sizes
47+
self._test_op(
48+
Model(output_size=1),
49+
(torch.randn(1, 8, 20, 20),),
50+
flow,
51+
)
52+
self._test_op(
53+
Model(output_size=(1, 1)),
54+
(torch.randn(1, 8, 20, 20),),
55+
flow,
56+
)
57+
self._test_op(
58+
Model(output_size=(10, 10)),
59+
(torch.randn(1, 8, 20, 20),),
60+
flow,
61+
)
62+
self._test_op(
63+
Model(output_size=(5, 10)),
64+
(torch.randn(1, 8, 20, 20),),
65+
flow,
66+
)
67+
68+
def test_adaptive_avgpool2d_batch_sizes(self, flow: TestFlow) -> None:
69+
# Test with batch inputs
70+
self._test_op(
71+
Model(),
72+
(torch.randn(2, 8, 20, 20),),
73+
flow,
74+
)
75+
self._test_op(
76+
Model(),
77+
(torch.randn(8, 8, 20, 20),),
78+
flow,
79+
)
80+
self._test_op(
81+
Model(),
82+
(torch.randn(16, 8, 20, 20),),
83+
flow,
84+
)
85+
86+
def test_adaptive_avgpool2d_input_sizes(self, flow: TestFlow) -> None:
87+
# Test with different input sizes
88+
self._test_op(
89+
Model(),
90+
(torch.randn(1, 4, 20, 20),),
91+
flow,
92+
)
93+
self._test_op(
94+
Model(),
95+
(torch.randn(1, 16, 20, 20),),
96+
flow,
97+
)
98+
self._test_op(
99+
Model(),
100+
(torch.randn(1, 8, 10, 10),),
101+
flow,
102+
)
103+
self._test_op(
104+
Model(),
105+
(torch.randn(1, 8, 30, 30),),
106+
flow,
107+
)
108+
self._test_op(
109+
Model(),
110+
(torch.randn(1, 8, 15, 25),),
111+
flow,
112+
)
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
7+
# pyre-unsafe
8+
9+
10+
import torch
11+
from executorch.backends.test.suite.flow import TestFlow
12+
13+
from executorch.backends.test.suite.operators import (
14+
dtype_test,
15+
operator_test,
16+
OperatorTest,
17+
)
18+
19+
20+
class Model(torch.nn.Module):
21+
def __init__(
22+
self,
23+
output_size=(4, 4, 4),
24+
):
25+
super().__init__()
26+
self.adaptive_avgpool = torch.nn.AdaptiveAvgPool3d(
27+
output_size=output_size,
28+
)
29+
30+
def forward(self, x):
31+
return self.adaptive_avgpool(x)
32+
33+
34+
@operator_test
35+
class AdaptiveAvgPool3d(OperatorTest):
36+
@dtype_test
37+
def test_adaptive_avgpool3d_dtype(self, flow: TestFlow, dtype) -> None:
38+
# Input shape: (batch_size, channels, depth, height, width)
39+
self._test_op(
40+
Model().to(dtype),
41+
((torch.rand(1, 4, 8, 8, 8) * 10).to(dtype),),
42+
flow,
43+
)
44+
45+
def test_adaptive_avgpool3d_output_size(self, flow: TestFlow) -> None:
46+
# Test with different output sizes
47+
self._test_op(
48+
Model(output_size=1),
49+
(torch.randn(1, 4, 8, 8, 8),),
50+
flow,
51+
)
52+
self._test_op(
53+
Model(output_size=(1, 1, 1)),
54+
(torch.randn(1, 4, 8, 8, 8),),
55+
flow,
56+
)
57+
self._test_op(
58+
Model(output_size=(6, 6, 6)),
59+
(torch.randn(1, 4, 8, 8, 8),),
60+
flow,
61+
)
62+
self._test_op(
63+
Model(output_size=(2, 4, 6)),
64+
(torch.randn(1, 4, 8, 8, 8),),
65+
flow,
66+
)
67+
68+
def test_adaptive_avgpool3d_batch_sizes(self, flow: TestFlow) -> None:
69+
# Test with batch inputs
70+
self._test_op(
71+
Model(),
72+
(torch.randn(2, 4, 8, 8, 8),),
73+
flow,
74+
)
75+
self._test_op(
76+
Model(),
77+
(torch.randn(8, 4, 8, 8, 8),),
78+
flow,
79+
)
80+
self._test_op(
81+
Model(),
82+
(torch.randn(16, 4, 8, 8, 8),),
83+
flow,
84+
)
85+
86+
def test_adaptive_avgpool3d_input_sizes(self, flow: TestFlow) -> None:
87+
# Test with different input sizes
88+
self._test_op(
89+
Model(),
90+
(torch.randn(1, 2, 8, 8, 8),),
91+
flow,
92+
)
93+
self._test_op(
94+
Model(),
95+
(torch.randn(1, 8, 8, 8, 8),),
96+
flow,
97+
)
98+
self._test_op(
99+
Model(),
100+
(torch.randn(1, 4, 6, 6, 6),),
101+
flow,
102+
)
103+
self._test_op(
104+
Model(),
105+
(torch.randn(1, 4, 10, 10, 10),),
106+
flow,
107+
)
108+
self._test_op(
109+
Model(),
110+
(torch.randn(1, 4, 7, 9, 11),),
111+
flow,
112+
)

0 commit comments

Comments
 (0)