-
Couldn't load subscription status.
- Fork 36
Implement returned for AbstractDict; deprecate {values, keys} method
#1096
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
Conversation
| julia> @model function demo(xs) | ||
| s ~ InverseGamma(2, 3) | ||
| m_shifted ~ Normal(10, √s) | ||
| m = m_shifted - 10 | ||
| for i in eachindex(xs) | ||
| xs[i] ~ Normal(m, √s) | ||
| end | ||
| return (m, ) | ||
| julia> @model function demo() | ||
| m ~ Normal() | ||
| return (mp1 = m + 1,) | ||
| end | ||
| demo (generic function with 2 methods) |
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.
I think a toy model suffices to demonstrate the behaviour, anything more just muddies the waters.
Benchmark Report for Commit fac86d2Computer InformationBenchmark Results |
src/model.jl
Outdated
| function returned(model::Model, parameters::Union{NamedTuple,AbstractDict{<:VarName}}) | ||
| # use `nothing` as the fallback to ensure that any missing parameters cause an error | ||
| ctx = InitContext(Random.default_rng(), InitFromParams(parameters, nothing)) | ||
| new_model = setleafcontext(model, ctx) | ||
| # We can't use new_model() because that overwrites it with an InitContext of its own. | ||
| return first(evaluate!!(new_model, VarInfo())) |
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.
The use of Random.default_rng() here, I think, still shows that there is inherent randomness in returned(), which is something that's been on my mind for a while. I don't think there are any real situations where this could matter, but here is a contrived example where it would:
@model function f()
x ~ Normal()
# todo: guard against __model__.context not having an rng
return rand(__model__.context.rng)
endTechnically, if you wanted returned on the model above to be reproducible, you would need to pass an rng argument to returned. This edge case is quite pathological (#721 is very related and I agree with the general principle that the user should not be trying to use the model's rng), so I'm not bothered enough to change the signature of returned, but I guess technically, it does exist.
What I'd really like is for there to be some FakeRNG <: AbstractRNG for which every call to rand() errors, and that would signify that "this is not meant to be used". But, eh.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1096 +/- ##
==========================================
+ Coverage 81.30% 81.43% +0.13%
==========================================
Files 40 40
Lines 3749 3749
==========================================
+ Hits 3048 3053 +5
+ Misses 701 696 -5 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
DynamicPPL.jl documentation for PR #1096 is available at: |
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.
One comment for discussion on accumulators, happy otherwise.
src/model.jl
Outdated
| ctx = InitContext(Random.default_rng(), InitFromParams(parameters, nothing)) | ||
| new_model = setleafcontext(model, ctx) | ||
| # We can't use new_model() because that overwrites it with an InitContext of its own. | ||
| return first(evaluate!!(new_model, VarInfo())) |
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 VarInfo() be called with an empty tuple of accumulators? None of the values are used, and it's a bug in a model to rely on the presence of any particular accumulator.
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 a bug in a model to rely on the presence of any particular accumulator
Is that specified somewhere? I'm not opposed to us declaring it as such, but I'm not aware if it's said anywhere.
(Note to self: even without tracking logp, InitFromParams is still safer than fix because of #1097; if that is fixed, then we can use fix, which would probably be faster)
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.
Is that specified somewhere?
Bug wasn't quite the right term. But it's unsupported syntax. I think that's implicit in such behaviour requiring refering to __varinfo__. The only other way of accessing accumulators that I can think of is through @addlogprob, and that one explicitly guards against missing accumulators.
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.
OK, happy to go with empty accs. I agree that using __varinfo__ is the dangerous thing and if people use it then they should be responsible for guarding against the presence/lack of any accumulators they need.
I was under the impression that some demo models had return (s = s, m = m, logp = getlogp(__varinfo__)) but I can't find it anymore, maybe that was removed the last time round already.
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.
I'll lump the mention about __varinfo__ into TuringLang/docs#660
HISTORY.md
Outdated
| ## 0.38.3 | ||
|
|
||
| Add an implementation of `returned(::Model, ::AbstractDict{<:VarName})`. | ||
| Also tweaked the implementation of `returned(::Model, ::NamedTuple)` to accumulate log-probabilities correctly. |
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.
If my proposal for no accumulators is accepted then this sentence needs changing.
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!
Closes #1095.