π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(feature): run LightGlue pruning in the input dtype (#5838)
Dmytro Mishkinba8160118667
diff --git a/changelog.d/5838.fixed.md b/changelog.d/5838.fixed.mdnew file mode 100644index 000000000..31df08994--- /dev/null+++ b/changelog.d/5838.fixed.md@@ -0,0 +1,9 @@+`LightGlue` and `LightGlueMatcher` with point pruning on (the default `width_confidence=0.99`, and the `superpoint`,+`disk`, `aliked`, `raco-aliked`, `dedodeb`, `dedodeg`, `sift` and `xfeat` feature types) raised `RuntimeError: Index put+requires the source and destination dtypes match` for float64, float16 and bfloat16 models, and for float32 models with+`mp=True` on CPU, where autocast computes the scores in bfloat16. This happened at any number of keypoints, because the+scores are scattered back whenever pruning is enabled. Pruning now keeps the dtype the scores are computed in: the input+dtype, or with `mp=True` the dtype autocast gives them, bfloat16 on CPU and float32 on CUDA and MPS. The early return+for every point pruned now gives its zero scores in that dtype as well. The early return for an image without+descriptors runs no layer and gives the input dtype, so with `mp=True` on CPU its scores are float32 while the other+paths give bfloat16. Both early returns gave float32 before. Without `mp=True`, float32 results are unchanged. (#5837)diff --git a/kornia/feature/lightglue.py b/kornia/feature/lightglue.pyindex dd8a94e04..522aaffb2 100644--- a/kornia/feature/lightglue.py+++ b/kornia/feature/lightglue.py@@ -751,20 +751,22 @@ class LightGlue(nn.Module):# Guard against empty-descriptors after pruningif desc0.shape[1] == 0 or desc1.shape[1] == 0:+ # No layer runs here, so the outputs keep the input dtype, also under mp=True.+ dtype = desc0.dtypeempty_m0 = torch.full((b, m), -1, device=device, dtype=torch.long)empty_m1 = torch.full((b, n), -1, device=device, dtype=torch.long)- empty_s0 = torch.zeros((b, m), device=device)- empty_s1 = torch.zeros((b, n), device=device)+ empty_s0 = torch.zeros((b, m), device=device, dtype=dtype)+ empty_s1 = torch.zeros((b, n), device=device, dtype=dtype)return {- "log_assignment": torch.empty((b, 1, 1), device=device),+ "log_assignment": torch.empty((b, 1, 1), device=device, dtype=dtype),"matches0": empty_m0,"matches1": empty_m1,"matching_scores0": empty_s0,"matching_scores1": empty_s1,"stop": 0,"matches": [torch.empty((0, 2), device=device, dtype=torch.long) for _ in range(b)],- "scores": [torch.empty((0,), device=device) for _ in range(b)],+ "scores": [torch.empty((0,), device=device, dtype=dtype) for _ in range(b)],"prune0": torch.ones_like(empty_s0) * self.conf.n_layers,"prune1": torch.ones_like(empty_s1) * self.conf.n_layers,}@@ -834,12 +836,15 @@ class LightGlue(nn.Module):# If adaptive width pruning removed all keypoints from either side,# return empty matches instead of crashing in filter_matches (see #3564).if desc0.shape[-2] == 0 or desc1.shape[-2] == 0:+ # The other paths' scores are the exp of the log assignment. Autocast runs that exp in float32 on CUDA and+ # MPS and leaves it in the computed dtype on CPU (bfloat16), so take the dtype from the same op.+ dtype = desc0.new_zeros(()).exp().dtypematches = [torch.zeros((0, 2), dtype=torch.long, device=device) for _ in range(b)]- mscores = [torch.zeros(0, device=device) for _ in range(b)]+ mscores = [torch.zeros(0, device=device, dtype=dtype) for _ in range(b)]m0_ = torch.full((b, m), -1, device=device, dtype=torch.long)m1_ = torch.full((b, n), -1, device=device, dtype=torch.long)- mscores0_ = torch.zeros((b, m), device=device)- mscores1_ = torch.zeros((b, n), device=device)+ mscores0_ = torch.zeros((b, m), device=device, dtype=dtype)+ mscores1_ = torch.zeros((b, n), device=device, dtype=dtype)return {"matches": matches,"scores": mscores,@@ -872,8 +877,8 @@ class LightGlue(nn.Module):m1_ = torch.full((b, n), -1, device=m1.device, dtype=m1.dtype)m0_[:, ind0] = torch.where(m0 == -1, -1, ind1.gather(1, m0.clamp(min=0)))m1_[:, ind1] = torch.where(m1 == -1, -1, ind0.gather(1, m1.clamp(min=0)))- mscores0_ = torch.zeros((b, m), device=mscores0.device)- mscores1_ = torch.zeros((b, n), device=mscores1.device)+ mscores0_ = torch.zeros((b, m), device=mscores0.device, dtype=mscores0.dtype)+ mscores1_ = torch.zeros((b, n), device=mscores1.device, dtype=mscores1.dtype)mscores0_[:, ind0] = mscores0mscores1_[:, ind1] = mscores1m0, m1, mscores0, mscores1 = m0_, m1_, mscores0_, mscores1_diff --git a/tests/feature/test_lightglue.py b/tests/feature/test_lightglue.pyindex bc27be89d..a3098de17 100644--- a/tests/feature/test_lightglue.py+++ b/tests/feature/test_lightglue.py@@ -503,3 +503,167 @@ class TestKeypointRangeMessage:data["image0"]["image_size"] = torch.tensor([[8.0, 8.0]], device=device) # keypoints reach 64with pytest.raises(Exception, match="Keypoints must lie inside the image"):lg(data)+++class TestLightGluePruningDtype(BaseTester):+ """``LightGlue`` prunes points in the input dtype (#5837).++ ``LightGlue("disk")`` keeps the preset's pruning (``width_confidence=0.99``, ``depth_confidence=0.95``), with random+ weights in place of the download, two layers, and pruning at any number of keypoints on every device. Layer 0 is+ wired so that each pruning decision has a wide margin in every dtype: the descriptors enter unchanged, the+ matchability reads dimension 0 and the token confidence dimension 1, both +-20. A point of matchability -20 is+ pruned unless its confidence is -20 too, which keeps it as a low-confidence point. Image 1 is image 0 permuted, and+ the last layer's matchability is a constant 10, so a point kept in both images is a mutual match with its copy and+ scores ``sigmoid(10) ** 2``.+ """++ _M = 16+ # Sign of descriptor dimension 0 (matchability) and dimension 1 (token confidence) of each point.+ _MATCHABLE = (1, -1) * 8+ _CONFIDENT = (1, -1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1)++ @staticmethod+ def _model(device, dtype, **conf):+ torch.manual_seed(0)+ with unittest.mock.patch("kornia.feature.lightglue.load_state_dict_from_url", return_value={}):+ lg = LightGlue("disk", n_layers=2, **conf)+ with torch.no_grad():+ lg.input_proj.weight.zero_()+ lg.input_proj.bias.zero_()+ lg.input_proj.weight[:128].copy_(torch.eye(128))+ for linear in (lg.log_assignment[0].matchability, lg.token_confidence[0].token[0]):+ linear.weight.zero_()+ linear.bias.zero_()+ lg.log_assignment[0].matchability.weight[0, 0] = 1.0+ lg.token_confidence[0].token[0].weight[0, 1] = 1.0+ lg.log_assignment[1].matchability.weight.zero_()+ lg.log_assignment[1].matchability.bias.fill_(10.0)+ return lg.to(device, dtype).eval()++ def _scene(self, device, dtype, matchable=_MATCHABLE):+ g = torch.Generator().manual_seed(0)+ desc = 3 * torch.randn(1, self._M, 128, generator=g)+ desc[0, :, 0] = 20 * torch.tensor(matchable, dtype=torch.float32)+ desc[0, :, 1] = 20 * torch.tensor(self._CONFIDENT, dtype=torch.float32)+ kpts = torch.rand(1, self._M, 2, generator=g) * torch.tensor([320.0, 240.0])+ perm = torch.randperm(self._M, generator=g)+ size = torch.tensor([[320, 240]], device=device)+ data = {+ "image0": {"keypoints": kpts.to(device, dtype), "descriptors": desc.to(device, dtype), "image_size": size},+ "image1": {+ "keypoints": kpts[:, perm].to(device, dtype),+ "descriptors": desc[:, perm].to(device, dtype),+ "image_size": size,+ },+ }+ return data, perm++ @staticmethod+ def _forward(model, data):+ thresholds = dict.fromkeys(LightGlue.pruning_keypoint_thresholds, -1) # prune at any number of keypoints+ with torch.no_grad(), unittest.mock.patch.dict(LightGlue.pruning_keypoint_thresholds, thresholds):+ return model(data)++ @staticmethod+ def _floating_dtypes(out):+ tensors = [v for v in out.values() if torch.is_tensor(v)]+ tensors += [t for v in out.values() if isinstance(v, list) for t in v]+ return {t.dtype for t in tensors if t.is_floating_point()}++ def test_pruning_runs_in_the_input_dtype_5837(self, device, dtype):+ data, perm = self._scene(device, dtype)+ out = self._forward(self._model(device, dtype), data)+ ref = self._forward(self._model(device, torch.float32), self._scene(device, torch.float32)[0])++ pruned = (torch.tensor(self._MATCHABLE) < 0) & (torch.tensor(self._CONFIDENT) > 0)+ inv = torch.argsort(perm)+ kept_score = torch.tensor(10.0, dtype=torch.float64).sigmoid().item() ** 2+ self.assert_close(out["prune0"], torch.where(pruned, 1, 2)[None].to(device))+ self.assert_close(out["prune1"], torch.where(pruned[perm], 1, 2)[None].to(device))+ self.assert_close(out["matches0"], torch.where(pruned, -1, inv)[None].to(device))+ self.assert_close(out["matches1"], torch.where(pruned[perm], -1, perm)[None].to(device))+ expected_scores0 = torch.where(pruned, 0.0, kept_score)[None].to(device, dtype)+ self.assert_close(out["matching_scores0"], expected_scores0)+ self.assert_close(out["matching_scores1"], expected_scores0[:, perm])+ self.assert_close(out["scores"][0], expected_scores0[0, ~pruned.to(device)])+ assert self._floating_dtypes(out) == {dtype}+ # The same model and scene in float32, within the tolerance of the dtype.+ for key in ("matches0", "matches1", "prune0", "prune1"):+ self.assert_close(out[key], ref[key])+ for key in ("matching_scores0", "matching_scores1"):+ self.assert_close(out[key], ref[key].to(dtype))++ def test_no_descriptors_returns_the_input_dtype_5837(self, device, dtype):+ empty = {+ "keypoints": torch.empty(1, 0, 2, device=device, dtype=dtype),+ "descriptors": torch.empty(1, 0, 128, device=device, dtype=dtype),+ "image_size": torch.tensor([[320, 240]], device=device),+ }+ out = self._forward(self._model(device, dtype), {"image0": empty, "image1": empty})+ assert out["matching_scores0"].shape == (1, 0)+ assert self._floating_dtypes(out) == {dtype}++ @staticmethod+ def _skip_without_empty_attention(device, dtype):+ # The layers after the one that pruned every point still run, on empty sequences (#3564).+ qkv = torch.empty(1, 1, 0, 8, device=device, dtype=dtype)+ try:+ torch.nn.functional.scaled_dot_product_attention(qkv, qkv, qkv)+ except RuntimeError:+ pytest.skip("torch rejects attention over empty sequences on this device (MPS on torch 2.5.1)")++ def test_all_points_pruned_returns_the_input_dtype_5837(self, device, dtype):+ self._skip_without_empty_attention(device, dtype)+ # No low-confidence rescue without depth confidence, and every matchability -20.+ data, _ = self._scene(device, dtype, matchable=(-1,) * self._M)+ out = self._forward(self._model(device, dtype, depth_confidence=-1), data)+ self.assert_close(out["prune0"], torch.ones(1, self._M, device=device, dtype=torch.long))+ self.assert_close(out["matches0"], torch.full((1, self._M), -1, device=device))+ self.assert_close(out["matching_scores0"], torch.zeros(1, self._M, device=device, dtype=dtype))+ assert out["scores"][0].shape == (0,)+ assert self._floating_dtypes(out) == {dtype}++ def _mixed_precision_score_dtype(self, device, dtype):+ # With mp=True, autocast sets the dtype the scores are computed in: CPU computes the log assignment and its exp+ # in bfloat16, MPS runs that exp in float32. The model without pruning gives the reference.+ if dtype != torch.float32:+ pytest.skip("mp=True autocasts a float32 model")+ plain = self._forward(self._model(device, dtype, mp=True, width_confidence=-1), self._scene(device, dtype)[0])+ score_dtype = plain["matching_scores0"].dtype+ if device.type in ("cpu", "mps"):+ assert score_dtype == (torch.bfloat16 if device.type == "cpu" else torch.float32)+ return score_dtype++ def test_mixed_precision_prunes_in_the_score_dtype_5837(self, device, dtype):+ score_dtype = self._mixed_precision_score_dtype(device, dtype)+ data = self._scene(device, dtype)[0]+ out = self._forward(self._model(device, dtype, mp=True), data)+ ref = self._forward(self._model(device, dtype), data)+ for key in ("matches0", "matches1", "prune0", "prune1"):+ self.assert_close(out[key], ref[key])+ assert (out["prune0"] == 1).any()+ for key in ("matching_scores0", "matching_scores1"):+ assert out[key].dtype == score_dtype+ self.assert_close(out[key].to(dtype), ref[key], rtol=0.0, atol=1e-2)+ assert out["scores"][0].dtype == score_dtype+ # The early return for an image without descriptors runs no layer, and returns the input dtype.+ empty = {+ "keypoints": torch.empty(1, 0, 2, device=device, dtype=dtype),+ "descriptors": torch.empty(1, 0, 128, device=device, dtype=dtype),+ "image_size": torch.tensor([[320, 240]], device=device),+ }+ out = self._forward(self._model(device, dtype, mp=True), {"image0": empty, "image1": empty})+ assert self._floating_dtypes(out) == {dtype}++ def test_mixed_precision_all_points_pruned_returns_the_score_dtype_5837(self, device, dtype):+ score_dtype = self._mixed_precision_score_dtype(device, dtype)+ self._skip_without_empty_attention(device, dtype)+ data, _ = self._scene(device, dtype, matchable=(-1,) * self._M)+ out = self._forward(self._model(device, dtype, mp=True, depth_confidence=-1), data)+ self.assert_close(out["prune0"], torch.ones(1, self._M, device=device, dtype=torch.long))+ self.assert_close(out["matches0"], torch.full((1, self._M), -1, device=device))+ for key in ("matching_scores0", "matching_scores1"):+ assert out[key].dtype == score_dtype+ assert (out[key] == 0).all()+ assert out["scores"][0].dtype == score_dtype+ assert out["scores"][0].shape == (0,)