-
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
Unify {Untyped,Typed}{Vector,}VarInfo
'constructors'
#879
base: breaking
Are you sure you want to change the base?
Conversation
Benchmark Report for Commit 416d729Computer Information
Benchmark Results
|
{Untyped,Typed}{Vector,}VarInfo
constructors{Untyped,Typed}{Vector,}VarInfo
'constructors'
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## breaking #879 +/- ##
===========================================
Coverage ? 84.89%
===========================================
Files ? 34
Lines ? 3841
Branches ? 0
===========================================
Hits ? 3261
Misses ? 580
Partials ? 0 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
symbols have been observed. `VarInfo{<:NamedTuple}` is aliased `TypedVarInfo`. | ||
symbols have been observed. `VarInfo{<:NamedTuple}` is aliased `NTVarInfo`. |
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.
Unsurprisingly, the changes are pretty much restricted to this file. The rest of the changes are pretty much just updating VarInfo -> TypedVarInfo.
logp::Base.RefValue{Tlogp} | ||
num_produce::Base.RefValue{Int} | ||
end | ||
const VectorVarInfo = VarInfo{<:VarNamedVector} | ||
VarInfo(meta=Metadata()) = VarInfo(meta, Ref{LogProbType}(0.0), Ref(0)) |
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 idea going forward is that VarInfo
is purely a low-level constructor for the struct itself, i.e., it is used to construct the VarInfo object from its fields, rather than doing something to instantiate those fields. Thus, one should not expect to be using it except at the very lowest levels of DynamicPPL, e.g. if one is testing basic properties of VarInfo objects.
function UntypedVarInfo( | ||
model::Model, | ||
sampler::AbstractSampler=SampleFromPrior(), | ||
context::AbstractContext=DefaultContext(), | ||
) | ||
# No rng | ||
return UntypedVarInfo(Random.default_rng(), model, sampler, context) | ||
end | ||
function UntypedVarInfo(rng::Random.AbstractRNG, model::Model, context::AbstractContext) | ||
# No sampler | ||
return UntypedVarInfo(rng, model, SampleFromPrior(), context) | ||
end | ||
function UntypedVarInfo(model::Model, context::AbstractContext) | ||
# No sampler, no rng | ||
return UntypedVarInfo(model, SampleFromPrior(), context) | ||
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.
I kinda dislike that i had to copy-paste this 4 times, once for each function. But I don't see a better way around it beyond doing indiscriminate f(args...) = f(g(args...))
.
We could make it quite a bit simpler by forbidding f(rng, model, context)
and f(model, context)
, i.e., if we want to specify a context then you also have to specify a sampler.
""" | ||
function untyped_varinfo( | ||
function TypedVarInfo(vi::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 all shifted from lower in the file, it's not new code
I need to do a proper review, but some thoughts and questions based on just the description of changes: If The Vector versions are not exported, which I think is good. I view UntypedVectorVarInfo as the king-in-waiting, that will replace UntypedVarInfo once #881 is done. I also dream of a future where all VarInfos are typed, which we could handle using BangBang functions: If new elements need to be added to the NamedTuple we do so by creating a new object, otherwise we operate in-place. In that case I think we would have plain VarInfo be the only user-facing constructor. Do others have thoughts on the future external interface of this stuff? |
Yup - TypedVarInfo and TypedVectorVarInfo aren't types. I'm not opposed to making them lowercase but I guess for consistency everything should be lowercased so (I'd actually prefer if it was About interface: part of the thing I had in mind with this PR was to keep the interface relatively easy to change in the future. For example replacing MD with VNV is quite straightforward and basically means deleting the existing However, if we think that ultimately we just have a single |
I think if this was the final word on VarInfo, we should make the function names lowercase, because it's a pretty clear violation of Julia style conventions. However, I can see an argument for keeping them as they are to have less disruption if/when we change these again.
This sounds fair to me.
I like this idea, assuming others are onboard with my vision of a future with only a single |
Partly solves #862 (There's still the SimpleVarInfos; but this PR is big enough).
Why
There are a couple of issues that this PR attempts to solve.
First is the ambiguous meaning of
TypedVarInfo
. Its literal definition is:DynamicPPL.jl/src/varinfo.jl
Line 99 in eed80e5
This was fine in a pre-VarNamedVector world, because
UntypedVarInfo
meant VarInfo with Metadata, andTypedVarInfo
(implicitly) meant VarInfo with NamedTuple of Metadata. However, with VNV now on board, a VarInfo with a NT of VNV also falls underTypedVarInfo
. This means that sometimesTypedVarInfo
means "VarInfo with NT of anything" (paricularly when used in type signatures), but sometimes it still means "VarInfo with NT of Metadata" (see e.g. the similarly namedtyped_varinfo
function).Second is the inconsistent interface between different VarInfo constructors. We used to have
VarInfo(rng, model, ...)
as a very convenient way of constructing a TypedVarInfo (read: a VarInfo with NT of Metadata). None of the other possibilities had equally convenient methods.What
This PR therefore does the following:
UntypedVarInfo([rng, ]model[, sampler, context])
--> VarInfo with MetadataTypedVarInfo([rng, ]model[, sampler, context])
--> VarInfo with NT of MetadataUntypedVectorVarInfo([rng, ]model[, sampler, context])
--> VarInfo with VNVTypedVectorVarInfo([rng, ]model[, sampler, context])
--> VarInfo with NT of VNVNote that all four functions are implemented such that if the rng is the same, the values will be the same, regardless of which type of VarInfo is requested.
VarInfo(rng, model, sampler, context)
is deprecated, it can be directly replaced withTypedVarInfo(rng, model, sampler, context)
. I didn't want to get rid of it entirely because that would break quite a few things.metadata
argument inVarInfo(rng, model, sampler, context, metadata)
no longer works. It was basically being used to specify whether it should be a Metadata or a VarNamedVector inside. If you wanted Metadata (the default) then use the{Untyped,Typed}VarInfo
; if you wanted VarNamedVector then use{Untyped,Typed}VectorVarInfo
.TypedVarInfo
is now only a function that constructs a VarInfo with a NamedTuple of Metadata. It is no longer a type. The type itself has been renamed toNTVarInfo
.The type
VectorVarInfo = VarInfo{<:VarNamedVector}
has been renamed toUntypedVectorVarInfo
for consistency. This was not exported (and still isn't).