# Improve speed of vector-jacobian product

**URL:** https://discourse.julialang.org/t/improve-speed-of-vector-jacobian-product/106312
**Category:** Performance
**Tags:** reversediff, autodiff
**Created:** [November 16, 2023, 11:51am UTC](https://discourse.julialang.org/t/improve-speed-of-vector-jacobian-product/106312 "2023-11-16T11:51:24Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [November 16, 2023, 12:36pm UTC](https://discourse.julialang.org/t/improve-speed-of-vector-jacobian-product/106312/2 "2023-11-16T12:36:20Z")

</div>

> [@rveltz](#):
>
> ```julia
> for i=1:Nv
> inner(i, result, Nv, invDv, g, Vtmp)
> end
> 
> ```

Reverse-mode differentiation may not understand that each iteration of this loop updates a different element of `result` independently — I’m guessing that it makes a copy of the `result` array for every iteration of the loop, before running the loop backwards in order to backpropagate the vjp.

Moreover, you are effectively doing a sparse matrix–vector multiplication in which the elements of the matrix depend on your parameters, and AD tools often struggle with backpropagating through a sparse-matrix construction for reasons I explained in this thread: [Zygote.jl: How to get the gradient of sparse matrix - #6 by stevengj](https://discourse.julialang.org/t/zygote-jl-how-to-get-the-gradient-of-sparse-matrix/59067/6)

I’ve typically found that you need to write a manual vJp for some step(s) in most reasonable-scale scientific problems, unless you are using a package like [DiffEqFlux.jl](https://github.com/SciML/DiffEqFlux.jl) that has done that for you. AD tools are more reliable for cookie-cutter ML-style problems where you are plugging together large components that it already knows about, and are only fiddling around the edges with small scalar functions or code written in a functional/non-mutating style (e.g. composing vectorized primitives).

---

_[View the full topic](https://discourse.julialang.org/t/improve-speed-of-vector-jacobian-product/106312)._
