Repository navigation
Make the polar AD rules a lot more GPU friendly #331
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
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,59 +1,101 @@ | ||
| """ | ||
| left_polar_pullback!(ΔA, A, WP, ΔWP) | ||
| left_polar_pullback!( | ||
| ΔA, A, WP, ΔWP; | ||
| rank_atol::Real = default_pullback_rank_atol(WP[2]), | ||
| gauge_atol::Real = default_pullback_gauge_atol(ΔWP...) | ||
| ) | ||
|
|
||
| Adds the pullback from the left polar decomposition of `A` to `ΔA` given the output `WP` and | ||
| cotangent `ΔWP` of `left_polar(A)`. | ||
|
|
||
| Eigenvalues of `P` smaller than `rank_atol` are treated as zero, in which case `A` is rank | ||
| deficient and `W` is not uniquely determined on the null space of `P`. Components of `ΔW` | ||
| along this null space are discarded. The block of `ΔP` within the null space, where `P` | ||
| is not differentiable, is discarded as well. | ||
| """ | ||
| function left_polar_pullback!(ΔA::AbstractMatrix, A, WP, ΔWP; kwargs...) | ||
| function left_polar_pullback!( | ||
| ΔA::AbstractMatrix, A, WP, ΔWP; | ||
| rank_atol::Real = default_pullback_rank_atol(WP[2]), | ||
| gauge_atol::Real = default_pullback_gauge_atol(ΔWP...) | ||
| ) | ||
| # Extract the Polar components | ||
| W, P = WP | ||
|
|
||
| # Extract and check the cotangents | ||
| ΔW, ΔP = ΔWP | ||
| D, U = eigh_full(P) | ||
|
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. One could of course ask the question, if we compute the polar via the svd, then we already have
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. That's true and I got tunnel vision on the case that needed |
||
| d = diagview(D) | ||
| if !iszerotangent(ΔP) | ||
| ΔP = project_hermitian(ΔP) | ||
| ΔP = _remove_null_block!(ΔP, U, d, rank_atol) | ||
| end | ||
| M = zero(P) | ||
| !iszerotangent(ΔW) && mul!(M, W', ΔW, 1, 1) | ||
| !iszerotangent(ΔP) && mul!(M, ΔP, P, -1, 1) | ||
| C = _sylvester(P, P, M' - M) | ||
| C, Δgauge = _sylvester_psd(U, d, M - M', rank_atol) | ||
| !iszerotangent(ΔP) && (C .+= ΔP) | ||
| ΔA = mul!(ΔA, W, C, 1, 1) | ||
| if !iszerotangent(ΔW) | ||
| ΔWP = ΔW / P | ||
| WdΔWP = W' * ΔWP | ||
| ΔWP = mul!(ΔWP, W, WdΔWP, -1, 1) | ||
| ΔA .+= ΔWP | ||
| # (1 - W * W') * ΔW * pinv(P) | ||
| ΔWU = ΔW * U | ||
| WᴴΔWU = W' * ΔWU | ||
| ΔWU = mul!(ΔWU, W, WᴴΔWU, -1, 1) | ||
| Δgauge = max(Δgauge, maximum(abs, (abs.(transpose(d)) .< rank_atol) .* ΔWU; init = zero(real(eltype(ΔWU))))) | ||
| ΔWU .*= transpose(inv_safe.(d, rank_atol)) | ||
| ΔA = mul!(ΔA, ΔWU, U', 1, 1) | ||
| end | ||
| Δgauge ≤ gauge_atol || | ||
| @warn "`left_polar` cotangents sensitive to gauge choice: (|Δgauge| = $Δgauge)" | ||
| return ΔA | ||
| end | ||
|
|
||
| """ | ||
| right_polar_pullback!(ΔA, A, PWᴴ, ΔPWᴴ) | ||
| right_polar_pullback!( | ||
| ΔA, A, PWᴴ, ΔPWᴴ; | ||
| rank_atol::Real = default_pullback_rank_atol(PWᴴ[1]), | ||
| gauge_atol::Real = default_pullback_gauge_atol(ΔPWᴴ...) | ||
| ) | ||
|
|
||
| Adds the pullback from the left polar decomposition of `A` to `ΔA` given the output `PWᴴ` | ||
| Adds the pullback from the right polar decomposition of `A` to `ΔA` given the output `PWᴴ` | ||
| and cotangent `ΔPWᴴ` of `right_polar(A)`. | ||
|
|
||
| Eigenvalues of `P` smaller than `rank_atol` are treated as zero, in which case `A` is rank | ||
| deficient and `Wᴴ` is not uniquely determined on the null space of `P`. Components of `ΔWᴴ` | ||
| along this null space are discarded. The block of `ΔP` within the null space, where `P` is | ||
| not differentiable, is discarded as well. | ||
| """ | ||
| function right_polar_pullback!(ΔA::AbstractMatrix, A, PWᴴ, ΔPWᴴ; kwargs...) | ||
| function right_polar_pullback!( | ||
| ΔA::AbstractMatrix, A, PWᴴ, ΔPWᴴ; | ||
| rank_atol::Real = default_pullback_rank_atol(PWᴴ[1]), | ||
| gauge_atol::Real = default_pullback_gauge_atol(ΔPWᴴ...) | ||
| ) | ||
| # Extract the Polar components | ||
| P, Wᴴ = PWᴴ | ||
|
|
||
| # Extract and check the cotangents | ||
| ΔP, ΔWᴴ = ΔPWᴴ | ||
| D, U = eigh_full(P) | ||
| d = diagview(D) | ||
| if !iszerotangent(ΔP) | ||
| ΔP = project_hermitian(ΔP) | ||
| ΔP = _remove_null_block!(ΔP, U, d, rank_atol) | ||
| end | ||
| M = zero(P) | ||
| !iszerotangent(ΔWᴴ) && mul!(M, ΔWᴴ, Wᴴ', 1, 1) | ||
| !iszerotangent(ΔP) && mul!(M, P, ΔP, -1, 1) | ||
| C = _sylvester(P, P, M' - M) | ||
| C, Δgauge = _sylvester_psd(U, d, M - M', rank_atol) | ||
| !iszerotangent(ΔP) && (C .+= ΔP) | ||
| ΔA = mul!(ΔA, C, Wᴴ, 1, 1) | ||
| if !iszerotangent(ΔWᴴ) | ||
| PΔWᴴ = P \ ΔWᴴ | ||
| PΔWᴴW = PΔWᴴ * Wᴴ' | ||
| PΔWᴴ = mul!(PΔWᴴ, PΔWᴴW, Wᴴ, -1, 1) | ||
| ΔA .+= PΔWᴴ | ||
| # pinv(P) * ΔWᴴ * (1 - Wᴴ' * Wᴴ) | ||
| UᴴΔWᴴ = U' * ΔWᴴ | ||
| UᴴΔWᴴW = UᴴΔWᴴ * Wᴴ' | ||
| UᴴΔWᴴ = mul!(UᴴΔWᴴ, UᴴΔWᴴW, Wᴴ, -1, 1) | ||
| Δgauge = max(Δgauge, maximum(abs, (abs.(d) .< rank_atol) .* UᴴΔWᴴ; init = zero(real(eltype(UᴴΔWᴴ))))) | ||
| UᴴΔWᴴ .*= inv_safe.(d, rank_atol) | ||
| ΔA = mul!(ΔA, U, UᴴΔWᴴ, 1, 1) | ||
| end | ||
| Δgauge ≤ gauge_atol || | ||
| @warn "`right_polar` cotangents sensitive to gauge choice: (|Δgauge| = $Δgauge)" | ||
| return ΔA | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,41 @@ | ||
| function left_polar_pushforward!(ΔA, A, WP, ΔWP; kwargs...) | ||
| function left_polar_pushforward!( | ||
| ΔA, A, WP, ΔWP; rank_atol::Real = default_pullback_rank_atol(WP[2]), kwargs... | ||
| ) | ||
| W, P = WP | ||
| ΔW, ΔP = ΔWP | ||
| D, U = eigh_full(P) | ||
| d = diagview(D) | ||
| mul!(ΔP, adjoint(W), ΔA, +1, 0) | ||
| K̇ = _sylvester(P, P, adjoint(ΔP) - ΔP) | ||
| mul!(ΔW, ΔA, inv(P), +1, 0) | ||
| K̇, = _sylvester_psd(U, d, ΔP - adjoint(ΔP), rank_atol) | ||
| # ΔA * pinv(P) | ||
| ΔAU = ΔA * U | ||
| ΔAU .*= transpose(inv_safe.(d, rank_atol)) | ||
| mul!(ΔW, ΔAU, adjoint(U), +1, 0) | ||
| WᴴdAiP = W' * ΔW | ||
| mul!(ΔW, W, WᴴdAiP, -1, +1) | ||
| ΔW = mul!(ΔW, W, K̇, +1, +1) | ||
| ΔP = mul!(ΔP, K̇, P, -1, +1) | ||
| ΔP = _remove_null_block!(ΔP, U, d, rank_atol) | ||
| return (ΔW, ΔP) | ||
| end | ||
|
|
||
| function right_polar_pushforward!(ΔA, A, PWᴴ, ΔPWᴴ; kwargs...) | ||
| function right_polar_pushforward!( | ||
| ΔA, A, PWᴴ, ΔPWᴴ; rank_atol::Real = default_pullback_rank_atol(PWᴴ[1]), kwargs... | ||
| ) | ||
| P, Wᴴ = PWᴴ | ||
| ΔP, ΔWᴴ = ΔPWᴴ | ||
| D, U = eigh_full(P) | ||
| d = diagview(D) | ||
| mul!(ΔP, ΔA, adjoint(Wᴴ), +1, 0) | ||
| K̇ = _sylvester(P, P, adjoint(ΔP) - ΔP) | ||
| mul!(ΔWᴴ, inv(P), ΔA, +1, 0) | ||
| K̇, = _sylvester_psd(U, d, ΔP - adjoint(ΔP), rank_atol) | ||
| # pinv(P) * ΔA | ||
| UᴴΔA = U' * ΔA | ||
| UᴴΔA .*= inv_safe.(d, rank_atol) | ||
| mul!(ΔWᴴ, U, UᴴΔA, +1, 0) | ||
| iPdAW = ΔWᴴ * Wᴴ' | ||
| mul!(ΔWᴴ, iPdAW, Wᴴ, -1, +1) | ||
| ΔWᴴ = mul!(ΔWᴴ, K̇, Wᴴ, +1, +1) | ||
| ΔP = mul!(ΔP, P, K̇, -1, +1) | ||
| ΔP = _remove_null_block!(ΔP, U, d, rank_atol) | ||
| return (ΔWᴴ, ΔP) | ||
| 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.
There is a slight mismatch (by a factor of 2) between
abs.(dd) .< tolused here, andused in the next function.