Skip to content

Allow nonnumeric observations in model checks - #1546

Merged
shravanngoswamii merged 3 commits into
mainfrom
sg/check-nonnumeric-observations
Nov 24, 2026
Merged

shravanngoswamii merged 3 commits into
mainfrom
sg/check-nonnumeric-observations

Conversation

@shravanngoswamii

@shravanngoswamii shravanngoswamii commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Valid observations need not be numeric.

using DynamicPPL, Distributions

struct SymbolChoice <: DiscreteUnivariateDistribution end
Base.eltype(::Type{SymbolChoice}) = Symbol
Distributions.loglikelihood(::SymbolChoice, x::Symbol) =
    x in (:left, :right) ? -log(2) : -Inf

@model function choice(y)
    x ~ Normal()
    y ~ SymbolChoice()
end

check_model(choice(:right))

Closes #1545

@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.34%. Comparing base (956dded) to head (c80b4e3).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1546   +/-   ##
=======================================
  Coverage   85.34%   85.34%           
=======================================
  Files          52       52           
  Lines        4714     4714           
=======================================
  Hits         4023     4023           
  Misses        691      691           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

Copy link
Copy Markdown
Contributor

DynamicPPL.jl documentation for PR #1546 is available at:
https://TuringLang.github.io/DynamicPPL.jl/previews/PR1546/

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Benchmarks

Performance Ratio: gradient time divided by log-density time.

For very small models these ratios are noisy across runs and machines; raw primal and gradient timings are more reliable. The benchmarks are aimed at DynamicPPL developers and mainly catch obvious allocation or type-stability regressions. See benchmark notes for details.

Primal times: main 956dded, PR c80b4e3

================================================================================================================================
                                                                     primal                            gradient                 
                                                             ----------------------  -------------------------------------------
Model                                         dim    linked        main          PR     FwdDiff    RvsDiff    Mooncake    Enzyme
--------------------------------------------------------------------------------------------------------------------------------
Simple assume observe*                          1     false     5.25 ns     4.17 ns       10.56    1200.84       10.68     10.24
Simple assume observe*                          1      true     5.25 ns     4.17 ns       10.59    1235.82       10.63     11.68
Smorgasbord                                   201     false     5.42 μs      4.2 μs      100.40     145.25        5.75      1.25
Smorgasbord                                   201      true     6.41 μs     4.99 μs       98.48     167.60        5.87      1.50
Smorgasbord fixed                             100     false     2.57 μs     2.16 μs       68.01     162.09        6.30      1.29
Smorgasbord fixed                             100      true     3.03 μs     2.44 μs       70.33     189.19        6.72      1.49
NamedTuple-field LHS variable*                  1     false     5.26 ns     4.17 ns       10.52    1292.02       10.79      9.95
NamedTuple-field LHS variable*                  1      true     5.26 ns     4.17 ns       10.66    1330.24        8.50     10.21
MvNormal argument-supplied observation*         1     false     70.7 ns     65.1 ns        2.12     651.49        3.25      2.24
MvNormal argument-supplied observation*         1      true     72.4 ns     65.3 ns        2.21     616.48        3.12      2.05
Loop univariate 1k                           1000     false     16.9 μs     12.2 μs     1366.60     378.19        3.49      0.89
Loop univariate 1k                           1000      true     19.0 μs     14.3 μs     1651.63     329.49        3.03      0.79
Multivariate 1k                              1000     false    924.0 ns    892.0 ns     2840.25     663.91        1.65      3.01
Multivariate 1k                              1000      true    918.0 ns    884.0 ns     3407.66     650.25        1.81      3.10
Loop univariate 10k                         10000     false    164.0 μs    117.0 μs    20472.51     468.39        3.73      0.80
Loop univariate 10k                         10000      true    176.0 μs    136.0 μs    18118.70     403.00        3.14      0.70
Multivariate 10k                            10000     false     9.23 μs      7.7 μs    59151.34     690.27        1.55      3.38
Multivariate 10k                            10000      true     9.05 μs     8.12 μs    54000.68     716.41        1.53      2.69
Dynamic                                       err     false     1.36 μs     1.09 μs         err      43.80       11.62      4.21
Dynamic                                        10      true     1.85 μs     1.44 μs        1.73      57.73        9.71      6.39
Submodel*                                       1     false     5.26 ns     4.19 ns       10.50    1447.87       12.86      9.83
Submodel*                                       1      true     5.26 ns     4.19 ns       10.56    1444.86       10.76     11.06
LDA                                             6      true     3.57 μs     2.82 μs        1.58      15.95       29.98     31.89
================================================================================================================================
Environment
Julia Version 1.13.4
Commit c1ac32ab733 (2026-11-13 17:30 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 4 × AMD EPYC 9V74 80-Core Processor
  WORD_SIZE: 64
  LLVM: libLLVM-20.1.8 (ORCJIT, znver4)
  GC: Built with stock GC
Threads: 1 default, 1 interactive, 1 GC (on 4 virtual cores)

@shravanngoswamii
shravanngoswamii marked this pull request as ready for review November 20, 2026 15:03

@yebai yebai left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up so quickly. A local run confirms that the reproducer in #1545 now passes check_model.

One side effect: applicable(isnan, x) is false for a tuple, so a NaN inside a tuple observation now passes the check silently; on main it threw a MethodError:

struct D <: DiscreteUnivariateDistribution end
Distributions.loglikelihood(::D, ::Tuple) = 0.0
@model m(y) = (x ~ Normal(); y ~ D())
check_model(m((:a, NaN)))  # true on this branch

The suggestions below use dispatch instead, as the issue proposes, with recursion into tuples added: isnan for numbers and false for anything else, so Missing no longer needs its own method. The only behaviour this drops is isnan defined on a type that is not a Number. The tests then go through the public check_model; a local run shows that each fails on main, and that the tuple test also fails with the applicable form.

The PR also needs a rebase for a conflict in HISTORY.md.

Comment thread src/debug_utils.jl Resolved
Comment thread test/debug_utils.jl Resolved
Comment thread test/debug_utils.jl Resolved
Comment thread HISTORY.md Resolved
@shravanngoswamii
shravanngoswamii added this pull request to the merge queue Nov 24, 2026
Merged via the queue into main with commit d3aeca3 Nov 24, 2026
23 checks passed
@shravanngoswamii
shravanngoswamii deleted the sg/check-nonnumeric-observations branch November 24, 2026 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

check_model throws MethodError: no method matching isnan(::Symbol) for an observed value that isn't a number

2 participants