Skip to content

fix incorrect torch version test #2786

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
class TestTorchVersion(unittest.TestCase):
def test_torch_version_at_least(self):
test_cases = [
("2.5.0a0+git9f17037", "2.5.0", True),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should keep the full version string in the test case

("2.5.0a0+git9f17037", "2.4.0", True),
("2.5.0.dev20240708+cu121", "2.5.0", True),
("2.5.0.dev20240708+cu121", "2.4.0", True),
("2.5.0", "2.4.0", True),
("2.5.0", "2.5.0", True),
("2.4.0", "2.4.0", True),
("2.4.0", "2.5.0", False),
("2.5.0a0+git9f17037", "2.5.0", False), # [2, 5, -1] < [2, 5, 0]
("2.5.0a0+git9f17037", "2.4.0", True), # [2, 5, -1] > [2, 4, 0]
("2.5.0.dev20240708+cu121", "2.5.0", False), # [2, 5, -1] < [2, 5, 0]
("2.5.0.dev20240708+cu121", "2.4.0", True), # [2, 5, -1] > [2, 4, 0]
("2.5.0", "2.4.0", True), # [2, 5, 0] > [2, 4, 0]
("2.5.0", "2.5.0", True), # [2, 5, 0] >= [2, 5, 0]
("2.4.0", "2.4.0", True), # [2, 4, 0] >= [2, 4, 0]
("2.4.0", "2.5.0", False), # [2, 4, 0] < [2, 5, 0]
]

for torch_version, compare_version, expected_result in test_cases:
Expand Down
32 changes: 19 additions & 13 deletions torchao/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,27 +348,33 @@ def _is_float8_type(dtype: torch.dtype) -> bool:


def parse_version(version_string):
# Extract just the X.Y.Z part from the version string
match = re.match(r"(\d+\.\d+\.\d+)", version_string)
"""
Parse version string representing pre-release with -1

Examples: "2.5.0.dev20240708+cu121" -> [2, 5, -1], "2.5.0" -> [2, 5, 0]
"""
# Check for pre-release indicators
is_prerelease = bool(re.search(r"(git|dev)", version_string))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also verify the version string for pytorch, if you build it from source

match = re.match(r"(\d+)\.(\d+)\.(\d+)", version_string)
if match:
version = match.group(1)
return [int(x) for x in version.split(".")]
major, minor, patch = map(int, match.groups())
if is_prerelease:
patch = -1
return [major, minor, patch]
else:
raise ValueError(f"Invalid version string format: {version_string}")


def compare_versions(v1, v2):
v1_parts = parse_version(v1)
v2_parts = parse_version(v2)
return (v1_parts > v2_parts) - (v1_parts < v2_parts)


def is_fbcode():
return not hasattr(torch.version, "git_version")


def torch_version_at_least(min_version):
return is_fbcode() or compare_versions(torch.__version__, min_version) >= 0
if is_fbcode():
return True

# Parser for local identifiers
return parse_version(torch.__version__) >= parse_version(min_version)


def _deprecated_torch_version_at_least(version_str: str) -> str:
Expand Down Expand Up @@ -983,13 +989,13 @@ def is_sm_at_least_100():
def check_cpu_version(device, version="2.6.0"):
if isinstance(device, torch.device):
device = device.type
return device == "cpu" and compare_versions(torch.__version__, version) >= 0
return device == "cpu" and torch_version_at_least(version)


def check_xpu_version(device, version="2.8.0"):
if isinstance(device, torch.device):
device = device.type
return device == "xpu" and compare_versions(torch.__version__, version) >= 0
return device == "xpu" and torch_version_at_least(version)


def ceil_div(a, b):
Expand Down
Loading