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

improve inference in LinearAlgebra/symmetric #371

Merged
merged 3 commits into from
Feb 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
61 changes: 37 additions & 24 deletions src/rulesets/LinearAlgebra/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ end

function rrule(T::Type{<:LinearAlgebra.HermOrSym}, A::AbstractMatrix, uplo)
Ω = T(A, uplo)
function HermOrSym_pullback(ΔΩ)
return (NO_FIELDS, _symherm_back(typeof(Ω), ΔΩ, Ω.uplo), DoesNotExist())
@inline function HermOrSym_pullback(ΔΩ)
return (NO_FIELDS, _symherm_back(typeof(Ω), ΔΩ, uplo), DoesNotExist())
end
return Ω, HermOrSym_pullback
end
Expand All @@ -26,7 +26,7 @@ function rrule(TM::Type{<:Matrix}, A::LinearAlgebra.HermOrSym)
TA = _symhermtype(A)
T∂A = TA{eltype(ΔΩ),typeof(ΔΩ)}
uplo = A.uplo
∂A = T∂A(_symherm_back(A, ΔΩ, uplo), uplo)
∂A = T∂A(_symherm_back(typeof(A), ΔΩ, Symbol(uplo)), uplo)
return NO_FIELDS, ∂A
end
return TM(A), Matrix_pullback
Expand All @@ -44,33 +44,46 @@ function _symherm_forward(A, ΔA)
end

# for Ω = HermOrSym(A, uplo), pull back ΔΩ to get ∂A
_symherm_back(::Type{<:Symmetric}, ΔΩ, uplo) = _symmetric_back(ΔΩ, uplo)
function _symherm_back(::Type{<:Hermitian}, ΔΩ::AbstractMatrix{<:Real}, uplo)
return _symmetric_back(ΔΩ, uplo)
@inline function _symherm_back(::Type{T}, ΔΩ, uplo::Symbol) where {T}
if T <: Symmetric
return _symmetric_back(ΔΩ, uplo)
elseif T <: Hermitian
if ΔΩ isa AbstractMatrix{<:Real}
return _symmetric_back(ΔΩ, uplo)
else
return _hermitian_back(ΔΩ, uplo)
end
end
error()
end
_symherm_back(::Type{<:Hermitian}, ΔΩ, uplo) = _hermitian_back(ΔΩ, uplo)
_symherm_back(Ω, ΔΩ, uplo) = _symherm_back(typeof(Ω), ΔΩ, uplo)

function _symmetric_back(ΔΩ, uplo)
@inline function _symmetric_back(ΔΩ, uplo::Symbol)
if ΔΩ isa Diagonal
return ΔΩ
elseif ΔΩ isa LinearAlgebra.AbstractTriangular
if istriu(ΔΩ)
return Matrix(uplo === :U ? ΔΩ : transpose(ΔΩ))
else
return Matrix(uplo === :U ? transpose(ΔΩ) : ΔΩ)
end
end
L, U, D = LowerTriangular(ΔΩ), UpperTriangular(ΔΩ), Diagonal(ΔΩ)
return uplo == 'U' ? U .+ transpose(L) - D : L .+ transpose(U) - D
return uplo === :U ? U .+ transpose(L) - D : L .+ transpose(U) - D
end
_symmetric_back(ΔΩ::Diagonal, uplo) = ΔΩ
_symmetric_back(ΔΩ::UpperTriangular, uplo) = Matrix(uplo == 'U' ? ΔΩ : transpose(ΔΩ))
_symmetric_back(ΔΩ::LowerTriangular, uplo) = Matrix(uplo == 'U' ? transpose(ΔΩ) : ΔΩ)

function _hermitian_back(ΔΩ, uplo)
L, U, rD = LowerTriangular(ΔΩ), UpperTriangular(ΔΩ), real.(Diagonal(ΔΩ))
return uplo == 'U' ? U .+ L' - rD : L .+ U' - rD
end
_hermitian_back(ΔΩ::Diagonal, uplo) = real.(ΔΩ)
function _hermitian_back(ΔΩ::LinearAlgebra.AbstractTriangular, uplo)
∂UL = ΔΩ .- Diagonal(_extract_imag.(diag(ΔΩ)))
return if istriu(ΔΩ)
return Matrix(uplo == 'U' ? ∂UL : ∂UL')
else
return Matrix(uplo == 'U' ? ∂UL' : ∂UL)
@inline function _hermitian_back(ΔΩ, uplo::Symbol)
if ΔΩ isa Diagonal
return real.(ΔΩ)
elseif ΔΩ isa LinearAlgebra.AbstractTriangular
∂UL = ΔΩ .- Diagonal(_extract_imag.(diag(ΔΩ)))
if istriu(ΔΩ)
return Matrix(uplo === :U ? ∂UL : ∂UL')
else
return Matrix(uplo === :U ? ∂UL' : ∂UL)
end
end
L, U, rD = LowerTriangular(ΔΩ), UpperTriangular(ΔΩ), real.(Diagonal(ΔΩ))
return uplo === :U ? U .+ L' - rD : L .+ U' - rD
end

#####
Expand Down
19 changes: 15 additions & 4 deletions test/rulesets/LinearAlgebra/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,33 @@
@testset "rrule" begin
# on old versions of julia this combination doesn't infer but we don't care as
# it infers fine on modern versions.
check_inferred = !(VERSION <= v"1.5" && T <: ComplexF64 && SymHerm <: Hermitian)
check_inferred = true#!(VERSION <= v"1.5" && T <: ComplexF64 && SymHerm <: Hermitian)
simeonschaub marked this conversation as resolved.
Show resolved Hide resolved

x = randn(T, N, N)
∂x = randn(T, N, N)
ΔΩ = randn(T, N, N)
@testset "back(::$MT)" for MT in (Matrix, LowerTriangular, UpperTriangular)
_ΔΩ = MT(ΔΩ)
rrule_test(
SymHerm, MT(ΔΩ), (x, ∂x), (uplo, nothing);
check_inferred = check_inferred
SymHerm, _ΔΩ, (x, ∂x), (uplo, nothing);
check_inferred=false,
)
if check_inferred
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
@inferred (function (SymHerm, x, ΔΩ, ::Val{uplo}) where {uplo}
return rrule(SymHerm, x, uplo)[2](ΔΩ)
end)(SymHerm, x, MT(ΔΩ), Val(uplo))
end
end
@testset "back(::Diagonal)" begin
rrule_test(
SymHerm, Diagonal(ΔΩ), (x, Diagonal(∂x)), (uplo, nothing);
check_inferred = check_inferred
check_inferred=false,
simeonschaub marked this conversation as resolved.
Show resolved Hide resolved
)
if check_inferred
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
@inferred (function (SymHerm, x, ΔΩ, ::Val{uplo}) where {uplo}
return rrule(SymHerm, x, uplo)[2](ΔΩ)
end)(SymHerm, x, Diagonal(ΔΩ), Val(uplo))
end
end
end
end
Expand Down