-
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
base: main
Are you sure you want to change the base?
Changes from all commits
33ab47b
838ecab
be9afda
883dadb
1e03d65
d8c9427
ec2a674
9d0f666
8940756
aa3a2ee
267886d
64c849f
dc7bbeb
c5cf3de
17c2158
c89643d
fbcd972
dd2c5a3
36a22ca
10ca00a
0af8ec4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
|
|
||
| function has_equal_storage(A::Diagonal, B::Diagonal) | ||
| return diagview(A) === diagview(B) | ||
| end | ||
| function has_equal_storage(A::AbstractMatrix, B::AbstractMatrix) | ||
| return A === B | ||
| end | ||
|
|
||
| function has_equal_storage(A::Diagonal, B::AbstractVector) | ||
| return diagview(A) === B | ||
| end | ||
| function has_equal_storage(A::AbstractVector, B::Diagonal) | ||
| return A === diagview(B) | ||
| end | ||
| has_equal_storage(A::AbstractMatrix, B::AbstractVector) = false | ||
| has_equal_storage(A::AbstractVector, B::AbstractMatrix) = false |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| function svd_pushforward!( | ||
| ΔA, A, USVᴴ, ΔUSVᴴ, ind = Colon(); | ||
| rank_atol = default_pullback_rank_atol(USVᴴ[2]), | ||
| degeneracy_atol = default_pullback_rank_atol(USVᴴ[2]) | ||
| ) | ||
| 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) | ||
| zero!(ΔS) # make off-diagonal entries zero in case of full ΔS (svd_full!) | ||
| ΔS₁ = view(diagview(ΔS), 1:r) | ||
| ΔS₁ .= real.(diagview(UᴴΔAV₁)) | ||
| end | ||
| if !iszerotangent(ΔU) || !iszerotangent(ΔVᴴ) | ||
| hUᴴΔAV₁ = inv_safe.(transpose(S₁) .- S₁, degeneracy_atol) .* project_hermitian(UᴴΔAV₁) | ||
| aUᴴΔAV₁ = inv_safe.(transpose(S₁) .+ S₁, degeneracy_atol) .* project_antihermitian(UᴴΔAV₁) | ||
| 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] .* inv_safe.(U₁[I])) | ||
|
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 would be suprised if that is needed or makes a difference.
Member
Author
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 would be quite surprised too but I'm otherwise very confused where the |
||
| Δ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 | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit of a confusing comment: what exactly is missing?
Might be useful to keep track of this, in case it gets fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's again a situation of
mul!(::Diagonal{T, CuVector{T}}, [horrific view of adjoint of view], CuArray)which GPUArrays cannot dispatch onto at all.