-
Notifications
You must be signed in to change notification settings - Fork 231
Enable keyword arguments for particle methods #2660
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
1
commit into
main
Choose a base branch
from
py/smc-kwargs
base: main
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.
+88
−28
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,23 +19,22 @@ struct ParticleMCMCContext{R<:AbstractRNG} <: DynamicPPL.AbstractContext | |
| rng::R | ||
| end | ||
|
|
||
| struct TracedModel{V<:AbstractVarInfo,M<:Model,E<:Tuple} <: AdvancedPS.AbstractGenericModel | ||
| struct TracedModel{V<:AbstractVarInfo,M<:Model,T<:Tuple,NT<:NamedTuple} <: | ||
| AdvancedPS.AbstractTuringLibtaskModel | ||
| model::M | ||
| varinfo::V | ||
| evaluator::E | ||
| resample::Bool | ||
| fargs::T | ||
| kwargs::NT | ||
| end | ||
|
|
||
| function TracedModel( | ||
| model::Model, varinfo::AbstractVarInfo, rng::Random.AbstractRNG, resample::Bool | ||
| ) | ||
| model = DynamicPPL.setleafcontext(model, ParticleMCMCContext(rng)) | ||
| args, kwargs = DynamicPPL.make_evaluate_args_and_kwargs(model, varinfo) | ||
| isempty(kwargs) || error( | ||
| "Particle sampling methods do not currently support models with keyword arguments.", | ||
| ) | ||
| evaluator = (model.f, args...) | ||
| return TracedModel(model, varinfo, evaluator, resample) | ||
| fargs = (model.f, args...) | ||
| return TracedModel(model, varinfo, resample, fargs, kwargs) | ||
| end | ||
|
|
||
| function AdvancedPS.advance!( | ||
|
|
@@ -53,16 +52,16 @@ function AdvancedPS.delete_retained!(trace::TracedModel) | |
| # In such a case, we need to ensure that when we continue sampling (i.e. | ||
| # the next time we hit tilde_assume!!), we don't use the values in the | ||
| # reference particle but rather sample new values. | ||
| return TracedModel(trace.model, trace.varinfo, trace.evaluator, true) | ||
| return TracedModel(trace.model, trace.varinfo, true, trace.fargs, trace.kwargs) | ||
| end | ||
|
|
||
| function AdvancedPS.reset_model(trace::TracedModel) | ||
| return trace | ||
| end | ||
|
|
||
| function Libtask.TapedTask(taped_globals, model::TracedModel; kwargs...) | ||
| function Libtask.TapedTask(taped_globals, model::TracedModel) | ||
| return Libtask.TapedTask( | ||
| taped_globals, model.evaluator[1], model.evaluator[2:end]...; kwargs... | ||
| taped_globals, model.fargs[1], model.fargs[2:end]...; model.kwargs... | ||
| ) | ||
| end | ||
|
|
||
|
|
@@ -124,6 +123,7 @@ function AbstractMCMC.sample( | |
| ) | ||
| check_model && _check_model(model, sampler) | ||
| error_if_threadsafe_eval(model) | ||
| check_model_kwargs(model) | ||
| # need to add on the `nparticles` keyword argument for `initialstep` to make use of | ||
| return AbstractMCMC.mcmcsample( | ||
| rng, | ||
|
|
@@ -138,6 +138,31 @@ function AbstractMCMC.sample( | |
| ) | ||
| end | ||
|
|
||
| function check_model_kwargs(model::DynamicPPL.Model) | ||
| if !isempty(model.defaults) | ||
| # If there are keyword arguments, we need to check that the user has | ||
| # accounted for this by overloading `might_produce`. | ||
| might_produce = Libtask.might_produce(typeof((Core.kwcall, NamedTuple(), model.f))) | ||
| if !might_produce | ||
| io = IOBuffer() | ||
| ctx = IOContext(io, :color => true) | ||
| print( | ||
| ctx, | ||
| "Models with keyword arguments need special treatment to be used" * | ||
| " with particle methods. Please run:\n\n", | ||
| ) | ||
| printstyled( | ||
| ctx, | ||
| " using Libtask; Libtask.@might_produce($(model.f))"; | ||
|
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 think we could/should reexport |
||
| bold=true, | ||
| color=:blue, | ||
| ) | ||
| print(ctx, "\n\nbefore sampling from this model with particle methods.\n") | ||
| error(String(take!(io))) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| function Turing.Inference.initialstep( | ||
| rng::AbstractRNG, | ||
| model::DynamicPPL.Model, | ||
|
|
@@ -146,6 +171,7 @@ function Turing.Inference.initialstep( | |
| nparticles::Int, | ||
| kwargs..., | ||
| ) | ||
| check_model_kwargs(model) | ||
| # Reset the VarInfo. | ||
| vi = DynamicPPL.setacc!!(vi, ProduceLogLikelihoodAccumulator()) | ||
| vi = DynamicPPL.empty!!(vi) | ||
|
|
@@ -254,6 +280,7 @@ function Turing.Inference.initialstep( | |
| rng::AbstractRNG, model::DynamicPPL.Model, spl::PG, vi::AbstractVarInfo; kwargs... | ||
| ) | ||
| error_if_threadsafe_eval(model) | ||
| check_model_kwargs(model) | ||
| vi = DynamicPPL.setacc!!(vi, ProduceLogLikelihoodAccumulator()) | ||
|
|
||
| # Create a new set of particles | ||
|
|
@@ -495,7 +522,7 @@ end | |
| # details of the compiler, we set a bunch of methods as might_produce = true. We start with | ||
| # adding to ProduceLogLikelihoodAccumulator, which is what calls `produce`, and go up the | ||
| # call stack. | ||
| Libtask.might_produce(::Type{<:Tuple{typeof(DynamicPPL.accloglikelihood!!),Vararg}}) = true | ||
| Libtask.@might_produce(DynamicPPL.accloglikelihood!!) | ||
| function Libtask.might_produce( | ||
| ::Type{ | ||
| <:Tuple{ | ||
|
|
@@ -507,15 +534,11 @@ function Libtask.might_produce( | |
| ) | ||
| return true | ||
| end | ||
| function Libtask.might_produce( | ||
| ::Type{<:Tuple{typeof(DynamicPPL.accumulate_observe!!),Vararg}} | ||
| ) | ||
| return true | ||
| end | ||
| Libtask.might_produce(::Type{<:Tuple{typeof(DynamicPPL.tilde_observe!!),Vararg}}) = true | ||
| # Could the next two could have tighter type bounds on the arguments, namely a GibbsContext? | ||
| Libtask.@might_produce(DynamicPPL.accumulate_observe!!) | ||
| Libtask.@might_produce(DynamicPPL.tilde_observe!!) | ||
| # Could tilde_assume!! have tighter type bounds on the arguments, namely a GibbsContext? | ||
| # That's the only thing that makes tilde_assume calls result in tilde_observe calls. | ||
| Libtask.might_produce(::Type{<:Tuple{typeof(DynamicPPL.tilde_assume!!),Vararg}}) = true | ||
| Libtask.might_produce(::Type{<:Tuple{typeof(DynamicPPL.evaluate!!),Vararg}}) = true | ||
| Libtask.might_produce(::Type{<:Tuple{typeof(DynamicPPL.init!!),Vararg}}) = true | ||
| Libtask.@might_produce(DynamicPPL.tilde_assume!!) | ||
| Libtask.@might_produce(DynamicPPL.evaluate!!) | ||
| Libtask.@might_produce(DynamicPPL.init!!) | ||
| Libtask.might_produce(::Type{<:Tuple{<:DynamicPPL.Model,Vararg}}) = true | ||
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 |
|---|---|---|
|
|
@@ -43,7 +43,7 @@ ADTypes = "1" | |
| AbstractMCMC = "5.9" | ||
| AbstractPPL = "0.11, 0.12, 0.13" | ||
| AdvancedMH = "0.8.9" | ||
| AdvancedPS = "0.7" | ||
| AdvancedPS = "0.7.2" | ||
| AdvancedVI = "0.6" | ||
| Aqua = "0.8" | ||
| BangBang = "0.4" | ||
|
|
@@ -77,3 +77,6 @@ StatsBase = "0.33, 0.34" | |
| StatsFuns = "0.9.5, 1" | ||
| TimerOutputs = "0.5" | ||
| julia = "1.10" | ||
|
|
||
| [sources] | ||
| AdvancedPS = {url = "https://github.com/TuringLang/AdvancedPS.jl", rev = "py/kwargs"} | ||
|
Comment on lines
+81
to
+82
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. Likewise |
||
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.
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.
This will have to be removed pending release of TuringLang/AdvancedPS.jl#118