🐍 Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Replace deprecated torch.cuda.amp decorators for torch 2.4 (#2967)
Loïc Huderef824367062e
diff --git a/kornia/contrib/models/efficient_vit/nn/ops.py b/kornia/contrib/models/efficient_vit/nn/ops.pyindex 2eecc705a..3663354c1 100644--- a/kornia/contrib/models/efficient_vit/nn/ops.py+++ b/kornia/contrib/models/efficient_vit/nn/ops.py@@ -8,11 +8,11 @@ from __future__ import annotationsimport torchimport torch.nn.functional as Ffrom torch import nn-from torch.cuda.amp import autocastfrom kornia.contrib.models.efficient_vit.nn.act import build_actfrom kornia.contrib.models.efficient_vit.nn.norm import build_normfrom kornia.contrib.models.efficient_vit.utils import get_same_padding, val2tuple+from kornia.utils._compat import autocast__all__ = ["ConvLayer",diff --git a/kornia/feature/lightglue.py b/kornia/feature/lightglue.pyindex 67ef03331..1864bba7d 100644--- a/kornia/feature/lightglue.py+++ b/kornia/feature/lightglue.py@@ -25,6 +25,7 @@ from kornia.core import ()from kornia.core.check import KORNIA_CHECKfrom kornia.feature.laf import laf_to_three_points, scale_laf+from kornia.utils._compat import custom_fwdtry:from flash_attn.modules.mha import FlashCrossAttention@@ -41,7 +42,7 @@ def math_clamp(x, min_, max_): # type: ignorereturn max(min(x, min_), min_)[email protected]_fwd(cast_inputs=torch.float32)+@custom_fwd(cast_inputs=torch.float32)def normalize_keypoints(kpts: Tensor, size: Tensor) -> Tensor:if isinstance(size, torch.Size):size = Tensor(size)[None]diff --git a/kornia/utils/_compat.py b/kornia/utils/_compat.pyindex 7992358f4..20b787930 100644--- a/kornia/utils/_compat.py+++ b/kornia/utils/_compat.py@@ -1,4 +1,4 @@-from typing import TYPE_CHECKING, Callable, ContextManager, List, Optional, Tuple, TypeVar+from typing import TYPE_CHECKING, Any, Callable, ContextManager, List, Optional, Tuple, TypeVarimport torchfrom packaging import version@@ -52,3 +52,19 @@ elif torch_version_ge(1, 10, 0):else:# TODO: remove this branch when kornia relies on torch >= 1.10.0torch_inference_mode = torch.no_grad++if TYPE_CHECKING: # TODO (@johnnv1): remove this branch when bump the pytorch CI to support torch 2.4+ custom_fwd: Callable[..., Any]+ autocast: Callable[..., Any]+elif torch_version_ge(2, 4):+ from functools import partial++ from torch.amp import autocast as _autocast+ from torch.amp import custom_fwd as _custom_fwd++ custom_fwd = partial(_custom_fwd, device_type="cuda")+ autocast = partial(_autocast, "cuda")++else:+ custom_fwd = torch.cuda.amp.custom_fwd+ autocast = torch.cuda.amp.autocast