Skip to content

Commit

Permalink
Use subsume when checking for repeated varnames (#602)
Browse files Browse the repository at this point in the history
* when checking if a varname has been seen before, check subsume rather
than just equality

* bump patch version

* added tests for getproperty
  • Loading branch information
torfjelde authored May 10, 2024
1 parent 5347acd commit 6563e60
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

2 comments on commit 6563e60

@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/106531

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.26.1 -m "<description of version>" 6563e6074ea287c489d475188914790fa5c9aad0
git push origin v0.26.1

Please sign in to comment.