Skip to content

Commit

Permalink
Add type promotion rules for NoTangent and ZeroTangent, and add `…
Browse files Browse the repository at this point in the history
…eltype` for `NoTangent` (#682)

* Add promotion rules for ZeroTangent and NoTangent

* Make NoTangent have an eltype of itself.

* bump version
  • Loading branch information
BioTurboNick authored Sep 17, 2024
1 parent 9627bd6 commit 2f2c941
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChainRulesCore"
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
version = "1.24.0"
version = "1.25.0"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand Down
3 changes: 3 additions & 0 deletions src/tangent_types/abstract_zero.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Base.:/(z::AbstractZero, ::Any) = z

Base.convert(::Type{T}, x::AbstractZero) where {T<:Number} = zero(T)
# (::Type{T})(::AbstractZero, ::AbstractZero...) where {T<:Number} = zero(T)
Base.promote_rule(T::Type{<:Number}, S::Type{<:AbstractZero}) = T

(::Type{Complex})(x::AbstractZero, y::Real) = Complex(false, y)
(::Type{Complex})(x::Real, y::AbstractZero) = Complex(x, false)
Expand Down Expand Up @@ -92,6 +93,8 @@ end
"""
struct NoTangent <: AbstractZero end

Base.eltype(::Type{NoTangent}) = NoTangent

"""
zero_tangent(primal)
Expand Down
21 changes: 21 additions & 0 deletions test/tangent_types/abstract_zero.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@
@test convert(Float32, ZeroTangent()) === 0.0f0
@test convert(ComplexF64, ZeroTangent()) === 0.0 + 0.0im

@test promote_type(ZeroTangent, Bool) == Bool
@test promote_type(Bool, ZeroTangent) == Bool
@test promote_type(ZeroTangent, Int64) == Int64
@test promote_type(Int64, ZeroTangent) == Int64
@test promote_type(ZeroTangent, Float32) == Float32
@test promote_type(Float32, ZeroTangent) == Float32
@test promote_type(ZeroTangent, ComplexF64) == ComplexF64
@test promote_type(ComplexF64, ZeroTangent) == ComplexF64

@test z[1] === z
@test z[1:3] === z
@test z[1, 2] === z
Expand Down Expand Up @@ -110,6 +119,18 @@
@test dot(dne, 17.2) == dne
@test dot(11.9, dne) == dne

@test eltype(dne) === NoTangent
@test eltype(NoTangent) === NoTangent

@test promote_type(NoTangent, Bool) == Bool
@test promote_type(Bool, NoTangent) == Bool
@test promote_type(NoTangent, Int64) == Int64
@test promote_type(Int64, NoTangent) == Int64
@test promote_type(NoTangent, Float32) == Float32
@test promote_type(Float32, NoTangent) == Float32
@test promote_type(NoTangent, ComplexF64) == ComplexF64
@test promote_type(ComplexF64, NoTangent) == ComplexF64

@test ZeroTangent() + dne == dne
@test dne + ZeroTangent() == dne
@test ZeroTangent() - dne == dne
Expand Down

2 comments on commit 2f2c941

@simeonschaub
Copy link
Member

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/115383

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.25.0 -m "<description of version>" 2f2c941712f9e2cd11f476666a63dc462ed6440a
git push origin v1.25.0

Please sign in to comment.