Skip to content

Make the polar AD rules a lot more GPU friendly - #331

Open
kshyatt wants to merge 13 commits into
mainfrom
ksh/polar
Open

kshyatt wants to merge 13 commits into
mainfrom
ksh/polar

Conversation

@kshyatt

@kshyatt kshyatt commented Nov 20, 2026

Copy link
Copy Markdown
Member

After staring at the polar PBs/PFs I convinced myself the input to sylvester is 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 using eigh which 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.

@kshyatt
kshyatt requested a review from Jutho November 20, 2026 10:09
@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.40659% with 12 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...ixAlgebraKitEnzymeExt/MatrixAlgebraKitEnzymeExt.jl 84.09% 7 Missing ⚠️
...gebraKitMooncakeExt/MatrixAlgebraKitMooncakeExt.jl 91.52% 5 Missing ⚠️
Files with missing lines Coverage Δ
...ixAlgebraKitAMDGPUExt/MatrixAlgebraKitAMDGPUExt.jl 87.35% <ø> (+2.91%) ⬆️
...MatrixAlgebraKitCUDAExt/MatrixAlgebraKitCUDAExt.jl 100.00% <ø> (ø)
src/MatrixAlgebraKit.jl 100.00% <ø> (ø)
src/common/pullbacks.jl 95.83% <100.00%> (+1.23%) ⬆️
src/implementations/polar.jl 96.22% <100.00%> (ø)
src/pullbacks/polar.jl 100.00% <100.00%> (ø)
src/pullbacks/svd.jl 95.47% <100.00%> (+0.44%) ⬆️
src/pushforwards/polar.jl 100.00% <100.00%> (ø)
...gebraKitMooncakeExt/MatrixAlgebraKitMooncakeExt.jl 72.67% <91.52%> (+1.79%) ⬆️
...ixAlgebraKitEnzymeExt/MatrixAlgebraKitEnzymeExt.jl 78.76% <84.09%> (+0.85%) ⬆️

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/common/pullbacks.jl Resolved
Comment thread src/pullbacks/polar.jl Resolved
@kshyatt

kshyatt commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

@Jutho is there more to do here, or do we feel ok merging?

@Jutho Jutho left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/common/pullbacks.jl
Comment on lines +17 to +21
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', Δ

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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, Δ

Comment thread src/common/pullbacks.jl
Comment on lines +27 to +31
isnull = abs.(d) .< tol
any(isnull) || return X
Y = U' * X * U
Y .*= isnull .& transpose(isnull)
return mul!(X, U, Y * U', -1, 1)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Suggested change
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did this ever work before?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I choose not to question it

if !isempty(P)
S .= sqrt.(S)
USsqrt = rmul!(U, S)
Ssqrt = reshape(sqrt.(diagview(S)), 1, :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if that would also work:

Suggested change
Ssqrt = reshape(sqrt.(diagview(S)), 1, :)
Ssqrt = transpose(sqrt.(diagview(S)))

Comment thread src/pullbacks/polar.jl
Comment on lines 38 to 46
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/pullbacks/svd.jl
# (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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be more performant or is it the same?

Suggested change
ΔX⊥ = ΔX - X * XᴴΔX
ΔX⊥ = mul!(copy(ΔX), X, XᴴΔX, -1, 1)

Comment thread src/pullbacks/svd.jl
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))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also only take out the part that is not gauge dependent, i.e. the hermitian part, as follows:

Suggested change
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)

Comment thread src/pushforwards/polar.jl
Comment on lines +11 to 15
Δ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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of this could again go into an if clause:

Suggested change
Δ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

Comment thread src/pushforwards/polar.jl
Comment on lines +32 to 36
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Comment thread src/pullbacks/svd.jl
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₂))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Not sure why this was not included in the initial rules.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants