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

Fixes for SimpleVarInfo with Ref #527

Merged
merged 4 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DynamicPPL"
uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8"
version = "0.23.14"
version = "0.23.15"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand Down
18 changes: 10 additions & 8 deletions src/simple_varinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,11 @@ end
Base.isempty(vi::SimpleVarInfo) = isempty(vi.values)

getlogp(vi::SimpleVarInfo) = vi.logp
getlogp(vi::SimpleVarInfo{<:Any,<:Ref}) = vi.logp[]

setlogp!!(vi::SimpleVarInfo, logp) = Setfield.@set vi.logp = logp
acclogp!!(vi::SimpleVarInfo, logp) = Setfield.@set vi.logp = getlogp(vi) + logp

"""
keys(vi::SimpleVarInfo)

Return an iterator of keys present in `vi`.
"""
Base.keys(vi::SimpleVarInfo) = keys(vi.values)
Base.keys(vi::SimpleVarInfo{<:NamedTuple}) = map(k -> VarName{k}(), keys(vi.values))

function setlogp!!(vi::SimpleVarInfo{<:Any,<:Ref}, logp)
vi.logp[] = logp
return vi
Expand All @@ -280,6 +274,14 @@ function acclogp!!(vi::SimpleVarInfo{<:Any,<:Ref}, logp)
return vi
end

"""
keys(vi::SimpleVarInfo)

Return an iterator of keys present in `vi`.
"""
Base.keys(vi::SimpleVarInfo) = keys(vi.values)
Base.keys(vi::SimpleVarInfo{<:NamedTuple}) = map(k -> VarName{k}(), keys(vi.values))

function Base.show(io::IO, ::MIME"text/plain", svi::SimpleVarInfo)
if !(svi.transformation isa NoTransformation)
print(io, "Transformed ")
Expand Down
12 changes: 9 additions & 3 deletions src/test_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,22 @@ Return a tuple of instances for different implementations of `AbstractVarInfo` w
each `vi`, supposedly, satisfying `vi[vn] == get(example_values, vn)` for `vn` in `varnames`.
"""
function setup_varinfos(model::Model, example_values::NamedTuple, varnames)
# <:VarInfo
# VarInfo
vi_untyped = VarInfo()
model(vi_untyped)
vi_typed = DynamicPPL.TypedVarInfo(vi_untyped)
# <:SimpleVarInfo
# SimpleVarInfo
svi_typed = SimpleVarInfo(example_values)
svi_untyped = SimpleVarInfo(OrderedDict())

# SimpleVarInfo{<:Any,<:Ref}
svi_typed_ref = SimpleVarInfo(example_values, Ref(getlogp(svi_typed)))
svi_untyped_ref = SimpleVarInfo(OrderedDict(), Ref(getlogp(svi_untyped)))

lp = getlogp(vi_typed)
return map((vi_untyped, vi_typed, svi_typed, svi_untyped)) do vi
return map((
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's slightly strange that we performed a functionality test for setlogp!! inside this utility function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, maybe. But it's really not something we rely on. This is just to make the varinfos as consistent with each other as possible, i.e. they should have the same values and the same logp.

Alternatively, we can always just use zero instead, but then that would also require setlogp!! 😕

vi_untyped, vi_typed, svi_typed, svi_untyped, svi_typed_ref, svi_untyped_ref
)) do vi
# Set them all to the same values.
DynamicPPL.setlogp!!(update_values!!(vi, example_values, varnames), lp)
end
Expand Down
Loading