Skip to content

Commit

Permalink
Merge pull request #773 from MasonProtter/patch-1
Browse files Browse the repository at this point in the history
Use handwritten rules for `zero` and `one`
  • Loading branch information
oxinabox authored Jan 25, 2024
2 parents e37c9a6 + cc93409 commit e569283
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
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.59.0"
version = "1.59.1"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
25 changes: 22 additions & 3 deletions src/rulesets/Base/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,30 @@
# that also have FastMath versions.

@scalar_rule copysign(y, x) (ifelse(signbit(x)!=signbit(y), -one(y), +one(y)), NoTangent())

@scalar_rule one(x) ZeroTangent()
@scalar_rule zero(x) ZeroTangent()
@scalar_rule transpose(x) true

# `zero`

function frule((_, _), ::typeof(zero), x)
return (zero(x), ZeroTangent())
end

function rrule(::typeof(zero), x)
zero_pullback(_) = (NoTangent(), ZeroTangent())
return (zero(x), zero_pullback)
end

# `one`

function frule((_, _), ::typeof(one), x)
return (one(x), ZeroTangent())
end

function rrule(::typeof(one), x)
one_pullback(_) = (NoTangent(), ZeroTangent())
return (one(x), one_pullback)
end

# `adjoint`

frule((_, Δz), ::typeof(adjoint), z::Number) = (z', Δz')
Expand Down
10 changes: 10 additions & 0 deletions test/rulesets/Base/base.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
@testset "base.jl" begin
@testset "zero/one" begin
for f in [zero, one]
for x in [1.0, 1.0im, [10.0+im 11.0-im; 12.0+2im 13.0-3im]]
test_frule(f, x)
test_rrule(f, x)
end
end
test_frule(zero, [1.0, 2.0, 3.0])
test_rrule(zero, [1.0, 2.0, 3.0])
end
@testset "copysign" begin
# don't go too close to zero as the numerics may jump over it yielding wrong results
@testset "at $y" for y in (-1.1, 0.1, 100.0)
Expand Down

2 comments on commit e569283

@oxinabox
Copy link
Member Author

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

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.59.1 -m "<description of version>" e569283d59d08a65244bed9c37f49fdefbe541ab
git push origin v1.59.1

Please sign in to comment.