Repository navigation
Conversation
Tridiagonal, SymTridiagonal, Bidiagonal and PermutedDimsArray are now vectorized like dense matrices, and their from_vec projects onto their structure. This makes them compatible with tangents of other types (e.g. dense output cotangents in j′vp), fixes the double counting of the off-diagonals of SymTridiagonal (same as #164), fixes the silently transposed cotangents of PermutedDimsArray, and adds support for Bidiagonal. Symmetric/Hermitian reuse the from_vec of their parent to project onto its structure, which adds support for e.g. Diagonal, triangular and Tridiagonal parents. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Follow-up to #252 (stacked on it; I'll retarget to
mainonce #252 is merged).Motivation
j′vpis only correct iffrom_vecmaps cotangents correctly, and it uses the samefrom_vecfor primals and cotangents. With dense coordinates this holds iffrom_vecis the orthogonal projection onto the matrices of the given structure. Moreover,j′vppairsto_vec(f(x))withto_vec(ȳ), so all matrix types have to use the same coordinates (dense, column-major) for tangents of different types to be compatible (cf. #186).Diagonal, triangular,Symmetric/Hermitian,Adjoint/Transposeand sparse matrices are already vectorized this way, but the following types were not. On 0.12.34:Tridiagonalj′vpwith a dense output cotangent throws aDimensionMismatchSymTridiagonalgrad(sum, x)has 2.0 on the off-diagonals; alsoDimensionMismatchBidiagonalExpected a struct type(theuplo::Charfield)PermutedDimsArrayj′vp(fdm, identity, A, PermutedDimsArray(A, (2, 1)))silently returnsA'instead ofAChanges
Tridiagonal,SymTridiagonal,BidiagonalandPermutedDimsArrayare vectorized densely (in the order of the array itself), and theirfrom_vecprojects onto their structure (forSymTridiagonal: the off-diagonals are averaged).Symmetric/Hermitianreuse thefrom_vecof their parent to project the symmetrized matrix onto the parent's structure. This adds support for e.g.Diagonal, triangular andTridiagonalparents, which failed before (Fix double counting of Symmetric/Hermitian off-diagonals #252 did not change this). Since this relies on the parent being vectorized densely, anArgumentErroris thrown otherwise (e.g. for custom matrix types vectorized by the generic struct fallback). Parents whosefrom_vecreturns a different type (e.g. views, which are vectorized viacopy) yield aSymmetric/Hermitianof that type.Behaviour changes
The length of
to_vec(x)changes forTridiagonalandSymTridiagonal, and the order changes forPermutedDimsArray, so code that compares values in the vectorized space would be affected. The tests of ChainRulesTestUtils pass locally with this branch.Tests
j′vp/jvpthroughidentitywith dense tangents for all four types (real and complex), comparing with the orthogonal projection.Diagonal,UpperTriangular,LowerTriangularandTridiagonalparents ofSymmetric/Hermitian.ArgumentError.Longer term, the projections could be replaced by
ChainRulesCore.ProjectTo.🤖 Generated with Claude Code