Skip to content

compiler: promote tilde writes without changing retained values - #1548

Open
yebai wants to merge 1 commit into
mainfrom
tilde-promote
Open

yebai wants to merge 1 commit into
mainfrom
tilde-promote

Conversation

@yebai

@yebai yebai commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Split of #1529 (part 4.3 of 12; part 4 comes as nine PRs).

A tilde write into existing storage could leave it unusable for a later tilde, or silently change a value the model did not write:

  • after x = (0, 0); x[2] ~ Normal(), x is the mixed tuple (0, draw), so a later x[1] ~ Normal() throws "same-type tuples only" under rand, VarInfo or LogDensityFunction;
  • a Float64 draw into Union{Missing,Int}[2^53 + 1, missing] rounds the retained 2^53 + 1 to 2^53.

Tilde writes now go through DynamicPPL's own setter instead of Accessors.set. A write into a tuple whose elements share one type promotes every element, so the first example gives (0.0, draw) and the later tilde succeeds. A write throws an ArgumentError if promotion would change a retained element, as in the second example, or if it would leave a tuple with mixed element types. Arrays with a plain isbits element type, such as Vector{Int}, are not checked, including when nested, because their unset entries hold arbitrary bits; a Float64 draw into [2^53 + 1, 0] therefore still rounds the retained element, as on main. Thread-safe evaluation, which accepts only in-place writes, keeps its existing path.

The mixed-tuple check changes the result for tuple arguments. For @model f(x) = (x[1] ~ Normal(); x), a plain call f((missing, missing)) returned (draw, missing) and now throws; pass Union{Missing,Float64}[missing, missing] or a tuple of the draw's type instead. HISTORY lists these changes as breaking.

The new setter also fixes three smaller cases:

  • x = fill(0.0); x[] ~ Normal() no longer throws under rand and LogDensityFunction, because VarNamedTuple storage now uses the same setter;
  • a keyword-index tilde such as x[X=2] ~ Normal() on a DimArray now writes in place, so x stays a DimArray rather than becoming its parent array; rand(model) still throws for keyword indices, which VarNamedTuple storage does not accept;
  • a latent tilde such as x[1] ~ Normal(), where x is missing or a nothing assigned in the model body, throws an ArgumentError naming the variable instead of a MethodError.

LogDensityFunction evaluation and ForwardDiff gradients of DynamicPPL.TestUtils.DEMO_MODELS are unchanged in inference, allocations and time.

@github-actions

Copy link
Copy Markdown
Contributor

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

@codecov

codecov Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.52055% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.49%. Comparing base (956dded) to head (75084b6).

Files with missing lines Patch % Lines
src/compiler.jl 91.66% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1548      +/-   ##
==========================================
+ Coverage   85.34%   85.49%   +0.14%     
==========================================
  Files          52       52              
  Lines        4714     4783      +69     
==========================================
+ Hits         4023     4089      +66     
- Misses        691      694       +3     

☔ 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

github-actions Bot commented Nov 21, 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 75084b6

================================================================================================================================
                                                                     primal                            gradient                 
                                                             ----------------------  -------------------------------------------
Model                                         dim    linked        main          PR     FwdDiff    RvsDiff    Mooncake    Enzyme
--------------------------------------------------------------------------------------------------------------------------------
Simple assume observe*                          1     false     5.37 ns     5.26 ns        9.42    1412.83        9.65      6.65
Simple assume observe*                          1      true     5.38 ns     5.26 ns        9.71    1386.30       10.20      6.45
Smorgasbord                                   201     false      5.3 μs     5.38 μs       79.94     137.89        5.47      1.33
Smorgasbord                                   201      true     6.32 μs     6.46 μs       76.93     160.95        5.63      1.46
Smorgasbord fixed                             100     false     2.76 μs      2.6 μs       52.92     162.27        7.16      1.32
Smorgasbord fixed                             100      true     3.15 μs     3.06 μs       62.52     182.51        6.66      1.49
NamedTuple-field LHS variable*                  1     false     5.38 ns     5.26 ns        9.51    1284.33       10.22      6.68
NamedTuple-field LHS variable*                  1      true     5.38 ns     5.26 ns        9.74    1455.05       10.20      6.68
MvNormal argument-supplied observation*         1     false     81.5 ns     74.0 ns        2.24     649.43        3.10      2.52
MvNormal argument-supplied observation*         1      true     80.7 ns     73.5 ns        2.45     646.31        3.37      2.41
Loop univariate 1k                           1000     false     15.7 μs     17.0 μs      852.02     323.56        3.07      0.77
Loop univariate 1k                           1000      true     18.3 μs     18.2 μs     1329.16     304.46        2.81      0.74
Multivariate 1k                              1000     false     1.13 μs     1.01 μs     1131.66     615.21        2.05      3.18
Multivariate 1k                              1000      true     1.15 μs     1.04 μs     1472.29     633.77        1.89      2.84
Loop univariate 10k                         10000     false    152.0 μs    165.0 μs    13160.91     377.33        3.12      0.72
Loop univariate 10k                         10000      true    177.0 μs    177.0 μs    13738.57     354.47        2.89      0.67
Multivariate 10k                            10000     false     9.19 μs     11.7 μs    22306.83     651.49        1.94      2.97
Multivariate 10k                            10000      true     10.2 μs     9.93 μs    25598.31     722.05        1.78      3.35
Dynamic                                       err     false     1.42 μs     1.51 μs         err      50.75       15.75      4.78
Dynamic                                        10      true      1.9 μs      2.0 μs        1.68      62.01       13.70      6.42
Submodel*                                       1     false      5.4 ns     5.27 ns       10.02    1704.41        8.09      6.32
Submodel*                                       1      true     5.39 ns     5.27 ns        9.86    1816.65        8.07      6.74
LDA                                             6      true     3.79 μs      3.9 μs        1.55      12.54       28.44     35.58
================================================================================================================================
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 7763 64-Core Processor
  WORD_SIZE: 64
  LLVM: libLLVM-20.1.8 (ORCJIT, znver3)
  GC: Built with stock GC
Threads: 1 default, 1 interactive, 1 GC (on 4 virtual cores)

A draw into the second element of `(0, 0)` left the mixed tuple
`(0, draw)`, so a later tilde into the first element threw when values
were recorded, and a `Float64` draw into
`Union{Missing,Int}[2^53 + 1, missing]` rounded the retained element.
Tilde writes now promote same-type tuples, and throw when promotion
would change a retained element.

`VarNamedTuple` storage now uses the same setter, so `x[] ~ dist` can
record values for zero-dimensional arrays. Keyword-index writes, such
as `x[X=2]` on a `DimArray`, keep the array type.

Assisted-by: Claude Code <noreply@anthropic.com>
Assisted-by: Codex <codex@openai.com>

This branch has not been deployed

No deployments
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.

1 participant