Skip to content

Commit 6563e60

Browse files
authored
Use subsume when checking for repeated varnames (#602)
* when checking if a varname has been seen before, check subsume rather than just equality * bump patch version * added tests for getproperty
1 parent 5347acd commit 6563e60

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
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.26.0"
3+
version = "0.26.1"
44

55

66
[deps]

src/debug_utils.jl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,41 @@ function record_varname!(context::DebugContext, varname::VarName, dist)
246246
end
247247
context.varnames_seen[varname] += 1
248248
else
249+
# We need to check:
250+
# 1. Does this `varname` subsume any of the other keys.
251+
# 2. Does any of the other keys subsume `varname`.
252+
vns = collect(keys(context.varnames_seen))
253+
# Is `varname` subsumed by any of the other keys?
254+
idx_parent = findfirst(Base.Fix2(subsumes, varname), vns)
255+
if idx_parent !== nothing
256+
varname_parent = vns[idx_parent]
257+
if context.error_on_failure
258+
error(
259+
"varname $(varname_parent) used multiple times in model (subsumes $varname)",
260+
)
261+
else
262+
@warn "varname $(varname_parent) used multiple times in model (subsumes $varname)"
263+
end
264+
# Update count of parent.
265+
context.varnames_seen[varname_parent] += 1
266+
else
267+
# Does `varname` subsume any of the other keys?
268+
idx_child = findfirst(Base.Fix1(subsumes, varname), vns)
269+
if idx_child !== nothing
270+
varname_child = vns[idx_child]
271+
if context.error_on_failure
272+
error(
273+
"varname $(varname_child) used multiple times in model (subsumed by $varname)",
274+
)
275+
else
276+
@warn "varname $(varname_child) used multiple times in model (subsumed by $varname)"
277+
end
278+
279+
# Update count of child.
280+
context.varnames_seen[varname_child] += 1
281+
end
282+
end
283+
249284
context.varnames_seen[varname] = 1
250285
end
251286
end

test/debug_utils.jl

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,57 @@
5959
model = ModelOuterWorking()
6060
@test check_model(model; error_on_failure=true)
6161
end
62+
63+
@testset "subsumes (x then x[1])" begin
64+
@model function buggy_subsumes_demo_model()
65+
x = Vector{Float64}(undef, 2)
66+
x ~ MvNormal(zeros(2), I)
67+
x[1] ~ Normal()
68+
return nothing
69+
end
70+
buggy_model = buggy_subsumes_demo_model()
71+
72+
@test_logs (:warn,) (:warn,) check_model(buggy_model)
73+
issuccess = check_model(
74+
buggy_model; context=SamplingContext(), record_varinfo=false
75+
)
76+
@test !issuccess
77+
@test_throws ErrorException check_model(buggy_model; error_on_failure=true)
78+
end
79+
80+
@testset "subsumes (x[1] then x)" begin
81+
@model function buggy_subsumes_demo_model()
82+
x = Vector{Float64}(undef, 2)
83+
x[1] ~ Normal()
84+
x ~ MvNormal(zeros(2), I)
85+
return nothing
86+
end
87+
buggy_model = buggy_subsumes_demo_model()
88+
89+
@test_logs (:warn,) (:warn,) check_model(buggy_model)
90+
issuccess = check_model(
91+
buggy_model; context=SamplingContext(), record_varinfo=false
92+
)
93+
@test !issuccess
94+
@test_throws ErrorException check_model(buggy_model; error_on_failure=true)
95+
end
96+
97+
@testset "subsumes (x.a then x)" begin
98+
@model function buggy_subsumes_demo_model()
99+
x = (a=nothing,)
100+
x.a ~ Normal()
101+
x ~ Normal()
102+
return nothing
103+
end
104+
buggy_model = buggy_subsumes_demo_model()
105+
106+
@test_logs (:warn,) (:warn,) check_model(buggy_model)
107+
issuccess = check_model(
108+
buggy_model; context=SamplingContext(), record_varinfo=false
109+
)
110+
@test !issuccess
111+
@test_throws ErrorException check_model(buggy_model; error_on_failure=true)
112+
end
62113
end
63114

64115
@testset "incorrect use of condition" begin

0 commit comments

Comments
 (0)