Repository navigation
Conversation
|
@Jutho is there more to do here, or do we feel ok merging? |
Jutho
left a comment
There was a problem hiding this comment.
Ok, so far I've only reviewed the generic pullback and pushforward rules in the main module. Once this is processed, I will look at the actual extension packages.
| Y = U' * C * U | ||
| dd = d .+ transpose(d) | ||
| Δ = maximum(abs, (abs.(dd ./ 2) .< tol) .* Y; init = zero(real(eltype(Y)))) | ||
| Y .*= inv_safe.(dd, tol) | ||
| return U * Y * U', Δ |
There was a problem hiding this comment.
| Y = U' * C * U | |
| dd = d .+ transpose(d) | |
| Δ = maximum(abs, (abs.(dd ./ 2) .< tol) .* Y; init = zero(real(eltype(Y)))) | |
| Y .*= inv_safe.(dd, tol) | |
| return U * Y * U', Δ | |
| CU = C * U | |
| Y = U' * CU | |
| dd = d .+ transpose(d) | |
| Δ = maximum(abs, (abs.(dd ./ 2) .< tol) .* Y; init = zero(real(eltype(Y)))) | |
| Y .*= inv_safe.(dd, tol) | |
| UY = mul!(CU, U, Y) | |
| UYUd = mul!(Y, Y, U') | |
| return UYUd, Δ |
| isnull = abs.(d) .< tol | ||
| any(isnull) || return X | ||
| Y = U' * X * U | ||
| Y .*= isnull .& transpose(isnull) | ||
| return mul!(X, U, Y * U', -1, 1) |
There was a problem hiding this comment.
| isnull = abs.(d) .< tol | |
| any(isnull) || return X | |
| Y = U' * X * U | |
| Y .*= isnull .& transpose(isnull) | |
| return mul!(X, U, Y * U', -1, 1) | |
| isnull = abs.(d) .< tol | |
| any(isnull) || return X | |
| XU = X * U | |
| Y = U' * XU | |
| Y .*= isnull .& transpose(isnull) | |
| return mul!(X, U, mul!(XU, Y, U'), -1, 1) |
An alternative would also be to directly only keep the complement, which is possible with one less allocation, if we are happy with temporarily destroying X halfway through
| isnull = abs.(d) .< tol | |
| any(isnull) || return X | |
| Y = U' * X * U | |
| Y .*= isnull .& transpose(isnull) | |
| return mul!(X, U, Y * U', -1, 1) | |
| isnull = abs.(d) .< tol | |
| any(isnull) || return X | |
| XU = X * U | |
| Y = mul!(X, U', XU) # same memory as `X` | |
| Y .*= 1 .- (isnull .& transpose(isnull)) | |
| return mul!(X, mul!(XU, U, Y), U') |
| W, P = WP | ||
| W = mul!(W, U, Vᴴ) | ||
| if !isempty(P) | ||
| S .= sqrt.(S) |
There was a problem hiding this comment.
I choose not to question it
| if !isempty(P) | ||
| S .= sqrt.(S) | ||
| USsqrt = rmul!(U, S) | ||
| Ssqrt = reshape(sqrt.(diagview(S)), 1, :) |
There was a problem hiding this comment.
Not sure if that would also work:
| Ssqrt = reshape(sqrt.(diagview(S)), 1, :) | |
| Ssqrt = transpose(sqrt.(diagview(S))) |
| 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 |
There was a problem hiding this comment.
Maybe we can also put a m > n check in this if clause. It doesn't contribute if m == n, since then 1 - W * W' is just zero.
| # (rotations of the span of `X` into other directions) are gauge dependent. | ||
| function _null_gauge_dependence(X, ΔX) | ||
| XᴴΔX = X' * ΔX | ||
| ΔX⊥ = ΔX - X * XᴴΔX |
There was a problem hiding this comment.
Would this be more performant or is it the same?
| ΔX⊥ = ΔX - X * XᴴΔX | |
| ΔX⊥ = mul!(copy(ΔX), X, XᴴΔX, -1, 1) |
| XᴴΔX = X' * ΔX | ||
| ΔX⊥ = ΔX - X * XᴴΔX | ||
| init = zero(real(eltype(XᴴΔX))) | ||
| return max(maximum(abs, project_antihermitian(XᴴΔX); init), maximum(abs, ΔX⊥; init)) |
There was a problem hiding this comment.
We could also only take out the part that is not gauge dependent, i.e. the hermitian part, as follows:
| return max(maximum(abs, project_antihermitian(XᴴΔX); init), maximum(abs, ΔX⊥; init)) | |
| XᴴΔX = X' * ΔX | |
| ΔX⊥ = mul!(copy(ΔX), X, project_hermitian!(XᴴΔX), -1, 1) | |
| init = zero(real(eltype(XᴴΔX))) | |
| return maximum(abs, ΔX⊥; init) |
| Δ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) |
There was a problem hiding this comment.
All of this could again go into an if clause:
| Δ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) | |
| if size(W, 1) > size(W, 2) | |
| Δ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) | |
| end |
| 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) |
There was a problem hiding this comment.
| 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) | |
| if size(Wᴴ, 2) > size(Wᴴ, 1) | |
| 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) | |
| end |
| U₁ᴴΔU₂ = U₁' * ΔU₂ | ||
| mul!(ΔU₀, U₂, U₁ᴴΔU₂', -1, 1) | ||
| mul!(ΔU₂, U₁, U₁ᴴΔU₂, -1, 1) | ||
| ΔgaugeU = max(ΔgaugeU, _null_gauge_dependence(U₂, ΔU₂)) |
There was a problem hiding this comment.
Nice. Not sure why this was not included in the initial rules.
After staring at the polar PBs/PFs I convinced myself the input to
sylvesteris always Hermitian, so we can get away without using the Schur decomposition which GPUs don't support yet. This means we can get away with just usingeighwhich we do support. For the forward rules I added some logic similar to what we did for QR/LQ and focus only on the gauge invariant part. This also lets us handle rank-deficient inputs which is nice. I didn't go through super intensively to optimize this yet so there may be low hanging performance fruits.