Skip to content

Commit 664c1ea

Browse files
committed
feat: add torchao/torch compatibility check helper
torchao 0.18.0 removed its torch < 2.11 code paths and skips loading its compiled extensions on older torch (see https://github.com/pytorch/ao/releases/tag/v0.18.0). Add a pure query function describing when an installed torchao/torch pair is unsupported, plus type hints on the existing version_ge.
1 parent b28edb5 commit 664c1ea

2 files changed

Lines changed: 76 additions & 1 deletion

File tree

src/coreai_opt/_utils/version_utils.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,37 @@
33
# Use of this source code is governed by a BSD-3-Clause license that can
44
# be found in the LICENSE file or at https://opensource.org/licenses/BSD-3-Clause
55

6+
from types import ModuleType
7+
68
from packaging import version
79

810

9-
def version_ge(module, target_version):
11+
def version_ge(module: ModuleType, target_version: str) -> bool:
1012
return version.parse(module.__version__) >= version.parse(target_version)
13+
14+
15+
_MIN_TORCHAO_REQUIRING_TORCH_2_11 = "0.18.0"
16+
_MIN_TORCH_FOR_NEW_TORCHAO = "2.11.0.dev0"
17+
_TORCHAO_RELEASE_NOTES_URL = "https://github.com/pytorch/ao/releases/tag/v0.18.0"
18+
19+
20+
def torchao_torch_incompatibility(torchao_version: str, torch_version: str) -> str | None:
21+
"""Describe why the installed torchao and torch versions are incompatible.
22+
23+
Args:
24+
torchao_version: The installed torchao version.
25+
torch_version: The installed torch version.
26+
27+
Returns:
28+
A message explaining the incompatibility, or ``None`` if the pair is supported.
29+
"""
30+
if version.parse(torchao_version) < version.parse(_MIN_TORCHAO_REQUIRING_TORCH_2_11):
31+
return None
32+
if version.parse(torch_version) >= version.parse(_MIN_TORCH_FOR_NEW_TORCHAO):
33+
return None
34+
return (
35+
f"torchao {torchao_version} does not support torch<2.11 "
36+
f"(found torch {torch_version}). See the torchao "
37+
f"{_MIN_TORCHAO_REQUIRING_TORCH_2_11} release notes for more information: "
38+
f"{_TORCHAO_RELEASE_NOTES_URL}"
39+
)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2026 Apple Inc.
2+
#
3+
# Use of this source code is governed by a BSD-3-Clause license that can
4+
# be found in the LICENSE file or at https://opensource.org/licenses/BSD-3-Clause
5+
6+
import pytest
7+
8+
from coreai_opt._utils.version_utils import torchao_torch_incompatibility
9+
10+
INCOMPATIBLE = [
11+
("0.18.0", "2.8.0"),
12+
("0.18.0", "2.9.1"),
13+
("0.18.0", "2.10.0"),
14+
("0.18.0", "2.10.0+cu128"),
15+
("0.19.0", "2.10.0"),
16+
# A source-built torchao reports a local version, which sorts above the base.
17+
("0.18.0+gitabc1234", "2.10.0"),
18+
]
19+
20+
COMPATIBLE = [
21+
# torch is new enough.
22+
("0.18.0", "2.11.0"),
23+
("0.18.0", "2.11.0+cu128"),
24+
("0.18.0", "2.12.0.dev20260805+cu128"),
25+
# A 2.11 pre-release counts as 2.11.
26+
("0.18.0", "2.11.0rc1"),
27+
# torchao still supports older torch.
28+
("0.17.0", "2.8.0"),
29+
("0.16.0", "2.10.0"),
30+
("0.15.0", "2.8.0"),
31+
]
32+
33+
34+
@pytest.mark.parametrize(("torchao_version", "torch_version"), INCOMPATIBLE)
35+
def test_returns_message_for_incompatible_pair(torchao_version, torch_version):
36+
message = torchao_torch_incompatibility(torchao_version, torch_version)
37+
38+
assert message is not None
39+
assert torchao_version in message
40+
assert torch_version in message
41+
assert "https://github.com/pytorch/ao/releases/tag/v0.18.0" in message
42+
43+
44+
@pytest.mark.parametrize(("torchao_version", "torch_version"), COMPATIBLE)
45+
def test_returns_none_for_compatible_pair(torchao_version, torch_version):
46+
assert torchao_torch_incompatibility(torchao_version, torch_version) is None

0 commit comments

Comments
 (0)