# Turing.jl + Enyzme Examples

**URL:** <https://discourse.julialang.org/t/turing-jl-enyzme-examples/121606>\
**Category:** Probabilistic Programming\
**Created:** [October 22, 2024, 6:39pm UTC](https://discourse.julialang.org/t/turing-jl-enyzme-examples/121606 "2024-10-22T18:39:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshualeond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshualeond/32/230_2.png) [@joshualeond](https://discourse.julialang.org/u/joshualeond)\
**Post date:** [October 22, 2024, 6:39pm UTC](https://discourse.julialang.org/t/turing-jl-enyzme-examples/121606/1 "2024-10-22T18:39:00Z")

</div>

Hi there, are there any working examples of using Turing.jl with the Enzyme.jl AD backend?

I’ve seen several Github issues like [this one](https://github.com/TuringLang/Turing.jl/pull/1887) but I think they may be outdated at this point. Also the following Discourse topic where it looks like Enzyme is ready for everyday use with Turing: [Enzyme ready for everyday use? (2024)](https://discourse.julialang.org/t/enzyme-ready-for-everyday-use-2024/118819). However, I didn’t see an example referenced in that post.

Any info would be appreciated.

Thanks!  
Josh

---

<div class="post-metadata">

**Author:** ![mhauru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mhauru/32/208880_2.png) [@mhauru](https://discourse.julialang.org/u/mhauru)\
**Post date:** [October 24, 2024, 4:31pm UTC](https://discourse.julialang.org/t/turing-jl-enyzme-examples/121606/3 "2024-10-24T16:31:42Z")

</div>

Hi @joshualeond! The Turing.jl PR you linked is still being worked on, it just goes in bursts of activity. Many, many Turing models work with Enzyme, but it’s not yet at a point where I would have great confidence that any given model you throw at it will be fine. Definitely worth a try though, to see how your use case fares. We are in the process of running more of our test suite with Enzyme, reporting any issues that come up.

Usage is as simple as

```julia
import ADTypes
import Enzyme
using Turing

Enzyme.API.runtimeActivity!(true)

@model function m()
    x ~ Normal()
    y ~ Normal(x, 1)
end

model = m()
sampler = NUTS(; adtype=ADTypes.AutoEnzyme(; mode=Enzyme.Forward))
sample(model, sampler, 1000)

```

---

<div class="post-metadata">

**Author:** ![wsmoses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsmoses/32/26497_2.png) [@wsmoses](https://discourse.julialang.org/u/wsmoses)\
**Post date:** [October 24, 2024, 6:20pm UTC](https://discourse.julialang.org/t/turing-jl-enyzme-examples/121606/4 "2024-10-24T18:20:18Z")

</div>

I’ll note that the above code uses an old version of Enzyme (I think Turing needs to fix some compat’s).

---

<div class="post-metadata">

**Author:** ![PTWaade](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ptwaade/32/215893_2.png) [@PTWaade](https://discourse.julialang.org/u/PTWaade)\
**Post date:** [March 19, 2025, 9:47am UTC](https://discourse.julialang.org/t/turing-jl-enyzme-examples/121606/5 "2025-03-19T09:47:38Z")

</div>

Just a small note for others reading this that the Enzyme API for setting the runtime activity has been updated, so that the usage would now be:

```julia
import ADTypes
import Enzyme
using Turing

@model function m()
    x ~ Normal()
    y ~ Normal(x, 1)
end

model = m()
sampler = NUTS(; adtype=ADTypes.AutoEnzyme(; mode=Enzyme.set_runtime_activity(Enzyme.Reverse)))
sample(model, sampler, 1000)

```

---

<div class="post-metadata">

**Author:** ![PTWaade](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ptwaade/32/215893_2.png) [@PTWaade](https://discourse.julialang.org/u/PTWaade)\
**Post date:** [March 19, 2025, 9:48am UTC](https://discourse.julialang.org/t/turing-jl-enyzme-examples/121606/6 "2025-03-19T09:48:19Z")

</div>

(Do correct me if I got this wrong!)
