# ForwardDiff AD Error using distributed within Turing @model

**URL:** <https://discourse.julialang.org/t/forwarddiff-ad-error-using-distributed-within-turing-model/93111>\
**Category:** Probabilistic Programming\
**Tags:** turing, forwarddiff, bayesian-inference, ordinarydiffeq\
**Created:** [January 17, 2023, 7:16pm UTC](https://discourse.julialang.org/t/forwarddiff-ad-error-using-distributed-within-turing-model/93111 "2023-01-17T19:16:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhishek-venket](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abhishek-venket/32/45950_2.png) [@abhishek-venket](https://discourse.julialang.org/u/abhishek-venket)\
**Post date:** [January 17, 2023, 7:17pm UTC](https://discourse.julialang.org/t/forwarddiff-ad-error-using-distributed-within-turing-model/93111/1 "2023-01-17T19:17:00Z")

</div>

I am trying to estimate the posterior distributions of a couple of parameters of a system of ODEs. The probabilistic model is `data ~ MvNormal(y_sim, σ² * I),` where `data` contains sensor measurements of a quantity of interest from several experiments:

```julia
exp_data = map((date) -> get_exp_data(date), dates)
CO2_exp = vcat(exp_data...)

```

and `y_sim` is computed from `get_sim_data` which solves the ODE system for inputs from the corresponding experiments and a given `𝜃`.  
The Turing model is given here:

```julia
@everywhere @model function outlet_CO2_fit(data)
    #𝑝(𝜃): Priors of parameters 𝜃
    γ ~ filldist(truncated(Normal(-5.5, 0.3); lower = -7.0, upper = -4.0),1)
    α ~ filldist(truncated(Normal(1.6, 0.05); lower = 1.5, upper = 2.0),1)
    σ² ~ filldist(truncated(Normal(0.01, 0.003), 0, 0.1), 1)

    𝜃 = [α[1], 10^γ[1]]
    outlet_mol_frac_sim = pmap((date) -> get_sim_data(𝜃, date), dates)
    y_sim = 1e3.*vcat(outlet_mol_frac_sim...)
    data = 1e3.*data

    #𝑝(𝒟│𝜃): Likelihood pdf of data 𝒟 given parameters 𝜃
    data ~ MvNormal(y_sim, σ²[1] * I)
    return nothing
end
model = outlet_CO2_fit(CO2_exp)
advi = ADVI(1, 1000)
opt = Variational.TruncatedADAGrad(0.01, 1.0, 10)
q = vi(model, advi; optimizer = opt)

```

Now when I run this variational inference model fitting program in a serial manner, the optimization runs without any problem. However, when I run the code with `julia -p 4` to distribute the ODE solves over multiple cores, I get this error:

> First call to automatic differentiation for the Jacobian  
> failed. This means that the user `f` function is not compatible  
> with automatic differentiation.  
> MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag,  
> tacktrace:  
> [1] jacobian!  
> @ ~/.julia/packages/OrdinaryDiffEq/tAXVW/src/derivative\_wrappers.jl:230  
> [2] calc\_J!  
> @ ~/.julia/packages/OrdinaryDiffEq/tAXVW/src/derivative\_utils.jl:169 [inlined]  
> [3] calc\_W!  
> @ ~/.julia/packages/OrdinaryDiffEq/tAXVW/src/derivative\_utils.jl:716  
> [4] update\_W!  
> @ ~/.julia/packages/OrdinaryDiffEq/tAXVW/src/derivative\_utils.jl:824 [inlined]  
> [5] update\_W!  
> @ ~/.julia/packages/OrdinaryDiffEq/tAXVW/src/derivative\_utils.jl:823 [inlined]  
> [6] nlsolve!  
> @ ~/.julia/packages/OrdinaryDiffEq/tAXVW/src/nlsolve/nlsolve.jl:25  
> [7] perform\_step!  
> @ ~/.julia/packages/OrdinaryDiffEq/tAXVW/src/perform\_step/sdirk\_perform\_step.jl:481  
> [8] perform\_step!  
> @ ~/.julia/packages/OrdinaryDiffEq/tAXVW/src/perform\_step/sdirk\_perform\_step.jl:458 [inlined]  
> [9] solve!  
> @ ~/.julia/packages/OrdinaryDiffEq/tAXVW/src/solve.jl:514  
> [10] macro expansion  
> @ ./timing.jl:380 [inlined]  
> [11] #evolve\_system#469  
> @ ~/ToolsCA.jl/src/evolve.jl:51  
> [12] get\_sim\_data  
> @ ~/ToolsCA.jl/validation/param\_fitting/mass\_conv\_fit/dist\_fit\_frost\_2.jl:54  
> [13] #9  
> @ ~/ToolsCA.jl/validation/param\_fitting/mass\_conv\_fit/dist\_fit\_frost\_2.jl:80  
> [14] #110  
> @ /opt/julia-1.7.3/share/julia/stdlib/v1.7/Distributed/src/process\_messages.jl:278  
> [15] run\_work\_thunk  
> @ /opt/julia-1.7.3/share/julia/stdlib/v1.7/Distributed/src/process\_messages.jl:63  
> [16] macro expansion  
> @ /opt/julia-1.7.3/share/julia/stdlib/v1.7/Distributed/src/process\_messages.jl:278 [inlined]  
> [17] #109  
> @ ./task.jl:429  
> Stacktrace:  
> [1] (::Base.var"#898#900")(x::Task)  
> @ Base ./asyncmap.jl:177  
> [2] foreach(f::Base.var"#898#900", itr::Vector{Any})  
> @ Base ./abstractarray.jl:2712  
> [3] maptwice(wrapped\_f::Function, chnl::Channel{Any}, worker\_tasks::Vector{Any}, c::Vector{String})  
> @ Base ./asyncmap.jl:177  
> [4] wrap\_n\_exec\_twice  
> @ ./asyncmap.jl:153 [inlined]  
> [5] #async\_usemap#883  
> @ ./asyncmap.jl:103 [inlined]  
> [6] #asyncmap#882  
> @ ./asyncmap.jl:81 [inlined]  
> [7] pmap(f::Function, p::WorkerPool, c::Vector{String}; distributed::Bool, batch\_size::Int64, on\_error::Nothing, retry\_delays::Vector{Any}, retry\_check::Nothing)  
> @ Distributed /opt/julia-1.7.3/share/julia/stdlib/v1.7/Distributed/src/pmap.jl:126  
> [8] pmap(f::Function, p::WorkerPool, c::Vector{String})  
> @ Distributed /opt/julia-1.7.3/share/julia/stdlib/v1.7/Distributed/src/pmap.jl:101  
> [9] pmap(f::Function, c::Vector{String}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})  
> @ Distributed /opt/julia-1.7.3/share/julia/stdlib/v1.7/Distributed/src/pmap.jl:156  
> [10] pmap(f::Function, c::Vector{String})  
> @ Distributed /opt/julia-1.7.3/share/julia/stdlib/v1.7/Distributed/src/pmap.jl:156  
> [11] outlet\_CO2\_fit( **model** ::DynamicPPL.Model
