Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(core): make torch version checks torch.compile fullgraph-safe (#3802)

Edgar Riba1aab6f12d294
diff --git a/kornia/core/_compat.py b/kornia/core/_compat.py
index fa5af28b5..195bde3fe 100644
--- a/kornia/core/_compat.py
+++ b/kornia/core/_compat.py
@@ -19,10 +19,9 @@
import warnings
from functools import wraps
from inspect import isclass
-from typing import Any, Callable, Optional
+from typing import Any, Callable, Optional, Tuple
import torch
-from packaging import version
def torch_version() -> str:
@@ -37,6 +36,37 @@ def torch_version() -> str:
return torch.__version__.partition("+")[0]
+def _parse_torch_version(v: str) -> Tuple[int, int, int]:
+ """Parse ``torch.__version__`` into a ``(major, minor, patch)`` int tuple.
+
+ Build metadata (``+cu118``) and any pre-release/dev suffix on a component
+ (``2.4.0rc1``) are dropped, so the result orders identically to the release
+ versions Kornia compares against. Computed once at import time so the
+ ``torch_version_*`` comparators never run a string method β€” those are the
+ calls ``torch.compile``'s dynamo tracer cannot lower, and reaching them on a
+ hot path (e.g. ``is_autocast_enabled`` in the augmentation base) breaks the
+ whole graph.
+ """
+ base = v.partition("+")[0]
+ parts = base.split(".")
+ nums = []
+ for part in parts[:3]:
+ digits = ""
+ for ch in part:
+ if not ch.isdigit():
+ break
+ digits += ch
+ nums.append(int(digits) if digits else 0)
+ while len(nums) < 3:
+ nums.append(0)
+ return (nums[0], nums[1], nums[2])
+
+
+# Parsed once at import: subsequent comparisons are pure integer-tuple compares,
+# which dynamo constant-folds cleanly (unlike ``partition``/``packaging.version``).
+_TORCH_VERSION: Tuple[int, int, int] = _parse_torch_version(torch.__version__)
+
+
def torch_version_lt(major: int, minor: int, patch: int) -> bool:
"""Check if the current PyTorch version is less than the specified version.
@@ -50,8 +80,7 @@ def torch_version_lt(major: int, minor: int, patch: int) -> bool:
False otherwise.
"""
- _version = version.parse(torch_version())
- return _version < version.parse(f"{major}.{minor}.{patch}")
+ return _TORCH_VERSION < (major, minor, patch)
def torch_version_le(major: int, minor: int, patch: int) -> bool:
@@ -67,8 +96,7 @@ def torch_version_le(major: int, minor: int, patch: int) -> bool:
False otherwise.
"""
- _version = version.parse(torch_version())
- return _version <= version.parse(f"{major}.{minor}.{patch}")
+ return _TORCH_VERSION <= (major, minor, patch)
def torch_version_ge(major: int, minor: int, patch: Optional[int] = None) -> bool:
@@ -95,11 +123,9 @@ def torch_version_ge(major: int, minor: int, patch: Optional[int] = None) -> boo
>>> torch_version_ge(3, 0, 0) # True if PyTorch >= 3.0.0
False
"""
- _version = version.parse(torch_version())
if patch is None:
- return _version >= version.parse(f"{major}.{minor}")
- else:
- return _version >= version.parse(f"{major}.{minor}.{patch}")
+ return _TORCH_VERSION[:2] >= (major, minor)
+ return _TORCH_VERSION >= (major, minor, patch)
def _emit_deprecation_warning(
diff --git a/tests/augmentation/test_augmentation.py b/tests/augmentation/test_augmentation.py
index 703cc29e7..892537bcd 100644
--- a/tests/augmentation/test_augmentation.py
+++ b/tests/augmentation/test_augmentation.py
@@ -881,6 +881,15 @@ class TestRandomHorizontalFlip(BaseTester):
repr = "RandomHorizontalFlip(p=0.5, p_batch=1.0, same_on_batch=False)"
assert str(f) == repr
+ def test_dynamo(self, device, dtype, torch_optimizer):
+ torch.manual_seed(0)
+ input = torch.rand(2, 3, 4, 5, device=device, dtype=dtype)
+ op = RandomHorizontalFlip(p=1.0)
+ params = op.forward_parameters(input.shape)
+ expected = op(input, params=params)
+ compiled = torch.compile(op, fullgraph=True)
+ self.assert_close(compiled(input, params=params), expected)
+
def test_random_hflip(self, device, dtype):
f = RandomHorizontalFlip(p=1.0, keepdim=True)
f1 = RandomHorizontalFlip(p=0.0, keepdim=True)
@@ -1060,6 +1069,15 @@ class TestRandomVerticalFlip(BaseTester):
repr = "RandomVerticalFlip(p=0.5, p_batch=1.0, same_on_batch=False)"
assert str(f) == repr
+ def test_dynamo(self, device, dtype, torch_optimizer):
+ torch.manual_seed(0)
+ input = torch.rand(2, 3, 4, 5, device=device, dtype=dtype)
+ op = RandomVerticalFlip(p=1.0)
+ params = op.forward_parameters(input.shape)
+ expected = op(input, params=params)
+ compiled = torch.compile(op, fullgraph=True)
+ self.assert_close(compiled(input, params=params), expected)
+
def test_random_vflip(self, device, dtype):
f = RandomVerticalFlip(p=1.0)
f1 = RandomVerticalFlip(p=0.0)