# ForwardDiff.derivative! of inplace functions

**URL:** https://discourse.julialang.org/t/forwarddiff-derivative-of-inplace-functions/54014
**Category:** Performance
**Tags:** forwarddiff
**Created:** [January 26, 2021, 9:41pm UTC](https://discourse.julialang.org/t/forwarddiff-derivative-of-inplace-functions/54014 "2021-01-26T21:41:55Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![jokasimr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jokasimr/32/21442_2.png) [@jokasimr](https://discourse.julialang.org/u/jokasimr)
#### Post date: [January 26, 2021, 9:41pm UTC](https://discourse.julialang.org/t/forwarddiff-derivative-of-inplace-functions/54014/1 "2021-01-26T21:41:55Z")

</div>

I’m learning how to use the FiniteDifference.jl package without allocating, and I want to implement a function for doing Jacobian vector products inplace. 🙂

The goal is to accomplish the same as `jvp_finite_difference!`, but without as much numerical errors.

```julia
# Function to be differentiated, sum(x) .* x.^2
function f!(out, x)
           broadcast!(^, out, x, 2)
           broadcast!(*, out, out, sum(x))
end

function jvp_finite_difference!(out, f!, y, z, x, v, epsilon=1e-8)
    broadcast!(*, z, v, epsilon)
    broadcast!(+, z, z, x)
    f!(y, x)
    f!(out, z)
    broadcast!(-, out, out, y)
    broadcast!(/, out, out, epsilon)
end

# This works
x = randn(10); v = copy(x); out = copy(x); y = copy(x); z = copy(x)
jvp_finite_difference!(out, f!, y, z, x, v)

using ForwardDiff

# Implementation using ForwardDiff
function jvp!(out, f!, y, z, x, v)
        g!(y, t) = begin
            broadcast!(*, z, v, t)
            broadcast!(+, z, z, x)
            f!(y, z)
        end
        ForwardDiff.derivative!(out, g!, y, 0)
end

# This fails with the error below
jvp!(out, f!, y, z, x, v)

```

```julia
ERROR: MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{var"#g!#63"{typeof(f!),Array{Float64,1},Array{Float64,1},Array{Float64,1}},Int64},Float64,1}

```

It clearly says in the [FiniteDiff.jl docs](http://www.juliadiff.org/ForwardDiff.jl/stable/user/limitations/) that any storage used inside functions must be generic enough, so i guess this is the problem.

Using really generic storage works, but makes the performance drop 🤔

```julia
# This works, but slow
x = ones(10); v = ones(10); result = ones(10); y = ones(Real, 10); z = copy(y)
jvp!(out, f!, y, z, x, v)

```

What am I missing here? Is there a less generic, more performant, type of storage I should use that still works with ForwardDiff.jl? Or something else?

Very thankful for help and suggestions!

---

<div class="post-metadata">

### Author: ![jdlara-berkeley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jdlara-berkeley/32/4096_2.png) [@jdlara-berkeley](https://discourse.julialang.org/u/jdlara-berkeley)
#### Post date: [July 5, 2021, 10:17pm UTC](https://discourse.julialang.org/t/forwarddiff-derivative-of-inplace-functions/54014/2 "2021-07-05T22:17:43Z")

</div>

You need to implement a cache for the in place vector. I have been playing this problem too. I hope this helps: [ForwardDiff caches usage - #6 by jdlara-berkeley](https://discourse.julialang.org/t/forwarddiff-caches-usage/64058/6)
