Skip to content

Commit

Permalink
Merge pull request #791 from JuliaDiff/dw/svdvals
Browse files Browse the repository at this point in the history
Generalize `rrule` for `svdvals`
  • Loading branch information
oxinabox authored Apr 16, 2024
2 parents f9f0722 + 8f74dea commit d11755b
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 88 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChainRules"
uuid = "082447d4-558c-5d27-93f4-14fc19e9eca2"
version = "1.63.0"
version = "1.64.0"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
16 changes: 16 additions & 0 deletions src/rulesets/LinearAlgebra/factorization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,22 @@ function svd_rev(USV::SVD, Ū, s̄, V̄)
return Ā
end

#####
##### `svdvals`
#####

function rrule(::typeof(svdvals), A::AbstractMatrix{<:Number})
F = svd(A)
U = F.U
Vt = F.Vt
project_A = ProjectTo(A)
function svdvals_pullback(s̄)
=isa AbstractZero ?: Diagonal(unthunk(s̄))
return (NoTangent(), project_A(U ** Vt))
end
return F.S, svdvals_pullback
end

#####
##### `eigen`
#####
Expand Down
22 changes: 0 additions & 22 deletions src/rulesets/LinearAlgebra/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -277,28 +277,6 @@ function _svd_eigvals_sign!(c, U, V)
return c
end

#####
##### `svdvals`
#####

# NOTE: rrule defined because `svdvals` calls mutating `svdvals!` internally.
# can be removed when mutation is supported by reverse-mode AD packages
function rrule(::typeof(svdvals), A::LinearAlgebra.RealHermSymComplexHerm{<:BLAS.BlasReal,<:StridedMatrix})
λ, back = rrule(eigvals, A)
S = abs.(λ)
p = sortperm(S; rev=true)
permute!(S, p)
function svdvals_pullback(ΔS)
∂λ = real.(ΔS)
invpermute!(∂λ, p)
∂λ .*= sign.(λ)
_, ∂A = back(∂λ)
return NoTangent(), unthunk(∂A)
end
svdvals_pullback(ΔS::AbstractZero) = (NoTangent(), ΔS)
return S, svdvals_pullback
end

#####
##### matrix functions
#####
Expand Down
127 changes: 84 additions & 43 deletions test/rulesets/LinearAlgebra/factorization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,61 +102,102 @@ end
end
end
end
@testset "svd" begin
for n in [4, 6, 10], m in [3, 5, 9]
@testset "($n x $m) svd" begin
X = randn(n, m)
test_rrule(svd, X; atol=1e-6, rtol=1e-6)
end
end

for n in [4, 6, 10], m in [3, 5, 10]
@testset "($n x $m) getproperty" begin
X = randn(n, m)
F = svd(X)
rand_adj = adjoint(rand(reverse(size(F.V))...))
@testset "singular value decomposition" begin
@testset "svd" begin
for n in [4, 6, 10], m in [3, 5, 9]
@testset "($n x $m) svd" begin
X = randn(n, m)
test_rrule(svd, X; atol=1e-6, rtol=1e-6)
end
end

test_rrule(getproperty, F, :U; check_inferred=false)
test_rrule(getproperty, F, :S; check_inferred=false)
test_rrule(getproperty, F, :Vt; check_inferred=false)
test_rrule(getproperty, F, :V; check_inferred=false, output_tangent=rand_adj)
for n in [4, 6, 10], m in [3, 5, 10]
@testset "($n x $m) getproperty" begin
X = randn(n, m)
F = svd(X)
rand_adj = adjoint(rand(reverse(size(F.V))...))

test_rrule(getproperty, F, :U; check_inferred=false)
test_rrule(getproperty, F, :S; check_inferred=false)
test_rrule(getproperty, F, :Vt; check_inferred=false)
test_rrule(
getproperty, F, :V; check_inferred=false, output_tangent=rand_adj
)
end
end
end

@testset "Thunked inputs" begin
X = randn(4, 3)
F, dX_pullback = rrule(svd, X)
for p in [:U, :S, :V, :Vt]
Y, dF_pullback = rrule(getproperty, F, p)
= randn(size(Y)...)
@testset "Thunked inputs" begin
X = randn(4, 3)
F, dX_pullback = rrule(svd, X)
for p in [:U, :S, :V, :Vt]
Y, dF_pullback = rrule(getproperty, F, p)
= randn(size(Y)...)

_, dF_unthunked, _ = dF_pullback(Ȳ)

_, dF_unthunked, _ = dF_pullback(Ȳ)
# helper to let us check how things are stored.
p_access = p == :V ? :Vt : p
backing_field(c, p) = getproperty(ChainRulesCore.backing(c), p_access)
@assert !(backing_field(dF_unthunked, p) isa AbstractThunk)

