Skip to content

Commit 49a1998

Browse files
committed
update FAQ
1 parent 0848518 commit 49a1998

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

faq/index.qmd

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ To understand more about how Turing determines whether a variable is treated as
4747

4848
## Can I use parallelism / threads in my model?
4949

50-
Yes, but with important caveats! There are two types of parallelism to consider:
50+
Yes, but with some important caveats:
5151

5252
### 1. Parallel Sampling (Multiple Chains)
5353
Turing.jl fully supports sampling multiple chains in parallel:
@@ -58,16 +58,24 @@ Turing.jl fully supports sampling multiple chains in parallel:
5858
See the [Core Functionality guide]({{<meta core-functionality>}}#sampling-multiple-chains) for examples.
5959

6060
### 2. Threading Within Models
61-
Using threads inside your model (e.g., `Threads.@threads`) requires more care:
61+
62+
Using threads inside your model (e.g., `Threads.@threads`) requires more care.
63+
In particular, only threaded **observe** statements are safe to use; threaded **assume** statements can lead to crashes or incorrect results.
64+
Please see the [Threadsafe Evaluation page]({{< meta usage/threadsafe-evaluation >}}) for complete details.
6265

6366
```julia
6467
@model function f(y)
6568
x = Vector{Float64}(undef, length(y))
6669
Threads.@threads for i in eachindex(y)
67-
x[i] ~ Normal() # UNSAFE: `assume` statements in @threads can crash!
68-
y[i] ~ Normal(x[i]) # `observe` statements are okay
70+
# This would be unsafe!
71+
# x[i] ~ Normal()
72+
# This is safe:
73+
y[i] ~ Normal()
6974
end
7075
end
76+
# If you have parallel tilde-statements or `@addlogprob!` in a model,
77+
# you must mark the model as threadsafe:
78+
model = setthreadsafe(f(y), true)
7179
```
7280

7381
**Important limitations:**
@@ -76,8 +84,6 @@ end
7684
- **Assume statements** (sampling statements): Often crash unpredictably or produce incorrect results
7785
- **AD backend compatibility**: Many AD backends don't support threading. Check the [multithreaded column in ADTests](https://turinglang.org/ADTests/) for compatibility
7886

79-
For safe parallelism within models, consider vectorised operations instead of explicit threading.
80-
8187
## How do I check the type stability of my Turing model?
8288

8389
Type stability is crucial for performance. Check out:

0 commit comments

Comments
 (0)