Repository navigation
Fix svd() for non-symmetric dense/triangular operators and negative diagonals - #159
Open
flyingdoubleG wants to merge 1 commit into
Open
flyingdoubleG wants to merge 1 commit into
flyingdoubleG wants to merge 1 commit into
Conversation
LinearOperator._svd goes through a symmetric eigendecomposition, so it is only valid for symmetric matrices. DenseLinearOperator and TriangularLinearOperator did not override it, so svd() and torch.linalg.svd() silently returned a wrong decomposition for non-symmetric dense and triangular operators (and failed for rectangular ones). Kronecker products of such factors were wrong as well. DiagLinearOperator._svd built V by multiplying the identity evecs elementwise with a column of signs, which drops the signs, so the SVD of a diagonal with negative entries did not reconstruct the matrix. Use torch.linalg.svd for dense and triangular operators, build V as diag(signs) for diagonals, and enable the svd test for triangular operators.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
LinearOperator.svd()is documented as computingA = U S V^Tfor a generalA(M x N). The default_svd, however, goes through_symeig, so it is only valid for symmetric matrices. The code comment says so: "Will need to overwrite this function for non-psd LinearOperators". Two common non-symmetric operators do not override it, sosvd()andtorch.linalg.svd(op)silently return a wrong decomposition for them. A third operator has a separate sign bug.svd()reconstruction error on mainDenseLinearOperator(torch.randn(5, 5))DenseLinearOperator(torch.randn(5, 3))RuntimeError(eighon a non-square matrix)TriangularLinearOperator(...)KroneckerProductLinearOperatorof non-symmetric dense factorsKroneckerProductTriangularLinearOperatorDiagLinearOperator(torch.tensor([1., -2., 3.]))For the diagonal,
_svdbuildsV = evecs * sign(evals).unsqueeze(-1), whereevecsis aConstantDiagLinearOperator(the identity). That elementwise product keeps only the diagonal of the(N, 1)column, so the signs are lost andU S V^T = diag(|evals|).Changes
DenseLinearOperator._svdandTriangularLinearOperator._svdusetorch.linalg.svd. For dense operators this isfull_matrices=False, so rectangular operators work. The Kronecker_svd, which combines the factors' SVDs, is then correct for these factors as well.DiagLinearOperator._svdreturnsV = DiagLinearOperator(signs).Tests
TestDenseLinearOperator.test_svd_not_symmetric: square, rectangular (both orientations) and batched non-symmetric matrices, throughsvd()andtorch.linalg.svd.TestDiagLinearOperator.test_svd_negative_diagonal: inherited by the batch, multi-batch and Kronecker-diagonal test cases.TestTriangularLinearOperator.test_svdwas overridden withpass. I removed the override, so the generictest_svd(forward and backward) now runs for lower and upper triangular operators.On main, 7 of these tests fail. With this PR they pass. The full suite gives 5050 passed and 1 failed. The failure is
test_psd_safe_cholesky_psd, which also fails on main without these changes. I ranufmtandflake8on the changed files.