Skip to content
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

Open
wants to merge 8 commits into
base: breaking
Choose a base branch
from

Conversation

penelopeysm
Copy link
Member

@penelopeysm penelopeysm commented Apr 1, 2025

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:

const TypedVarInfo = VarInfo{<:NamedTuple}

This was fine in a pre-VarNamedVector world, because UntypedVarInfo meant VarInfo with Metadata, and TypedVarInfo (implicitly) meant VarInfo with NamedTuple of Metadata. However, with VNV now on board, a VarInfo with a NT of VNV also falls under TypedVarInfo. This means that sometimes TypedVarInfo 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 named typed_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:

  1. Introduces 4 new functions:
    • UntypedVarInfo([rng, ]model[, sampler, context]) --> VarInfo with Metadata
    • TypedVarInfo([rng, ]model[, sampler, context]) --> VarInfo with NT of Metadata
    • UntypedVectorVarInfo([rng, ]model[, sampler, context]) --> VarInfo with VNV
    • TypedVectorVarInfo([rng, ]model[, sampler, context]) --> VarInfo with NT of VNV

Note 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.

  1. VarInfo(rng, model, sampler, context) is deprecated, it can be directly replaced with TypedVarInfo(rng, model, sampler, context). I didn't want to get rid of it entirely because that would break quite a few things.

  2. metadata argument in VarInfo(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.

  3. 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 to NTVarInfo.

  4. The type VectorVarInfo = VarInfo{<:VarNamedVector} has been renamed to UntypedVectorVarInfo for consistency. This was not exported (and still isn't).

Copy link
Contributor

github-actions bot commented Apr 1, 2025

Benchmark Report for Commit 416d729

Computer Information

Julia Version 1.11.4
Commit 8561cc3d68d (2025-03-10 11:36 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 4 × AMD EPYC 7763 64-Core Processor
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, znver3)
Threads: 1 default, 0 interactive, 1 GC (on 4 virtual cores)

Benchmark Results

|                 Model | Dimension |  AD Backend |      VarInfo Type | Linked | Eval Time / Ref Time | AD Time / Eval Time |
|-----------------------|-----------|-------------|-------------------|--------|----------------------|---------------------|
| Simple assume observe |         1 | forwarddiff |             typed |  false |                 10.3 |                 1.5 |
|           Smorgasbord |       201 | forwarddiff |             typed |  false |                648.9 |                42.3 |
|           Smorgasbord |       201 | forwarddiff | simple_namedtuple |   true |                387.3 |                51.7 |
|           Smorgasbord |       201 | forwarddiff |           untyped |   true |               1283.2 |                27.9 |
|           Smorgasbord |       201 | forwarddiff |       simple_dict |   true |               3863.0 |                21.1 |
|           Smorgasbord |       201 | reversediff |             typed |   true |               1510.1 |                29.3 |
|           Smorgasbord |       201 |    mooncake |             typed |   true |                970.6 |                 5.3 |
|    Loop univariate 1k |      1000 |    mooncake |             typed |   true |               5902.1 |                 3.9 |
|       Multivariate 1k |      1000 |    mooncake |             typed |   true |               1129.7 |                 8.4 |
|   Loop univariate 10k |     10000 |    mooncake |             typed |   true |              65042.4 |                 3.6 |
|      Multivariate 10k |     10000 |    mooncake |             typed |   true |               9241.2 |                 9.7 |
|               Dynamic |        10 |    mooncake |             typed |   true |                136.4 |                12.5 |
|              Submodel |         1 |    mooncake |             typed |   true |                 27.9 |                 8.7 |
|                   LDA |        12 | reversediff |             typed |   true |                515.1 |                 4.7 |

@penelopeysm penelopeysm changed the title Unify {Untyped,Typed}{Vector,}VarInfo constructors Unify {Untyped,Typed}{Vector,}VarInfo 'constructors' Apr 1, 2025
Copy link

codecov bot commented Apr 1, 2025

Codecov Report

Attention: Patch coverage is 75.37313% with 33 lines in your changes missing coverage. Please review.

Please upload report for BASE (breaking@cc5e581). Learn more about missing BASE report.

Files with missing lines Patch % Lines
src/varinfo.jl 76.99% 26 Missing ⚠️
src/model_utils.jl 0.00% 6 Missing ⚠️
src/experimental.jl 0.00% 1 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

symbols have been observed. `VarInfo{<:NamedTuple}` is aliased `TypedVarInfo`.
symbols have been observed. `VarInfo{<:NamedTuple}` is aliased `NTVarInfo`.
Copy link
Member Author

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.

Comment on lines 94 to +97
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))
Copy link
Member Author

@penelopeysm penelopeysm Apr 1, 2025

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.

Comment on lines +173 to 188
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
Copy link
Member Author

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)
Copy link
Member Author

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

@penelopeysm penelopeysm marked this pull request as ready for review April 2, 2025 00:14
@penelopeysm penelopeysm requested a review from mhauru April 2, 2025 09:23
@mhauru
Copy link
Member

mhauru commented Apr 2, 2025

I need to do a proper review, but some thoughts and questions based on just the description of changes:

If {Typed,Untyped}VarInfo is no longer a type, but a function, should its name be lowercase?

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?

@penelopeysm
Copy link
Member Author

penelopeysm commented Apr 2, 2025

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 untyped_varinfo, typed_varinfo, untyped_vector_varinfo, typed_vector_varinfo. We did use to have the first two (I deleted them in this PR in favour of the type-lookalikes) but for some reason they weren't used very widely - it wouldn't be too hard to change the names.

(I'd actually prefer if it was md_varinfo, nt_md_varinfo, vnv_varinfo, and nt_vnv_varinfo)

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 {Unt,T}ypedVarInfo methods and renaming {Unt,T}ypedVectorVarInfo to them. Personally I view this PR as for the here and now - partly motivated by the need for this in AD testing.

However, if we think that ultimately we just have a single VarInfo() then I could leave that method in instead of deprecating it and then re-introducing it later. Maybe edit the docstring to add a warning that it's an implementation detail as to exactly what form of VarInfo this will return.

@mhauru
Copy link
Member

mhauru commented Apr 2, 2025

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.

Personally I view this PR as for the here and now

This sounds fair to me.

However, if we think that ultimately we just have a single VarInfo() then I could leave that method in instead of deprecating it and then re-introducing it later. Maybe edit the docstring to add a warning that it's an implementation detail as to exactly what form of VarInfo this will return.

I like this idea, assuming others are onboard with my vision of a future with only a single VarInfo type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants