Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Avoid CPU autocast warning in DeDoDe (#4110)

Jiahao Zhang0ba3414cf840
diff --git a/kornia/feature/dedode/decoder.py b/kornia/feature/dedode/decoder.py
index ef8e1fe26..e9389bc9d 100644
--- a/kornia/feature/dedode/decoder.py
+++ b/kornia/feature/dedode/decoder.py
@@ -165,10 +165,8 @@ class ConvRefiner(nn.Module):
surrounding class.
"""
_b, _c, _hs, _ws = feats.shape
- # AMP is intentionally scoped to "cuda" only: float16 autocast on CPU is not
- # supported by PyTorch and a no-op on MPS. Use amp_dtype=torch.float32 on
- # non-CUDA devices to disable AMP entirely.
- with torch.autocast("cuda", enabled=self.amp, dtype=self.amp_dtype):
+ # AMP runs only for CUDA tensors.
+ with torch.autocast("cuda", enabled=self.amp and feats.is_cuda, dtype=self.amp_dtype):
x0 = self.block1(feats)
x = self.hidden_blocks(x0)
if self.residual:
diff --git a/kornia/feature/dedode/encoder.py b/kornia/feature/dedode/encoder.py
index 16e109cfd..99cca9cf4 100644
--- a/kornia/feature/dedode/encoder.py
+++ b/kornia/feature/dedode/encoder.py
@@ -48,9 +48,6 @@ class VGG19(nn.Module):
self.amp_dtype = amp_dtype
def forward(self, x: torch.Tensor, **kwargs): # type: ignore[no-untyped-def]
- # AMP is intentionally scoped to "cuda" only: float16 autocast on CPU is not
- # supported by PyTorch and a no-op on MPS. Use amp_dtype=torch.float32 on
- # non-CUDA devices to disable AMP entirely.
"""Run this DeDoDe module forward.
Inputs are image, feature, or token tensors used by the DeDoDe detector/descriptor pipeline. `B` denotes batch
@@ -66,7 +63,8 @@ class VGG19(nn.Module):
Output tensor or dictionary produced by the module while preserving the shape contract documented by the
surrounding class.
"""
- with torch.autocast("cuda", enabled=self.amp, dtype=self.amp_dtype):
+ # AMP runs only for CUDA tensors.
+ with torch.autocast("cuda", enabled=self.amp and x.is_cuda, dtype=self.amp_dtype):
feats = []
sizes = []
for layer in self.layers:
diff --git a/tests/feature/test_dedode.py b/tests/feature/test_dedode.py
index 1519dfc68..6d12270da 100644
--- a/tests/feature/test_dedode.py
+++ b/tests/feature/test_dedode.py
@@ -15,10 +15,31 @@
# limitations under the License.
#
+import warnings
+
import pytest
import torch
from kornia.feature.dedode import DeDoDe
+from kornia.feature.dedode.decoder import ConvRefiner
+
+from testing.base import BaseTester
+
+
+class TestConvRefiner(BaseTester):
+ def test_amp_matches_input_device(self, device):
+ refiner = ConvRefiner(in_dim=4, hidden_dim=4, out_dim=4).to(device)
+ autocast_enabled = []
+ refiner.block1.register_forward_pre_hook(
+ lambda _module, _inputs: autocast_enabled.append(torch.is_autocast_enabled("cuda"))
+ )
+
+ with warnings.catch_warnings(record=True) as caught:
+ warnings.simplefilter("always")
+ refiner(torch.rand(1, 4, 8, 8, device=device))
+
+ assert autocast_enabled == [device.type == "cuda"]
+ assert not any("device_type of 'cuda'" in str(w.message) for w in caught)
@pytest.mark.skip(reason="DeDoDe is ummaintained")