# Using ForwardDiff results in a stack overflow

**URL:** https://discourse.julialang.org/t/using-forwarddiff-results-in-a-stack-overflow/90399
**Category:** General Usage
**Tags:** forwarddiff
**Created:** [November 17, 2022, 2:06pm UTC](https://discourse.julialang.org/t/using-forwarddiff-results-in-a-stack-overflow/90399 "2022-11-17T14:06:15Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![c\_sell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c_sell/32/38854_2.png) [@c\_sell](https://discourse.julialang.org/u/c_sell)
#### Post date: [November 17, 2022, 2:06pm UTC](https://discourse.julialang.org/t/using-forwarddiff-results-in-a-stack-overflow/90399/1 "2022-11-17T14:06:15Z")

</div>

Hi, im using the ForwardDiff package. The following code is only a simplification of the real problem.  
But I am getting there as well a stack overflow in the line of the LU decomposition.

```julia
using ForwardDiff, LinearAlgebra, SparseArrays

function foo(x)
  r = [1,2,1];
  c = [1,2,2];
  v = [x[1],x[2],x[1]];
  rhs = [1.0,2.0];
  A = sparse(r,c,v);
  LU = lu(A)
  y = LU \ rhs;
  return y
end
J = ForwardDiff.jacobian(foo,[1.,2.])
```

---

<div class="post-metadata">

### Author: ![j-fu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j-fu/32/11373_2.png) [@j-fu](https://discourse.julialang.org/u/j-fu)
#### Post date: [November 17, 2022, 11:52pm UTC](https://discourse.julialang.org/t/using-forwarddiff-results-in-a-stack-overflow/90399/2 "2022-11-17T23:52:32Z")

</div>

Interesting that this pops up as a stack overflow error…

The point is that `\` for SparseMatrixCSC is channeled to UMFPACK from SuiteSparse which is implemented in C and thus not able to work with ForwardDiff.Dual numbers.

That said, recently, the package Sparspak.jl was announced which is a pure Julia implementation of the classical Sparspak by George and Liu and thus provides an LU factorization working with general number types. The ExtendableSparse.jl package (shameless plug: I am the author…) provides the ExtendableSparseMatrix type which has a `\` which falls back to Sparspak on number types not supported by SuiteSparse/UMFPACK .

So now we have:

```julia
using ExtendableSparse, ForwardDiff, LinearAlgebra, SparseArrays
function foo(x)
         r = [1,2,1];
         c = [1,2,2];
         v = [x[1],x[2],x[1]];
         rhs = [1.0,2.0];
         A = ExtendableSparseMatrix(sparse(r,c,v));
         LU = lu(A)
         y = LU \ rhs;
         return y
end

J = ForwardDiff.jacobian(foo,[1.,2.])
2×2 Matrix{Float64}:
 -1.0 0.5
  0.0 -0.5

```

---

<div class="post-metadata">

### Author: ![c\_sell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c_sell/32/38854_2.png) [@c\_sell](https://discourse.julialang.org/u/c_sell)
#### Post date: [November 18, 2022, 7:28am UTC](https://discourse.julialang.org/t/using-forwarddiff-results-in-a-stack-overflow/90399/3 "2022-11-18T07:28:14Z")

</div>

Hi,  
thank you for your quick reply. Somehow I am still getting an Error with your code (I copied your version).

ERROR: MethodError: Cannot `convert` an object of type  
Sparspak.SpkProblem.Problem{Int64{},Float64} to an object of type  
Sparspak.SpkProblem.Problem{Int64{},ForwardDiff.Dual{ForwardDiff.Tag{typeof(foo), Float64}, Float64, 2}}

---

<div class="post-metadata">

### Author: ![j-fu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j-fu/32/11373_2.png) [@j-fu](https://discourse.julialang.org/u/j-fu)
#### Post date: [November 18, 2022, 8:42am UTC](https://discourse.julialang.org/t/using-forwarddiff-results-in-a-stack-overflow/90399/4 "2022-11-18T08:42:37Z")

</div>

Strange, I tried this even on Julia 1.6 in a clean environment, and it all works.

The error message indicates that you somehow tried to differentiate through a sparse solve with a sparse matrix already created with Float64 entries instead of ForwardDiff.Dual.

---

<div class="post-metadata">

### Author: ![c\_sell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c_sell/32/38854_2.png) [@c\_sell](https://discourse.julialang.org/u/c_sell)
#### Post date: [November 18, 2022, 12:16pm UTC](https://discourse.julialang.org/t/using-forwarddiff-results-in-a-stack-overflow/90399/5 "2022-11-18T12:16:46Z")

</div>

After i restarted my enviroment everything worked fine.  
Thanks for your professional help 🙂
