-
Notifications
You must be signed in to change notification settings - Fork 6
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
Automatic derivatives of SLEEF trig functions cause stack-overflow #11
Comments
can you try |
julia> using ForwardDiff: derivative
julia> using SLEEF
julia> derivative(SLEEF.sin_fast, 1.0)
ERROR: StackOverflowError:
Stacktrace:
[1] sin_fast(::Dual{ForwardDiff.Tag{typeof(SLEEF.sin_fast),Float64},Float64,1}) at
/Users/mason/.julia/packages/SLEEF/pKpdp/src/SLEEF.jl:105 (repeats 80000 times) |
Ref JuliaLang/julia#26552, particularly JuliaLang/julia#26552 (comment). |
Ok thanks @tkoolen , it seems that this can be improved. |
So I guess the best way of going about this is something like the following? using ForwardDiff: derivative
f(x::Union{Float32,Float64}) = exp(x)
for func in (:f,)
priv = Symbol('_',func)
@eval begin
$priv(x::T) where {T<:Union{Float32,Float64}} = $func(x)
$func(x::Real) = $priv(float(x))
end
end
julia> derivative(f, 1.0)
ERROR: MethodError: no method matching _f(::ForwardDiff.Dual{ForwardDiff.Tag{typeof(f),Float64},Float64,1})
altough it's not clear to my why forwarddiff doesn't work without a custom wrapper. |
I was puzzeled as to why the base math function worked with ForwardDiff, but now I see that ForwardDiff relies on DiffRules to be able to differentiate the base math functions. In principle it should be able to support SLEEF, ideally without having to add anything to DiffRules, but I am not sure how to do that. |
Is this the same reason SLEEF functions don't work with Zygote?
|
Not sure if this is a SLEEF problem for a ForwardDiff problem but I would have expected a different error than a stack overflow when trying to take automatic derivatives of SLEEF trig functions.
Note: defining the action of trig functions on
ForwardDiff.Dual
s does give the correct behaviour.The text was updated successfully, but these errors were encountered: