diff --git a/src/mariepy/implicit.py b/src/mariepy/implicit.py index 1341205..ad4a944 100644 --- a/src/mariepy/implicit.py +++ b/src/mariepy/implicit.py @@ -648,10 +648,8 @@ def sample(cells: torch.Tensor, dofs: torch.Tensor | None) -> torch.Tensor: dofs = torch.sort( torch.randperm(n_coil, generator=generator)[:rank].to(grid.device) ).values - body_basis, _, _ = torch.linalg.svd(sample(region_cells, dofs), full_matrices=False) - coil_basis, _, _ = torch.linalg.svd( - sample(cells, None).conj().transpose(0, 1), full_matrices=False - ) + body_basis = _left_singular_vectors(sample(region_cells, dofs)) + coil_basis = _left_singular_vectors(sample(cells, None).conj().transpose(0, 1)) def fit() -> tuple: rows = _row_numbers(grid, cells, n_components) @@ -679,21 +677,16 @@ def fit() -> tuple: if fresh_dofs.numel() == 0 and fresh_cells.numel() == 0: break if fresh_dofs.numel(): - body_basis, _, _ = torch.linalg.svd( - torch.cat([body_basis, sample(region_cells, fresh_dofs)], dim=1), - full_matrices=False, + body_basis = _left_singular_vectors( + torch.cat([body_basis, sample(region_cells, fresh_dofs)], dim=1) ) dofs = torch.sort(torch.cat([dofs, fresh_dofs])).values if fresh_cells.numel(): - coil_basis, _, _ = torch.linalg.svd( + coil_basis = _left_singular_vectors( torch.cat( - [ - coil_basis, - sample(fresh_cells, None).conj().transpose(0, 1), - ], + [coil_basis, sample(fresh_cells, None).conj().transpose(0, 1)], dim=1, - ), - full_matrices=False, + ) ) cells = torch.sort(torch.cat([cells, fresh_cells])).values @@ -771,6 +764,26 @@ def _settled( return bool(tol * torch.linalg.vector_norm(now[:rank]) > reached) +def _left_singular_vectors(matrix: torch.Tensor) -> torch.Tensor: + """Give the left singular vectors of ``matrix``, strongest first. + + A basis extended by columns it already spans has many singular values at one + and many near zero, which LAPACK's divide and conquer SVD can fail to + converge on, depending on the CPU. The matrix is reduced by QR first, and if + the SVD of its triangular factor still fails, the vectors come from the + eigendecomposition of that factor's Gram matrix instead. The two agree on + every vector whose singular value stands above the square root of machine + precision, which is all the cross approximation keeps. + """ + q, r = torch.linalg.qr(matrix) + try: + u, _, _ = torch.linalg.svd(r, full_matrices=False) + except torch.linalg.LinAlgError: + _, u = torch.linalg.eigh(r @ r.conj().transpose(0, 1)) + u = u.flip(-1) + return q @ u + + def _outside(candidate: torch.Tensor, held: torch.Tensor) -> torch.Tensor: """Those of ``candidate`` that ``held`` does not already carry.""" fresh = torch.unique(candidate) diff --git a/tests/test_implicit.py b/tests/test_implicit.py index 398e188..186ae97 100644 --- a/tests/test_implicit.py +++ b/tests/test_implicit.py @@ -326,3 +326,36 @@ def test_the_cross_approximation_follows_the_tolerance_it_is_given(device): left[tol] = implicit._missed(operator, coupling, coil_side, 2) assert left[tol] <= 10 * tol assert left[1e-4] < left[1e-2] + + +def _extended_basis(device): + generator = torch.Generator().manual_seed(0) + tall = torch.randn(400, 12, dtype=torch.complex128, generator=generator) + basis = torch.linalg.qr(tall).Q + spanned = basis[:, :5] @ torch.randn( + 5, 6, dtype=torch.complex128, generator=generator + ) + return basis.to(device), torch.cat([basis, spanned], dim=1).to(device) + + +def _assert_strongest_vectors_span(vectors, basis): + assert vectors.shape == (basis.shape[0], 18) + gram = vectors.conj().transpose(0, 1) @ vectors + assert torch.allclose(gram, torch.eye(18, dtype=gram.dtype, device=gram.device)) + strongest = vectors[:, :12] + projected = strongest @ (strongest.conj().transpose(0, 1) @ basis) + assert torch.allclose(projected, basis, atol=1e-10) + + +def test_an_extended_basis_keeps_its_span_in_its_strongest_singular_vectors(device): + basis, extended = _extended_basis(device) + _assert_strongest_vectors_span(implicit._left_singular_vectors(extended), basis) + + +def test_a_failed_svd_still_gives_the_strongest_singular_vectors(device, monkeypatch): + def unconverged(*args, **kwargs): + raise torch.linalg.LinAlgError("linalg.svd: The algorithm failed to converge") + + basis, extended = _extended_basis(device) + monkeypatch.setattr(torch.linalg, "svd", unconverged) + _assert_strongest_vectors_span(implicit._left_singular_vectors(extended), basis)