# helper to let us check how things are stored.
p_access = p == :V ? :Vt : p
backing_field(c, p) = getproperty(ChainRulesCore.backing(c), p_access)
@assert !(backing_field(dF_unthunked, p) isa AbstractThunk)
dF_thunked = map(f -> Thunk(() -> f), dF_unthunked)
@assert backing_field(dF_thunked, p) isa AbstractThunk

dself_thunked, dX_thunked = dX_pullback(dF_thunked)
dself_unthunked, dX_unthunked = dX_pullback(dF_unthunked)
@test dself_thunked == dself_unthunked
@test dX_thunked == dX_unthunked
end
end

dF_thunked = map(f->Thunk(()->f), dF_unthunked)
@assert backing_field(dF_thunked, p) isa AbstractThunk
@testset "Helper functions" begin
X = randn(10, 10)
Y = randn(10, 10)
@test ChainRules._mulsubtrans!!(copy(X), Y) Y .* (X - X')
@test ChainRules._eyesubx!(copy(X)) I - X

dself_thunked, dX_thunked = dX_pullback(dF_thunked)
dself_unthunked, dX_unthunked = dX_pullback(dF_unthunked)
@test dself_thunked == dself_unthunked
@test dX_thunked == dX_unthunked
Z = randn(Float32, 10, 10)
result = ChainRules._mulsubtrans!!(copy(Z), Y)
@test result Y .* (Z - Z')
@test eltype(result) == Float64
end
end

@testset "Helper functions" begin
X = randn(10, 10)
Y = randn(10, 10)
@test ChainRules._mulsubtrans!!(copy(X), Y) Y .* (X - X')
@test ChainRules._eyesubx!(copy(X)) I - X
@testset "svdvals" begin
for n in [4, 6, 10]
for m in [3, 5, 9]
@testset "($n x $m) svdvals" begin
X = randn(n, m)
test_rrule(svdvals, X; atol=1e-6, rtol=1e-6)
end
end

@testset "rrule for svdvals(::$SymHerm{$T}) ($n x $n, uplo=$uplo)" for SymHerm in
(
Symmetric, Hermitian
),
T in (SymHerm === Symmetric ? (Float64,) : (Float64, ComplexF64)),
uplo in (:L, :U)

A, ΔS = randn(T, n, n), randn(n)
symA = SymHerm(A, uplo)

Z = randn(Float32, 10, 10)
result = ChainRules._mulsubtrans!!(copy(Z), Y)
@test result Y .* (Z - Z')
@test eltype(result) == Float64
S = svdvals(symA)
S_ad, back = @inferred rrule(svdvals, symA)
@test S_ad S # inexact because rrule uses svd not svdvals
∂self, ∂symA = @inferred back(ΔS)
@test ∂self === NoTangent()
@test ∂symA isa typeof(symA)
@test ∂symA.uplo == symA.uplo

# pull the cotangent back to A to test against finite differences
∂A = rrule(SymHerm, A, uplo)[2](∂symA)[2]
@test ∂A j′vp(_fdm, A -> svdvals(SymHerm(A, uplo)), ΔS, A)[1]

@test @inferred(back(ZeroTangent())) == (NoTangent(), ZeroTangent())
end
end
end
end

Expand Down
22 changes: 0 additions & 22 deletions test/rulesets/LinearAlgebra/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -275,28 +275,6 @@
@test @maybe_inferred(back(ZeroTangent())) == (NoTangent(), ZeroTangent())
@test @maybe_inferred(back(CT())) == (NoTangent(), ZeroTangent())
end

@testset "rrule for svdvals(::$SymHerm{$T}) uplo=$uplo" for SymHerm in (Symmetric, Hermitian),
T in (SymHerm === Symmetric ? (Float64,) : (Float64, ComplexF64)),
uplo in (:L, :U)

A, ΔS = randn(T, n, n), randn(n)
symA = SymHerm(A, uplo)

S = svdvals(symA)
S_ad, back = @maybe_inferred rrule(svdvals, symA)
@test S_ad S # inexact because rrule uses svd not svdvals
∂self, ∂symA = @maybe_inferred back(ΔS)
@test ∂self === NoTangent()
@test ∂symA isa typeof(symA)
@test ∂symA.uplo == symA.uplo

# pull the cotangent back to A to test against finite differences
∂A = rrule(SymHerm, A, uplo)[2](∂symA)[2]
@test ∂A j′vp(_fdm, A -> svdvals(SymHerm(A, uplo)), ΔS, A)[1]

@test @maybe_inferred(back(ZeroTangent())) == (NoTangent(), ZeroTangent())
end
end

@testset "Symmetric/Hermitian matrix functions" begin
Expand Down

2 comments on commit d11755b

@oxinabox
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/104995

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.64.0 -m "<description of version>" d11755b8170e5829d20445dae4f14b98b0f698e3
git push origin v1.64.0

Please sign in to comment.