-
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
Conversation
…orfjelde/determine-varinfo
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
See the tests for what we can properly check here. It honestly seems really good for our purposes 👀 |
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
That seems like an elegant trick! |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #728 +/- ##
==========================================
+ Coverage 86.34% 86.49% +0.15%
==========================================
Files 34 36 +2
Lines 4254 4272 +18
==========================================
+ Hits 3673 3695 +22
+ Misses 581 577 -4 ☔ View full report in Codecov by Sentry. |
Pull Request Test Coverage Report for Build 12236332369Details
💛 - Coveralls |
fallback to current behavior + `supports_varinfo` to `is_suitable_varinfo`
longer needed on Julia 1.10 and onwards + added error hint for when JET.jl has not been loaded
provided context, but uses `SamplingContext` by default (as this should be a stricter check than just evaluation)
the ambiguous `VarINfo`
in sampling context now so no need to handle this explicitly elsewhere
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This honestly seem to work really well. I've now made it so that |
…o torfjelde/determine-varinfo
Added docs for `determine_suitable_varinfo` and existing methods that should be documented, e.g. `untyped_varinfo`, `typed_varinfo`, and `default_varinfo`
Added some docs 👍 |
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.
Thanks @torfjelde, this seems really handy. Had a few localised comments, nothing major.
Base.Experimental.register_error_hint(MethodError) do io, exc, argtypes, _ | ||
requires_jet = | ||
exc.f === DynamicPPL.Experimental._determine_varinfo_jet && | ||
length(argtypes) >= 2 && | ||
argtypes[1] <: Model && | ||
argtypes[2] <: AbstractContext | ||
requires_jet |= | ||
exc.f === DynamicPPL.Experimental.is_suitable_varinfo && | ||
length(argtypes) >= 3 && | ||
argtypes[1] <: Model && | ||
argtypes[2] <: AbstractContext && | ||
argtypes[3] <: AbstractVarInfo | ||
if requires_jet | ||
print( | ||
io, | ||
"\n$(exc.f) requires JET.jl to be loaded. Please run `using JET` before calling $(exc.f).", | ||
) | ||
end | ||
end |
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?
@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.Experimental.determine_suitable_varinfo(demo5()) isa | ||
DynamicPPL.TypedVarInfo | ||
# Should fail if we're including errors in the model body. | ||
@test DynamicPPL.Experimental.determine_suitable_varinfo( | ||
demo5(); only_ddpl=false | ||
) isa DynamicPPL.UntypedVarInfo |
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 is the example mentioned above @mhauru :)
Co-authored-by: Markus Hauru <[email protected]>
You happy with this now @mhauru ?:) It's only failiing because of the x86 OOM thingy |
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.
Yup, am happy, thanks!
After a quick experiment with JET.jl I found some bugs in DynamicPPL.jl (#726), but also realized that we can JET.jl to properly check whether the a given model supports the usage of
TypedVarInfo
rather than requiringUntypedVarInfo
.This has been a looooooong standing issue, and this seems to work really, really well.
The problem
In Turing.jl, we use
TypedVarInfo
almost everywhere due to the performance charactersitics that come with it. The problem is that we do so by simply evaluating the givenmodel
once and then using the resulting (hopefully, concretety typed) varinfo for all subsequent computations. This works nicely for most typical models, but fails horribly (and uninformably) for a good chunk of models, such asHere we will execute the model once and get, say, a
TypedVarInfo
containing the variablesx
andy
(becausex
happend to result in atrue
sample). If we then re-use this varinfo for sampling, we will ofc run into issues sincez
is nowhere to be seen.Technically we can handle this by just widing the container a bit, but if we do that, we need to cpature the new varinfo, which isn't always possible, e.g. when using the
LogDensityFunction
in a sampler.As a result, we have a lot of code that just makes the assumption "surely this model is 'static' in what variables and types it contains", which can sometimes be false.
The solution
This PR introduces a
determine_varinfo
method, which can automagically figure out whether we can use the type stable varinfo properly (i.e. without having to always capture the resulting varinfo, etc.) or if we need to use the untyped varinfo using abstract interpretation offered by JET.jl, all done statically.Effectively what
determine_varinfo
does is:NamedTuple{(:x, :y)}
cannot handle the value forz
being updated (because the entry does not exist).Note that this method doesn't say anything about whether there might be type instabilities; this only checks if we would encounter errors. We can also use JET to check type instabilites, etc., but I think that's a separate functionality and thus PR.