Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Update torch.inverse to torch.linalg.inv (#2824)

wangshuai092f04c1754cfe
diff --git a/kornia/utils/helpers.py b/kornia/utils/helpers.py
index 3f6e2bdd0..f5d1a3be7 100644
--- a/kornia/utils/helpers.py
+++ b/kornia/utils/helpers.py
@@ -156,7 +156,7 @@ def _torch_inverse_cast(input: Tensor) -> Tensor:
dtype: torch.dtype = input.dtype
if dtype not in (torch.float32, torch.float64):
dtype = torch.float32
- return torch.inverse(input.to(dtype)).to(input.dtype)
+ return torch.linalg.inv(input.to(dtype)).to(input.dtype)
def _torch_histc_cast(input: Tensor, bins: int, min: int, max: int) -> Tensor:
diff --git a/tests/utils/test_helpers.py b/tests/utils/test_helpers.py
index 70a00dea9..3c1bb413b 100644
--- a/tests/utils/test_helpers.py
+++ b/tests/utils/test_helpers.py
@@ -62,7 +62,7 @@ def test_extract_device_dtype(tensor_list, out_device, out_dtype, will_throw_err
class TestInverseCast:
- @pytest.mark.parametrize("input_shape", [(1, 3, 4, 4), (2, 4, 5, 5)])
+ @pytest.mark.parametrize("input_shape", [(4, 4), (1, 3, 4, 4), (2, 4, 5, 5)])
def test_smoke(self, device, dtype, input_shape):
x = torch.rand(input_shape, device=device, dtype=dtype)
y = _torch_inverse_cast(x)
@@ -83,6 +83,11 @@ class TestInverseCast:
op_jit = torch.jit.script(op)
assert_close(op(x), op_jit(x))
+ def test_not_invertible(self, device, dtype):
+ with pytest.raises(RuntimeError):
+ x = torch.tensor([[0.0, 0.0], [0.0, 0.0]], device=device, dtype=dtype)
+ _ = _torch_inverse_cast(x)
+
class TestHistcCast:
def test_smoke(self, device, dtype):