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 sqrt(::Diagonal) #509

Merged
merged 8 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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 = "1.9.0"
version = "1.10.0"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
10 changes: 10 additions & 0 deletions src/rulesets/LinearAlgebra/structured.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ end
##### `Diagonal`
#####

_sqrt_pullback(Δ::Tangent, y) = return NoTangent(), Δ / (2y)
_sqrt_pullback(Δ::Diagonal, y) = return NoTangent(), Δ / (2y)
_sqrt_pullback(Δ::Matrix, y) = return NoTangent(), Diagonal(Δ) / (2y)
mzgubic marked this conversation as resolved.
Show resolved Hide resolved
_sqrt_pullback(Δ::AbstractThunk, y) = return _sqrt_pullback(unthunk(Δ), y)
function ChainRulesCore.rrule(::typeof(sqrt), d::Diagonal)
y = sqrt(d)
sqrt_pullback(Δ) = _sqrt_pullback(Δ, y)
return y, sqrt_pullback
end

# these functions are defined outside the rrule because otherwise type inference breaks
# see https://github.com/JuliaLang/julia/issues/40990
_Diagonal_pullback(ȳ::AbstractMatrix) = return (NoTangent(), diag(ȳ)) # should we emit a warning here? this shouldn't be called if project works right
Expand Down
4 changes: 4 additions & 0 deletions test/rulesets/LinearAlgebra/structured.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@
end
end
end
@testset "sqrt(::Diagonal)" begin
test_rrule(sqrt, Diagonal([1.0, 2, 3]))
test_rrule(sqrt, Diagonal([1.0, 2, 3]); output_tangent=Diagonal(rand(3)) + zeros(3,3))
end
@testset "$f, $T" for
f in (Adjoint, adjoint, Transpose, transpose),
T in (Float64, ComplexF64)
Expand Down