# PreallocationTools.jl with nested ForwardDiff and sparsity pattern detection errors

**URL:** <https://discourse.julialang.org/t/preallocationtools-jl-with-nested-forwarddiff-and-sparsity-pattern-detection-errors/107897>\
**Category:** Numerics\
**Tags:** question\
**Created:** [December 21, 2023, 10:45am UTC](https://discourse.julialang.org/t/preallocationtools-jl-with-nested-forwarddiff-and-sparsity-pattern-detection-errors/107897 "2023-12-21T10:45:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![asterycs](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@asterycs](https://discourse.julialang.org/u/asterycs)\
**Post date:** [December 21, 2023, 10:45am UTC](https://discourse.julialang.org/t/preallocationtools-jl-with-nested-forwarddiff-and-sparsity-pattern-detection-errors/107897/1 "2023-12-21T10:45:04Z")

</div>

Hello!

I’m trying to combine sparsity pattern detection with a nested ForwardDiff call together with PreallocationTools. A contrived, minimal example is below:

```julia
using ForwardDiff
using PreallocationTools
using Symbolics

function foo(x, cache)
    d = get_tmp(cache, x)

    d[:] = x

    0.5 * x'*x
end

function residual(r, x, cache)
    function foo_wrap(x)
        foo(x, cache)
    end
    
    r[:] = ForwardDiff.gradient(foo_wrap, x)
end

cache = DiffCache(zeros(2))

pattern = Symbolics.jacobian_sparsity((r, x) -> residual(r, x, cache), zeros(2), zeros(2))

```

which fails with the following stacktrace:

```julia
ERROR: ArgumentError: cannot reinterpret `Float64` as `ForwardDiff.Dual{ForwardDiff.Tag{var"#foo_wrap#7"{DiffCache{Vector{Float64}, Vector{Float64}}}, Num}, Num, 2}`, type `ForwardDiff.Dual{ForwardDiff.Tag{var"#foo_wrap#7"{DiffCache{Vector{Float64}, Vector{Float64}}}, Num}, Num, 2}` is not a bits type
Stacktrace:
  [1] (::Base.var"#throwbits#323")(S::Type, T::Type, U::Type)
    @ Base ./reinterpretarray.jl:16
  [2] reinterpret(#unused#::Type{ForwardDiff.Dual{ForwardDiff.Tag{var"#foo_wrap#7"{DiffCache{Vector{Float64}, Vector{Float64}}}, Num}, Num, 2}}, a::SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true})
    @ Base ./reinterpretarray.jl:62
  [3] get_tmp(dc::DiffCache{Vector{Float64}, Vector{Float64}}, u::Vector{ForwardDiff.Dual{ForwardDiff.Tag{var"#foo_wrap#7"{DiffCache{Vector{Float64}, Vector{Float64}}}, Num}, Num, 2}})
    @ PreallocationTools ~/.julia/packages/PreallocationTools/mJSsc/src/PreallocationTools.jl:124

```

Based on the discussion [here](https://github.com/SciML/PreallocationTools.jl/issues/72) I thought that I could just use `cache = FixedSizeDiffCache(zeros(Symbolics.Num, 2), 2)` instead in order to make non-bitstypes work, but this yields a similar error:

```julia
ERROR: ArgumentError: cannot reinterpret `ForwardDiff.Dual{nothing, Num, 2}` as `ForwardDiff.Dual{ForwardDiff.Tag{var"#foo_wrap#13"{FixedSizeDiffCache{Vector{Num}, Vector{ForwardDiff.Dual{nothing, Num, 2}}}}, Num}, Num, 2}`, type `ForwardDiff.Dual{ForwardDiff.Tag{var"#foo_wrap#13"{FixedSizeDiffCache{Vector{Num}, Vector{ForwardDiff.Dual{nothing, Num, 2}}}}, Num}, Num, 2}` is not a bits type

```

I’m unsure whether this is a supported use case or not. Would anyone happen to have any insights?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 27, 2023, 8:16am UTC](https://discourse.julialang.org/t/preallocationtools-jl-with-nested-forwarddiff-and-sparsity-pattern-detection-errors/107897/2 "2023-12-27T08:16:24Z")

</div>

This was a bug that is now fixed. Update packages and you should be fine.

---

<div class="post-metadata">

**Author:** ![asterycs](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@asterycs](https://discourse.julialang.org/u/asterycs)\
**Post date:** [December 27, 2023, 8:42am UTC](https://discourse.julialang.org/t/preallocationtools-jl-with-nested-forwarddiff-and-sparsity-pattern-detection-errors/107897/3 "2023-12-27T08:42:53Z")

</div>

Thank you for your reply! I tested the example just now with 0.4.13 and with master. I tried both `DiffCache` and `FixedSizeDiffCache` but I get the same errors in all cases.

Since this seems to be a bug, shall I open an issue on GitHub?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 27, 2023, 10:02am UTC](https://discourse.julialang.org/t/preallocationtools-jl-with-nested-forwarddiff-and-sparsity-pattern-detection-errors/107897/4 "2023-12-27T10:02:16Z")

</div>

OrdinaryDiffEq v6.65?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 27, 2023, 10:03am UTC](https://discourse.julialang.org/t/preallocationtools-jl-with-nested-forwarddiff-and-sparsity-pattern-detection-errors/107897/5 "2023-12-27T10:03:04Z")

</div>

Oh wait, that was a different bug with the same error message that I just happened to read on the same day. Ugh.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 31, 2023, 8:37pm UTC](https://discourse.julialang.org/t/preallocationtools-jl-with-nested-forwarddiff-and-sparsity-pattern-detection-errors/107897/6 "2023-12-31T20:37:33Z")

</div>

Nesting is now handled in the v0.4.15 release by [More extensive handling of dispatching on type by ChrisRackauckas · Pull Request #90 · SciML/PreallocationTools.jl · GitHub](https://github.com/SciML/PreallocationTools.jl/pull/90)

---

<div class="post-metadata">

**Author:** ![asterycs](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@asterycs](https://discourse.julialang.org/u/asterycs)\
**Post date:** [January 1, 2024, 9:05pm UTC](https://discourse.julialang.org/t/preallocationtools-jl-with-nested-forwarddiff-and-sparsity-pattern-detection-errors/107897/7 "2024-01-01T21:05:57Z")

</div>

Cheers Chris, thank you very much! I can confirm that the minimal test case in the first post indeed works with v0.4.15 (and also v0.4.16)!

I’ll just document here in case anyone else happens to run into this issue:

Julia 1.10 was just released, and the `Project.toml` of both `PreallocationTools` and `Symbolics` where already updated to require the newest version. My distribution doesn’t ship 1.10 yet so I’m still on Julia 1.9. In this case, to install the newest version of the above two packages one can clone the repositories and add the local copies with `Pkg`. The updated `Symbolics` (v5.15.1) is needed as some of the relevant methods were migrated there from `PreallocationTools`.
