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

Project ZeroTangent to natural tangent for some number types #574

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/projection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ end
# understands, including a mix of Zeros & reals. Other cases, we just let through:
(project::ProjectTo{<:Number})(dx::Tangent{<:Complex}) = project(Complex(dx.re, dx.im))
(::ProjectTo{<:Number})(dx::Tangent{<:Number}) = dx
(::ProjectTo{T})(::ZeroTangent) where {T<:Real} = zero(T)

# Arrays
# If we don't have a more specialized `ProjectTo` rule, we just assume that there is
Expand Down
11 changes: 5 additions & 6 deletions test/projection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ struct NoSuperType end
@test ProjectTo(1.0)(2) === 2.0

# Tangents
ProjectTo(1.0f0 + 2im)(Tangent{ComplexF64}(; re=1, im=NoTangent())) ===
1.0f0 + 0.0f0im

@test 1.0 === ProjectTo(1.0)(Tangent{ComplexF64}(; re=1, im=NoTangent()))
complex_tangent = Tangent{ComplexF64}(; re=1, im=NoTangent())
@test ProjectTo(1.0f0 + 2im)(complex_tangent) === 1.0f0 + 0.0f0im
@test ProjectTo(1.0)(complex_tangent) === 1.0
Comment on lines +40 to +42
Copy link
Member

Choose a reason for hiding this comment

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

unreleated fix?

@test ProjectTo(1.0)(ZeroTangent()) === 0.0
end

@testset "Dual" begin # some weird Real subtype that we should basically leave alone
Expand Down Expand Up @@ -212,7 +212,7 @@ struct NoSuperType end
@test ProjectTo(I)(123) === NoTangent()
@test ProjectTo(2 * I)(I * 3im) === 0.0 * I
@test ProjectTo((4 + 5im) * I)(Tangent{typeof(im * I)}(; λ = 6)) === (6.0 + 0.0im) * I
@test ProjectTo(7 * I)(Tangent{typeof(2I)}()) == ZeroTangent()
@test ProjectTo(7 * I)(Tangent{typeof(2I)}()) == 0.0I
Copy link
Member

Choose a reason for hiding this comment

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

hmm not sure that this is a good thing.
Does this also happen to other matrix types?

end

@testset "LinearAlgebra: $adj vectors" for adj in [transpose, adjoint]
Expand Down Expand Up @@ -413,7 +413,6 @@ struct NoSuperType end
@test pb(ZeroTangent()) isa AbstractZero # was a method ambiguity!

# all projectors preserve Zero, and specific type, via one fallback method:
@test ProjectTo(pi)(ZeroTangent()) === ZeroTangent()
@test ProjectTo(pi)(NoTangent()) === NoTangent()
pv = ProjectTo(sprand(30, 0.3))
@test pv(ZeroTangent()) === ZeroTangent()
Expand Down
2 changes: 1 addition & 1 deletion test/rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ _second(t) = Base.tuple_type_head(Base.tuple_type_tail(t))
frx, nice_pushforward = frule((dself, 1), nice, 1)
@test nice_pushforward === ZeroTangent()
rrx, nice_pullback = rrule(nice, 1)
@test (NoTangent(), ZeroTangent()) === nice_pullback(1)
@test (NoTangent(), 0.0) === nice_pullback(1)

# Test that these run. Do not care about numerical correctness.
@test frule((nothing, 1.0, 1.0, 1.0), varargs_function, 0.5, 0.5, 0.5) == (1.5, 3.0)
Expand Down