Skip to content

Commit dd47793

Browse files
committed
Fix and test AD rules for SVD
1 parent 978effe commit dd47793

7 files changed

Lines changed: 34 additions & 16 deletions

File tree

‎ext/MatrixAlgebraKitAMDGPUExt/MatrixAlgebraKitAMDGPUExt.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,6 @@ function _sylvester(A::AnyROCMatrix, B::AnyROCMatrix, C::AnyROCMatrix)
185185
return ROCArray(hX)
186186
end
187187

188-
svd_rank(S::AnyROCVector, rank_atol) = findlast(s -> s ≥ rank_atol, S)
188+
svd_rank(S::AnyROCVector; rank_atol = MatrixAlgebraKit.default_pullback_rank_atol(S)) = findlast(s -> s ≥ rank_atol, S)
189189

190190
end

‎ext/MatrixAlgebraKitCUDAExt/MatrixAlgebraKitCUDAExt.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,6 @@ function _sylvester(A::AnyCuMatrix, B::AnyCuMatrix, C::AnyCuMatrix)
197197
return CuArray(hX)
198198
end
199199

200-
svd_rank(S::AnyCuVector, rank_atol) = findlast(s -> s ≥ rank_atol, S)
200+
svd_rank(S::AnyCuVector; rank_atol = MatrixAlgebraKit.default_pullback_rank_atol(S)) = findlast(s -> s ≥ rank_atol, S)
201201

202202
end

