Skip to content

More intuitive constructor names for TypedVarInfo and UntypedVarInfo #862

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

Closed
yebai opened this issue Mar 26, 2025 · 3 comments
Closed

More intuitive constructor names for TypedVarInfo and UntypedVarInfo #862

yebai opened this issue Mar 26, 2025 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@yebai
Copy link
Member

yebai commented Mar 26, 2025

Current way to construct typed and untyped VarInfo:

# Untyped VarInfo 
vi = VarInfo()
model(rng, vi)
vi

# Typed VarInfo 
VarInfo(rng, model)

A more intuitive way would be

# Untyped VarInfo 
UntypedVarInfo(rng, model)


# Typed VarInfo 
TypedVarInfo(rng, model)

See, e.g.: https://github.com/TuringLang/DynamicPPL.jl/pull/861/files#diff-453c0f6cda4a57b858cf8a049aa4e0d01085433ca084c71982ff18da3684e324R38-R46

@penelopeysm
Copy link
Member

The SimpleVarInfos need better constructors too imo.

@penelopeysm
Copy link
Member

see also

"""
setup_varinfos(model::Model, example_values::NamedTuple, varnames; include_threadsafe::Bool=false)
Return a tuple of instances for different implementations of `AbstractVarInfo` with
each `vi`, supposedly, satisfying `vi[vn] == get(example_values, vn)` for `vn` in `varnames`.
If `include_threadsafe` is `true`, then the returned tuple will also include thread-safe versions
of the varinfo instances.
"""
function setup_varinfos(
model::Model, example_values::NamedTuple, varnames; include_threadsafe::Bool=false
)
# VarInfo
vi_untyped_metadata = VarInfo(DynamicPPL.Metadata())
vi_untyped_vnv = VarInfo(DynamicPPL.VarNamedVector())
model(vi_untyped_metadata)
model(vi_untyped_vnv)
vi_typed_metadata = DynamicPPL.TypedVarInfo(vi_untyped_metadata)
vi_typed_vnv = DynamicPPL.TypedVarInfo(vi_untyped_vnv)
# SimpleVarInfo
svi_typed = SimpleVarInfo(example_values)
svi_untyped = SimpleVarInfo(OrderedDict())
svi_vnv = SimpleVarInfo(DynamicPPL.VarNamedVector())
# SimpleVarInfo{<:Any,<:Ref}
svi_typed_ref = SimpleVarInfo(example_values, Ref(getlogp(svi_typed)))
svi_untyped_ref = SimpleVarInfo(OrderedDict(), Ref(getlogp(svi_untyped)))
svi_vnv_ref = SimpleVarInfo(DynamicPPL.VarNamedVector(), Ref(getlogp(svi_vnv)))
lp = getlogp(vi_typed_metadata)
varinfos = map((
vi_untyped_metadata,
vi_untyped_vnv,
vi_typed_metadata,
vi_typed_vnv,
svi_typed,
svi_untyped,
svi_vnv,
svi_typed_ref,
svi_untyped_ref,
svi_vnv_ref,
)) do vi
# Set them all to the same values.
DynamicPPL.setlogp!!(update_values!!(vi, example_values, varnames), lp)
end
if include_threadsafe
varinfos = (varinfos..., map(DynamicPPL.ThreadSafeVarInfo deepcopy, varinfos)...)
end
return varinfos
end

@penelopeysm
Copy link
Member

Closed by #879, released in 0.36 #829

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

No branches or pull requests

2 participants