Skip to content

Fix isnan for NamedTuple distributions #897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# DynamicPPL Changelog

## 0.35.9

Fixed the `isnan` check introduced in 0.35.7 for distributions which returned NamedTuple.

## 0.35.8

Added the `DynamicPPL.TestUtils.AD.run_ad` function to test the correctness and/or benchmark the performance of an automatic differentiation backend on DynamicPPL models.
Expand Down
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.35.8"
version = "0.35.9"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
6 changes: 5 additions & 1 deletion src/debug_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ function _has_missings(x::AbstractArray)
return false
end

_has_nans(x::NamedTuple) = any(_has_nans, x)
_has_nans(x::AbstractArray) = any(_has_nans, x)
_has_nans(x) = isnan(x)

# assume
function record_pre_tilde_assume!(context::DebugContext, vn, dist, varinfo)
record_varname!(context, vn, dist)
Expand Down Expand Up @@ -291,7 +295,7 @@ function record_pre_tilde_observe!(context::DebugContext, left, dist, varinfo)
)
end
# Check for NaN's as well
if any(isnan, left)
if _has_nans(left)
error(
"Encountered a NaN value on the left-hand side of an" *
" observe statement; this may indicate that your data" *
Expand Down
11 changes: 9 additions & 2 deletions test/debug_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,15 @@
x[i] ~ Normal(a)
end
end
model = demo_nan_in_data([1.0, NaN])
@test_throws ErrorException check_model(model; error_on_failure=true)
m = demo_nan_in_data([1.0, NaN])
@test_throws ErrorException check_model(m; error_on_failure=true)
# Test NamedTuples with nested arrays, see #898
@model function demo_nan_complicated(nt)
nt ~ product_distribution((x=Normal(), y=Dirichlet([2, 4])))
return x ~ Normal()
end
m = demo_nan_complicated((x=1.0, y=[NaN, 0.5]))
@test_throws ErrorException check_model(m; error_on_failure=true)
end

@testset "incorrect use of condition" begin
Expand Down
Loading