# \[ANN\] Turing.jl 0.12.0 release

**URL:** <https://discourse.julialang.org/t/ann-turing-jl-0-12-0-release/38927>\
**Category:** Probabilistic Programming\
**Created:** [May 7, 2020, 3:11am UTC](https://discourse.julialang.org/t/ann-turing-jl-0-12-0-release/38927 "2020-05-07T03:11:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)\
**Post date:** [May 7, 2020, 3:11am UTC](https://discourse.julialang.org/t/ann-turing-jl-0-12-0-release/38927/1 "2020-05-07T03:11:17Z")

</div>

Hey folks.

Turing 0.12.0 is out, and it comes with a lovely set of changes and updates. There are some deprecations and API changes, so please read carefully if 1) you use custom distributions or 2) you use the parallel sampling `psample`.

See all the changes in our [HISTORY.md](https://github.com/TuringLang/Turing.jl/blob/master/HISTORY.md) file, but for now here’s the main changes from the user side.

# Custom distributions

The interface for defining new distributions with a constrained support and making them compatible with `Turing` has changed. To make a custom distribution type `CustomDistribution` compatible with `Turing` , the user needs to define the method `bijector(d::CustomDistribution)` that returns an instance of type `Bijector` implementing the `Bijectors.Bijector` API.

You can read more about Bijectors.jl [here](https://github.com/TuringLang/Bijectors.jl). Please reach out to the team on Discourse in the probabilistic programming category, or on Slack/Zulip.

# Parallelism

Upstream changes in [AbstractMCMC.jl](https://github.com/TuringLang/AbstractMCMC.jl/) have deprecated the `psample` function, which previously sampled multiple chains with thread parallelism. `psample` was initially a test to see if we could make it work, but it’s been time to make it more in line with the design patterns we use.

To that end, `sample` now has additional signatures which allow you to specify a type of parallelism (threaded or process-based) to use on each chain. Here’s some examples:

```julia
# Sample n_chains using one process per chain
sample(model, sampler, MCMCDistributed(), n_samples, n_chains)

# Sample n_chains using one thread per chain
sample(model, sampler, MCMCThreads(), n_samples, n_chains)

```

I hope everyone will try this out – the distributed process sampling is a new feature that I’m fond of.

# Thread safety

In other parallelism news, `observe` statements are threadsafe. As an example, the following code is now thread-safe:

```julia
@model function example(data)
    Threads.@threads for i in eachindex(data)
        data[i] ~ Normal(0,1)
    end
end 

```

This should help if you observe a large amount of data and want to parallelize `logpdf` calls.

# Model tools

The macros `@varinfo` , `@logpdf` , and `@sampler` are removed. Instead one can access the internal variables `_varinfo` , `_model` , `_sampler` , and `_context` in the `@model` definition.

This was to remove macros from inside the model definition, which was kind of an antipattern. Since `@logpdf` was removed, it’s worth noting that you can access the current log density using `DynamicPPL.getlogp(_varinfo)`.

# Particle sampler methods

`SMC` and `PG` now have new methods that make it far easier to set the sampler threshold and resampler method.

For example, if you want to sample with a threshold of 0.4, you can do so with `SMC(0.4)`. For `PG`, this is `PG(n_particles, 0.4)`.

* * *

That’s all for now folks. Thanks for reading, and please give us whatever feedback you have.

---

<div class="post-metadata">

**Author:** ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)\
**Post date:** [May 7, 2020, 1:45pm UTC](https://discourse.julialang.org/t/ann-turing-jl-0-12-0-release/38927/2 "2020-05-07T13:45:17Z")

</div>

@torfjelde also wanted me to mention that you can now [add docstrings](https://github.com/TuringLang/DynamicPPL.jl/pull/99) to your models. The following should now work:

```julia
julia> using DynamicPPL, Distributions

julia> """
           demo(x)
           
       This is a demo model.
       """
       @model function demo(x)
           m ~ Normal()
           x ~ Normal(m, 1)
           
           return m
       end
demo

help?> demo
search: demo denominator ReadOnlyMemoryError widemul StridedMatrix DenseMatrix divrem code_llvm @code_llvm StridedVecOrMat DimensionMismatch DenseVecOrMat

  demo(x)

  This is a demo model.

julia> m = demo(1)
Model{var"###evaluator#307",(:x,),Tuple{Int64},(),ModelGen{var"###generator#308",(:x,),(),Tuple{}}}(##evaluator#307, (x = 1,), ModelGen{var"###generator#308",(:x,),(),Tuple{}}(##generator#308, NamedTuple()))

julia> m()
-0.2797280925807616

```

It’s a small change, but also a cool one that make Turing models seem more tightly integrated with the Julia system.

---

<div class="post-metadata">

**Author:** ![jack\_rabbit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jack_rabbit/32/10914_2.png) [@jack\_rabbit](https://discourse.julialang.org/u/jack_rabbit)\
**Post date:** [May 7, 2020, 2:30pm UTC](https://discourse.julialang.org/t/ann-turing-jl-0-12-0-release/38927/3 "2020-05-07T14:30:54Z")

</div>

Thanks a lot for writing this package. Could you please provide a complete working example about how to write a custom distribution using the new interface?

---

<div class="post-metadata">

**Author:** ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)\
**Post date:** [May 7, 2020, 3:40pm UTC](https://discourse.julialang.org/t/ann-turing-jl-0-12-0-release/38927/4 "2020-05-07T15:40:17Z")

</div>

Sure. @torfjelde is going to update the custom distribution documentation [here](https://turing.ml/dev/docs/using-turing/advanced) – I’ll post back here when it’s done.

---

<div class="post-metadata">

**Author:** ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)\
**Post date:** [May 14, 2020, 6:05pm UTC](https://discourse.julialang.org/t/ann-turing-jl-0-12-0-release/38927/5 "2020-05-14T18:05:13Z")

</div>

The updated distribution guide is here: [Advanced Usage](https://turing.ml/dev/docs/using-turing/advanced#31-domain-transformation)

---

<div class="post-metadata">

**Author:** ![jack\_rabbit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jack_rabbit/32/10914_2.png) [@jack\_rabbit](https://discourse.julialang.org/u/jack_rabbit)\
**Post date:** [May 14, 2020, 6:31pm UTC](https://discourse.julialang.org/t/ann-turing-jl-0-12-0-release/38927/6 "2020-05-14T18:31:30Z")

</div>

Thanks you very much. I also found there are concrete examples in [this discussion](https://github.com/TuringLang/Turing.jl/issues/1270).
