-
Notifications
You must be signed in to change notification settings - Fork 30
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
base: master
Are you sure you want to change the base?
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 84.66% 84.88% +0.21%
==========================================
Files 35 36 +1
Lines 4180 4207 +27
==========================================
+ Hits 3539 3571 +32
+ Misses 641 636 -5 ☔ View full report in Codecov by Sentry. |
Pull Request Test Coverage Report for Build 12097699282Details
💛 - 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 |
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
fixed call in docs
…o torfjelde/determine-varinfo
filtering the frames
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…o torfjelde/determine-varinfo
Uhmm this is weird. Looking at the CI logs, e.g. https://github.com/TuringLang/DynamicPPL.jl/actions/runs/12094832976/job/33727325064?pr=728#step:6:59, this PR is running with [email protected]. However, looking at the Project.toml, this is not allowed in the current commit o.O As in, it seems as if the CI is running with the Project.toml of the master for some reason? Is this intentional? |
Hmm, this is interesting. One of the demo models fails the JET.jl check specifically on ubuntu with Julia 1.10; however, I cannot reproduce this error locally on either of my devices (one is macos and one is linux). I'm guessing the source of the issue is this line DynamicPPL.jl/src/test_utils/models.jl Line 505 in 889c370
in combination with the following impl in Bijectors.jl Somehow, this results in a I'm uncertain if this is the line product_distribution([InverseGamma(2, 3) for _ in 1:d]) being inferred to a product of return logabsdetjac.((bijector(d),), eachcol(x)) and type inference just fails to infer this statement properly. I'll try to replace the broadcast with a function _logabsdetjac_dist(d::MultivariateDistribution, x::AbstractMatrix)
return map(Base.Fix1(logabsdetjac, bijector(d)), eachcol(x))
end which should be much better type-inference-wise. |
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Aye, it indeed seems like this requires a fix to Bijectors.jl. Buuuut if we're hitting this with JET.jl, then we're likely also hitting this with some other things sometimes, e.g. I'd assume that Mooncake.jl would also hit type instabilities here? @willtebbutt |
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.