Skip to content

Commit

Permalink
Merge 8640ff4 into 5347acd
Browse files Browse the repository at this point in the history
  • Loading branch information
torfjelde committed May 9, 2024
2 parents 5347acd + 8640ff4 commit aa603c0
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
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.26.0"
version = "0.26.1"


[deps]
Expand Down
35 changes: 35 additions & 0 deletions src/debug_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,41 @@ function record_varname!(context::DebugContext, varname::VarName, dist)
end
context.varnames_seen[varname] += 1
else
# We need to check:
# 1. Does this `varname` subsume any of the other keys.
# 2. Does any of the other keys subsume `varname`.
vns = collect(keys(context.varnames_seen))
# Is `varname` subsumed by any of the other keys?
idx_parent = findfirst(Base.Fix2(subsumes, varname), vns)
if idx_parent !== nothing
varname_parent = vns[idx_parent]
if context.error_on_failure
error(
"varname $(varname_parent) used multiple times in model (subsumes $varname)",
)
else
@warn "varname $(varname_parent) used multiple times in model (subsumes $varname)"
end
# Update count of parent.
context.varnames_seen[varname_parent] += 1
else
# Does `varname` subsume any of the other keys?
idx_child = findfirst(Base.Fix1(subsumes, varname), vns)
if idx_child !== nothing
varname_child = vns[idx_child]
if context.error_on_failure
error(
"varname $(varname_child) used multiple times in model (subsumed by $varname)",
)
else
@warn "varname $(varname_child) used multiple times in model (subsumed by $varname)"
end

# Update count of child.
context.varnames_seen[varname_child] += 1
end
end

context.varnames_seen[varname] = 1
end
end
Expand Down
51 changes: 51 additions & 0 deletions test/debug_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,57 @@
model = ModelOuterWorking()
@test check_model(model; error_on_failure=true)
end

@testset "subsumes (x then x[1])" begin
@model function buggy_subsumes_demo_model()
x = Vector{Float64}(undef, 2)
x ~ MvNormal(zeros(2), I)
x[1] ~ Normal()
return nothing
end
buggy_model = buggy_subsumes_demo_model()

@test_logs (:warn,) (:warn,) check_model(buggy_model)
issuccess = check_model(
buggy_model; context=SamplingContext(), record_varinfo=false
)
@test !issuccess
@test_throws ErrorException check_model(buggy_model; error_on_failure=true)
end

@testset "subsumes (x[1] then x)" begin
@model function buggy_subsumes_demo_model()
x = Vector{Float64}(undef, 2)
x[1] ~ Normal()
x ~ MvNormal(zeros(2), I)
return nothing
end
buggy_model = buggy_subsumes_demo_model()

@test_logs (:warn,) (:warn,) check_model(buggy_model)
issuccess = check_model(
buggy_model; context=SamplingContext(), record_varinfo=false
)
@test !issuccess
@test_throws ErrorException check_model(buggy_model; error_on_failure=true)
end

@testset "subsumes (x.a then x)" begin
@model function buggy_subsumes_demo_model()
x = (a=nothing,)
x.a ~ Normal()
x ~ Normal()
return nothing
end
buggy_model = buggy_subsumes_demo_model()

@test_logs (:warn,) (:warn,) check_model(buggy_model)
issuccess = check_model(
buggy_model; context=SamplingContext(), record_varinfo=false
)
@test !issuccess
@test_throws ErrorException check_model(buggy_model; error_on_failure=true)
end
end

@testset "incorrect use of condition" begin
Expand Down

0 comments on commit aa603c0

Please sign in to comment.