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

Preserve input types for various rules #89

Merged
merged 4 commits into from
Nov 8, 2022
Merged

Conversation

ptiede
Copy link
Contributor

@ptiede ptiede commented Nov 3, 2022

This pull request ensures that the input type is preserved for various rules.
Previously there were potentially a few places where 64 bit NaN's would always be produced
regardless of the input. To fix this I replaced any instance of :NaN with oftype($x, NaN).

Additionally, this pull-request fixes the issue with the ldexp rule from #88 which is similar in nature.

@codecov-commenter
Copy link

codecov-commenter commented Nov 3, 2022

Codecov Report

Base: 97.31% // Head: 97.31% // No change to project coverage 👍

Coverage data is based on head (5767bca) compared to base (489e294).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@           Coverage Diff           @@
##           master      #89   +/-   ##
=======================================
  Coverage   97.31%   97.31%           
=======================================
  Files           3        3           
  Lines         186      186           
=======================================
  Hits          181      181           
  Misses          5        5           
Impacted Files Coverage Δ
src/rules.jl 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@devmotion
Copy link
Member

To fix this I replaced any instance of :NaN with oftype($x, NaN).

:NaN is the DiffRules way to say that some derivative does not exist or is not implemented. You never want to use these "derivatives" anyway, every time you do you are already screwed.

Generally, oftype($x, NaN) seems like the wrong approach as x might not even be a floating point number. Just using float($x) instead seems also wrong in the two-argument functions since it might cause undesired promotions if x is not a floating point number (imagine eg the other argument being of type Float32).

@ptiede
Copy link
Contributor Author

ptiede commented Nov 3, 2022

Ok I reverted the :NaN for the two argument functions.

However for the ldexp I think the point remains. In that case you do need the oftype(float($x), exp2($y)) to prevent a spurious promotion for the derivative with respect to x.

src/rules.jl Outdated
@@ -85,7 +85,7 @@ _abs_deriv(x) = signbit(x) ? -one(x) : one(x)
@define_diffrule Base.atan(x, y) = :( $y / ($x^2 + $y^2) ), :( -$x / ($x^2 + $y^2) )
@define_diffrule Base.hypot(x, y) = :( $x / hypot($x, $y) ), :( $y / hypot($x, $y) )
@define_diffrule Base.log(b, x) = :( log($x) * inv(-log($b)^2 * $b) ), :( inv($x) / log($b) )
@define_diffrule Base.ldexp(x, y) = :( exp2($y) ), :NaN
@define_diffrule Base.ldexp(x, y) = :( oftype(float($x), exp2($y)) ), :(oftype(float($x), NaN))
Copy link
Member

Choose a reason for hiding this comment

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

Could we even just use

Suggested change
@define_diffrule Base.ldexp(x, y) = :( oftype(float($x), exp2($y)) ), :(oftype(float($x), NaN))
@define_diffrule Base.ldexp(x, y) = :( oftype($x, exp2($y) ), :NaN

? At least it seems the definitions in Base already assume that x is a floating point number:

julia> methods(ldexp)
# 7 methods for generic function "ldexp":
[1] ldexp(x::Float16, q::Integer) in Base.Math at math.jl:826
[2] ldexp(x::T, e::Integer) where T<:Union{Float16, Float32, Float64} in Base.Math at math.jl:783
[3] ldexp(x::BigFloat, n::Int64) in Base.MPFR at mpfr.jl:648
[4] ldexp(x::BigFloat, n::Union{Int16, Int32, Int64, Int8}) in Base.MPFR at mpfr.jl:658
[5] ldexp(x::BigFloat, n::UInt64) in Base.MPFR at mpfr.jl:653
[6] ldexp(x::BigFloat, n::Union{UInt16, UInt32, UInt64, UInt8}) in Base.MPFR at mpfr.jl:659
[7] ldexp(x::BigFloat, n::Integer) in Base.MPFR at mpfr.jl:660

But maybe

Suggested change
@define_diffrule Base.ldexp(x, y) = :( oftype(float($x), exp2($y)) ), :(oftype(float($x), NaN))
@define_diffrule Base.ldexp(x, y) = :( oftype(float($x), exp2($y) ), :NaN

is safer - although maybe even safer would be something like oftype(ldexp($x, $y), ... (or something similar without evaluating the primal) in case the arguments are promoted in some way in some other, non-Base definitions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tend to agree for the first option. I would be worried about including the primal in the calculation since that is a much more expensive operation than exp2(y) which is just some bit shuffling since y is a integer.

src/rules.jl Outdated Show resolved Hide resolved
Copy link
Member

@devmotion devmotion left a comment

Choose a reason for hiding this comment

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

Looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants