π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(core): keep the captured inverse finite at extreme scales in safe_inverse_with_mask (#5633)
εΊζ’η3f448fd7cdc1
diff --git a/changelog.d/5633.fixed.md b/changelog.d/5633.fixed.mdnew file mode 100644index 000000000..c29a99a4a--- /dev/null+++ b/changelog.d/5633.fixed.md@@ -0,0 +1,8 @@+Under graph capture (`torch.jit.trace`, `torch.export`, ONNX export), `safe_inverse_with_mask` and+`_torch_inverse_cast` now return the finite inverse of a regular 2x2, 3x3 or 4x4 matrix at any scale the inverse fits+in, and the mask of `safe_inverse_with_mask` is `True` for it (#5507). Previously the closed-form `adj / det`+overflowed or underflowed the dtype long before the inverse did, so the traced mask rejected `1e13 * I` and+`1e-13 * I` of order 4 in float32 that eager accepts, and `2**40 * I` came back as the zero matrix with mask `True`.+The adjugate is now taken on a copy balanced by powers of two per row and column, which leaves every result whose+adjugate and determinant were normal numbers unchanged to the bit; where one of them overflowed or went subnormal the+result was `inf`, `nan`, zero or rounded, and is now the inverse. Eager mode is unchanged.diff --git a/kornia/core/utils.py b/kornia/core/utils.pyindex f40b30661..89f7dfada 100644--- a/kornia/core/utils.py+++ b/kornia/core/utils.py@@ -266,6 +266,57 @@ def _has_closed_form_inverse(input: torch.Tensor) -> bool:return input.shape[-2] == n and n in (2, 3, 4)+def _closed_form_inverse(input: torch.Tensor) -> torch.Tensor:+ """Inverse of batched 2x2, 3x3 or 4x4 matrices as ``adj / det``, computed on a scaled copy.++ The entries of the adjugate and the determinant are products of up to ``n`` entries, which overflow or+ underflow the dtype long before the inverse does: for ``1e13 * I`` or ``1e-13 * I`` of order 4 in+ float32 they are ``inf`` or ``0`` while the inverse is ``1e-13 * I`` or ``1e13 * I``. So the matrix is+ balanced first, each row divided by the power of two below its largest magnitude and then each column of+ that by the power of two below its own, ``inv(A) = D_c inv(D_r A D_c) D_r``, and the rows and columns of+ the result are scaled back by the same factors. A power of two keeps every rounding step the one of the+ unscaled computation, so the result is bit for bit the former ``adj / det`` wherever its adjugate and+ determinant were normal numbers; where one of them overflowed or went subnormal the former result was+ ``inf``, ``nan``, zero or rounded (``diag(2 ** -100, 2 ** -40)`` in float32 has a subnormal determinant+ and was off by 1.2e-7), and the new one is the inverse.++ The scaling works on the exponents and never forms the row-scaled matrix: ``[[2 ** 100, 2 ** -60],+ [2 ** 100, -2 ** -60]]`` in float32 would lose its second column to underflow on the way, and its inverse+ is finite. The combined exponent of an entry can exceed what the dtype holds while the entry itself fits,+ so it is applied as three powers of two of about a third of it each; every intermediate lies between the+ start and the end, and is exact wherever the end is a normal number. A zero row or column is left alone, and+ so is one holding a NaN, whose exponent would index the table of powers of two out of range (a NaN casts to+ the most negative int64 on x86 and CUDA); its determinant is 0 or NaN either way.++ Raises:+ NotImplementedError: for shapes other than ``(..., n, n)`` with ``n`` in 2, 3, 4.+ """+ exponent = torch.floor(torch.log2(input.abs())) # -inf at a zero entry, which no maximum below picks+ row = exponent.amax(-1, keepdim=True)+ row = torch.where(torch.isfinite(row), row, torch.zeros_like(row))+ col = (exponent - row).amax(-2, keepdim=True)+ col = torch.where(torch.isfinite(col), col, torch.zeros_like(col))+ adj, det = _adjugate_closed_form(_times_power_of_two(input, -(row + col)))+ return _times_power_of_two(adj / det[..., None, None], -(col.transpose(-2, -1) + row.transpose(-2, -1)))+++def _times_power_of_two(x: torch.Tensor, exponent: torch.Tensor) -> torch.Tensor:+ """``x * 2 ** exponent`` in three steps of about a third of the exponent each.++ No step and no intermediate leaves the dtype while the result is inside it. ``exponent`` is integer+ valued and is clipped to three times the largest exponent of a normal number of the dtype, beyond which a+ nonzero result is ``inf`` or ``0`` anyway and a zero one stays ``0`` instead of turning into ``0 * inf``.+ The powers of two are read from a table, not computed by ``pow``, which is not exact on every device+ (MPS), and the scaling has to be exact to leave every rounding step as it was.+ """+ largest = 1022 if x.dtype == torch.float64 else 126+ exponent = exponent.clamp(-3.0 * largest, 3.0 * largest)+ step = torch.round(exponent / 3)+ powers = torch.tensor([2.0**k for k in range(-largest, largest + 2)], dtype=x.dtype, device=x.device)+ scale = powers[(step + largest).to(torch.int64)]+ return x * scale * scale * powers[(exponent - 2 * step + largest).to(torch.int64)]++def _torch_inverse_cast(input: torch.Tensor) -> torch.Tensor:"""Make torch.inverse work with other than fp32/64.@@ -288,8 +339,7 @@ def _torch_inverse_cast(input: torch.Tensor) -> torch.Tensor:KORNIA_CHECK_IS_TENSOR(input, "Input must be torch.Tensor")dtype = _normalize_to_float32_or_float64(input.dtype)if _is_tracing_or_exporting() and _has_closed_form_inverse(input):- adj, det = _adjugate_closed_form(input.to(dtype))- return (adj / det[..., None, None]).to(input.dtype)+ return _closed_form_inverse(input.to(dtype)).to(input.dtype)return torch.linalg.inv(input.to(dtype)).to(input.dtype)@@ -532,13 +582,14 @@ def safe_inverse_with_mask(A: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]# output, and ``0 * nan`` leaks it into every shared parameter.A_detached = A_cast.detach()if _is_tracing_or_exporting() and _has_closed_form_inverse(A):- # ``linalg_inv_ex`` has no ONNX lowering; the adjugate form is basic arithmetic.- adj_detached, det_detached = _adjugate_closed_form(A_detached)+ # ``linalg_inv_ex`` has no ONNX lowering; the adjugate form is basic arithmetic. It is computed on a+ # scaled copy (:func:`_closed_form_inverse`), so a regular matrix at a scale whose adjugate or+ # determinant leaves the dtype keeps its finite inverse and its ``True`` mask, as in eager mode.mask = ~_is_singular(A_detached)- safe_det = torch.where(mask, det_detached, torch.ones_like(det_detached))- mask = mask & _rows_finite((adj_detached / safe_det[..., None, None]).to(dtype_original))- adj, det = _adjugate_closed_form(torch.where(mask[..., None, None], A_cast, eye))- return (adj / det[..., None, None]).to(dtype_original), mask+ inverse_detached = _closed_form_inverse(torch.where(mask[..., None, None], A_detached, eye))+ mask = mask & _rows_finite(inverse_detached.to(dtype_original))+ inverse = _closed_form_inverse(torch.where(mask[..., None, None], A_cast, eye))+ return inverse.to(dtype_original), maskinverse_detached, info = inv_ex(A_detached)mask = (info == 0) & _rows_finite(inverse_detached.to(dtype_original))diff --git a/tests/core/test_helpers.py b/tests/core/test_helpers.pyindex e8eabf38d..95b5a1b72 100644--- a/tests/core/test_helpers.py+++ b/tests/core/test_helpers.py@@ -21,6 +21,7 @@ import torchfrom kornia.core.exceptions import BaseError, DeviceError, TypeCheckErrorfrom kornia.core.utils import (_adjugate_closed_form,+ _closed_form_inverse,_det_perm_closed_form,_extract_device_dtype,_inverse_3x3_closed_form,@@ -57,6 +58,43 @@ def _scales(dtype):return [1e-3, 1.0, 1e3, 2.0**-20, 2.0**20]+def _regular_at_extreme_scales(device, dtype):+ """Well-conditioned 4x4 matrices scaled so that the products of four entries overflow or underflow the dtype+ while the inverse fits: the identity and a dense matrix at ``big`` and ``1 / big``, the dense matrix with two+ columns, then two rows, scaled by ``small``, the identity at the largest finite value of the dtype, where ``log2``+ rounds up to the exponent above, and a block matrix whose column factor ``tiny / huge`` and the row factor ``low``+ of its other block add up to an exponent beyond the dtype, by which the zeros of its inverse are scaled back,+ while no entry of its row- or column-scaled copy is subnormal (MPS flushes those to zero). Powers of two keep the+ scaling exact."""+ big = 2.0**40 if dtype == torch.float32 else 2.0**300+ small = 2.0**-80 if dtype == torch.float32 else 2.0**-600+ huge, tiny, low = (2.0**80, 2.0**-40, 2.0**-20) if dtype == torch.float32 else (2.0**800, 2.0**-200, 2.0**-100)+ A = torch.tensor(+ [[4.0, 1.0, 2.0, 1.0], [1.0, 5.0, 1.0, 2.0], [2.0, 1.0, 6.0, 1.0], [1.0, 2.0, 1.0, 7.0]],+ device=device,+ dtype=dtype,+ )+ eye = torch.eye(4, device=device, dtype=dtype)+ D = torch.diag(torch.tensor([1.0, 1.0, small, small], device=device, dtype=dtype))+ compound = eye * low+ compound[:2, :2] = torch.tensor([[huge, tiny], [huge, -tiny]], device=device, dtype=dtype)+ return torch.stack([eye * big, eye / big, A * big, A / big, A @ D, D @ A, eye * torch.finfo(dtype).max, compound])+++def _regular_with_a_vanishing_column(n, device, dtype):+ """``[[2 ** 100, 2 ** -60], [2 ** 100, -2 ** -60]]`` (``2 ** 1000`` and ``2 ** -600`` in float64) in the corner of+ the identity of order ``n``, and its inverse, ``[[1 / (2 huge), 1 / (2 huge)], [1 / (2 tiny), -1 / (2 tiny)]]`` in+ the corner. Regular, with a finite inverse and a finite ``adj / det``, while the second column is ``2 ** -160`` of+ the first in both rows and would underflow in a row scaling that is carried out. ``torch.linalg.inv`` of torch+ 2.5.1 returns ``[[1 / huge, 0], [1 / tiny, -1 / tiny]]`` for it, off by 1 in ``A @ inv``; torch 2.14 is exact."""+ huge, tiny = (2.0**100, 2.0**-60) if dtype == torch.float32 else (2.0**1000, 2.0**-600)+ A = torch.eye(n, device=device, dtype=dtype)+ A[:2, :2] = torch.tensor([[huge, tiny], [huge, -tiny]], device=device, dtype=dtype)+ inverse = torch.eye(n, device=device, dtype=dtype)+ inverse[:2, :2] = torch.tensor([[0.5 / huge, 0.5 / huge], [0.5 / tiny, -0.5 / tiny]], device=device, dtype=dtype)+ return A, inverse++# One row is the sum of two others, or a multiple of the other, in small integers: exactly singular, and every# product of the closed-form determinant is exact._DEPENDENT_ROWS = {@@ -227,6 +265,68 @@ class TestInverseCast:with pytest.raises(NotImplementedError):_adjugate_closed_form(torch.eye(5, device=device, dtype=dtype))+ @pytest.mark.parametrize("n", [2, 3, 4])+ def test_scaled_closed_form_is_adj_over_det_bit_for_bit(self, device, dtype, n):+ # The closed-form inverse balances rows and columns by powers of two before the adjugate (#5507). A power+ # of two changes no rounding step, so wherever the adjugate and the determinant are normal numbers the+ # result is the same to the bit. The last matrix has a second column that is ``2 ** -160`` of the first in+ # every row: a row scaling that is carried out would flush it, the exponent arithmetic does not.+ if dtype not in (torch.float32, torch.float64):+ pytest.skip("the capture path computes in float32 or float64")+ torch.manual_seed(0)+ x = torch.randn(64, n, n, device=device, dtype=dtype)+ x = x * torch.logspace(-3, 3, 64, dtype=dtype).to(device)[:, None, None] # ``logspace`` has no MPS kernel+ x[::2] *= torch.logspace(-2, 2, n, dtype=dtype).to(device)[None, :, None]+ x = torch.cat([x, _regular_with_a_vanishing_column(n, device, dtype)[0][None]])+ adj, det = _adjugate_closed_form(x)+ assert torch.isfinite(adj).all()+ assert torch.isfinite(det).all()+ assert torch.equal(_closed_form_inverse(x), adj / det[..., None, None])++ def test_trace_keeps_the_inverse_finite_at_an_extreme_scale_5507(self, device, dtype):+ # Under capture ``1e13 * I`` of order 4 had ``adj inf`` and ``det inf`` in float32 and came back as NaN,+ # while eager returned ``1e-13 * I``. The scaled adjugate keeps the traced inverse finite and eager-close.+ if dtype not in (torch.float32, torch.float64):+ pytest.skip("tracing under half precision is not a supported surface")+ # The last matrix is regular with a column ``2 ** -160`` of the other in every row; ``_is_singular`` reads it+ # as singular in eager mode and under capture alike, so it is tested on this mask-free path. It is read+ # against its exact inverse, which the powers of two reach to the bit, not against eager, whose LU loses the+ # column on torch 2.5.1.+ vanishing, vanishing_inverse = _regular_with_a_vanishing_column(4, device, dtype)+ A = torch.cat([_regular_at_extreme_scales(device, dtype), vanishing[None]])+ traced = torch.jit.trace(_torch_inverse_cast, A, check_trace=False)+ inverse = traced(A)+ assert torch.isfinite(inverse).all()+ rtol = 1e-5 if dtype == torch.float32 else 1e-12+ assert_close(inverse[:-1], _torch_inverse_cast(A[:-1]), atol=0.0, rtol=rtol)+ assert torch.equal(inverse[-1], vanishing_inverse)++ @pytest.mark.parametrize("n", [3, 4])+ def test_trace_scales_the_inverse_back_beyond_the_largest_power_of_two_5507(self, device, dtype, n):+ # The entry (1, 2) of the inverse of this matrix is ``-2 ** 45`` in float32, but its row factor ``2 ** -20``+ # and the column factor ``2 ** -126`` of the ``2 ** -26`` entries scale it back by ``2 ** 146``, beyond the+ # largest power of two of the dtype (``2 ** 1122`` in float64). One power of two capped at that largest+ # one returns ``-2 ** 26``. Every entry and the exact inverse are normal powers of two.+ if dtype not in (torch.float32, torch.float64):+ pytest.skip("tracing under half precision is not a supported surface")+ h, low, r = (100, 26, 20) if dtype == torch.float32 else (800, 222, 100)+ A = torch.eye(n, device=device, dtype=dtype)+ A[:3, :3] = torch.tensor(+ [[2.0**h, 2.0**-low, 1.0], [2.0**h, -(2.0**-low), 0.0], [0.0, 0.0, 2.0**-r]], device=device, dtype=dtype+ )+ expected = torch.eye(n, device=device, dtype=dtype)+ expected[:3, :3] = torch.tensor(+ [+ [2.0 ** -(h + 1), 2.0 ** -(h + 1), -(2.0 ** (r - h - 1))],+ [2.0 ** (low - 1), -(2.0 ** (low - 1)), -(2.0 ** (low + r - 1))],+ [0.0, 0.0, 2.0**r],+ ],+ device=device,+ dtype=dtype,+ )+ traced = torch.jit.trace(_torch_inverse_cast, A[None], check_trace=False)+ assert torch.equal(traced(A[None])[0], expected)+class TestExportHelpers:def test_is_exporting_eager(self):@@ -703,18 +803,53 @@ class TestInverseWithMask:# Powers of two keep the scaling exact. Half input is decided in float32, where these scales are harmless.if dtype not in (torch.float32, torch.float64):pytest.skip("half input is decided in float32")- big = 2.0**40 if dtype == torch.float32 else 2.0**300- small = 2.0**-80 if dtype == torch.float32 else 2.0**-600- A = torch.tensor(- [[4.0, 1.0, 2.0, 1.0], [1.0, 5.0, 1.0, 2.0], [2.0, 1.0, 6.0, 1.0], [1.0, 2.0, 1.0, 7.0]],- device=device,- dtype=dtype,- )- D = torch.diag(torch.tensor([1.0, 1.0, small, small], device=device, dtype=dtype))- A = torch.stack([A * big, A / big, A @ D, D @ A])+ A = _regular_at_extreme_scales(device, dtype)assert not _is_singular(A).any()assert safe_inverse_with_mask(A)[1].all()- assert safe_solve_with_mask(torch.ones(4, 4, device=device, dtype=dtype), A)[2].all()+ assert safe_solve_with_mask(torch.ones(len(A), 4, device=device, dtype=dtype), A)[2].all()++ def test_regular_matrix_at_an_extreme_scale_is_valid_under_trace_5507(self, device, dtype):+ # Under capture the inverse is the adjugate over the determinant, and their entries overflow or underflow+ # the dtype long before the inverse does: ``1e13 * I`` of order 4 reads ``inf / inf`` in float32 and+ # ``1e-13 * I`` reads ``x / 0``, so the traced mask rejected both matrices that eager accepted. The adjugate+ # is now taken on a row- and column-scaled copy, and the traced verdict and inverse are the eager ones.+ if dtype not in (torch.float32, torch.float64):+ pytest.skip("half input is decided in float32")+ A = _regular_at_extreme_scales(device, dtype)+ expected_inverse, expected_mask = safe_inverse_with_mask(A)+ traced = torch.jit.trace(safe_inverse_with_mask, (A,), check_trace=False)+ inverse, mask = traced(A)+ assert mask.tolist() == expected_mask.tolist() == [True] * len(A)+ assert torch.isfinite(inverse).all()+ assert_close(inverse, expected_inverse, atol=0.0, rtol=1e-5 if dtype == torch.float32 else 1e-12)++ @pytest.mark.parametrize("n", [2, 3, 4])+ def test_nonfinite_matrix_is_invalid_under_trace_5507(self, device, dtype, n, monkeypatch):+ # The traced inverse balances the matrix by exponents read as indices into a table of powers of two. A NaN+ # entry has a NaN exponent, and a NaN cast to int64 is 0 on arm64 but the most negative int64 on x86 and+ # CUDA, out of the table's range; the exponent of a NaN or infinite row or column is 0 instead. The traced+ # mask then rejects the matrix and its row is the identity, as in eager mode.+ from kornia.core import utils++ if dtype not in (torch.float32, torch.float64):+ pytest.skip("half input is decided in float32")+ exponents = []+ times_power_of_two = utils._times_power_of_two++ def spy(x, exponent):+ exponents.append(exponent)+ return times_power_of_two(x, exponent)++ monkeypatch.setattr(utils, "_times_power_of_two", spy)+ eye = torch.eye(n, device=device, dtype=dtype)+ A = torch.stack([eye * 2, eye, eye])+ A[1, 0, 1] = float("nan")+ A[2, 0, 1] = float("inf")+ inverse, mask = torch.jit.trace(safe_inverse_with_mask, (A,), check_trace=False)(A)+ assert mask.tolist() == safe_inverse_with_mask(A)[1].tolist() == [True, False, False]+ assert torch.equal(inverse[1:], eye.expand(2, n, n))+ assert exponents+ assert all(torch.isfinite(e).all() for e in exponents)def test_rule_calls_a_zero_row_or_column_singular(self, device, dtype):# The rule divides each row and column by its largest magnitude; a zero row or column has to stay