|
| 1 | +import torch |
| 2 | +from .harness import DispatchTestCase |
| 3 | +from parameterized import parameterized |
| 4 | +from torch.testing._internal.common_utils import run_tests |
| 5 | +from torch_tensorrt import Input |
| 6 | +from torch_tensorrt.dynamo.conversion import UnsupportedOperatorException |
| 7 | + |
| 8 | + |
| 9 | +# FIXME: check about implicit and explicit batch |
| 10 | +class TestSplitConverterNoDim(DispatchTestCase): |
| 11 | + @parameterized.expand( |
| 12 | + [ |
| 13 | + ("split_size_or_sections_no_dim", 2), |
| 14 | + ] |
| 15 | + ) |
| 16 | + def test_split(self, _, split_size_or_tensor): |
| 17 | + class TestModule(torch.nn.Module): |
| 18 | + def __init__(self): |
| 19 | + super().__init__() |
| 20 | + |
| 21 | + def forward(self, input): |
| 22 | + out = torch.split(input, split_size_or_tensor) |
| 23 | + return out |
| 24 | + |
| 25 | + input = [torch.randn(10).reshape(5, 2)] |
| 26 | + self.run_test( |
| 27 | + TestModule(), |
| 28 | + input, |
| 29 | + expected_ops={torch.ops.aten.split.Tensor}, |
| 30 | + disable_passes=True, |
| 31 | + ) |
| 32 | + |
| 33 | + @parameterized.expand( |
| 34 | + [ |
| 35 | + ("split_size_or_sections_list_no_dim_list", [1, 4]), |
| 36 | + ] |
| 37 | + ) |
| 38 | + def test_split_list(self, _, split_size_or_tensor): |
| 39 | + class TestModule(torch.nn.Module): |
| 40 | + def __init__(self): |
| 41 | + super().__init__() |
| 42 | + |
| 43 | + def forward(self, input): |
| 44 | + out = torch.split(input, split_size_or_tensor) |
| 45 | + return out |
| 46 | + |
| 47 | + input = [torch.randn(10).reshape(5, 2)] |
| 48 | + self.run_test( |
| 49 | + TestModule(), |
| 50 | + input, |
| 51 | + expected_ops={torch.ops.aten.split_with_sizes.default}, |
| 52 | + disable_passes=True, |
| 53 | + ) |
| 54 | + |
| 55 | + @parameterized.expand( |
| 56 | + [ |
| 57 | + ("split_size_or_sections_dims", 2, 1), |
| 58 | + ] |
| 59 | + ) |
| 60 | + def test_split(self, _, split_size_or_tensor, dim): |
| 61 | + class TestModule(torch.nn.Module): |
| 62 | + def __init__(self): |
| 63 | + super().__init__() |
| 64 | + |
| 65 | + def forward(self, input): |
| 66 | + out = torch.split(input, split_size_or_tensor, dim) |
| 67 | + return out |
| 68 | + |
| 69 | + input = [torch.randn(10).reshape(5, 2)] |
| 70 | + self.run_test( |
| 71 | + TestModule(), |
| 72 | + input, |
| 73 | + expected_ops={torch.ops.aten.split.Tensor}, |
| 74 | + disable_passes=True, |
| 75 | + ) |
| 76 | + |
| 77 | + @parameterized.expand( |
| 78 | + [ |
| 79 | + ("split_size_or_sections_list_dims", [1, 1], 1), |
| 80 | + ] |
| 81 | + ) |
| 82 | + def test_split_dim_list(self, _, split_size_or_tensor, dim): |
| 83 | + class TestModule(torch.nn.Module): |
| 84 | + def __init__(self): |
| 85 | + super().__init__() |
| 86 | + |
| 87 | + def forward(self, input): |
| 88 | + out = torch.split(input, split_size_or_tensor, dim) |
| 89 | + return out |
| 90 | + |
| 91 | + input = [torch.randn(10).reshape(5, 2)] |
| 92 | + self.run_test( |
| 93 | + TestModule(), |
| 94 | + input, |
| 95 | + expected_ops={torch.ops.aten.split_with_sizes.default}, |
| 96 | + disable_passes=True, |
| 97 | + ) |
| 98 | + |
| 99 | + @parameterized.expand( |
| 100 | + [ |
| 101 | + ("split_size_or_sections_list_dims_not_full_list", [1, 1], 1), |
| 102 | + ] |
| 103 | + ) |
| 104 | + def test_split_dim_list(self, _, split_size_or_tensor, dim): |
| 105 | + class TestModule(torch.nn.Module): |
| 106 | + def __init__(self): |
| 107 | + super().__init__() |
| 108 | + |
| 109 | + def forward(self, input): |
| 110 | + out = torch.split(input, split_size_or_tensor, dim) |
| 111 | + return out |
| 112 | + |
| 113 | + input = [torch.randn(15).reshape(5, 3)] |
| 114 | + with self.assertRaises(RuntimeError): |
| 115 | + self.run_test( |
| 116 | + TestModule(), |
| 117 | + input, |
| 118 | + expected_ops={torch.ops.aten.split_with_sizes.default}, |
| 119 | + disable_passes=True, |
| 120 | + ) |
| 121 | + |
| 122 | + @parameterized.expand( |
| 123 | + [ |
| 124 | + ("select_split_size_or_sections_dim_dynamic_shape", 2, 1), |
| 125 | + ] |
| 126 | + ) |
| 127 | + def test_split_dynamic(self, _, split_size_or_tensor, dim): |
| 128 | + class TestModule(torch.nn.Module): |
| 129 | + def __init__(self): |
| 130 | + super().__init__() |
| 131 | + |
| 132 | + def forward(self, input): |
| 133 | + out = torch.split(input, split_size_or_tensor, dim) |
| 134 | + return out |
| 135 | + |
| 136 | + input_specs = [ |
| 137 | + Input( |
| 138 | + shape=(1, 10, -1), |
| 139 | + dtype=torch.float32, |
| 140 | + shape_ranges=[((1, 10, 1), (1, 10, 10), (1, 10, 10))], |
| 141 | + ), |
| 142 | + ] |
| 143 | + self.run_test_with_dynamic_shape( |
| 144 | + TestModule(), |
| 145 | + input_specs, |
| 146 | + expected_ops={torch.ops.aten.split.Tensor}, |
| 147 | + disable_passes=True, |
| 148 | + ) |
| 149 | + |
| 150 | + @parameterized.expand( |
| 151 | + [ |
| 152 | + ("select_chunk_dim", 6, 0), |
| 153 | + ] |
| 154 | + ) |
| 155 | + def test_split_dynamic(self, _, chunk, dim): |
| 156 | + class TestModule(torch.nn.Module): |
| 157 | + def __init__(self): |
| 158 | + super().__init__() |
| 159 | + |
| 160 | + def forward(self, input): |
| 161 | + out = torch.ops.aten.chunk(input, chunk, dim) |
| 162 | + return out |
| 163 | + |
| 164 | + input = [torch.randn(11)] |
| 165 | + with self.assertRaises(UnsupportedOperatorException): |
| 166 | + self.run_test( |
| 167 | + TestModule(), |
| 168 | + input, |
| 169 | + expected_ops={torch.ops.aten.split.Tensor}, |
| 170 | + disable_passes=True, |
| 171 | + ) |
| 172 | + |
| 173 | + |
| 174 | +if __name__ == "__main__": |
| 175 | + run_tests() |
0 commit comments