# Models with persistent memory usage

**URL:** <https://discourse.julialang.org/t/models-with-persistent-memory-usage/93215>\
**Category:** Probabilistic Programming\
**Tags:** question, turing, forwarddiff\
**Created:** [January 19, 2023, 2:10pm UTC](https://discourse.julialang.org/t/models-with-persistent-memory-usage/93215 "2023-01-19T14:10:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pfarndt](https://avatars.discourse-cdn.com/v4/letter/p/8dc957/32.png) [@pfarndt](https://discourse.julialang.org/u/pfarndt)\
**Post date:** [January 19, 2023, 2:10pm UTC](https://discourse.julialang.org/t/models-with-persistent-memory-usage/93215/1 "2023-01-19T14:10:37Z")

</div>

I am using `Turing` a lot but now I have a model which would keep some persistent memory to be computational efficient. A minimal example would be:

```julia
using Turing, Optim, ForwardDiff

mutable struct Acomputer{T <: Number}
    a::Vector{T} # <- in real I would need more 
    para::T
end

function set_para!(ac::Acomputer, p)
    ac.para = p
end

function get_a!(ac::Acomputer)
    fill!(ac.a, ac.para) # <- in real this is more involved
    ac.a
end

```

the model would be defined like this:

```julia
@model function m(ac, x)
    para ~ Uniform(0,10)

    set_para!(ac, para)
    a = get_a!(ac)
    x .~ Poisson.(a)
end

```

I can now optimise it w/o the use of gradients:

```julia
ac = Acomputer(zeros(2), 3.0)
optimize(m(ac, [4,6]), MLE(), NelderMead())

```

giving the expected result:

```julia
ModeResult with maximized lp of -3.66
1-element Named Vector{Float64}
A │ 
──────┼────────
:para │ 5.00009

```

However - if I want to make use AD, i.e. `ForwardDiff`, I cannot do this anymore:

```julia
ac = Acomputer{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 1}}(zeros(2), 3.0)
optimize(m(ac, [4,6]), MLE())

```

I get

```julia
MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 1})
Closest candidates are:
  (::Type{T})(::Real, ::RoundingMode) where T<:AbstractFloat at rounding.jl:200
  (::Type{T})(::T) where T<:Number at boot.jl:772
  (::Type{T})(::AbstractChar) where T<:Union{AbstractChar, Number} at char.jl:50
  ...

```

… and don’t know how to resolve this issue. Is there any help?

---

<div class="post-metadata">

**Author:** ![torfjelde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torfjelde/32/206542_2.png) [@torfjelde](https://discourse.julialang.org/u/torfjelde)\
**Post date:** [February 1, 2023, 2:55pm UTC](https://discourse.julialang.org/t/models-with-persistent-memory-usage/93215/2 "2023-02-01T14:55:56Z")

</div>

First off, be careful here. I don’t know what you’re doing, but if you start sampling with this, you’re likely breaking the Markov assumption which is essential to _Markov_ Chain Monte Carlo (MCMC).

But with that being said, the following seems to be the problem:

> ac = Acomputer{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 1}}(zeros(2), 3.0)

You’re telling `Acomputer` that it should expect `Dual` numbers, but then you give it `zeros(2)::Vector{Float64}` and `3.0` (which is a `Float64`). This is causing the issue.

---

<div class="post-metadata">

**Author:** ![pfarndt](https://avatars.discourse-cdn.com/v4/letter/p/8dc957/32.png) [@pfarndt](https://discourse.julialang.org/u/pfarndt)\
**Post date:** [February 2, 2023, 9:02pm UTC](https://discourse.julialang.org/t/models-with-persistent-memory-usage/93215/3 "2023-02-02T21:02:40Z")

</div>

Not really - if I just run

```julia
ac = Acomputer{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 1}}(zeros(2), 3.0)

```

then it `ac` will be of the correct type, s.t. all the internal parameters are of the correct type, i.e. `Dual` numbers. As far as I understand this is prerequisite to make AD possible. The above error only occurs when I want to `optimize` the model.

But probably there is also another way to achieve what I want to do.

In principle: My model is computationally expansive and allocates a lot of memory. I want to circumvent that subsequent model calls always (re)allocate this memory.

---

<div class="post-metadata">

**Author:** ![torfjelde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torfjelde/32/206542_2.png) [@torfjelde](https://discourse.julialang.org/u/torfjelde)\
**Post date:** [February 6, 2023, 5:12pm UTC](https://discourse.julialang.org/t/models-with-persistent-memory-usage/93215/4 "2023-02-06T17:12:16Z")

</div>

Okay so then I think I need the full traceback 😕
