|
| 1 | +# (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary. |
| 2 | + |
| 3 | +# pyre-strict |
| 4 | + |
| 5 | +from typing import Callable, List |
| 6 | + |
| 7 | +import torch |
| 8 | + |
| 9 | +from executorch.backends.test.compliance_suite import ( |
| 10 | + dtype_test, |
| 11 | + operator_test, |
| 12 | + OperatorTest, |
| 13 | +) |
| 14 | + |
| 15 | +class CatModel(torch.nn.Module): |
| 16 | + def __init__(self, dim: int = 0): |
| 17 | + super().__init__() |
| 18 | + self.dim = dim |
| 19 | + |
| 20 | + def forward(self, x1, x2, x3): |
| 21 | + return torch.cat([x1, x2, x3], dim=self.dim) |
| 22 | + |
| 23 | +@operator_test |
| 24 | +class TestCat(OperatorTest): |
| 25 | + @dtype_test |
| 26 | + def test_cat_dtype(self, dtype, tester_factory: Callable) -> None: |
| 27 | + # Test with different dtypes |
| 28 | + model = CatModel() |
| 29 | + self._test_op( |
| 30 | + model, |
| 31 | + ( |
| 32 | + torch.rand(2, 3).to(dtype), |
| 33 | + torch.rand(3, 3).to(dtype), |
| 34 | + torch.rand(4, 3).to(dtype), |
| 35 | + ), |
| 36 | + tester_factory |
| 37 | + ) |
| 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] |
| 43 | + self._test_op( |
| 44 | + CatModel(), |
| 45 | + ( |
| 46 | + torch.randn(2, 3), |
| 47 | + torch.randn(3, 3), |
| 48 | + torch.randn(4, 3), |
| 49 | + ), |
| 50 | + tester_factory |
| 51 | + ) |
| 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] |
| 58 | + self._test_op( |
| 59 | + CatModel(dim=0), |
| 60 | + ( |
| 61 | + torch.randn(2, 3), |
| 62 | + torch.randn(3, 3), |
| 63 | + torch.randn(4, 3), |
| 64 | + ), |
| 65 | + tester_factory |
| 66 | + ) |
| 67 | + |
| 68 | + # Concatenate along dimension 1 |
| 69 | + # Tensors of shapes [3, 2], [3, 3], [3, 4] -> Result will be of shape [3, 9] |
| 70 | + self._test_op( |
| 71 | + CatModel(dim=1), |
| 72 | + ( |
| 73 | + torch.randn(3, 2), |
| 74 | + torch.randn(3, 3), |
| 75 | + torch.randn(3, 4), |
| 76 | + ), |
| 77 | + tester_factory |
| 78 | + ) |
| 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] |
| 82 | + self._test_op( |
| 83 | + CatModel(dim=2), |
| 84 | + ( |
| 85 | + torch.randn(2, 3, 1), |
| 86 | + torch.randn(2, 3, 2), |
| 87 | + torch.randn(2, 3, 3), |
| 88 | + ), |
| 89 | + tester_factory |
| 90 | + ) |
| 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] |
| 98 | + self._test_op( |
| 99 | + CatModel(dim=-1), |
| 100 | + ( |
| 101 | + torch.randn(3, 2), |
| 102 | + torch.randn(3, 3), |
| 103 | + torch.randn(3, 4), |
| 104 | + ), |
| 105 | + tester_factory |
| 106 | + ) |
| 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] |
| 111 | + self._test_op( |
| 112 | + CatModel(dim=-2), |
| 113 | + ( |
| 114 | + torch.randn(2, 3), |
| 115 | + torch.randn(3, 3), |
| 116 | + torch.randn(4, 3), |
| 117 | + ), |
| 118 | + tester_factory |
| 119 | + ) |
| 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] |
| 126 | + self._test_op( |
| 127 | + CatModel(), |
| 128 | + ( |
| 129 | + torch.randn(2), |
| 130 | + torch.randn(3), |
| 131 | + torch.randn(4), |
| 132 | + ), |
| 133 | + tester_factory |
| 134 | + ) |
| 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] |
| 138 | + self._test_op( |
| 139 | + CatModel(dim=0), |
| 140 | + ( |
| 141 | + torch.randn(1, 3, 4), |
| 142 | + torch.randn(2, 3, 4), |
| 143 | + torch.randn(3, 3, 4), |
| 144 | + ), |
| 145 | + tester_factory |
| 146 | + ) |
| 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] |
| 150 | + self._test_op( |
| 151 | + CatModel(dim=1), |
| 152 | + ( |
| 153 | + torch.randn(2, 1, 4), |
| 154 | + torch.randn(2, 2, 4), |
| 155 | + torch.randn(2, 3, 4), |
| 156 | + ), |
| 157 | + tester_factory |
| 158 | + ) |
| 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] |
| 162 | + self._test_op( |
| 163 | + CatModel(dim=2), |
| 164 | + ( |
| 165 | + torch.randn(2, 3, 1), |
| 166 | + torch.randn(2, 3, 2), |
| 167 | + torch.randn(2, 3, 3), |
| 168 | + ), |
| 169 | + tester_factory |
| 170 | + ) |
| 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] |
| 175 | + self._test_op( |
| 176 | + CatModel(), |
| 177 | + ( |
| 178 | + torch.randn(2, 3), |
| 179 | + torch.randn(2, 3), |
| 180 | + torch.randn(2, 3), |
| 181 | + ), |
| 182 | + tester_factory |
| 183 | + ) |
0 commit comments