-
Notifications
You must be signed in to change notification settings - Fork 36
Using JET.jl to determine if typed varinfo is okay #728
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
Merged
Merged
Changes from 6 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 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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
module DynamicPPLJETExt | ||
|
||
using DynamicPPL: DynamicPPL | ||
using JET: JET | ||
|
||
""" | ||
is_tilde_instance(x) | ||
|
||
Return `true` if `x` is a method instance of a tilde function, otherwise `false`. | ||
""" | ||
is_tilde_instance(x) = false | ||
is_tilde_instance(frame::JET.VirtualFrame) = is_tilde_instance(frame.linfo) | ||
is_tilde_instance(mi::Core.MethodInstance) = is_tilde_instance(mi.specTypes.parameters[1]) | ||
is_tilde_instance(::Type{typeof(DynamicPPL.tilde_assume!!)}) = true | ||
is_tilde_instance(::Type{typeof(DynamicPPL.tilde_observe!!)}) = true | ||
is_tilde_instance(::Type{typeof(DynamicPPL.dot_tilde_assume!!)}) = true | ||
is_tilde_instance(::Type{typeof(DynamicPPL.dot_tilde_observe!!)}) = true | ||
|
||
""" | ||
report_has_error_in_tilde(report) | ||
|
||
Return `true` if the given error `report` contains a tilde function in its frames, otherwise `false`. | ||
|
||
This is used to filter out reports that occur outside of the tilde pipeline, in an attempt to avoid | ||
warning the user about DynamicPPL doing something wrong when it is in fact an issue with the user's code. | ||
""" | ||
function report_has_error_in_tilde(report) | ||
frames = report.vst | ||
return any(is_tilde_instance, frames) | ||
end | ||
|
||
function DynamicPPL.determine_varinfo( | ||
model::DynamicPPL.Model, | ||
context::DynamicPPL.AbstractContext=DynamicPPL.DefaultContext(); | ||
verbose::Bool=false, | ||
only_tilde::Bool=true, | ||
) | ||
# First we try with the typed varinfo. | ||
varinfo = DynamicPPL.typed_varinfo(model) | ||
issuccess = true | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
# Let's make sure that both evaluation and sampling doesn't result in type errors. | ||
f_eval, argtypes_eval = DynamicPPL.DebugUtils.gen_evaluator_call_with_types( | ||
model, varinfo, context | ||
) | ||
result_eval = JET.report_call(f_eval, argtypes_eval) | ||
reports_eval = JET.get_reports(result_eval) | ||
if only_tilde | ||
reports_eval = filter(report_has_error_in_tilde, reports_eval) | ||
end | ||
# If we get reports => we had issues so we use the untyped varinfo. | ||
issuccess &= length(reports_eval) == 0 | ||
if issuccess | ||
# Evaluation succeeded, let's try sampling. | ||
f_sample, argtypes_sample = DynamicPPL.DebugUtils.gen_evaluator_call_with_types( | ||
model, varinfo, DynamicPPL.SamplingContext(context) | ||
) | ||
result_sample = JET.report_call(f_sample, argtypes_sample) | ||
reports_sample = JET.get_reports(result_sample) | ||
if only_tilde | ||
reports_sample = filter(report_has_error_in_tilde, reports_sample) | ||
end | ||
# If we get reports => we had issues so we use the untyped varinfo. | ||
issuccess &= length(reports_sample) == 0 | ||
if !issuccess && verbose | ||
# Show the user the issues. | ||
@warn "Sampling with typed varinfo failed with the following issues:" | ||
for report in reports_sample | ||
@warn report | ||
end | ||
end | ||
elseif verbose | ||
# Show the user the issues. | ||
@warn "Evaluaton with typed varinfo failed with the following issues:" | ||
for report in reports_eval | ||
@warn report | ||
end | ||
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) | ||
end | ||
end | ||
|
||
end |
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
@testset "DynamicPPLJETExt.jl" begin | ||
@testset "determine_varinfo" begin | ||
@model function demo1() | ||
x ~ Bernoulli() | ||
if x | ||
y ~ Normal() | ||
else | ||
z ~ Normal() | ||
end | ||
end | ||
model = demo1() | ||
@test DynamicPPL.determine_varinfo(model; verbose=true) isa | ||
DynamicPPL.UntypedVarInfo | ||
|
||
@model demo2() = x ~ Normal() | ||
@test DynamicPPL.determine_varinfo(demo2()) isa DynamicPPL.TypedVarInfo | ||
|
||
@model function demo3() | ||
# Just making sure that nothing strange happens when type inference fails. | ||
x = Vector(undef, 1) | ||
x[1] ~ Bernoulli() | ||
if x[1] | ||
y ~ Normal() | ||
else | ||
z ~ Normal() | ||
end | ||
end | ||
@test DynamicPPL.determine_varinfo(demo3(); verbose=true) isa | ||
DynamicPPL.UntypedVarInfo | ||
|
||
# Evaluation works (and it would even do so in practice), but sampling | ||
# fill fail due to storing `Cauchy{Float64}` in `Vector{Normal{Float64}}`. | ||
@model function demo4() | ||
x ~ Bernoulli() | ||
if x | ||
y ~ Normal() | ||
else | ||
y ~ Cauchy() # different distibution, but same transformation => should work | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
end | ||
end | ||
@test DynamicPPL.determine_varinfo(demo4(); verbose=true) isa | ||
DynamicPPL.UntypedVarInfo | ||
|
||
# In this model, the type error occurs in the user code rather than in DynamicPPL. | ||
@model function demo5() | ||
x ~ Normal() | ||
xs = Any[] | ||
push!(xs, x) | ||
# `sum(::Vector{Any})` can potentially error unless the dynamic manages to resolve the | ||
# correct `zero` method. As a result, this code will run, but JET will raise this is an issue. | ||
return sum(xs) | ||
end | ||
# Should pass if we're only checking the tilde statements. | ||
@test DynamicPPL.determine_varinfo(demo5(); verbose=true) isa | ||
DynamicPPL.TypedVarInfo | ||
# Should fail if we're including errors in the model body. | ||
@test DynamicPPL.determine_varinfo(demo5(); verbose=true, only_tilde=false) isa | ||
DynamicPPL.UntypedVarInfo | ||
end | ||
|
||
@testset "demo models" begin | ||
@testset "$(model.f)" for model in DynamicPPL.TestUtils.DEMO_MODELS | ||
varinfo = DynamicPPL.DynamicPPL.determine_varinfo(model) | ||
# They should all result in typed. | ||
@test varinfo isa DynamicPPL.TypedVarInfo | ||
# But let's also make sure that they're not lying. | ||
f_eval, argtypes_eval = DynamicPPL.DebugUtils.gen_evaluator_call_with_types( | ||
model, varinfo | ||
) | ||
JET.test_call(f_eval, argtypes_eval) | ||
|
||
f_sample, argtypes_sample = DynamicPPL.DebugUtils.gen_evaluator_call_with_types( | ||
model, varinfo, DynamicPPL.SamplingContext() | ||
) | ||
JET.test_call(f_sample, argtypes_sample) | ||
end | ||
end | ||
end |
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.
Uh oh!
There was an error while loading. Please reload this page.