-
Notifications
You must be signed in to change notification settings - Fork 824
Support PyTorch 2.9 #2743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Support PyTorch 2.9 #2743
Changes from all commits
2e60ab6
8570a19
ecbfcf5
52da149
466c124
7ae3e38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| from coremltools._deps import ( | ||
| _HAS_HF, | ||
| _HAS_TORCH, | ||
| _HAS_TORCH_EXPORT_API, | ||
| _HAS_TORCHAO, | ||
| MSG_TORCH_NOT_FOUND, | ||
| MSG_TORCHAO_NOT_FOUND, | ||
|
|
@@ -571,6 +572,41 @@ def forward(self, x): | |
| ) | ||
| assert isinstance(model._get_mil_internal(), ct.converters.mil.Program) | ||
|
|
||
| @staticmethod | ||
| @pytest.mark.skipif(not _HAS_TORCH_EXPORT_API, reason="torch.export API not available.") | ||
| def test_convert_exported_program_training_dialect(): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't properly test this, in CI, until we update the version of PyTorch that it uses.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bumped the CI torch pin to 2.9.0 (and |
||
| # Starting with torch 2.9, torch.export.export() returns an | ||
| # ExportedProgram in the new "TRAINING" IR dialect by default. convert() | ||
| # should lower it automatically via run_decompositions instead of | ||
| # requiring the user to call run_decompositions() themselves. | ||
| class Network(torch.nn.Module): | ||
| def __init__(self): | ||
| super().__init__() | ||
| self.hidden = torch.nn.Linear(8, 4) | ||
| self.relu = torch.nn.ReLU() | ||
|
|
||
| def forward(self, x): | ||
| return self.relu(self.hidden(x)) | ||
|
|
||
| torch_model = Network().eval() | ||
| example_input = torch.rand(2, 8) | ||
| exported_program = torch.export.export(torch_model, (example_input,)) | ||
|
|
||
| # With torch >= 2.9 the default export dialect is no longer ATEN, so the | ||
| # installed torch here actually drives convert()'s auto-lowering path. | ||
| # Assert it to guarantee this test exercises that path instead of | ||
| # silently passing as a no-op on a torch whose default is still ATEN. | ||
| if Version(torch.__version__) >= Version("2.9.0"): | ||
| assert exported_program.dialect not in ("ATEN", "EDGE") | ||
|
|
||
| # Convert directly, without a manual run_decompositions() step. | ||
| mlmodel = ct.convert( | ||
| exported_program, | ||
| inputs=[ct.TensorType(name="x", shape=example_input.shape)], | ||
| convert_to="mlprogram", | ||
| ) | ||
| assert isinstance(mlmodel._get_mil_internal(), ct.converters.mil.Program) | ||
|
|
||
| @staticmethod | ||
| @pytest.mark.skipif(ct.utils._macos_version() < (12, 0), reason='Model produces specification 6.') | ||
| @pytest.mark.parametrize( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't we also want to test the version of PyTorch installed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call — added
assert exported_program.dialect not in ("ATEN", "EDGE")(guarded on torch >= 2.9) so the test provably drives this path on the installed torch instead of passing as a no-op.