Skip to content

Commit

Permalink
Fix for check_model on arrays with undef (#608)
Browse files Browse the repository at this point in the history
* fixes for debugtils

* bump patch version
  • Loading branch information
torfjelde authored May 17, 2024
1 parent 9098747 commit 278a82a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 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.27.0"
version = "0.27.1"


[deps]
Expand Down
10 changes: 9 additions & 1 deletion src/debug_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,15 @@ end

# tilde
_has_missings(x) = ismissing(x)
_has_missings(x::AbstractArray) = any(ismissing, x)
function _has_missings(x::AbstractArray)
# Can't just use `any` because `x` might contain `undef`.
for i in eachindex(x)
if isassigned(x, i) && _has_missings(x[i])
return true
end
end
return false
end

# assume
function record_pre_tilde_assume!(context::DebugContext, vn, dist, varinfo)
Expand Down
12 changes: 12 additions & 0 deletions test/debug_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,16 @@

@test !DynamicPPL.has_static_constraints(model)
end

@testset "vector with `undef`" begin
# Source: https://github.com/TuringLang/Turing.jl/pull/2218
@model function demo_undef(ns...)
x = Array{Real}(undef, ns...)
@. x ~ Normal(0, 2)
end
for ns in [(2,), (2, 2), (2, 2, 2)]
model = demo_undef(ns...)
@test check_model(model; error_on_failure=true)
end
end
end

0 comments on commit 278a82a

Please sign in to comment.