You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
See the [Core Functionality guide]({{<metacore-functionality>}}#sampling-multiple-chains) for examples.
59
59
60
60
### 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.
62
65
63
66
```julia
64
67
@modelfunctionf(y)
65
68
x =Vector{Float64}(undef, length(y))
66
69
Threads.@threadsfor i ineachindex(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()
69
74
end
70
75
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)
71
79
```
72
80
73
81
**Important limitations:**
@@ -76,8 +84,6 @@ end
76
84
-**Assume statements** (sampling statements): Often crash unpredictably or produce incorrect results
77
85
-**AD backend compatibility**: Many AD backends don't support threading. Check the [multithreaded column in ADTests](https://turinglang.org/ADTests/) for compatibility
78
86
79
-
For safe parallelism within models, consider vectorised operations instead of explicit threading.
80
-
81
87
## How do I check the type stability of my Turing model?
82
88
83
89
Type stability is crucial for performance. Check out:
0 commit comments