-
Notifications
You must be signed in to change notification settings - Fork 32
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
Using JET.jl to determine if typed varinfo is okay #728
Merged
Merged
Changes from all commits
Commits
Show all changes
65 commits
Select commit
Hold shift + click to select a range
361c45e
fixed calls to `to_linked_internal_transform`
torfjelde 545cfab
fixed incorrect call to `acclogp_assume!!`
torfjelde abd432f
added `determine_varinfo` and an implementation using JET for this
torfjelde 5cd9009
Merge remote-tracking branch 'origin/torfjelde/minor-bugfixes' into t…
torfjelde d503c3c
made filtering for errors only in the tilde pipeline optional
torfjelde acb2cb0
formatting
torfjelde 902641f
fixed incorrect comment
torfjelde d93006b
added test for the branch we were currently imssing
torfjelde 64ff18a
formatting
torfjelde 90c2df0
Merge branch 'master' into torfjelde/minor-bugfixes
torfjelde a94dbd5
Merge branch 'torfjelde/minor-bugfixes' into torfjelde/determine-varinfo
torfjelde 67723d6
Merge branch 'master' into torfjelde/determine-varinfo
torfjelde 3d8ad44
renamed `determine_varinfo` to `determine_suitable_varinfo` with
torfjelde c06b080
removed now-redundant init used with Requires.jl, since this is no
torfjelde d1a5bab
`determine_suitable_varinfo` now only performs checks using the
torfjelde 5370e55
formatting
torfjelde dd408ee
updated error hint
torfjelde c253e9b
added def of `untyped_varinfo` which takes just `model` and `context`
torfjelde 891b46a
fixed incorrect call to `untyped_varinfo` in `_determine_varinfo_jet`
torfjelde 686ed9f
explicitly call `typed_varinfo` when we want such a thing rather than
torfjelde d7d785a
`typed_varinfo` and `untyped_varinfo` handles wrapping passed context
torfjelde dda56ec
use `determine_suitable_varinfo` in `LogDensityFunction` when not con…
torfjelde 46ea18c
formatting
torfjelde c20ede3
formatting
torfjelde 0b3c36e
fixed a bug in `DynamicPPLJETExt.is_tilde_instance`
torfjelde f76658a
updated docs
torfjelde 690b017
Update docs/src/internals/varinfo.md
torfjelde 97258f3
added back def of `untyped_varinfo` that shouldn't have been removed +
torfjelde 95bb3a9
Merge remote-tracking branch 'origin/torfjelde/determine-varinfo' int…
torfjelde 155ce66
Merge branch 'master' into torfjelde/determine-varinfo
torfjelde 4998d08
minor codestyle improvement
torfjelde 5c27677
temporary hack to debug what's happening
torfjelde 99d4df7
more debugging
torfjelde 3b9a9eb
use the `target_modules` kwarg in `report_call` instead of manually
torfjelde 99fb153
formatting
torfjelde 3588597
more debugging
torfjelde 7a302e5
Merge remote-tracking branch 'origin/torfjelde/determine-varinfo' int…
torfjelde 040cb54
more debugging
torfjelde 889c370
Merge branch 'master' into torfjelde/determine-varinfo
torfjelde c98fe49
more debugging: try with new bijectors.jl
torfjelde 123b644
formatting
torfjelde 37fabb0
removed the hacky debugging stuff used for the CI
torfjelde 33e5b98
Merge remote-tracking branch 'origin/torfjelde/determine-varinfo' int…
torfjelde 7ddec2c
removed now-redudant filtering methods since we use JET's own filters
torfjelde b6b4bff
bump Bijectors.jl compat entry to 0.15.1 in test so JET.jl tests pass
torfjelde e07ecdb
moved the JET.jl-dependent experimental `determine_varinfo` into a
torfjelde 8ba8f82
Merge branch 'master' into torfjelde/determine-varinfo
torfjelde 9ec1556
forgot to add the experimenta.jl file in previous commit
torfjelde 599488b
Merge remote-tracking branch 'origin/torfjelde/determine-varinfo' int…
torfjelde fa155a4
reverted changes to `default_varinfo` and `LogDensityFunction`
torfjelde 8496968
added a bunch of docs for introduced and existing methods
torfjelde fd82871
added doctests to `determine_suitable_varinfo`
torfjelde bb87ba0
added JET.jl as a dep to docs
torfjelde 62c5cd1
fixed referencing in docs
torfjelde 55dc91e
fixed docstring
torfjelde ae51778
Merge branch 'master' into torfjelde/determine-varinfo
torfjelde a692ec3
fixed doctest
torfjelde d5eb404
Merge remote-tracking branch 'origin/torfjelde/determine-varinfo' int…
torfjelde 17b6ec9
Update Project.toml
torfjelde bfa88b2
applied suggestions from @mhauru
torfjelde 82578cf
fixed doctests
torfjelde 3aad34f
finally fixed doctests
torfjelde da3eefe
removed unnecessary `typed_varinfo` and `untyped_varinfo` methods
torfjelde 325c5f9
added filter to ignore source of warnings in doctest
torfjelde 4a17e82
Merge branch 'master' into torfjelde/determine-varinfo
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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module DynamicPPLJETExt | ||
|
||
using DynamicPPL: DynamicPPL | ||
using JET: JET | ||
|
||
function DynamicPPL.Experimental.is_suitable_varinfo( | ||
model::DynamicPPL.Model, | ||
context::DynamicPPL.AbstractContext, | ||
varinfo::DynamicPPL.AbstractVarInfo; | ||
only_ddpl::Bool=true, | ||
) | ||
# Let's make sure that both evaluation and sampling doesn't result in type errors. | ||
f, argtypes = DynamicPPL.DebugUtils.gen_evaluator_call_with_types( | ||
model, varinfo, context | ||
) | ||
# If specified, we only check errors originating somewhere in the DynamicPPL.jl. | ||
# This way we don't just fall back to untyped if the user's code is the issue. | ||
result = if only_ddpl | ||
JET.report_call(f, argtypes; target_modules=(JET.AnyFrameModule(DynamicPPL),)) | ||
else | ||
JET.report_call(f, argtypes) | ||
end | ||
return length(JET.get_reports(result)) == 0, result | ||
end | ||
|
||
function DynamicPPL.Experimental._determine_varinfo_jet( | ||
model::DynamicPPL.Model, context::DynamicPPL.AbstractContext; only_ddpl::Bool=true | ||
) | ||
# First we try with the typed varinfo. | ||
varinfo = DynamicPPL.typed_varinfo(model, context) | ||
|
||
# Let's make sure that both evaluation and sampling doesn't result in type errors. | ||
issuccess, result = DynamicPPL.Experimental.is_suitable_varinfo( | ||
model, context, varinfo; only_ddpl | ||
) | ||
|
||
if !issuccess | ||
# Useful information for debugging. | ||
@debug "Evaluaton with typed varinfo failed with the following issues:" | ||
@debug result | ||
end | ||
|
||
# If we didn't fail anywhere, we return the type stable one. | ||
return if issuccess | ||
varinfo | ||
else | ||
# Warn the user that we can't use the type stable one. | ||
@warn "Model seems incompatible with typed varinfo. Falling back to untyped varinfo." | ||
DynamicPPL.untyped_varinfo(model, context) | ||
end | ||
end | ||
|
||
end |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
module Experimental | ||
|
||
using DynamicPPL: DynamicPPL | ||
|
||
torfjelde marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# This file only defines the names of the functions, and their docstrings. The actual implementations are in `ext/DynamicPPLJETExt.jl`, since we don't want to depend on JET.jl other than as a weak dependency. | ||
""" | ||
is_suitable_varinfo(model::Model, context::AbstractContext, varinfo::AbstractVarInfo; kwargs...) | ||
|
||
Check if the `model` supports evaluation using the provided `context` and `varinfo`. | ||
|
||
!!! warning | ||
Loading JET.jl is required before calling this function. | ||
|
||
# Arguments | ||
- `model`: The model to verify the support for. | ||
- `context`: The context to use for the model evaluation. | ||
- `varinfo`: The varinfo to verify the support for. | ||
|
||
# Keyword Arguments | ||
- `only_ddpl`: If `true`, only consider error reports occuring in the tilde pipeline. Default: `true`. | ||
mhauru marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Returns | ||
- `issuccess`: `true` if the model supports the varinfo, otherwise `false`. | ||
- `report`: The result of `report_call` from JET.jl. | ||
""" | ||
function is_suitable_varinfo end | ||
|
||
# Internal hook for JET.jl to overload. | ||
function _determine_varinfo_jet end | ||
|
||
""" | ||
determine_suitable_varinfo(model[, context]; only_ddpl::Bool=true) | ||
|
||
Return a suitable varinfo for the given `model`. | ||
|
||
See also: [`DynamicPPL.Experimental.is_suitable_varinfo`](@ref). | ||
|
||
!!! warning | ||
For full functionality, this requires JET.jl to be loaded. | ||
If JET.jl is not loaded, this function will assume the model is compatible with typed varinfo. | ||
|
||
# Arguments | ||
- `model`: The model for which to determine the varinfo. | ||
- `context`: The context to use for the model evaluation. Default: `SamplingContext()`. | ||
|
||
# Keyword Arguments | ||
mhauru marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- `only_ddpl`: If `true`, only consider error reports within DynamicPPL.jl. | ||
|
||
# Examples | ||
|
||
```jldoctest | ||
julia> using DynamicPPL.Experimental: determine_suitable_varinfo | ||
|
||
julia> using JET: JET # needs to be loaded for full functionality | ||
|
||
julia> @model function model_with_random_support() | ||
x ~ Bernoulli() | ||
if x | ||
y ~ Normal() | ||
else | ||
z ~ Normal() | ||
end | ||
end | ||
model_with_random_support (generic function with 2 methods) | ||
|
||
julia> model = model_with_random_support(); | ||
|
||
julia> # Typed varinfo cannot handle this random support model properly | ||
# as using a single execution of the model will not see all random variables. | ||
# Hence, this this model requires untyped varinfo. | ||
vi = determine_suitable_varinfo(model); | ||
┌ Warning: Model seems incompatible with typed varinfo. Falling back to untyped varinfo. | ||
└ @ DynamicPPLJETExt ~/.julia/dev/DynamicPPL.jl/ext/DynamicPPLJETExt.jl:48 | ||
|
||
julia> vi isa typeof(DynamicPPL.untyped_varinfo(model)) | ||
true | ||
|
||
julia> # In contrast, a simple model with no random support can be handled by typed varinfo. | ||
@model model_with_static_support() = x ~ Normal() | ||
model_with_static_support (generic function with 2 methods) | ||
|
||
julia> vi = determine_suitable_varinfo(model_with_static_support()); | ||
|
||
julia> vi isa typeof(DynamicPPL.typed_varinfo(model_with_static_support())) | ||
true | ||
``` | ||
""" | ||
function determine_suitable_varinfo( | ||
model::DynamicPPL.Model, | ||
context::DynamicPPL.AbstractContext=DynamicPPL.SamplingContext(); | ||
only_ddpl::Bool=true, | ||
) | ||
# If JET.jl has been loaded, and thus `determine_varinfo` has been defined, we use that. | ||
return if Base.get_extension(DynamicPPL, :DynamicPPLJETExt) !== nothing | ||
_determine_varinfo_jet(model, context; only_ddpl) | ||
else | ||
# Warn the user. | ||
@warn "JET.jl is not loaded. Assumes the model is compatible with typed varinfo." | ||
# Otherwise, we use the, possibly incorrect, default typed varinfo (to stay backwards compat). | ||
DynamicPPL.typed_varinfo(model, context) | ||
end | ||
end | ||
|
||
end |
This file contains 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 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.
Could there be some way to test this? I do see that it's tricky. I'm a bit uncomfortable having this in without any testing.
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.
Yeah was thinking the same. We could put in a test strictly before loading JET.jl ofc. It's a bit messy, but seems like the best way 😕
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.
Does wrapping the tests in separate modules save us?
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.
Nah. AFAIK extensions trigger if the package is loaded at any point, e.g. even if a dep loads it
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.
It's also a thing where it doesn't seem like we can nicely get the resulting error message (the error hint is not in the msg of the error or something). So I think we just leave this for now 😕
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.
Sure, it does seem nasty to test for. Have you tried locally that it does what you expect?