π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(feature): ALIKED compiles with torch.compile when the image size changes (compiled 2.1x on CUDA) (#6495)
Dmytro Mishkinbc98050aa4bc
diff --git a/changelog.d/6495.fixed.md b/changelog.d/6495.fixed.mdnew file mode 100644index 000000000..64b1242ab--- /dev/null+++ b/changelog.d/6495.fixed.md@@ -0,0 +1 @@+`ALIKED` compiles with `torch.compile` when the image size changes. The second size made Dynamo trace the sizes symbolically, and the padding of `extract_dense_map`, a modulo of the image size, reached the offset bound of `DeformableConv2d`, which inductor could not lower: the call raised `BackendCompilerFailed` (`TensorReferenceAnalysis has no attribute 'python_mod'`). The padding is now computed without a modulo and has the same value, so eager results are unchanged.diff --git a/kornia/feature/aliked/aliked.py b/kornia/feature/aliked/aliked.pyindex fc6520867..159f3e6c1 100644--- a/kornia/feature/aliked/aliked.py+++ b/kornia/feature/aliked/aliked.py@@ -422,8 +422,10 @@ class InputPadder:def __init__(self, h: int, w: int, divis_by: int = 8) -> None:self.ht = hself.wd = w- pad_ht = (((h // divis_by) + 1) * divis_by - h) % divis_by- pad_wd = (((w // divis_by) + 1) * divis_by - w) % divis_by+ # The distance to the next multiple, written without `%`: under dynamic-shape torch.compile a modulo of the+ # image size reached the float offset bound of DeformableConv2d, which inductor (torch 2.14) cannot lower.+ pad_ht = (h + divis_by - 1) // divis_by * divis_by - h+ pad_wd = (w + divis_by - 1) // divis_by * divis_by - wself._pad = [pad_wd // 2, pad_wd - pad_wd // 2, pad_ht // 2, pad_ht - pad_ht // 2]def pad(self, x: torch.Tensor) -> torch.Tensor:diff --git a/tests/feature/test_aliked.py b/tests/feature/test_aliked.pyindex d3463f6e9..e9492c054 100644--- a/tests/feature/test_aliked.py+++ b/tests/feature/test_aliked.py@@ -24,7 +24,7 @@ import torch.nn.functional as Fimport korniafrom kornia.feature import laf_is_filledfrom kornia.feature.aliked import ALIKED, ALIKEDFeatures-from kornia.feature.aliked.aliked import SDDH, _affine_from_cov, get_patches+from kornia.feature.aliked.aliked import SDDH, InputPadder, _affine_from_cov, get_patchesfrom kornia.feature.aliked.deform_conv2d import _bilinear_sample, _grid_sample_bilinear, deform_conv2dfrom testing.base import (@@ -438,6 +438,48 @@ class TestALIKED(BaseTester):output = aliked(torch.rand(0, 3, 64, 64, device=device, dtype=dtype))assert output == []+ def test_input_padder_pads_to_the_next_multiple(self):+ """The pad reaches the next multiple of ``divis_by``, split evenly with the extra pixel at the end."""+ for size in range(70):+ for divis_by in (1, 8, 32):+ left, right, top, bottom = InputPadder(size, size + 3, divis_by)._pad+ for pad, extent in ((top + bottom, size), (left + right, size + 3)):+ assert (extent + pad) % divis_by == 0+ assert 0 <= pad < divis_by+ assert bottom - top in (0, 1)+ assert right - left in (0, 1)++ def test_dynamo_dense_map_with_changing_sizes(self, dtype, device, torch_optimizer):+ """extract_dense_map compiles in one graph and matches eager, also once the image size changes.++ A second size makes Dynamo trace the sizes symbolically. The padding used to be a modulo of the image size,+ which reached the float offset bound of DeformableConv2d, and inductor (torch 2.14) failed to lower it:+ ``TensorReferenceAnalysis has no attribute 'python_mod'``.+ """+ if device.type == "mps" and dtype == torch.float64:+ pytest.skip("MPS does not support float64")+ torch.manual_seed(0)+ aliked = ALIKED(model_name="aliked-t16").to(device, dtype).eval()+ op = torch_optimizer(aliked.extract_dense_map, fullgraph=True)+ for hw in ((64, 96), (96, 72), (80, 112)):+ inp = torch.rand(1, 3, *hw, device=device, dtype=dtype)+ with torch.no_grad():+ expected, actual = aliked.extract_dense_map(inp), op(inp)+ for e, a in zip(expected, actual):+ self.assert_close(a, e)++ def test_dynamo_model_with_changing_sizes(self, dtype, device, torch_optimizer):+ """The compiled model, with the graph breaks of its keypoint selection, runs on changing image sizes."""+ if device.type == "mps" and dtype == torch.float64:+ pytest.skip("MPS does not support float64")+ aliked = ALIKED(model_name="aliked-t16").to(device, dtype).eval()+ op = torch_optimizer(aliked)+ for hw in ((64, 96), (96, 72)):+ with torch.no_grad():+ output = op(torch.rand(1, 3, *hw, device=device, dtype=dtype))+ assert len(output) == 1+ assert isinstance(output[0], ALIKEDFeatures)+def test_smoke(self, dtype, device):aliked = ALIKED().to(device, dtype).eval()inp = torch.rand(1, 3, 64, 64, device=device, dtype=dtype)