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
* Make threadsafe evaluation opt-in
* Reduce number of type parameters in methods
* Make `warned_warn_about_threads_threads_threads_threads` shorter
* Improve `setthreadsafe` docstring
* warn on bare `@threads` as well
* fix merge
* Fix performance issues
* Use maxthreadid() in TSVI
* Move convert_eltype code to threadsafe eval function
* Point to new Turing docs page
* Add a test for setthreadsafe
* Tidy up check_model
* Apply suggestions from code review
Fix outdated docstrings
Co-authored-by: Markus Hauru <markus@mhauru.org>
* Improve warning message
* Export `requires_threadsafe`
* Add an actual docstring for `requires_threadsafe`
---------
Co-authored-by: Markus Hauru <markus@mhauru.org>
Copy file name to clipboardExpand all lines: HISTORY.md
+38-1Lines changed: 38 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,12 +9,49 @@
9
9
This version provides a reimplementation of `LogDensityFunction` that provides performance improvements on the order of 2–10× for both model evaluation as well as automatic differentiation.
10
10
Exact speedups depend on the model size: larger models have less significant speedups because the bulk of the work is done in calls to `logpdf`.
11
11
12
-
For more information about how this is accomplished, please see https://github.com/TuringLang/DynamicPPL.jl/pull/1113 as well as the `src/fasteval.jl` file, which contains extensive comments.
12
+
For more information about how this is accomplished, please see https://github.com/TuringLang/DynamicPPL.jl/pull/1113 as well as the `src/logdensityfunction.jl` file, which contains extensive comments.
13
13
14
14
As a result of this change, `LogDensityFunction` no longer stores a VarInfo inside it.
15
15
In general, if `ldf` is a `LogDensityFunction`, it is now only valid to access `ldf.model` and `ldf.adtype`.
16
16
If you were previously relying on this behaviour, you will need to store a VarInfo separately.
17
17
18
+
#### Threadsafe evaluation
19
+
20
+
DynamicPPL models have traditionally supported running some probabilistic statements (e.g. tilde-statements, or `@addlogprob!`) in parallel.
21
+
Prior to DynamicPPL 0.39, thread safety for such models used to be enabled by default if Julia was launched with more than one thread.
22
+
23
+
In DynamicPPL 0.39, **thread-safe evaluation is now disabled by default**.
24
+
If you need it (see below for more discussion of when you _do_ need it), you **must** now manually mark it as so, using:
25
+
26
+
```julia
27
+
@modelf() =...
28
+
model =f()
29
+
model =setthreadsafe(model, true)
30
+
```
31
+
32
+
The problem with the previous on-by-default is that it can sacrifice a huge amount of performance when thread safety is not needed.
33
+
This is especially true when running Julia in a notebook, where multiple threads are often enabled by default.
34
+
Furthermore, it is not actually the correct approach: just because Julia has multiple threads does not mean that a particular model actually requires threadsafe evaluation.
35
+
36
+
**A model requires threadsafe evaluation if, and only if, the VarInfo object used inside the model is manipulated in parallel.**
37
+
This can occur if any of the following are inside `Threads.@threads` or other concurrency functions / macros:
38
+
39
+
- tilde-statements
40
+
- calls to `@addlogprob!`
41
+
- any direct manipulation of the special `__varinfo__` variable
42
+
43
+
If you have none of these inside threaded blocks, then you do not need to mark your model as threadsafe.
44
+
**Notably, the following do not require threadsafe evaluation:**
45
+
46
+
- Using threading for any computation that does not involve VarInfo. For example, you can calculate a log-probability in parallel, and then add it using `@addlogprob!` outside of the threaded block. This does not require threadsafe evaluation.
47
+
- Sampling with `AbstractMCMC.MCMCThreads()`.
48
+
49
+
For more information about threadsafe evaluation, please see [the Turing docs](https://turinglang.org/docs/usage/threadsafe-evaluation/).
50
+
51
+
When threadsafe evaluation is enabled for a model, an internal flag is set on the model.
52
+
The value of this flag can be queried using `DynamicPPL.requires_threadsafe(model)`, which returns a boolean.
53
+
This function is newly exported in this version of DynamicPPL.
54
+
18
55
#### Parent and leaf contexts
19
56
20
57
The `DynamicPPL.NodeTrait` function has been removed.
Copy file name to clipboardExpand all lines: docs/src/api.md
+8Lines changed: 8 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,6 +42,14 @@ The context of a model can be set using [`contextualize`](@ref):
42
42
contextualize
43
43
```
44
44
45
+
Some models require threadsafe evaluation (see [the Turing docs](https://turinglang.org/docs/usage/threadsafe-evaluation/) for more information on when this is necessary).
46
+
If this is the case, one must enable threadsafe evaluation for a model:
47
+
48
+
```@docs
49
+
setthreadsafe
50
+
requires_threadsafe
51
+
```
52
+
45
53
## Evaluation
46
54
47
55
With [`rand`](@ref) one can draw samples from the prior distribution of a [`Model`](@ref).
"It looks like you are using `Threads.@threads` in your model definition."*
385
+
"\n\nNote that since version 0.39 of DynamicPPL, threadsafe evaluation of models is disabled by default."*
386
+
" If you need it, you will need to explicitly enable it by creating the model, and then running `model = setthreadsafe(model, true)`."*
387
+
"\n\nThreadsafe model evaluation is only needed when parallelising tilde-statements (not arbitrary Julia code), and avoiding it can often lead to significant performance improvements."*
388
+
"\n\nPlease see https://turinglang.org/docs/usage/threadsafe-evaluation/ for more details of when threadsafe evaluation is actually required."
0 commit comments