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

Spurious type promotions due to Float64 coefficients in the diff rules #566

Closed
KristofferC opened this issue Nov 28, 2021 · 2 comments
Closed

Comments

@KristofferC
Copy link
Collaborator

julia> r = 1.0f0;

julia> sind(r) |> typeof
Float32

julia> d = ForwardDiff.Dual(1.0f0, 2.0f0);

julia> sind(d) |> typeof
ForwardDiff.Dual{Nothing, Float64, 1}

This last one should still be in the Float32 domain. All the coefficients in https://github.com/JuliaDiff/DiffRules.jl/blob/master/src/rules.jl#L29 probably need to take on the value type of the input dual.

@KristofferC KristofferC changed the title Spurious type promotions Spurious type promotions due to Float64 coefficients in the diff rules Nov 28, 2021
@devmotion
Copy link
Member

The problem in this specific rule (and many others in DiffRules) seems to be that the irrational π / 180 is computed and promoted to Float64 automatically, without taking into account the other terms. Simply rearranging the terms to - (π * sind($x)) / 180 would fix this problem. @torfjelde tried to address this problem for multiple rules in JuliaDiff/DiffRules.jl#55.

@devmotion
Copy link
Member

This issue was fixed in JuliaDiff/DiffRules.jl#55. With DiffRules >= 1.9.1:

julia> r = 1.0f0;

julia> sind(r) |> typeof
Float32

julia> d = ForwardDiff.Dual(1.0f0, 2.0f0);

julia> sind(d) |> typeof
ForwardDiff.Dual{Nothing, Float32, 1}

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

No branches or pull requests

2 participants