# Error in simple Turing model with ReverseDiff

**URL:** <https://discourse.julialang.org/t/error-in-simple-turing-model-with-reversediff/60128>\
**Category:** Probabilistic Programming\
**Tags:** turing\
**Created:** [April 27, 2021, 8:47pm UTC](https://discourse.julialang.org/t/error-in-simple-turing-model-with-reversediff/60128 "2021-04-27T20:47:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![opera\_malenky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/opera_malenky/32/8213_2.png) [@opera\_malenky](https://discourse.julialang.org/u/opera_malenky)\
**Post date:** [April 27, 2021, 8:47pm UTC](https://discourse.julialang.org/t/error-in-simple-turing-model-with-reversediff/60128/1 "2021-04-27T20:47:28Z")

</div>

I had a model that was running until about two weeks ago, but suddenly stopped. I thought I’d post here before filing an issue - it’s not clear to me if this is more of a reversediff issue or a Turing issue or something else.

Here’s a MWE:

```julia
using Turing, BenchmarkTools, ReverseDiff

@model function efa(P)
    λ ~ filldist(Normal(0, 10), P, 1)
    Ψ ~ filldist(truncated(Normal(0, 5), 0, Inf), P)
    f_var ~ filldist(truncated(Normal(0, 5), 0, Inf), 1)

    Σ1 = λ * f_var * λ' + Diagonal(Ψ) # Fails
    # Σ1 = λ * f_var * λ' + diagm(Ψ) # Fails
    # Σ1 = randn(P, P) + Diagonal(Ψ) # Works
    # Σ1 = λ * f_var * λ' + randn(P, P) # Works
    # Σ1 = λ * f_var * λ' + Diagonal(ones(P)) # Works
end

Turing.setadbackend(:reversediff)

efa_test = efa(3)
@time m1 = sample(efa_test, NUTS(1, 0.65), MCMCThreads(), 1, 4)

```

When I run the model above, I get an error in reversediff (in ReverseDiff’s `increment_deriv!` function, specifically). I narrowed it down to the line with `Σ1`. The first term on that line, `λ * f_var * λ'`, creates a PxP matrix, to which I want to add the second term, a P-variate Diagonal matrix. Trying to do so leads to the error.

What makes things weirder is that when I replace either of the two terms with a random PxP matrix, the model runs fine. Also, if I replace the `Ψ` vector with `ones(P)`, it runs fine. It’s not an issue with the first term, and it’s not the second term, but seems to be something specifically involving the combination of the two.

Am I missing something obvious?

---

<div class="post-metadata">

**Author:** ![EvoArt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evoart/32/25357_2.png) [@EvoArt](https://discourse.julialang.org/u/EvoArt)\
**Post date:** [April 27, 2021, 9:10pm UTC](https://discourse.julialang.org/t/error-in-simple-turing-model-with-reversediff/60128/2 "2021-04-27T21:10:50Z")

</div>

I also have a simple-ish model, fitting a logistic growth ODEproblem to data. The model runs using forwarddiff, but just spits out endless numerical errors with reversediff.

---

<div class="post-metadata">

**Author:** ![devmotion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devmotion/32/9201_2.png) [@devmotion](https://discourse.julialang.org/u/devmotion)\
**Post date:** [April 29, 2021, 12:58pm UTC](https://discourse.julialang.org/t/error-in-simple-turing-model-with-reversediff/60128/3 "2021-04-29T12:58:14Z")

</div>

Can you check if it works with ReverseDiff 1.7? Version 1.8 was released two weeks ago and it broke something in DiffEqSensitivity, so maybe it is the culprit.

---

<div class="post-metadata">

**Author:** ![opera\_malenky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/opera_malenky/32/8213_2.png) [@opera\_malenky](https://discourse.julialang.org/u/opera_malenky)\
**Post date:** [April 29, 2021, 1:51pm UTC](https://discourse.julialang.org/t/error-in-simple-turing-model-with-reversediff/60128/4 "2021-04-29T13:51:06Z")

</div>

That seems to be it! Using v1.7 makes it work again.

---

<div class="post-metadata">

**Author:** ![devmotion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devmotion/32/9201_2.png) [@devmotion](https://discourse.julialang.org/u/devmotion)\
**Post date:** [April 29, 2021, 2:10pm UTC](https://discourse.julialang.org/t/error-in-simple-turing-model-with-reversediff/60128/5 "2021-04-29T14:10:33Z")

</div>

Great! Can you open an issue with the failing example in the ReverseDiff repo?

---

<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:** [April 29, 2021, 2:15pm UTC](https://discourse.julialang.org/t/error-in-simple-turing-model-with-reversediff/60128/6 "2021-04-29T14:15:46Z")

</div>

> [@devmotion](#):
>
> Can you check if it works with ReverseDiff 1.7? Version 1.8 was released two weeks ago and it broke something in DiffEqSensitivity, so maybe it is the culprit.

Same error too.

---

<div class="post-metadata">

**Author:** ![opera\_malenky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/opera_malenky/32/8213_2.png) [@opera\_malenky](https://discourse.julialang.org/u/opera_malenky)\
**Post date:** [April 29, 2021, 2:56pm UTC](https://discourse.julialang.org/t/error-in-simple-turing-model-with-reversediff/60128/7 "2021-04-29T14:56:40Z")

</div>

Just to close the loop, I opened an issue over there.
