Skip to content

AutoFastDifferentiation not working with NamedTuple context #775

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

Open
pitx-perf opened this issue Apr 15, 2025 · 3 comments
Open

AutoFastDifferentiation not working with NamedTuple context #775

pitx-perf opened this issue Apr 15, 2025 · 3 comments
Labels
backend Related to one or more autodiff backends

Comments

@pitx-perf
Copy link

Issue

AutoFastDifferentiation is not able to deal with a NamedTuple context which could happen when the parameters of an optimization is a NamedTuple.

The error message:

ERROR: MethodError: no method matching variablize(::@NamedTuple{v1::Float64, v2::Float64}, ::Symbol) 
Closest candidates are:
  variablize(::AbstractArray, ::Symbol)
  variablize(::Number, ::Symbol)

MWE

Example taken from Optimization.jl, and modified to have a NamedTuple as p and use AutoFastDifferentiation

using Optimization
import FastDifferentiation

rosenbrock(u, p) = (p.v1 - u[1])^2 + p.v2 * (u[2] - u[1]^2)^2
u0 = zeros(2)
p = (v1=1.0, v2=100.0)

optf = OptimizationFunction(rosenbrock, AutoFastDifferentiation())
prob = OptimizationProblem(optf, u0, p)

sol = solve(prob, Optimization.LBFGS())

AutoForwardDiff works fine.

@gdalle gdalle added the backend Related to one or more autodiff backends label Apr 15, 2025
@gdalle
Copy link
Member

gdalle commented Apr 15, 2025

Hi @pitx-perf,
Thanks for reporting this. Indeed, AutoFastDifferentiation is one of those backends which require any Constant or Cache to be either a Number or an AbstractArray (as hinted in the Constant docstring for example). I just haven't had time to implement more flexibility, because I didn't think anyone was actually using the symbolic backends in sophisticated way. Do you want to try and contribute a fix? I'd be happy to guide you, and it's good for me to get more people familiar with the DI code.

@pitx-perf
Copy link
Author

Hi @gdalle,
Yes I'd be happy to contribute to a fix. I definitely need some time to get familiar with both DI and FastDifferentiation beforehand.
I faced this issue just trying several backends. No idea if FastDifferentiation is actually suited for my problem.

@gdalle
Copy link
Member

gdalle commented Apr 18, 2025

No pressure of course, just let me know if you end up not wanting to write the fix and I'll take care of it.
Out of curiosity, what is your problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Related to one or more autodiff backends
Projects
None yet
Development

No branches or pull requests

2 participants