-
Notifications
You must be signed in to change notification settings - Fork 7
Some basic svd forward rules and tests #247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kshyatt
wants to merge
17
commits into
main
Choose a base branch
from
ksh/svd_fwd
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
33ab47b
Some basic svd forward rules and tests
838ecab
Try to fix Enzyme?
be9afda
Use GPU safe rank calculation
883dadb
Another fix for GPU
1e03d65
Some more fixes
kshyatt d8c9427
Update src/pushforwards/svd.jl
kshyatt ec2a674
A little cleanup
kshyatt 9d0f666
Some small fixes again
kshyatt 8940756
Use sylvester fallback
kshyatt aa3a2ee
Fix diagms
kshyatt 267886d
Add a one! method for vectors
64c849f
Use UniformScaling for diagm
dc7bbeb
some changes
Jutho c5cf3de
more gauge fixing in eig/eigh
Jutho 17c2158
Get stuff working on CUDA
kshyatt c89643d
Fix missing import
kshyatt fbcd972
Run more complex test cases
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,77 @@ | ||||||||||||||||||||||||||
| function svd_pushforward!(ΔA, A, USVᴴ, ΔUSVᴴ, ind = Colon(); rank_atol = default_pullback_rank_atol(A), kwargs...) | ||||||||||||||||||||||||||
| U, Smat, Vᴴ = USVᴴ | ||||||||||||||||||||||||||
| m, n = size(U, 1), size(Vᴴ, 2) | ||||||||||||||||||||||||||
| (m, n) == size(ΔA) || throw(DimensionMismatch("size of ΔA ($(size(ΔA))) does not match size of U*S*Vᴴ ($m, $n)")) | ||||||||||||||||||||||||||
| minmn = min(m, n) | ||||||||||||||||||||||||||
| S = diagview(Smat) | ||||||||||||||||||||||||||
| ΔU, ΔS, ΔVᴴ = ΔUSVᴴ | ||||||||||||||||||||||||||
| r = svd_rank(S; rank_atol) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| U₁ = view(U, :, 1:r) | ||||||||||||||||||||||||||
| S₁ = view(S, 1:r) | ||||||||||||||||||||||||||
| V₁ᴴ = view(Vᴴ, 1:r, :) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # compact region | ||||||||||||||||||||||||||
| V₁ = adjoint(V₁ᴴ) | ||||||||||||||||||||||||||
| ΔAV₁ = ΔA * V₁ | ||||||||||||||||||||||||||
| UᴴΔAV₁ = U₁' * ΔAV₁ | ||||||||||||||||||||||||||
| if !iszerotangent(ΔS) | ||||||||||||||||||||||||||
| ΔS₁ = view(diagview(ΔS), 1:r) | ||||||||||||||||||||||||||
| ΔS₁ .= real.(diagview(UᴴΔAV₁)) | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
| if !iszerotangent(ΔU) || !iszerotangent(ΔVᴴ) | ||||||||||||||||||||||||||
| hUᴴΔAV₁ = inv_safe.(transpose(S₁) .- S₁) .* project_hermitian(UᴴΔAV₁) | ||||||||||||||||||||||||||
| aUᴴΔAV₁ = inv_safe.(transpose(S₁) .+ S₁) .* project_antihermitian(UᴴΔAV₁) | ||||||||||||||||||||||||||
|
Comment on lines
+23
to
+24
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think below only the sum and difference are actually used, we could use a kernel like MatrixAlgebraKit.jl/src/implementations/polar.jl Lines 209 to 220 in e271b4a
|
||||||||||||||||||||||||||
| if !iszerotangent(ΔU) | ||||||||||||||||||||||||||
| ΔU₁ = view(ΔU, :, 1:r) | ||||||||||||||||||||||||||
| K̇ = hUᴴΔAV₁ + aUᴴΔAV₁ | ||||||||||||||||||||||||||
| mul!(ΔU₁, U₁, K̇) | ||||||||||||||||||||||||||
| if m > r | ||||||||||||||||||||||||||
| ΔAV₁ = mul!(ΔAV₁, U₁, UᴴΔAV₁, -1, 1) | ||||||||||||||||||||||||||
| ΔU₁ .+= ΔAV₁ ./ transpose(S₁) | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
| if size(U, 2) > r # these columns of U are undetermined, but U' * U̇ should be antihermitian | ||||||||||||||||||||||||||
| U₂ = view(U, :, (r + 1):size(U, 2)) | ||||||||||||||||||||||||||
| ΔU₁ᴴU₂ = ΔU₁' * U₂ | ||||||||||||||||||||||||||
| ΔU₂ = view(ΔU, :, (r + 1):size(U, 2)) | ||||||||||||||||||||||||||
| mul!(ΔU₂, U₁, ΔU₁ᴴU₂, -1, 0) | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
| if !iszerotangent(ΔVᴴ) | ||||||||||||||||||||||||||
| ΔV₁ᴴ = view(ΔVᴴ, 1:r, :) | ||||||||||||||||||||||||||
| Ṁ = hUᴴΔAV₁ - aUᴴΔAV₁ | ||||||||||||||||||||||||||
| mul!(ΔV₁ᴴ, Ṁ', V₁ᴴ) | ||||||||||||||||||||||||||
| if n > r | ||||||||||||||||||||||||||
| UᴴΔA₁ = U₁' * ΔA | ||||||||||||||||||||||||||
| UᴴΔA₁ = mul!(UᴴΔA₁, UᴴΔAV₁, V₁ᴴ, -1, 1) | ||||||||||||||||||||||||||
| ΔV₁ᴴ .+= S₁ .\ UᴴΔA₁ | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
| if size(Vᴴ, 1) > r # these rows of Vᴴ are undetermined, but V * V̇ should be antihermitian | ||||||||||||||||||||||||||
| V₂ᴴ = view(Vᴴ, (r + 1):size(Vᴴ, 1), :) | ||||||||||||||||||||||||||
| V₂ᴴΔV₁ = V₂ᴴ * ΔV₁ᴴ' | ||||||||||||||||||||||||||
| ΔV₂ᴴ = view(ΔVᴴ, (r + 1):size(Vᴴ, 1), :) | ||||||||||||||||||||||||||
| mul!(ΔV₂ᴴ, V₂ᴴΔV₁, V₁ᴴ, -1, 0) | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
| if eltype(U) <: Complex && !iszerotangent(ΔU) && !iszerotangent(ΔVᴴ) # fix gauge for `gaugefix!` compatibility | ||||||||||||||||||||||||||
| _, I = findmax(abs, U₁; dims = 1) | ||||||||||||||||||||||||||
| infinitesimal_phases = imag.(ΔU₁[I] ./ U₁[I]) | ||||||||||||||||||||||||||
| ΔU₁ .-= im .* U₁ .* infinitesimal_phases | ||||||||||||||||||||||||||
| ΔV₁ᴴ .+= im .* transpose(infinitesimal_phases) .* V₁ᴴ | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
| return (ΔU, ΔS, ΔVᴴ) | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # TODO | ||||||||||||||||||||||||||
| #=function svd_trunc_pushforward!(ΔA, A, USVᴴ, ΔUSVᴴ, ind; rank_atol = default_pullback_rank_atol(A), kwargs...) | ||||||||||||||||||||||||||
| end=# | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| function svd_vals_pushforward!( | ||||||||||||||||||||||||||
| ΔA, A, USVᴴ, ΔS, ind = Colon(); | ||||||||||||||||||||||||||
| rank_atol::Real = default_pullback_rank_atol(USVᴴ[2]), | ||||||||||||||||||||||||||
| degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]) | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| ΔUSVᴴ = (nothing, diagonal(ΔS), nothing) | ||||||||||||||||||||||||||
| return svd_pushforward!(ΔA, A, USVᴴ, ΔUSVᴴ, ind; rank_atol, degeneracy_atol) | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.