Skip to content

Check the input length in logdensity - #1446

Open
shravanngoswamii wants to merge 2 commits into
mainfrom
sg/logdensity-dim-check
Open

Check the input length in logdensity#1446
shravanngoswamii wants to merge 2 commits into
mainfrom
sg/logdensity-dim-check

Conversation

@shravanngoswamii

@shravanngoswamii shravanngoswamii commented Aug 23, 2026

Copy link
Copy Markdown
Member

Fixes #1445.

LogDensityProblems.logdensity now checks the input length against ldf._dim and throws the same ArgumentError that the two-argument InitFromVector(vect, ldf) constructor already throws. Previously a shorter vector gave a BoundsError from inside model evaluation and a longer one silently returned the log density of the first dimension(ldf) elements.

ldf._dim is already stored, so the check costs nothing measurable. On a 21-dimensional model, logdensity takes 48.3 ns before and 48.2 ns after.

@codecov

codecov Bot commented Aug 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.57%. Comparing base (a8b0145) to head (682d000).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1446      +/-   ##
==========================================
- Coverage   81.70%   81.57%   -0.13%     
==========================================
  Files          50       50              
  Lines        3581     3583       +2     
==========================================
- Hits         2926     2923       -3     
- Misses        655      660       +5     

☔ 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.

@shravanngoswamii
shravanngoswamii force-pushed the sg/logdensity-dim-check branch from 39222c9 to d7a66c8 Compare August 23, 2026 19:38
@shravanngoswamii
shravanngoswamii changed the base branch from main to fix/issue-1407-logjac August 23, 2026 19:38
@github-actions

Copy link
Copy Markdown
Contributor

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

A shorter vector gave a BoundsError from inside model evaluation and a longer one
silently returned the density of the first `dimension(ldf)` elements.
@shravanngoswamii
shravanngoswamii force-pushed the sg/logdensity-dim-check branch from d7a66c8 to 682d000 Compare August 23, 2026 19:43
@shravanngoswamii
shravanngoswamii changed the base branch from fix/issue-1407-logjac to main August 23, 2026 19:43
@github-actions

Copy link
Copy Markdown
Contributor

Benchmarks @ 682d000

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.

===================================================================================================
                                               eval                       gradient                 
                                            ----------  -------------------------------------------
Model                        dim    linked      primal     FwdDiff    RvsDiff    Mooncake    Enzyme
---------------------------------------------------------------------------------------------------
Simple assume observe*         1     false     5.01 ns       12.03    1278.26       38.48     15.54
Simple assume observe*         1      true      5.0 ns       11.29    1456.26       38.02     16.70
Smorgasbord                  201     false     5.79 μs       71.50     136.53        6.96      7.57
Smorgasbord                  201      true     7.47 μs       71.00     139.73        6.28      5.56
Loop univariate 1k          1000     false     16.3 μs     1214.13     307.36        9.92      8.22
Loop univariate 1k          1000      true     19.8 μs     1577.27     266.14        8.08      6.87
Multivariate 1k             1000     false     25.0 μs      329.46      67.44        8.07      2.01
Multivariate 1k             1000      true     23.7 μs      297.98      67.34        8.90      2.05
Loop univariate 10k        10000     false    157.0 μs    17070.02     374.51       10.33      8.17
Loop univariate 10k        10000      true    193.0 μs    14858.25     308.13        8.37      6.72
Multivariate 10k           10000     false    218.0 μs     4613.17      80.43       10.17      1.87
Multivariate 10k           10000      true    221.0 μs     4577.92      79.48       10.16      1.85
Dynamic                       15     false     1.43 μs         err      43.96       13.48     11.00
Dynamic                       10      true     2.02 μs        1.85      56.46       11.96     18.74
Submodel*                      1     false     5.01 ns       12.10    1641.18       42.74     15.31
Submodel*                      1      true     4.98 ns       11.75    1710.19       39.84     17.35
LDA                           12      true     25.4 μs        0.67       1.93       28.46       err
===================================================================================================
Main @ a8b0145
===================================================================================================
                                               eval                       gradient                 
                                            ----------  -------------------------------------------
Model                        dim    linked      primal     FwdDiff    RvsDiff    Mooncake    Enzyme
---------------------------------------------------------------------------------------------------
Simple assume observe*         1     false     4.63 ns       12.60    1550.11       46.43     12.22
Simple assume observe*         1      true     4.63 ns       14.38    1742.72       39.68     12.62
Smorgasbord                  201     false     5.91 μs       67.38     135.57        6.99      9.81
Smorgasbord                  201      true     7.56 μs       68.81     152.15        6.22      6.92
Loop univariate 1k          1000     false     17.7 μs      952.51     314.04        7.95      6.65
Loop univariate 1k          1000      true     19.0 μs     1503.46     295.39        7.52      6.22
Multivariate 1k             1000     false     21.2 μs      394.89      82.09        9.10      2.88
Multivariate 1k             1000      true     27.8 μs      264.66      56.73        8.14      2.99
Loop univariate 10k        10000     false    172.0 μs    11712.54     338.27        8.47      6.73
Loop univariate 10k        10000      true    185.0 μs    12210.51     317.90        7.69      6.25
Multivariate 10k           10000     false    193.0 μs     6125.64      92.52       11.37      2.27
Multivariate 10k           10000      true    194.0 μs     5952.09      91.29       11.40      2.22
Dynamic                       15     false     1.43 μs         err      42.22       15.30     11.83
Dynamic                       10      true     1.97 μs        1.86      58.24       13.35     18.76
Submodel*                      1     false     4.63 ns       12.63    1680.92       42.06     12.22
Submodel*                      1      true     4.64 ns       12.60    1837.49       39.96     12.53
LDA                           12      true     23.4 μs        0.46       2.01       33.91       err
===================================================================================================
Environment
Julia Version 1.11.9
Commit 53a02c0720c (2026-02-06 00:27 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-16.0.6 (ORCJIT, znver4)
Threads: 1 default, 0 interactive, 1 GC (on 4 virtual cores)

@shravanngoswamii
shravanngoswamii requested a review from yebai August 23, 2026 21:52

@sunxd3 sunxd3 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.

looks fine

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.

logdensity silently accepts an over-long parameter vector

2 participants