Skip to content
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

Add rules for solutions to Sylvester and Lyapunov equations #384

Merged
merged 8 commits into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = "0.7.53"
version = "0.7.54"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
1 change: 1 addition & 0 deletions src/ChainRules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ include("rulesets/Statistics/statistics.jl")

include("rulesets/LinearAlgebra/utils.jl")
include("rulesets/LinearAlgebra/blas.jl")
include("rulesets/LinearAlgebra/lapack.jl")
include("rulesets/LinearAlgebra/dense.jl")
include("rulesets/LinearAlgebra/norm.jl")
include("rulesets/LinearAlgebra/matfun.jl")
Expand Down
83 changes: 83 additions & 0 deletions src/rulesets/LinearAlgebra/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,86 @@ function rrule(::typeof(pinv), A::AbstractMatrix{T}; kwargs...) where {T}
end
return Y, pinv_pullback
end

#####
##### `sylvester`
#####

# included because the primal uses `schur`, for which we don't have a rule

function frule(
(_, ΔA, ΔB, ΔC),
::typeof(sylvester),
A::StridedMatrix{T},
B::StridedMatrix{T},
C::StridedMatrix{T},
) where {T<:BlasFloat}
RA, QA = schur(A)
RB, QB = schur(B)
D = QA' * (C * QB)
Y, scale = LAPACK.trsyl!('N', 'N', RA, RB, D)
Ω = rmul!(QA * (Y * QB'), -inv(scale))
∂D = QA' * (mul!(muladd(ΔA, Ω, ΔC), Ω, ΔB, true, true) * QB)
∂Y, scale2 = LAPACK.trsyl!('N', 'N', RA, RB, ∂D)
∂Ω = rmul!(QA * (∂Y * QB'), -inv(scale2))
return Ω, ∂Ω
end

# included because the primal mutates and uses `schur` and LAPACK

function rrule(
::typeof(sylvester), A::StridedMatrix{T}, B::StridedMatrix{T}, C::StridedMatrix{T}
) where {T<:BlasFloat}
RA, QA = schur(A)
RB, QB = schur(B)
D = QA' * (C * QB)
Y, scale = LAPACK.trsyl!('N', 'N', RA, RB, D)
Ω = rmul!(QA * (Y * QB'), -inv(scale))
function sylvester_pullback(ΔΩ)
∂Ω = T <: Real ? real(ΔΩ) : ΔΩ
∂Y = QA' * (∂Ω * QB)
trans = T <: Complex ? 'C' : 'T'
∂D, scale2 = LAPACK.trsyl!(trans, trans, RA, RB, ∂Y)
∂Z = rmul!(QA * (∂D * QB'), -inv(scale2))
return NO_FIELDS, @thunk(∂Z * Ω'), @thunk(Ω' * ∂Z), @thunk(∂Z * inv(scale))
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should have a shorthand for inplaceable thunking multiplication.
Maybe @thunk should be smart in that way?

Not for this PR but just something to consider

Copy link
Member Author

Choose a reason for hiding this comment

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

That would be convenient. I haven't really put any effort into writing InplaceableThunks, since nothing supports them yet. Simple cases like this would be easy to transform but maybe could break? e.g. for the last thunk here, the macro would not know that scale is a number not a matrix. And is the case that we thunk just the multiplication common enough to make the code complexity worth it?

end
return Ω, sylvester_pullback
end

#####
##### `lyap`
#####

# included because the primal uses `schur`, for which we don't have a rule

function frule(
(_, ΔA, ΔC), ::typeof(lyap), A::StridedMatrix{T}, C::StridedMatrix{T}
) where {T<:BlasFloat}
R, Q = schur(A)
D = Q' * (C * Q)
Y, scale = LAPACK.trsyl!('N', T <: Complex ? 'C' : 'T', R, R, D)
Ω = rmul!(Q * (Y * Q'), -inv(scale))
∂D = Q' * (mul!(muladd(ΔA, Ω, ΔC), Ω, ΔA', true, true) * Q)
∂Y, scale2 = LAPACK.trsyl!('N', T <: Complex ? 'C' : 'T', R, R, ∂D)
∂Ω = rmul!(Q * (∂Y * Q'), -inv(scale2))
return Ω, ∂Ω
end

# included because the primal mutates and uses `schur` and LAPACK

function rrule(
::typeof(lyap), A::StridedMatrix{T}, C::StridedMatrix{T}
) where {T<:BlasFloat}
R, Q = schur(A)
D = Q' * (C * Q)
Y, scale = LAPACK.trsyl!('N', T <: Complex ? 'C' : 'T', R, R, D)
Ω = rmul!(Q * (Y * Q'), -inv(scale))
function lyap_pullback(ΔΩ)
∂Ω = T <: Real ? real(ΔΩ) : ΔΩ
∂Y = Q' * (∂Ω * Q)
∂D, scale2 = LAPACK.trsyl!(T <: Complex ? 'C' : 'T', 'N', R, R, ∂Y)
∂Z = rmul!(Q * (∂D * Q'), -inv(scale2))
return NO_FIELDS, @thunk(mul!(∂Z * Ω', ∂Z', Ω, true, true)), @thunk(∂Z * inv(scale))
end
return Ω, lyap_pullback
end
25 changes: 25 additions & 0 deletions src/rulesets/LinearAlgebra/lapack.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#####
##### `LAPACK.trsyl!`
#####

function ChainRules.frule(
(_, _, _, ΔA, ΔB, ΔC),
::typeof(LAPACK.trsyl!),
transa::AbstractChar,
transb::AbstractChar,
A::AbstractMatrix{T},
B::AbstractMatrix{T},
C::AbstractMatrix{T},
isgn::Int,
) where {T<:BlasFloat}
C, scale = LAPACK.trsyl!(transa, transb, A, B, C, isgn)
Y = (C, scale)
ΔAtrans = transa === 'T' ? transpose(ΔA) : (transa === 'C' ? ΔA' : ΔA)
ΔBtrans = transb === 'T' ? transpose(ΔB) : (transb === 'C' ? ΔB' : ΔB)
mul!(ΔC, ΔAtrans, C, -1, scale)
mul!(ΔC, C, ΔBtrans, -isgn, true)
ΔC, scale2 = LAPACK.trsyl!(transa, transb, A, B, ΔC, isgn)
rmul!(ΔC, inv(scale2))
∂Y = Composite{typeof(Y)}(ΔC, Zero())
return Y, ∂Y
end
18 changes: 18 additions & 0 deletions test/rulesets/LinearAlgebra/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,22 @@
test_frule(tr, randn(4, 4))
test_rrule(tr, randn(4, 4))
end
@testset "sylvester" begin
@testset "T=$T, m=$m, n=$n" for T in (Float64, ComplexF64), m in (2, 3), n in (1, 3)
A = randn(T, m, m)
B = randn(T, n, n)
C = randn(T, m, n)
test_frule(sylvester, A, B, C)
test_rrule(sylvester, A, B, C)
end
end
@testset "lyap" begin
n = 3
@testset "Float64" for T in (Float64, ComplexF64)
A = randn(T, n, n)
C = randn(T, n, n)
test_frule(lyap, A, C)
test_rrule(lyap, A, C)
end
end
end
27 changes: 27 additions & 0 deletions test/rulesets/LinearAlgebra/lapack.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@testset "LAPACK" begin
@testset "trsyl!" begin
@testset "T=$T, m=$m, n=$n, transa='$transa', transb='$transb', isgn=$isgn" for
T in (Float64, ComplexF64),
transa in (T <: Real ? ('N', 'C', 'T') : ('N', 'C')),
transb in (T <: Real ? ('N', 'C', 'T') : ('N', 'C')),
m in (2, 3),
n in (1, 3),
isgn in (1, -1)

# make A and B quasi upper-triangular (or upper-triangular for complex)
# and their tangents have the same sparsity pattern
A = schur(randn(T, m, m)).T
B = schur(randn(T, n, n)).T
C = randn(T, m, n)
test_frule(
LAPACK.trsyl!,
transa ⊢ nothing,
transb ⊢ nothing,
A ⊢ rand_tangent(A) .* (!iszero).(A), # Match sparsity pattern
B ⊢ rand_tangent(B) .* (!iszero).(B),
C,
isgn ⊢ nothing,
)
end
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ println("Testing ChainRules.jl")
include_test("rulesets/LinearAlgebra/symmetric.jl")
include_test("rulesets/LinearAlgebra/factorization.jl")
include_test("rulesets/LinearAlgebra/blas.jl")
include_test("rulesets/LinearAlgebra/lapack.jl")
end
println()

Expand Down