|
13 | 13 |
|
14 | 14 | from coreai_opt._utils.fx_utils import normalize_module_fqn |
15 | 15 | from coreai_opt._utils.torch_utils import ( |
| 16 | + ATEN_OP_TO_MODULE_TYPE, |
16 | 17 | mmap_module_state_dict, |
17 | 18 | move_model_to_eval, |
18 | 19 | move_model_to_train, |
19 | 20 | normalize_axis, |
20 | 21 | ) |
| 22 | +from coreai_opt._utils.version_utils import version_ge |
21 | 23 |
|
22 | 24 |
|
23 | 25 | class TestMoveModelContextManagers: |
@@ -163,3 +165,63 @@ def test_raises_on_non_cpu_tensor(tmp_path): |
163 | 165 |
|
164 | 166 | with pytest.raises(ValueError, match="requires CPU tensors"): |
165 | 167 | mmap_module_state_dict(model, tmp_path / "model.safetensors") |
| 168 | + |
| 169 | + |
| 170 | +class TestAtenOpToModuleType: |
| 171 | + """Tests for ATEN_OP_TO_MODULE_TYPE overload correctness across torch versions. |
| 172 | +
|
| 173 | + Regression test for: conv_transpose2d/3d were mapped to the `.input` overload |
| 174 | + which was deprecated in PyTorch 2.9. The mapping must use `.default` as the |
| 175 | + primary key while retaining the `.input` alias for backward compat on < 2.9. |
| 176 | + """ |
| 177 | + |
| 178 | + @staticmethod |
| 179 | + def test_conv_transpose2d_default_overload_present(): |
| 180 | + """conv_transpose2d.default must always be in the mapping (torch >= 2.9 primary path).""" |
| 181 | + assert torch.ops.aten.conv_transpose2d.default in ATEN_OP_TO_MODULE_TYPE |
| 182 | + assert ATEN_OP_TO_MODULE_TYPE[torch.ops.aten.conv_transpose2d.default] is nn.ConvTranspose2d |
| 183 | + |
| 184 | + @staticmethod |
| 185 | + def test_conv_transpose3d_default_overload_present(): |
| 186 | + """conv_transpose3d.default must always be in the mapping (torch >= 2.9 primary path).""" |
| 187 | + assert torch.ops.aten.conv_transpose3d.default in ATEN_OP_TO_MODULE_TYPE |
| 188 | + assert ATEN_OP_TO_MODULE_TYPE[torch.ops.aten.conv_transpose3d.default] is nn.ConvTranspose3d |
| 189 | + |
| 190 | + @staticmethod |
| 191 | + def test_conv_transpose2d_input_overload_compat(): |
| 192 | + """conv_transpose2d.input alias is present on torch < 2.9 for backward compat. |
| 193 | +
|
| 194 | + On torch >= 2.9 the `.input` overload may not exist at all; the test is |
| 195 | + skipped in that case since the `.default` path fully covers those versions. |
| 196 | + """ |
| 197 | + if not version_ge(torch, "2.9"): |
| 198 | + # On < 2.9 the .input overload exists and must also resolve to ConvTranspose2d |
| 199 | + assert torch.ops.aten.conv_transpose2d.input in ATEN_OP_TO_MODULE_TYPE |
| 200 | + assert ATEN_OP_TO_MODULE_TYPE[torch.ops.aten.conv_transpose2d.input] is nn.ConvTranspose2d |
| 201 | + else: |
| 202 | + # On >= 2.9 the .input overload may be absent; that's expected and fine |
| 203 | + pytest.skip("conv_transpose2d.input overload not expected on torch >= 2.9") |
| 204 | + |
| 205 | + @staticmethod |
| 206 | + def test_conv_transpose3d_input_overload_compat(): |
| 207 | + """conv_transpose3d.input alias is present on torch < 2.9 for backward compat.""" |
| 208 | + if not version_ge(torch, "2.9"): |
| 209 | + assert torch.ops.aten.conv_transpose3d.input in ATEN_OP_TO_MODULE_TYPE |
| 210 | + assert ATEN_OP_TO_MODULE_TYPE[torch.ops.aten.conv_transpose3d.input] is nn.ConvTranspose3d |
| 211 | + else: |
| 212 | + pytest.skip("conv_transpose3d.input overload not expected on torch >= 2.9") |
| 213 | + |
| 214 | + @staticmethod |
| 215 | + def test_all_other_ops_unaffected(): |
| 216 | + """Sanity-check that unrelated ops in the mapping were not disturbed.""" |
| 217 | + expected = { |
| 218 | + torch.ops.aten.conv1d.default: nn.Conv1d, |
| 219 | + torch.ops.aten.conv2d.default: nn.Conv2d, |
| 220 | + torch.ops.aten.conv3d.default: nn.Conv3d, |
| 221 | + torch.ops.aten.conv_transpose1d.default: nn.ConvTranspose1d, |
| 222 | + torch.ops.aten.linear.default: nn.Linear, |
| 223 | + torch.ops.aten.embedding.default: nn.Embedding, |
| 224 | + } |
| 225 | + for op, module_type in expected.items(): |
| 226 | + assert op in ATEN_OP_TO_MODULE_TYPE |
| 227 | + assert ATEN_OP_TO_MODULE_TYPE[op] is module_type |
0 commit comments