Skip to content

Commit

Permalink
Allow merge to work on VarInfo with different distributions (#562)
Browse files Browse the repository at this point in the history
* allow different distributions in merge

* added tests for merge with different distributions

* bump patch version since this is a bug

* Apply suggestions from code review

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
torfjelde and github-actions[bot] authored Nov 17, 2023
1 parent 485ebfb commit c9489aa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
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.24.0"
version = "0.24.1"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand Down
15 changes: 7 additions & 8 deletions src/varinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,9 @@ function merge_metadata(metadata_left::Metadata, metadata_right::Metadata)
push!(ranges, r)
offset = r[end]
# `dists`: only valid if they're the same.
dists_left = getdist(metadata_left, vn)
dists_right = getdist(metadata_right, vn)
@assert dists_left == dists_right
push!(dists, dists_left)
dist_right = getdist(metadata_right, vn)
# Give precedence to `metadata_right`.
push!(dists, dist_right)
# `orders`: giving precedence to `metadata_right`
push!(orders, getorder(metadata_right, vn))
# `flags`
Expand All @@ -418,8 +417,8 @@ function merge_metadata(metadata_left::Metadata, metadata_right::Metadata)
push!(ranges, r)
offset = r[end]
# `dists`
dists_left = getdist(metadata_left, vn)
push!(dists, dists_left)
dist_left = getdist(metadata_left, vn)
push!(dists, dist_left)
# `orders`
push!(orders, getorder(metadata_left, vn))
# `flags`
Expand All @@ -436,8 +435,8 @@ function merge_metadata(metadata_left::Metadata, metadata_right::Metadata)
push!(ranges, r)
offset = r[end]
# `dists`
dists_right = getdist(metadata_right, vn)
push!(dists, dists_right)
dist_right = getdist(metadata_right, vn)
push!(dists, dist_right)
# `orders`
push!(orders, getorder(metadata_right, vn))
# `flags`
Expand Down
24 changes: 24 additions & 0 deletions test/varinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,30 @@ DynamicPPL.getspace(::DynamicPPL.Sampler{MySAlg}) = (:s,)
end
end
end

@testset "different models" begin
@model function demo_merge_different_y()
x ~ Uniform()
return y ~ Normal()
end
@model function demo_merge_different_z()
x ~ Normal()
return z ~ Normal()
end
model_left = demo_merge_different_y()
model_right = demo_merge_different_z()

varinfo_left = VarInfo(model_left)
varinfo_right = VarInfo(model_right)

varinfo_merged = merge(varinfo_left, varinfo_right)
vns = [@varname(x), @varname(y), @varname(z)]
check_varinfo_keys(varinfo_merged, vns)

# Right has precedence.
@test varinfo_merged[@varname(x)] == varinfo_right[@varname(x)]
@test DynamicPPL.getdist(varinfo_merged, @varname(x)) isa Normal
end
end

@testset "VarInfo with selectors" begin
Expand Down

2 comments on commit c9489aa

@torfjelde
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/95566

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.24.1 -m "<description of version>" c9489aa1f1a40b43956184cec1968a7532c92a5a
git push origin v0.24.1

Please sign in to comment.