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

Functions with Arrays of AbstractInterpolations cannot be handled by ForwardDiff #298

Closed
keatonmill opened this issue Jan 26, 2018 · 1 comment

Comments

@keatonmill
Copy link

I've discovered an issue when passing Arrays of AbstractInterpolations to functions and then using ForwardDiff to process those functions -- even if the Interpolations are never used. Oddly, the error only occurs if you call the ForwardDiff package from within a function. A workaround is possible if you know the precise type of Interpolation you're going to have ahead of time or if you can pass an Array{Any}.

I'm not sure if this is a bug in Interpolations or ForwardDiff. I've submitted the same information via JuliaMath/Interpolations.jl#191

Code:

println("Start.")

using ForwardDiff
using Interpolations

function eff(p, thing)
    out = p[1] *  p[2]
    return out
end

function GradientWrapper(func, p, thing)
    f(x) = func(x, thing)
    return ForwardDiff.gradient(f,p)
end

n = 40
z = [i * j for i in 1:n, j in 1:n]
itp = interpolate(z, BSpline(Cubic(Natural())), OnGrid())


p = [5.0, 6.0]

test = [itp, itp]

println("Using type $(typeof(test))")
println("Calling the derivative directly:")
f(x) = eff(x,test)
println(ForwardDiff.gradient(f, p))
println("Calling the wrapper:")
println(GradientWrapper(eff, p, test))

test = Array{Any}(2)
test[1] = itp
test[2] = itp

println("Using type $(typeof(test))")
println("Calling the derivative directly:")
g(x) = eff(x, test)
println(ForwardDiff.gradient(g, p))
println("Calling the wrapper:")
println(GradientWrapper(eff, p, test))


test = Array{AbstractInterpolation}(2)
test[1] = itp
test[2] = itp

println("Using type $(typeof(test))")
println("Calling the derivative directly:")
h(x) = eff(x, test)
println(ForwardDiff.gradient(h, p))
println("Calling the wrapper -- this fails:")
println(GradientWrapper(eff, p, test))

println("Done.")

Output:

Start.
Using type Array{Interpolations.BSplineInterpolation{Float64,2,Array{Float64,2},Interpolations.BSpline{Interpolations.Cubic{Interpolations.Line}},Interpolations.OnGrid,1},1}
Calling the derivative directly:
[6.0, 5.0]
Calling the wrapper:
[6.0, 5.0]
Using type Array{Any,1}
Calling the derivative directly:
[6.0, 5.0]
Calling the wrapper:
[6.0, 5.0]
Using type Array{Interpolations.AbstractInterpolation,1}
Calling the derivative directly:
[6.0, 5.0]
Calling the wrapper -- this fails:
LoadError: MethodError: no method matching string(::Expr)
The applicable method may be too new: running in world age 2758, while current world is 21890.
Closest candidates are:
  string(::Any...) at strings/io.jl:120 (method too new to be called from this world context.)
  string(!Matched::BigInt) at gmp.jl:568 (method too new to be called from this world context.)
  string(!Matched::BigFloat) at mpfr.jl:885 (method too new to be called from this world context.)
  ...
while loading evensmallertest.jl, in expression starting on line 53
in GradientWrapper at evensmallertest.jl:13
in typeinf_ext at base/inference.jl:2622
in typeinf_code at base/inference.jl:2583
in typeinf_frame at base/inference.jl:2504
in typeinf at base/inference.jl:2787
in typeinf_work at base/inference.jl:2722
in abstract_eval at base/inference.jl:1950
in abstract_eval_call at base/inference.jl:1927
in abstract_call at base/inference.jl:1897
in abstract_call_gf_by_type at base/inference.jl:1420
in typeinf_edge at base/inference.jl:2535
in typeinf at base/inference.jl:2787
in typeinf_work at base/inference.jl:2722
in abstract_eval at base/inference.jl:1950
in abstract_eval_call at base/inference.jl:1927
in abstract_call at base/inference.jl:1897
in abstract_call_gf_by_type at base/inference.jl:1420
in typeinf_edge at base/inference.jl:2535
in typeinf at base/inference.jl:2787
in typeinf_work at base/inference.jl:2669
in abstract_interpret at base/inference.jl:2076
in abstract_eval at base/inference.jl:1950
in abstract_eval_call at base/inference.jl:1927
in abstract_call at base/inference.jl:1897
in abstract_call_gf_by_type at base/inference.jl:1420
in typeinf_edge at base/inference.jl:2535
in typeinf at base/inference.jl:2787
in typeinf_work at base/inference.jl:2669
in abstract_interpret at base/inference.jl:2076
in abstract_eval at base/inference.jl:1950
in abstract_eval_call at base/inference.jl:1927
in abstract_call at base/inference.jl:1701
in builtin_tfunction at base/inference.jl:1133
in builtin_tfunction at base/inference.jl:1215
in  at base/inference.jl:899
in getfield_tfunc at base/inference.jl:897
in limit_type_depth at base/inference.jl:692
@KristofferC
Copy link
Collaborator

Works now.

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