Skip to content

Commit 7ef5da7

Browse files
yebaigithub-actions[bot]devmotiontorfjelde
authored
Bugfix in VarInfo. (#516)
* Bugfix in `VarInfo`. * Update Project.toml * Update src/varinfo.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update src/varinfo.jl Co-authored-by: David Widmann <[email protected]> * Added tests. * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Fix for tests in #516 (#517) * fixed tests for linking of dirichlet with different dimensionality * added usage of same logp in TestUtils.setup_varinfos --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: David Widmann <[email protected]> Co-authored-by: Tor Erlend Fjelde <[email protected]>
1 parent 4a986df commit 7ef5da7

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "DynamicPPL"
22
uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8"
3-
version = "0.23.12"
3+
version = "0.23.13"
44

55
[deps]
66
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"

src/test_utils.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ function setup_varinfos(model::Model, example_values::NamedTuple, varnames)
5151
svi_typed = SimpleVarInfo(example_values)
5252
svi_untyped = SimpleVarInfo(OrderedDict())
5353

54+
lp = getlogp(vi_typed)
5455
return map((vi_untyped, vi_typed, svi_typed, svi_untyped)) do vi
5556
# Set them all to the same values.
56-
update_values!!(vi, example_values, varnames)
57+
DynamicPPL.setlogp!!(update_values!!(vi, example_values, varnames), lp)
5758
end
5859
end
5960

src/varinfo.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ setall!(vi::TypedVarInfo, val) = _setall!(vi.metadata, val)
366366
for f in names
367367
length = :(sum(length, metadata.$f.ranges))
368368
finish = :($start + $length - 1)
369-
push!(expr.args, :(metadata.$f.vals .= val[($start):($finish)]))
369+
push!(expr.args, :(copyto!(metadata.$f.vals, 1, val, $start, $length)))
370370
start = :($start + $length)
371371
end
372372
return expr

test/linking.jl

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,26 @@ end
9191
end
9292
end
9393

94+
# Related: https://github.com/TuringLang/DynamicPPL.jl/issues/504
9495
@testset "dirichlet" begin
95-
@model demo_dirichlet() = x ~ Dirichlet(2, 1.0)
96-
model = demo_dirichlet()
97-
vis = DynamicPPL.TestUtils.setup_varinfos(model, rand(model), (@varname(x),))
98-
@testset "$(short_varinfo_name(vi))" for vi in vis
99-
@test length(vi[:]) == 2
100-
@test iszero(getlogp(vi))
101-
# Linked.
102-
vi_linked = DynamicPPL.link!!(deepcopy(vi), model)
103-
@test length(vi_linked[:]) == 1
104-
@test !iszero(getlogp(vi_linked)) # should now include the log-absdet-jacobian correction
105-
# Invlinked.
106-
vi_invlinked = DynamicPPL.invlink!!(deepcopy(vi_linked), model)
107-
@test length(vi_invlinked[:]) == 2
108-
@test iszero(getlogp(vi_invlinked))
96+
@model demo_dirichlet(d::Int) = x ~ Dirichlet(d, 1.0)
97+
@testset "d=$d" for d in [2, 3, 5]
98+
model = demo_dirichlet(d)
99+
vis = DynamicPPL.TestUtils.setup_varinfos(model, rand(model), (@varname(x),))
100+
@testset "$(short_varinfo_name(vi))" for vi in vis
101+
lp = logpdf(Dirichlet(d, 1.0), vi[:])
102+
@test length(vi[:]) == d
103+
@test getlogp(vi) lp
104+
# Linked.
105+
vi_linked = DynamicPPL.link!!(deepcopy(vi), model)
106+
@test length(vi_linked[:]) == d - 1
107+
# Should now include the log-absdet-jacobian correction.
108+
@test !(getlogp(vi_linked) lp)
109+
# Invlinked.
110+
vi_invlinked = DynamicPPL.invlink!!(deepcopy(vi_linked), model)
111+
@test length(vi_invlinked[:]) == d
112+
@test getlogp(vi_invlinked) lp
113+
end
109114
end
110115
end
111116
end

0 commit comments

Comments
 (0)