|
1 | | -# (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary. |
| 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. |
2 | 6 |
|
3 | | -# pyre-strict |
| 7 | +# pyre-unsafe |
4 | 8 |
|
5 | | -from typing import Callable, List |
6 | 9 |
|
7 | 10 | import torch |
| 11 | +from executorch.backends.test.suite.flow import TestFlow |
8 | 12 |
|
9 | | -from executorch.backends.test.compliance_suite import ( |
| 13 | +from executorch.backends.test.suite.operators import ( |
10 | 14 | dtype_test, |
11 | 15 | operator_test, |
12 | 16 | OperatorTest, |
13 | 17 | ) |
14 | 18 |
|
| 19 | + |
15 | 20 | class CatModel(torch.nn.Module): |
16 | 21 | def __init__(self, dim: int = 0): |
17 | 22 | super().__init__() |
18 | 23 | self.dim = dim |
19 | | - |
| 24 | + |
20 | 25 | def forward(self, x1, x2, x3): |
21 | 26 | return torch.cat([x1, x2, x3], dim=self.dim) |
22 | 27 |
|
| 28 | + |
23 | 29 | @operator_test |
24 | | -class TestCat(OperatorTest): |
| 30 | +class Cat(OperatorTest): |
25 | 31 | @dtype_test |
26 | | - def test_cat_dtype(self, dtype, tester_factory: Callable) -> None: |
27 | | - # Test with different dtypes |
28 | | - model = CatModel() |
| 32 | + def test_cat_dtype(self, flow: TestFlow, dtype) -> None: |
29 | 33 | self._test_op( |
30 | | - model, |
| 34 | + CatModel(), |
31 | 35 | ( |
32 | 36 | torch.rand(2, 3).to(dtype), |
33 | 37 | torch.rand(3, 3).to(dtype), |
34 | 38 | torch.rand(4, 3).to(dtype), |
35 | | - ), |
36 | | - tester_factory |
| 39 | + ), |
| 40 | + flow, |
37 | 41 | ) |
38 | | - |
39 | | - def test_cat_basic(self, tester_factory: Callable) -> None: |
40 | | - # Basic test with default parameters |
41 | | - # Concatenate 3 tensors along dimension 0 |
42 | | - # Tensors of shapes [2, 3], [3, 3], [4, 3] -> Result will be of shape [9, 3] |
| 42 | + |
| 43 | + def test_cat_basic(self, flow: TestFlow) -> None: |
43 | 44 | self._test_op( |
44 | | - CatModel(), |
| 45 | + CatModel(), |
45 | 46 | ( |
46 | 47 | torch.randn(2, 3), |
47 | 48 | torch.randn(3, 3), |
48 | 49 | torch.randn(4, 3), |
49 | | - ), |
50 | | - tester_factory |
| 50 | + ), |
| 51 | + flow, |
51 | 52 | ) |
52 | | - |
53 | | - def test_cat_dimensions(self, tester_factory: Callable) -> None: |
54 | | - # Test concatenating along different dimensions |
55 | | - |
56 | | - # Concatenate along dimension 0 (default) |
57 | | - # Tensors of shapes [2, 3], [3, 3], [4, 3] -> Result will be of shape [9, 3] |
| 53 | + |
| 54 | + def test_cat_dimensions(self, flow: TestFlow) -> None: |
58 | 55 | self._test_op( |
59 | | - CatModel(dim=0), |
| 56 | + CatModel(dim=0), |
60 | 57 | ( |
61 | 58 | torch.randn(2, 3), |
62 | 59 | torch.randn(3, 3), |
63 | 60 | torch.randn(4, 3), |
64 | | - ), |
65 | | - tester_factory |
| 61 | + ), |
| 62 | + flow, |
66 | 63 | ) |
67 | | - |
68 | | - # Concatenate along dimension 1 |
69 | | - # Tensors of shapes [3, 2], [3, 3], [3, 4] -> Result will be of shape [3, 9] |
| 64 | + |
70 | 65 | self._test_op( |
71 | | - CatModel(dim=1), |
| 66 | + CatModel(dim=1), |
72 | 67 | ( |
73 | 68 | torch.randn(3, 2), |
74 | 69 | torch.randn(3, 3), |
75 | 70 | torch.randn(3, 4), |
76 | | - ), |
77 | | - tester_factory |
| 71 | + ), |
| 72 | + flow, |
78 | 73 | ) |
79 | | - |
80 | | - # Concatenate along dimension 2 |
81 | | - # Tensors of shapes [2, 3, 1], [2, 3, 2], [2, 3, 3] -> Result will be of shape [2, 3, 6] |
| 74 | + |
82 | 75 | self._test_op( |
83 | | - CatModel(dim=2), |
| 76 | + CatModel(dim=2), |
84 | 77 | ( |
85 | 78 | torch.randn(2, 3, 1), |
86 | 79 | torch.randn(2, 3, 2), |
87 | 80 | torch.randn(2, 3, 3), |
88 | | - ), |
89 | | - tester_factory |
| 81 | + ), |
| 82 | + flow, |
90 | 83 | ) |
91 | | - |
92 | | - def test_cat_negative_dim(self, tester_factory: Callable) -> None: |
93 | | - # Test with negative dimensions (counting from the end) |
94 | | - |
95 | | - # Concatenate along the last dimension (dim=-1) |
96 | | - # For tensors of shape [3, 2], [3, 3], [3, 4], this is equivalent to dim=1 |
97 | | - # Result will be of shape [3, 9] |
| 84 | + |
| 85 | + def test_cat_negative_dim(self, flow: TestFlow) -> None: |
98 | 86 | self._test_op( |
99 | | - CatModel(dim=-1), |
| 87 | + CatModel(dim=-1), |
100 | 88 | ( |
101 | 89 | torch.randn(3, 2), |
102 | 90 | torch.randn(3, 3), |
103 | 91 | torch.randn(3, 4), |
104 | | - ), |
105 | | - tester_factory |
| 92 | + ), |
| 93 | + flow, |
106 | 94 | ) |
107 | | - |
108 | | - # Concatenate along the second-to-last dimension (dim=-2) |
109 | | - # For tensors of shape [2, 3], [3, 3], [4, 3], this is equivalent to dim=0 |
110 | | - # Result will be of shape [9, 3] |
| 95 | + |
111 | 96 | self._test_op( |
112 | | - CatModel(dim=-2), |
| 97 | + CatModel(dim=-2), |
113 | 98 | ( |
114 | 99 | torch.randn(2, 3), |
115 | 100 | torch.randn(3, 3), |
116 | 101 | torch.randn(4, 3), |
117 | | - ), |
118 | | - tester_factory |
| 102 | + ), |
| 103 | + flow, |
119 | 104 | ) |
120 | | - |
121 | | - def test_cat_different_shapes(self, tester_factory: Callable) -> None: |
122 | | - # Test with tensors of different shapes |
123 | | - |
124 | | - # Concatenate 1D tensors |
125 | | - # Tensors of shapes [2], [3], [4] -> Result will be of shape [9] |
| 105 | + |
| 106 | + def test_cat_different_shapes(self, flow: TestFlow) -> None: |
126 | 107 | self._test_op( |
127 | | - CatModel(), |
| 108 | + CatModel(), |
128 | 109 | ( |
129 | 110 | torch.randn(2), |
130 | 111 | torch.randn(3), |
131 | 112 | torch.randn(4), |
132 | | - ), |
133 | | - tester_factory |
| 113 | + ), |
| 114 | + flow, |
134 | 115 | ) |
135 | | - |
136 | | - # Concatenate 3D tensors along dimension 0 |
137 | | - # Tensors of shapes [1, 3, 4], [2, 3, 4], [3, 3, 4] -> Result will be of shape [6, 3, 4] |
| 116 | + |
138 | 117 | self._test_op( |
139 | | - CatModel(dim=0), |
| 118 | + CatModel(dim=0), |
140 | 119 | ( |
141 | 120 | torch.randn(1, 3, 4), |
142 | 121 | torch.randn(2, 3, 4), |
143 | 122 | torch.randn(3, 3, 4), |
144 | | - ), |
145 | | - tester_factory |
| 123 | + ), |
| 124 | + flow, |
146 | 125 | ) |
147 | | - |
148 | | - # Concatenate 3D tensors along dimension 1 |
149 | | - # Tensors of shapes [2, 1, 4], [2, 2, 4], [2, 3, 4] -> Result will be of shape [2, 6, 4] |
| 126 | + |
150 | 127 | self._test_op( |
151 | | - CatModel(dim=1), |
| 128 | + CatModel(dim=1), |
152 | 129 | ( |
153 | 130 | torch.randn(2, 1, 4), |
154 | 131 | torch.randn(2, 2, 4), |
155 | 132 | torch.randn(2, 3, 4), |
156 | | - ), |
157 | | - tester_factory |
| 133 | + ), |
| 134 | + flow, |
158 | 135 | ) |
159 | | - |
160 | | - # Concatenate 3D tensors along dimension 2 |
161 | | - # Tensors of shapes [2, 3, 1], [2, 3, 2], [2, 3, 3] -> Result will be of shape [2, 3, 6] |
| 136 | + |
162 | 137 | self._test_op( |
163 | | - CatModel(dim=2), |
| 138 | + CatModel(dim=2), |
164 | 139 | ( |
165 | 140 | torch.randn(2, 3, 1), |
166 | 141 | torch.randn(2, 3, 2), |
167 | 142 | torch.randn(2, 3, 3), |
168 | | - ), |
169 | | - tester_factory |
| 143 | + ), |
| 144 | + flow, |
170 | 145 | ) |
171 | | - |
172 | | - def test_cat_same_shapes(self, tester_factory: Callable) -> None: |
173 | | - # Test with tensors of the same shape |
174 | | - # Tensors of shapes [2, 3], [2, 3], [2, 3] -> Result will be of shape [6, 3] |
| 146 | + |
| 147 | + def test_cat_same_shapes(self, flow: TestFlow) -> None: |
175 | 148 | self._test_op( |
176 | | - CatModel(), |
| 149 | + CatModel(), |
177 | 150 | ( |
178 | 151 | torch.randn(2, 3), |
179 | 152 | torch.randn(2, 3), |
180 | 153 | torch.randn(2, 3), |
181 | | - ), |
182 | | - tester_factory |
| 154 | + ), |
| 155 | + flow, |
183 | 156 | ) |
0 commit comments