ForwardDiff.jl icon indicating copy to clipboard operation
ForwardDiff.jl copied to clipboard

SpecialFunctions.gamma_inc goes into infinite recursion

Open bischtob opened this issue 6 years ago • 3 comments

The function SpecialFunctions.gamma_inc goes into infinite recursion when used with ForwardDiff.Duals. MWE:

using ForwardDiff, SpecialFunctions

julia> f = x -> gamma_inc(x, 1.0, 1)

julia> f(ForwardDiff.Dual(1.0, 1.0))
ERROR: StackOverflowError:
Stacktrace:
 [1] gamma_inc(::ForwardDiff.Dual{Nothing,Float64,1}, ::Float64, ::Int64) (repeats 80000 times)

julia> VERSION
v"1.2.0"

julia> Pkg.status("ForwardDiff")
 - ForwardDiff v0.10.6

bischtob avatar Nov 26 '19 00:11 bischtob

Might be https://github.com/JuliaLang/julia/issues/26552.

KristofferC avatar Nov 26 '19 12:11 KristofferC

The proper fix is to add dual methods for the function such that AD is support. Otherwise, it would just be a MethodError instead of stack overflow. The definitions usually go into DiffRules but I don't think there many definitions for ternaries so maybe it's easier to special case it in this package. I think we do that for other functions.

andreasnoack avatar Nov 26 '19 13:11 andreasnoack

The function SpecialFunctions.erfcx also goes into infinite recursion:

julia> using ForwardDiff, SpecialFunctions

julia> f(z) = real( erfcx(-im * z) )
f (generic function with 1 method)

julia> f( ForwardDiff.Dual(1.0, 1.0) )
ERROR: StackOverflowError:
Stacktrace:
 [1] Complex at .\complex.jl:12 [inlined] (repeats 2 times)
 [2] float at .\complex.jl:1012 [inlined]
 [3] erfcx(::Complex{ForwardDiff.Dual{Nothing,Float64,1}}) at C:\Users\...\.julia\packages\SpecialFunctions\O3MVw\src\erf.jl:28 (repeats 52170 times)
 [4] f(::ForwardDiff.Dual{Nothing,Float64,1}) at .\none:1
 [5] top-level scope at none:0

julia> VERSION
v"1.3.1"

julia> Pkg.status("ForwardDiff")
    Status `C:\Users\...\.julia\environments\v1.3\Project.toml`
  [f6369f11] ForwardDiff v0.10.8

Is there a known workaround, or some fix that I could help implement? Thanks

chaffeeje avatar Jan 14 '20 21:01 chaffeeje