‎ext/MatrixAlgebraKitEnzymeExt/MatrixAlgebraKitEnzymeExt.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ function EnzymeRules.reverse(
274274
Aval = nothing
275275
USVᴴval = something(cache_USVᴴ, USVᴴ.val)
276276
if !isa(A, Const)
277-
svd_pullback!(A.dval, Aval, USVᴴval, dUSVᴴ, ind)
277+
svd_pullback!(A.dval, Aval, USVᴴval, dUSVᴴ, collect(ind))
278278
end
279279
!isa(USVᴴ, Const) && make_zero!(USVᴴ.dval)
280280
return (nothing, nothing, nothing)

‎ext/MatrixAlgebraKitMooncakeExt/MatrixAlgebraKitMooncakeExt.jl‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ using LinearAlgebra
1515

1616
Mooncake.tangent_type(::Type{<:MatrixAlgebraKit.AbstractAlgorithm}) = Mooncake.NoTangent
1717

18+
# needed for GPU tests because Mooncake can't differentiate through CUDA kernels
19+
@is_primitive Mooncake.DefaultCtx Mooncake.ReverseMode Tuple{typeof(zero!), AbstractArray}
20+
function Mooncake.rrule!!(::CoDual{typeof(zero!)}, A_dA::CoDual)
21+
A, dA = arrayify(A_dA)
22+
Ac = copy(A)
23+
zero!(A)
24+
function zero_adjoint(::NoRData)
25+
copy!(A, Ac)
26+
zero!(dA)
27+
return NoRData(), NoRData()
28+
end
29+
return A_dA, zero_adjoint
30+
end
31+
1832
# two-argument in-place factorizations like LQ, QR, EIG
1933
for (f!, f, pb, adj) in (
2034
(:qr_full!, :qr_full, :qr_pullback!, :qr_adjoint),
@@ -614,7 +628,7 @@ function Mooncake.rrule!!(::CoDual{typeof(svd_trunc!)}, A_dA::CoDual, USVᴴ_dUS
614628
_warn_pullback_truncerror(dϵ)
615629

616630
# compute pullbacks
617-
svd_pullback!(dA, Ac, USVᴴ, dUSVᴴtrunc, ind)
631+
svd_pullback!(dA, Ac, USVᴴ, dUSVᴴtrunc, collect(ind))
618632
zero!.(dUSVᴴtrunc) # since this is allocated in this function this is probably not required
619633
zero!.(dUSVᴴ)
620634

@@ -671,7 +685,7 @@ function Mooncake.rrule!!(::CoDual{typeof(svd_trunc)}, A_dA::CoDual, alg_dalg::C
671685
dUSVᴴtrunc = last.(arrayify.(USVᴴtrunc, Base.front(Mooncake.tangent(USVᴴtrunc_dUSVᴴtrunc))))
672686
function svd_trunc_adjoint((_, _, _, dϵ)::Tuple{NoRData, NoRData, NoRData, Real})
673687
_warn_pullback_truncerror(dϵ)
674-
svd_pullback!(dA, A, USVᴴ, dUSVᴴtrunc, ind)
688+
svd_pullback!(dA, A, USVᴴ, dUSVᴴtrunc, collect(ind))
675689
zero!.(dUSVᴴtrunc) # since this is allocated in this function this is probably not required
676690
return ntuple(Returns(NoRData()), 3)
677691
end
@@ -741,7 +755,7 @@ function Mooncake.rrule!!(::CoDual{typeof(svd_trunc_no_error!)}, A_dA::CoDual, U
741755
dUSVᴴtrunc = last.(arrayify.(USVᴴtrunc, Mooncake.tangent(USVᴴtrunc_dUSVᴴtrunc)))
742756
function svd_trunc_adjoint(::NoRData)
743757
# compute pullbacks
744-
svd_pullback!(dA, Ac, USVᴴ, dUSVᴴtrunc, ind)
758+
svd_pullback!(dA, Ac, USVᴴ, dUSVᴴtrunc, collect(ind))
745759
zero!.(dUSVᴴ)
746760

747761
# restore state
@@ -794,7 +808,7 @@ function Mooncake.rrule!!(::CoDual{typeof(svd_trunc_no_error)}, A_dA::CoDual, al
794808
# define pullback
795809
dUSVᴴtrunc = last.(arrayify.(USVᴴtrunc, Mooncake.tangent(USVᴴtrunc_dUSVᴴtrunc)))
796810
function svd_trunc_adjoint(::NoRData)
797-
svd_pullback!(dA, A, USVᴴ, dUSVᴴtrunc, ind)
811+
svd_pullback!(dA, A, USVᴴ, dUSVᴴtrunc, collect(ind))
798812
zero!.(dUSVᴴtrunc) # since this is allocated in this function this is probably not required
799813
return ntuple(Returns(NoRData()), 3)
800814
end

‎src/pullbacks/svd.jl‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,16 @@ function check_and_prepare_svd_cotangents(
8484
bc = Base.broadcasted(S₁', S₁, aUᴴΔU₁, aVᴴΔV₁) do s₁, s₂, u, v
8585
return abs(s₁ - s₂) < degeneracy_atol ? u + v : zero(u) + zero(v)
8686
end
87-
Δgauge = max(Δgauge, norm(bc, Inf))
87+
Δgauge = max(Δgauge, maximum(abs, bc))
8888

8989
if !iszerotangent(ΔSmat)
9090
ΔS = diagview(ΔSmat)
9191
length(indS) == length(ΔS) || throw(DimensionMismatch(lazy"length of selected S values ($(length(indS))) does not match length of ΔS ($(length(ΔS)))"))
9292
ΔS₁ = zero(S₁)
93-
for (j, i) in enumerate(indS)
94-
if i <= r
95-
ΔS₁[i] = real(ΔS[j])
96-
else
97-
Δgauge = max(Δgauge, abs(ΔS[j]))
98-
end
99-
end
93+
good_indS = findall(i -> i <= r, indS)
94+
bad_indS = setdiff(1:length(indS), good_indS)
95+
ΔS₁[indS[good_indS]] = real.(ΔS[good_indS])
96+
Δgauge = max(Δgauge, mapreduce(abs, max, ΔS[bad_indS]))
10097
else
10198
ΔS₁ = nothing
10299
end

‎test/mooncake/svd.jl‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,11 @@ for T in (BLASFloats..., GenericFloats...), n in (17, m, 23)
2020
TestSuite.test_mooncake_svd(AT, m; atol = m * n * TestSuite.precision(T), rtol = m * n * TestSuite.precision(T))
2121
end
2222
end
23+
if T ∈ BLASFloats
24+
TestSuite.test_mooncake_svd(CuMatrix{T}, (m, n); atol = m * n * TestSuite.precision(T), rtol = m * n * TestSuite.precision(T))
25+
if m == n
26+
AT = Diagonal{T, CuVector{T}}
27+
TestSuite.test_mooncake_svd(AT, m; atol = m * n * TestSuite.precision(T), rtol = m * n * TestSuite.precision(T))
28+
end
29+
end
2330
end

‎test/testsuite/mooncake/svd.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ function test_mooncake_svd_trunc(
141141

142142
@testset "trunctol" begin
143143
S = svd_vals(A)
144-
trunc = trunctol(atol = S[1] / 2)
144+
trunc = trunctol(atol = collect(S)[1] / 2)
145145
alg_trunc = TruncatedAlgorithm(alg, trunc)
146146

147147
USVᴴ, USVᴴtrunc, ΔUSVᴴ_arrays, ΔUSVᴴtrunc_arrays = ad_svd_trunc_setup(A, alg_trunc)

0 commit comments

Comments
 (0)