π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Fix LightGlue float64 attention on Windows with torch 2.5 (#5994)
Dmytro Mishkin520f04ee1b07
diff --git a/changelog.d/5994.fixed.md b/changelog.d/5994.fixed.mdnew file mode 100644index 000000000..e20bcebc1--- /dev/null+++ b/changelog.d/5994.fixed.md@@ -0,0 +1 @@+LightGlue now uses math attention for CPU float64 inputs on Windows with PyTorch 2.5, avoiding corrupted attention outputs, premature stopping, and incorrect point pruning.diff --git a/kornia/feature/lightglue.py b/kornia/feature/lightglue.pyindex b7ee1ccd7..850547acc 100644--- a/kornia/feature/lightglue.py+++ b/kornia/feature/lightglue.py@@ -16,6 +16,7 @@#import math+import sysimport warningsfrom pathlib import Pathfrom types import SimpleNamespace@@ -25,6 +26,7 @@ import torchimport torch.nn.functional as Ffrom torch import nn+from kornia.core._compat import torch_version_ltfrom kornia.core.check import KORNIA_CHECKfrom kornia.core.download import hf_url, load_state_dict_from_urlfrom kornia.core.utils import is_exporting@@ -139,6 +141,7 @@ class Attention(nn.Module):def __init__(self, allow_flash: bool) -> None:super().__init__()self.allow_flash = allow_flash+ self._windows_old_torch = sys.platform == "win32" and torch_version_lt(2, 6, 0)torch.backends.cuda.enable_flash_sdp(allow_flash)def forward(@@ -165,7 +168,14 @@ class Attention(nn.Module):v = F.scaled_dot_product_attention(*args, attn_mask=mask).to(q.dtype) # type: ignorereturn v if mask is None else v.nan_to_num()args = [x.contiguous() for x in [q, k, v]]- v = F.scaled_dot_product_attention(*args, attn_mask=mask) # type: ignore+ if self._windows_old_torch and q.device.type == "cpu" and q.dtype == torch.float64 and q.ndim == 4:+ # The Windows torch 2.5 CPU flash kernel can return NaNs or corrupt finite values for valid float64+ # inputs. It only accepts 4D inputs: a singleton batch dimension selects math attention without changing+ # process-global backend flags or using a context manager that torch 2.5 cannot compile.+ q_, k_, v_ = (x.unsqueeze(0) for x in args)+ v = F.scaled_dot_product_attention(q_, k_, v_, attn_mask=mask).squeeze(0)+ else:+ v = F.scaled_dot_product_attention(*args, attn_mask=mask) # type: ignorereturn v if mask is None else v.nan_to_num()diff --git a/tests/feature/test_lightglue.py b/tests/feature/test_lightglue.pyindex 006fb5995..24d7e2f7e 100644--- a/tests/feature/test_lightglue.py+++ b/tests/feature/test_lightglue.py@@ -34,7 +34,7 @@ from kornia.feature.lightglue import (rotate_half,)-from testing.base import BaseTester, assert_close+from testing.base import BaseTester, assert_close, dynamo_is_available# ---------------------------------------------------------------------------# Pure function tests@@ -208,6 +208,109 @@ def test_attention_allow_flash_matches_on_cpu():# ---------------------------------------------------------------------------+class TestAttention(BaseTester):+ @pytest.mark.parametrize("batch_size", [1, 2])+ @pytest.mark.parametrize("legacy_windows", [False, True])+ @pytest.mark.parametrize("masked", [False, True])+ def test_cpu_attention_matches_explicit_softmax_5992(self, device, dtype, batch_size, legacy_windows, masked):+ if device.type != "cpu" or dtype not in (torch.float32, torch.float64):+ pytest.skip("regression for Windows CPU float64, with float32 as a control")+ g = torch.Generator().manual_seed(0)+ q, k, v = [torch.randn(batch_size, 4, 16, 64, generator=g, device=device, dtype=dtype) for _ in range(3)]+ mask = None+ logits = (q @ k.transpose(-2, -1)) / 8+ if masked:+ mask = torch.ones(16, 16, device=device, dtype=torch.bool).tril()+ mask[0] = False # Fully masked rows must still return zeros.+ if batch_size > 1:+ mask = mask.expand(batch_size, 1, -1, -1).clone()+ mask[1, :, :, ::2] = False # Different masks per pair, broadcast across heads.+ logits = logits.masked_fill(~mask, -torch.inf)+ expected = logits.softmax(-1).nan_to_num() @ v++ sdpa = torch.nn.functional.scaled_dot_product_attention++ def legacy_sdpa(q, k, v, **kwargs):+ # Reproduce the broken 4D float64 kernel even on healthy hosts. This makes removing the workaround+ # fail deterministically, while the native-platform case still exercises the installed torch kernel.+ if legacy_windows and q.dtype == torch.float64 and q.ndim == 4:+ return torch.full_like(q, float("nan"))+ return sdpa(q, k, v, **kwargs)++ flash_enabled = torch.backends.cuda.flash_sdp_enabled()+ try:+ attention = Attention(allow_flash=True)+ if legacy_windows:+ # Exercise the real fallback on every host, as well as the native platform selection above.+ attention._windows_old_torch = True+ backend_flags = (+ torch.backends.cuda.flash_sdp_enabled(),+ torch.backends.cuda.math_sdp_enabled(),+ torch.backends.cuda.mem_efficient_sdp_enabled(),+ torch.backends.cuda.cudnn_sdp_enabled(),+ )+ with unittest.mock.patch(+ "kornia.feature.lightglue.F.scaled_dot_product_attention", side_effect=legacy_sdpa+ ):+ actual = attention(q, k, v, mask)+ assert actual.isfinite().all()+ self.assert_close(actual, expected)+ assert backend_flags == (+ torch.backends.cuda.flash_sdp_enabled(),+ torch.backends.cuda.math_sdp_enabled(),+ torch.backends.cuda.mem_efficient_sdp_enabled(),+ torch.backends.cuda.cudnn_sdp_enabled(),+ )+ finally:+ torch.backends.cuda.enable_flash_sdp(flash_enabled)++ @pytest.mark.parametrize("masked", [False, True])+ def test_windows_float64_fallback_gradcheck(self, device, masked):+ if device.type != "cpu":+ pytest.skip("the fallback is CPU-only")+ flash_enabled = torch.backends.cuda.flash_sdp_enabled()+ try:+ attention = Attention(allow_flash=True)+ attention._windows_old_torch = True+ q, k, v = [torch.rand(1, 1, 2, 4, device=device, dtype=torch.float64, requires_grad=True) for _ in range(3)]+ mask = torch.tensor([[False, False], [True, True]], device=device) if masked else None+ self.gradcheck(lambda q, k, v: attention(q, k, v, mask), (q, k, v))+ finally:+ torch.backends.cuda.enable_flash_sdp(flash_enabled)++ def test_dynamo_windows_float64_fallback(self, device, torch_optimizer, optimizer_backend):+ if device.type != "cpu" or not optimizer_backend or optimizer_backend == "jit":+ pytest.skip("the fallback needs CPU torch.compile coverage")+ flash_enabled = torch.backends.cuda.flash_sdp_enabled()+ try:+ attention = Attention(allow_flash=True)+ attention._windows_old_torch = True+ q, k, v = [torch.rand(1, 1, 2, 4, device=device, dtype=torch.float64) for _ in range(3)]+ mask = torch.tensor([[False, False], [True, True]], device=device)+ compiled = torch_optimizer(attention, fullgraph=True)+ self.assert_close(compiled(q, k, v, mask), attention(q, k, v, mask))+ finally:+ torch.backends.cuda.enable_flash_sdp(flash_enabled)++ @pytest.mark.skipif(not dynamo_is_available(), reason="no Dynamo on this torch/python pair")+ def test_eager_backend_traces_windows_float64_fallback(self, device):+ # conftest deselects *dynamo* and *compile* tests unless KORNIA_TEST_OPTIMIZER is set, which CI does only on+ # torch 2.14. This name also runs on the scheduled torch 2.5.1 legs, where the fallback is live on Windows.+ if device.type != "cpu":+ pytest.skip("the fallback is CPU-only")+ flash_enabled = torch.backends.cuda.flash_sdp_enabled()+ try:+ attention = Attention(allow_flash=True)+ attention._windows_old_torch = True+ q, k, v = [torch.rand(1, 1, 2, 4, device=device, dtype=torch.float64) for _ in range(3)]+ mask = torch.tensor([[False, False], [True, True]], device=device)+ torch._dynamo.reset()+ compiled = torch.compile(attention, backend="eager", fullgraph=True)+ self.assert_close(compiled(q, k, v, mask), attention(q, k, v, mask))+ finally:+ torch.backends.cuda.enable_flash_sdp(flash_enabled)++class TestLearnableFourierPositionalEncoding(BaseTester):def test_smoke(self, device, dtype):enc = LearnableFourierPositionalEncoding(2, 64).to(device, dtype)