-
Notifications
You must be signed in to change notification settings - Fork 37
Make threadsafe evaluation opt-in #1151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
penelopeysm
wants to merge
8
commits into
breaking
Choose a base branch
from
py/opt-in-tsvi
base: breaking
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+236
−185
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f862acb
Make threadsafe evaluation opt-in
penelopeysm d54554a
fix debug
penelopeysm bcc7b0b
forgot to remove this stuff
penelopeysm 6fcdde0
fix kwargs
penelopeysm 3c06bfe
Reduce number of type parameters in methods
penelopeysm 30fcc20
Make `warned_warn_about_threads_threads_threads_threads` shorter
penelopeysm 1d7ba0a
Improve `setthreadsafe` docstring
penelopeysm c97858a
warn on bare `@threads` as well
penelopeysm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -301,7 +301,7 @@ function model(mod, linenumbernode, expr, warn) | |
| modeldef = build_model_definition(expr) | ||
|
|
||
| # Generate main body | ||
| modeldef[:body] = generate_mainbody(mod, modeldef[:body], warn) | ||
| modeldef[:body] = generate_mainbody(mod, modeldef[:body], warn, false) | ||
|
|
||
| return build_output(modeldef, linenumbernode) | ||
| end | ||
|
|
@@ -346,36 +346,58 @@ Generate the body of the main evaluation function from expression `expr` and arg | |
| If `warn` is true, a warning is displayed if internal variables are used in the model | ||
| definition. | ||
| """ | ||
| generate_mainbody(mod, expr, warn) = generate_mainbody!(mod, Symbol[], expr, warn) | ||
| generate_mainbody(mod, expr, warn, warn_threads) = | ||
| generate_mainbody!(mod, Symbol[], expr, warn, warn_threads) | ||
|
|
||
| generate_mainbody!(mod, found, x, warn) = x | ||
| function generate_mainbody!(mod, found, sym::Symbol, warn) | ||
| generate_mainbody!(mod, found, x, warn, warn_threads) = x | ||
| function generate_mainbody!(mod, found, sym::Symbol, warn, warn_threads) | ||
| if warn && sym in INTERNALNAMES && sym ∉ found | ||
| @warn "you are using the internal variable `$sym`" | ||
| push!(found, sym) | ||
| end | ||
|
|
||
| return sym | ||
| end | ||
| function generate_mainbody!(mod, found, expr::Expr, warn) | ||
| function generate_mainbody!(mod, found, expr::Expr, warn, warn_threads) | ||
| # Do not touch interpolated expressions | ||
| expr.head === :$ && return expr.args[1] | ||
|
|
||
| # Flag to determine whether we've issued a warning for threadsafe macros Note that this | ||
| # detection is not fully correct. We can only detect the presence of a macro that has | ||
| # the symbol `Threads.@threads`, however, we can't detect if that *is actually* | ||
| # Threads.@threads from Base.Threads. | ||
|
|
||
| # Do we don't want escaped expressions because we unfortunately | ||
| # escape the entire body afterwards. | ||
| Meta.isexpr(expr, :escape) && return generate_mainbody(mod, found, expr.args[1], warn) | ||
| Meta.isexpr(expr, :escape) && | ||
| return generate_mainbody(mod, found, expr.args[1], warn, warn_threads) | ||
|
|
||
| # If it's a macro, we expand it | ||
| if Meta.isexpr(expr, :macrocall) | ||
| return generate_mainbody!(mod, found, macroexpand(mod, expr; recursive=true), warn) | ||
| if ( | ||
| expr.args[1] == Symbol("@threads") || | ||
| expr.args[1] == Expr(:., :Threads, QuoteNode(Symbol("@threads"))) && | ||
| !warn_threads | ||
| ) | ||
| warn_threads = true | ||
| @warn ( | ||
| "It looks like you are using `Threads.@threads` in your model definition." * | ||
| "\n\nNote that since version 0.39 of DynamicPPL, threadsafe evaluation of models is disabled by default." * | ||
| " If you need it, you will need to explicitly enable it by creating the model, and then running `model = setthreadsafe(model, true)`." * | ||
| "\n\nAvoiding threadsafe evaluation can often lead to significant performance improvements. Please see https://turinglang.org/docs/THIS_PAGE_DOESNT_EXIST_YET for more details of when threadsafe evaluation is actually required." | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm writing this now. |
||
| ) | ||
| end | ||
| return generate_mainbody!( | ||
| mod, found, macroexpand(mod, expr; recursive=true), warn, warn_threads | ||
| ) | ||
| end | ||
|
|
||
| # Modify dotted tilde operators. | ||
| args_dottilde = getargs_dottilde(expr) | ||
| if args_dottilde !== nothing | ||
| L, R = args_dottilde | ||
| return generate_mainbody!( | ||
| mod, found, Base.remove_linenums!(generate_dot_tilde(L, R)), warn | ||
| mod, found, Base.remove_linenums!(generate_dot_tilde(L, R)), warn, warn_threads | ||
| ) | ||
| end | ||
|
|
||
|
|
@@ -385,8 +407,8 @@ function generate_mainbody!(mod, found, expr::Expr, warn) | |
| L, R = args_tilde | ||
| return Base.remove_linenums!( | ||
| generate_tilde( | ||
| generate_mainbody!(mod, found, L, warn), | ||
| generate_mainbody!(mod, found, R, warn), | ||
| generate_mainbody!(mod, found, L, warn, warn_threads), | ||
| generate_mainbody!(mod, found, R, warn, warn_threads), | ||
| ), | ||
| ) | ||
| end | ||
|
|
@@ -397,13 +419,16 @@ function generate_mainbody!(mod, found, expr::Expr, warn) | |
| L, R = args_assign | ||
| return Base.remove_linenums!( | ||
| generate_assign( | ||
| generate_mainbody!(mod, found, L, warn), | ||
| generate_mainbody!(mod, found, R, warn), | ||
| generate_mainbody!(mod, found, L, warn, warn_threads), | ||
| generate_mainbody!(mod, found, R, warn, warn_threads), | ||
| ), | ||
| ) | ||
| end | ||
|
|
||
| return Expr(expr.head, map(x -> generate_mainbody!(mod, found, x, warn), expr.args)...) | ||
| return Expr( | ||
| expr.head, | ||
| map(x -> generate_mainbody!(mod, found, x, warn, warn_threads), expr.args)..., | ||
| ) | ||
| end | ||
|
|
||
| function generate_assign(left, right) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a reminder note to change this before merging.