JuliaDiff / JuliaDiff/ForwardDiff.jl

Nested jacobian! calls

Open
#393 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
1k
Forks
160
PR merge metrics
No merged PRs in 30d

Description

Hi!

I'm having some troubles using nested inplace jacobian:

using ForwardDiff
using LinearAlgebra
#some data
x0 = randn(50000);
p0 = randn(2);

#pre allocate outputs
out = similar(x0);
outjac = Array{Float64}(undef, 50000, 2); 
outjac2 = Array{Float64}(undef,100000, 2)

#'regular' and in place function
f(p) =  @. p[1] * exp(-x0 * p[2]);
f!(out, p) =  @. out = p[1] * exp(-x0 * p[2]);

ForwardDiff.jacobian!(outjac2,(outjacin,p) -> (outjacin .= ForwardDiff.jacobian(f, p)), outjac, p0) #works like a charm
ForwardDiff.jacobian!(outjac2,(outjacin,p) -> ForwardDiff.jacobian!(outjacin, f!, out, p), outjac, p0); # breaks

It throws MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{getfield(Main, Symbol("##77#78")),Float64},Float64,2})

Both inner functions work and produce the same result:

g1 = (outjacin,p) -> ForwardDiff.jacobian!(outjacin, f!, out, p)
g1(outjac,p0)
g2 = (outjacin,p) -> (outjacin .= ForwardDiff.jacobian(f, p))
g2(outjac,p0)

Thanks for the help!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied reproducer around the nested ForwardDiff.jacobian! calls and compare it with the non-in-place jacobian path. Investigate how the inner jacobian! handles Dual values and determine what is needed for nested differentiation. Done means the in-place nested call no longer raises the Float64(::ForwardDiff.Dual) MethodError and produces the same result as the working path.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.