Skip to content

Commit 91bd4f5

Browse files
authored
[Backend Tester] Add adaptive maxpool tests (#13242)
Add tests for adaptive maxpooling operators. This is done in the context of #12898.
1 parent afa009a commit 91bd4f5

File tree

3 files changed

+395
-0
lines changed

3 files changed

+395
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
return_indices=False,
25+
):
26+
super().__init__()
27+
self.adaptive_maxpool = torch.nn.AdaptiveMaxPool1d(
28+
output_size=output_size,
29+
return_indices=return_indices,
30+
)
31+
32+
def forward(self, x):
33+
return self.adaptive_maxpool(x)
34+
35+
36+
@operator_test
37+
class AdaptiveMaxPool1d(OperatorTest):
38+
@dtype_test
39+
def test_adaptive_maxpool1d_dtype(self, flow: TestFlow, dtype) -> None:
40+
# Input shape: (batch_size, channels, length)
41+
self._test_op(
42+
Model().to(dtype),
43+
((torch.rand(1, 8, 100) * 10).to(dtype),),
44+
flow,
45+
)
46+
47+
def test_adaptive_maxpool1d_output_size(self, flow: TestFlow) -> None:
48+
# Test with different output sizes
49+
self._test_op(
50+
Model(output_size=1),
51+
(torch.randn(1, 8, 100),),
52+
flow,
53+
)
54+
self._test_op(
55+
Model(output_size=10),
56+
(torch.randn(1, 8, 100),),
57+
flow,
58+
)
59+
self._test_op(
60+
Model(output_size=50),
61+
(torch.randn(1, 8, 100),),
62+
flow,
63+
)
64+
65+
def test_adaptive_maxpool1d_return_indices(self, flow: TestFlow) -> None:
66+
# Test with return_indices=True
67+
class ModelWithIndices(torch.nn.Module):
68+
def __init__(self):
69+
super().__init__()
70+
self.adaptive_maxpool = torch.nn.AdaptiveMaxPool1d(
71+
output_size=5,
72+
return_indices=True,
73+
)
74+
75+
def forward(self, x):
76+
return self.adaptive_maxpool(x)
77+
78+
input_tensor = torch.randn(1, 8, 100)
79+
80+
self._test_op(
81+
ModelWithIndices(),
82+
(input_tensor,),
83+
flow,
84+
)
85+
86+
def test_adaptive_maxpool1d_batch_sizes(self, flow: TestFlow) -> None:
87+
# Test with batch inputs
88+
self._test_op(
89+
Model(),
90+
(torch.randn(2, 8, 100),),
91+
flow,
92+
)
93+
self._test_op(
94+
Model(),
95+
(torch.randn(8, 8, 100),),
96+
flow,
97+
)
98+
self._test_op(
99+
Model(),
100+
(torch.randn(16, 8, 100),),
101+
flow,
102+
)
103+
104+
def test_adaptive_maxpool1d_input_sizes(self, flow: TestFlow) -> None:
105+
# Test with different input sizes
106+
self._test_op(
107+
Model(),
108+
(torch.randn(1, 4, 100),),
109+
flow,
110+
)
111+
self._test_op(
112+
Model(),
113+
(torch.randn(1, 16, 100),),
114+
flow,
115+
)
116+
self._test_op(
117+
Model(),
118+
(torch.randn(1, 8, 50),),
119+
flow,
120+
)
121+
self._test_op(
122+
Model(),
123+
(torch.randn(1, 8, 200),),
124+
flow,
125+
)
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
return_indices=False,
25+
):
26+
super().__init__()
27+
self.adaptive_maxpool = torch.nn.AdaptiveMaxPool2d(
28+
output_size=output_size,
29+
return_indices=return_indices,
30+
)
31+
32+
def forward(self, x):
33+
return self.adaptive_maxpool(x)
34+
35+
36+
@operator_test
37+
class AdaptiveMaxPool2d(OperatorTest):
38+
@dtype_test
39+
def test_adaptive_maxpool2d_dtype(self, flow: TestFlow, dtype) -> None:
40+
# Input shape: (batch_size, channels, height, width)
41+
self._test_op(
42+
Model().to(dtype),
43+
((torch.rand(1, 8, 20, 20) * 10).to(dtype),),
44+
flow,
45+
)
46+
47+
def test_adaptive_maxpool2d_output_size(self, flow: TestFlow) -> None:
48+
# Test with different output sizes
49+
self._test_op(
50+
Model(output_size=1),
51+
(torch.randn(1, 8, 20, 20),),
52+
flow,
53+
)
54+
self._test_op(
55+
Model(output_size=(1, 1)),
56+
(torch.randn(1, 8, 20, 20),),
57+
flow,
58+
)
59+
self._test_op(
60+
Model(output_size=(10, 10)),
61+
(torch.randn(1, 8, 20, 20),),
62+
flow,
63+
)
64+
self._test_op(
65+
Model(output_size=(5, 10)),
66+
(torch.randn(1, 8, 20, 20),),
67+
flow,
68+
)
69+
70+
def test_adaptive_maxpool2d_return_indices(self, flow: TestFlow) -> None:
71+
# Test with return_indices=True
72+
class ModelWithIndices(torch.nn.Module):
73+
def __init__(self):
74+
super().__init__()
75+
self.adaptive_maxpool = torch.nn.AdaptiveMaxPool2d(
76+
output_size=(5, 5),
77+
return_indices=True,
78+
)
79+
80+
def forward(self, x):
81+
return self.adaptive_maxpool(x)
82+
83+
input_tensor = torch.randn(1, 8, 20, 20)
84+
85+
self._test_op(
86+
ModelWithIndices(),
87+
(input_tensor,),
88+
flow,
89+
)
90+
91+
def test_adaptive_maxpool2d_batch_sizes(self, flow: TestFlow) -> None:
92+
# Test with batch inputs
93+
self._test_op(
94+
Model(),
95+
(torch.randn(2, 8, 20, 20),),
96+
flow,
97+
)
98+
self._test_op(
99+
Model(),
100+
(torch.randn(8, 8, 20, 20),),
101+
flow,
102+
)
103+
self._test_op(
104+
Model(),
105+
(torch.randn(16, 8, 20, 20),),
106+
flow,
107+
)
108+
109+
def test_adaptive_maxpool2d_input_sizes(self, flow: TestFlow) -> None:
110+
# Test with different input sizes
111+
self._test_op(
112+
Model(),
113+
(torch.randn(1, 4, 20, 20),),
114+
flow,
115+
)
116+
self._test_op(
117+
Model(),
118+
(torch.randn(1, 16, 20, 20),),
119+
flow,
120+
)
121+
self._test_op(
122+
Model(),
123+
(torch.randn(1, 8, 10, 10),),
124+
flow,
125+
)
126+
self._test_op(
127+
Model(),
128+
(torch.randn(1, 8, 30, 30),),
129+
flow,
130+
)
131+
self._test_op(
132+
Model(),
133+
(torch.randn(1, 8, 15, 25),),
134+
flow,
135+
)
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
return_indices=False,
25+
):
26+
super().__init__()
27+
self.adaptive_maxpool = torch.nn.AdaptiveMaxPool3d(
28+
output_size=output_size,
29+
return_indices=return_indices,
30+
)
31+
32+
def forward(self, x):
33+
return self.adaptive_maxpool(x)
34+
35+
36+
@operator_test
37+
class AdaptiveMaxPool3d(OperatorTest):
38+
@dtype_test
39+
def test_adaptive_maxpool3d_dtype(self, flow: TestFlow, dtype) -> None:
40+
# Input shape: (batch_size, channels, depth, height, width)
41+
self._test_op(
42+
Model().to(dtype),
43+
((torch.rand(1, 4, 8, 8, 8) * 10).to(dtype),),
44+
flow,
45+
)
46+
47+
def test_adaptive_maxpool3d_output_size(self, flow: TestFlow) -> None:
48+
# Test with different output sizes
49+
self._test_op(
50+
Model(output_size=1),
51+
(torch.randn(1, 4, 8, 8, 8),),
52+
flow,
53+
)
54+
self._test_op(
55+
Model(output_size=(1, 1, 1)),
56+
(torch.randn(1, 4, 8, 8, 8),),
57+
flow,
58+
)
59+
self._test_op(
60+
Model(output_size=(6, 6, 6)),
61+
(torch.randn(1, 4, 8, 8, 8),),
62+
flow,
63+
)
64+
self._test_op(
65+
Model(output_size=(2, 4, 6)),
66+
(torch.randn(1, 4, 8, 8, 8),),
67+
flow,
68+
)
69+
70+
def test_adaptive_maxpool3d_return_indices(self, flow: TestFlow) -> None:
71+
# Test with return_indices=True
72+
class ModelWithIndices(torch.nn.Module):
73+
def __init__(self):
74+
super().__init__()
75+
self.adaptive_maxpool = torch.nn.AdaptiveMaxPool3d(
76+
output_size=(4, 4, 4),
77+
return_indices=True,
78+
)
79+
80+
def forward(self, x):
81+
return self.adaptive_maxpool(x)
82+
83+
input_tensor = torch.randn(1, 4, 8, 8, 8)
84+
85+
self._test_op(
86+
ModelWithIndices(),
87+
(input_tensor,),
88+
flow,
89+
)
90+
91+
def test_adaptive_maxpool3d_batch_sizes(self, flow: TestFlow) -> None:
92+
# Test with batch inputs
93+
self._test_op(
94+
Model(),
95+
(torch.randn(2, 4, 8, 8, 8),),
96+
flow,
97+
)
98+
self._test_op(
99+
Model(),
100+
(torch.randn(8, 4, 8, 8, 8),),
101+
flow,
102+
)
103+
self._test_op(
104+
Model(),
105+
(torch.randn(16, 4, 8, 8, 8),),
106+
flow,
107+
)
108+
109+
def test_adaptive_maxpool3d_input_sizes(self, flow: TestFlow) -> None:
110+
# Test with different input sizes
111+
self._test_op(
112+
Model(),
113+
(torch.randn(1, 2, 8, 8, 8),),
114+
flow,
115+
)
116+
self._test_op(
117+
Model(),
118+
(torch.randn(1, 8, 8, 8, 8),),
119+
flow,
120+
)
121+
self._test_op(
122+
Model(),
123+
(torch.randn(1, 4, 6, 6, 6),),
124+
flow,
125+
)
126+
self._test_op(
127+
Model(),
128+
(torch.randn(1, 4, 10, 10, 10),),
129+
flow,
130+
)
131+
self._test_op(
132+
Model(),
133+
(torch.randn(1, 4, 7, 9, 11),),
134+
flow,
135+
)

0 commit comments

Comments
 (0)