π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(feature): convert DeDoDe detector context back to the input dtype (#5760)
Shiven Solanki69d29fff7b19
diff --git a/changelog.d/5760.fixed.md b/changelog.d/5760.fixed.mdnew file mode 100644index 000000000..535bbb679--- /dev/null+++ b/changelog.d/5760.fixed.md@@ -0,0 +1,4 @@+`DeDoDe.detect` and `DeDoDe.forward` now run with float16 and bfloat16 weights on CPU and MPS. `DeDoDeDetector.forward`+lifted the decoder context to float32 for the interpolation and fed it back to the half-precision decoder, which raised+`RuntimeError: Input type (float) and bias type (c10::Half) should be the same`. The context is now converted back to+the input dtype after the interpolation. Results in float32 and float64 are unchanged.diff --git a/kornia/feature/dedode/detector.py b/kornia/feature/dedode/detector.pyindex e4cae3558..04c8c5fc7 100644--- a/kornia/feature/dedode/detector.py+++ b/kornia/feature/dedode/detector.py@@ -57,5 +57,5 @@ class DeDoDeDetector(nn.Module):if idx < len(scales) - 1:size = sizes[-(idx + 2)]logits = F.interpolate(logits, size=size, mode="bicubic", align_corners=False)- context = F.interpolate(context.float(), size=size, mode="bilinear", align_corners=False)+ context = F.interpolate(context.float(), size=size, mode="bilinear", align_corners=False).to(dtype)return logits.to(dtype) # type: ignorediff --git a/tests/feature/test_dedode.py b/tests/feature/test_dedode.pyindex 95ee32c82..bcd802913 100644--- a/tests/feature/test_dedode.py+++ b/tests/feature/test_dedode.py@@ -193,6 +193,16 @@ class TestDeDoDePadding(BaseTester):assert model.descriptor.seen[0].shape == (1, 3, 50, 80)+class TestDeDoDeHalfPrecision(BaseTester):+ def test_detect_runs_in_the_model_dtype_5756(self, device, dtype):+ model = DeDoDe(detector_model="L", descriptor_model="B", amp_dtype=dtype).to(device, dtype).eval()+ image = torch.rand(1, 3, 56, 84, device=device, dtype=dtype)+ keypoints, scores = model.detect(image, n=10)+ assert keypoints.shape == (1, 10, 2)+ assert scores.shape == (1, 10)+ assert torch.isfinite(scores).all()++class TestDeDoDeNoGrad(BaseTester):@staticmethoddef _model(device, dtype):@@ -208,9 +218,10 @@ class TestDeDoDeNoGrad(BaseTester):@staticmethoddef _skip_half(dtype):if dtype in (torch.float16, torch.bfloat16):- # Not a compile matter: eager `detect` raises "Input type (float) and bias type (c10::Half)" as well,- # because the detector lifts its context to float32 in front of half-precision decoder weights.- pytest.skip("DeDoDe with half-precision weights does not run in eager either")+ # Eager runs in half precision, but the compiled outputs differ from it by more than the tolerances below+ # (torch 2.14 CPU: descriptors by 4e-4 in float16 and 3e-3 in bfloat16; the last bfloat16 keypoints of the+ # top 100 move by a pixel).+ pytest.skip("compiled half-precision DeDoDe differs from eager by more than these tolerances")def test_outputs_are_ordinary_tensors_without_grad_5737(self, device, dtype):# `detect` and `describe` run under `torch.no_grad()`, not `torch.inference_mode()`: inference tensors