Skip to content

Conversation

@penelopeysm
Copy link
Member

@penelopeysm penelopeysm commented Dec 13, 2025

Closes #1176. The result of this PR on the model macro

using DynamicPPL, Distributions
@macroexpand @model function test(data)
    data.x ~ Normal()
end

is shown in the following diff. Essentially the result is to avoid setting to data.x directly:

 quote
     function test(__model__::Model, __varinfo__::AbstractVarInfo, data::Any; )
         #= REPL[4]:1 =#
         begin
             var"##dist#246" = Normal()
             var"##vn#243" = (DynamicPPL.resolve_varnames)((VarName){:data}((Accessors.opticcompose)((Accessors.PropertyLens){:x}())), var"##dist#246")
             var"##isassumption#244" = begin
                     if (DynamicPPL.contextual_isassumption)(__model__.context, (prefix)(__model__.context, var"##vn#243"))
                         if !((DynamicPPL.inargnames)(var"##vn#243", __model__)) || (DynamicPPL.inmissings)(var"##vn#243", __model__)
                             true
                         else
                             data.x === missing
                         end
                     else
                         false
                     end
                 end
             begin
                 #= /Users/pyong/ppl/dppl/src/compiler.jl:599 =#
                 var"##retval#248" = if (DynamicPPL.contextual_isfixed)(__model__.context, (prefix)(__model__.context, var"##vn#243"))
-                        data.x = (DynamicPPL.getfixed_nested)(__model__.context, (prefix)(__model__.context, var"##vn#243"))
+                        data = (Accessors.set)(data, (BangBang.AccessorsImpl.prefermutation)((Accessors.opticcompose)((Accessors.PropertyLens){:x}())), (DynamicPPL.getfixed_nested)(__model__.context, (prefix)(__model__.context, var"##vn#243")))
                     elseif var"##isassumption#244"
                         begin
                             (var"##value#247", __varinfo__) = (tilde_assume!!)(__model__.context, (DynamicPPL.unwrap_right_vn)((DynamicPPL.check_tilde_rhs)(var"##dist#246"), var"##vn#243")..., __varinfo__)
                             data = (Accessors.set)(data, (BangBang.AccessorsImpl.prefermutation)((Accessors.opticcompose)((Accessors.PropertyLens){:x}())), var"##value#247")
                             var"##value#247"
                         end
                     else
-                        data.x = if (DynamicPPL.inargnames)(var"##vn#243", __model__)
+                        var"##supplied_val#236" = if (DynamicPPL.inargnames)(var"##vn#243", __model__)
                                 data.x
                             else
                                 (DynamicPPL.getconditioned_nested)(__model__.context, (prefix)(__model__.context, var"##vn#243"))
                             end
-                        (var"##value#245", __varinfo__) = (tilde_observe!!)(__model__.context, (DynamicPPL.check_tilde_rhs)(var"##dist#246"), data.x, var"##vn#243", __varinfo__)
+                        (var"##value#245", __varinfo__) = (tilde_observe!!)(__model__.context, (DynamicPPL.check_tilde_rhs)(var"##dist#246"), var"##supplied_val#236", var"##vn#243", __varinfo__)
+                        data = (Accessors.set)(data, (BangBang.AccessorsImpl.prefermutation)((Accessors.opticcompose)((Accessors.PropertyLens){:x}())), var"##value#245")
                         var"##value#245"
                     end
                 #= /Users/pyong/ppl/dppl/src/compiler.jl:600 =#
                 return (var"##retval#248", __varinfo__)
             end
         end
     end
     begin
         $(Expr(:meta, :doc))
         function test(data::Any; )
             #= REPL[4]:1 =#
             return (Model){false}(test, NamedTuple{(:data,)}((data,)); )
         end
     end
 end

@github-actions
Copy link
Contributor

github-actions bot commented Dec 13, 2025

Benchmark Report

  • this PR's head: 485f8d502e5d1600a850c68bd8dc494c772047a5
  • base branch: 6266f644ce8caaa3b98fc65c1eb960f4f77243b1

Computer Information

Julia Version 1.11.7
Commit f2b3dbda30a (2025-09-08 12:10 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-16.0.6 (ORCJIT, znver3)
Threads: 1 default, 0 interactive, 1 GC (on 4 virtual cores)

Benchmark Results

┌───────────────────────┬───────┬─────────────┬───────────────────┬────────┬─────────────────────────────────┬────────────────────────────┬─────────────────────────────────┐
│                       │       │             │                   │        │        t(eval) / t(ref)         │     t(grad) / t(eval)      │        t(grad) / t(ref)         │
│                       │       │             │                   │        │ ──────────┬───────────┬──────── │ ───────┬─────────┬──────── │ ──────────┬───────────┬──────── │
│                 Model │   Dim │  AD Backend │           VarInfo │ Linked │      base │   this PR │ speedup │   base │ this PR │ speedup │      base │   this PR │ speedup │
├───────────────────────┼───────┼─────────────┼───────────────────┼────────┼───────────┼───────────┼─────────┼────────┼─────────┼─────────┼───────────┼───────────┼─────────┤
│               Dynamic │    10 │    mooncake │             typed │   true │    369.44 │    481.37 │    0.77 │   9.73 │    7.01 │    1.39 │   3594.83 │   3375.73 │    1.06 │
│                   LDA │    12 │ reversediff │             typed │   true │   2647.58 │   2694.30 │    0.98 │   5.08 │    5.16 │    0.98 │  13460.90 │  13914.97 │    0.97 │
│   Loop univariate 10k │ 10000 │    mooncake │             typed │   true │ 106214.87 │ 115480.92 │    0.92 │   3.81 │    4.44 │    0.86 │ 404627.33 │ 512680.90 │    0.79 │
├───────────────────────┼───────┼─────────────┼───────────────────┼────────┼───────────┼───────────┼─────────┼────────┼─────────┼─────────┼───────────┼───────────┼─────────┤
│    Loop univariate 1k │  1000 │    mooncake │             typed │   true │   7949.21 │   9130.52 │    0.87 │   4.76 │    4.22 │    1.13 │  37843.69 │  38568.23 │    0.98 │
│      Multivariate 10k │ 10000 │    mooncake │             typed │   true │  35101.40 │  57634.94 │    0.61 │  13.05 │    6.02 │    2.17 │ 458132.98 │ 347099.32 │    1.32 │
│       Multivariate 1k │  1000 │    mooncake │             typed │   true │   3803.31 │   5392.50 │    0.71 │   8.97 │    6.36 │    1.41 │  34124.06 │  34277.87 │    1.00 │
├───────────────────────┼───────┼─────────────┼───────────────────┼────────┼───────────┼───────────┼─────────┼────────┼─────────┼─────────┼───────────┼───────────┼─────────┤
│ Simple assume observe │     1 │ forwarddiff │             typed │  false │      2.68 │      2.38 │    1.12 │   3.96 │    4.54 │    0.87 │     10.62 │     10.83 │    0.98 │
│           Smorgasbord │   201 │ forwarddiff │             typed │  false │   1211.99 │   1318.87 │    0.92 │  61.50 │   82.65 │    0.74 │  74541.97 │ 109006.74 │    0.68 │
│           Smorgasbord │   201 │ forwarddiff │       simple_dict │   true │       err │       err │     err │    err │     err │     err │       err │       err │     err │
├───────────────────────┼───────┼─────────────┼───────────────────┼────────┼───────────┼───────────┼─────────┼────────┼─────────┼─────────┼───────────┼───────────┼─────────┤
│           Smorgasbord │   201 │ forwarddiff │ simple_namedtuple │   true │       err │       err │     err │    err │     err │     err │       err │       err │     err │
│           Smorgasbord │   201 │      enzyme │             typed │   true │   1660.87 │   1898.48 │    0.87 │   5.79 │    3.59 │    1.61 │   9614.88 │   6810.06 │    1.41 │
│           Smorgasbord │   201 │    mooncake │             typed │   true │   1729.57 │   1900.05 │    0.91 │   5.06 │    4.20 │    1.21 │   8757.46 │   7975.38 │    1.10 │
├───────────────────────┼───────┼─────────────┼───────────────────┼────────┼───────────┼───────────┼─────────┼────────┼─────────┼─────────┼───────────┼───────────┼─────────┤
│           Smorgasbord │   201 │ reversediff │             typed │   true │   1702.00 │   1926.80 │    0.88 │  90.11 │   87.57 │    1.03 │ 153367.26 │ 168737.19 │    0.91 │
│           Smorgasbord │   201 │ forwarddiff │      typed_vector │   true │   1672.76 │   1904.59 │    0.88 │  55.90 │   64.68 │    0.86 │  93507.23 │ 123189.39 │    0.76 │
│           Smorgasbord │   201 │ forwarddiff │           untyped │   true │   1674.34 │   1890.92 │    0.89 │ 119.27 │   68.16 │    1.75 │ 199704.93 │ 128883.03 │    1.55 │
├───────────────────────┼───────┼─────────────┼───────────────────┼────────┼───────────┼───────────┼─────────┼────────┼─────────┼─────────┼───────────┼───────────┼─────────┤
│           Smorgasbord │   201 │ forwarddiff │    untyped_vector │   true │   1673.12 │   1896.61 │    0.88 │  55.29 │   66.06 │    0.84 │  92510.98 │ 125296.44 │    0.74 │
│              Submodel │     1 │    mooncake │             typed │   true │      7.21 │      7.11 │    1.01 │   5.13 │    5.30 │    0.97 │     36.97 │     37.67 │    0.98 │
└───────────────────────┴───────┴─────────────┴───────────────────┴────────┴───────────┴───────────┴─────────┴────────┴─────────┴─────────┴───────────┴───────────┴─────────┘

@penelopeysm penelopeysm marked this pull request as draft December 13, 2025 14:21
@penelopeysm penelopeysm marked this pull request as ready for review December 13, 2025 14:39
@github-actions
Copy link
Contributor

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

@codecov
Copy link

codecov bot commented Dec 13, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.96%. Comparing base (6266f64) to head (485f8d5).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1177      +/-   ##
==========================================
+ Coverage   78.95%   78.96%   +0.01%     
==========================================
  Files          41       41              
  Lines        3896     3899       +3     
==========================================
+ Hits         3076     3079       +3     
  Misses        820      820              

☔ View full report in Codecov by Sentry.
📢 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.

@penelopeysm penelopeysm requested a review from mhauru December 16, 2025 08:39
Copy link
Member

@mhauru mhauru left a comment

Choose a reason for hiding this comment

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

One detail I was wondering about, but we can discuss that post-merge next year. Happy to merge.

$vn,
__varinfo__,
)
$(assign_or_set!!(left, value))
Copy link
Member

Choose a reason for hiding this comment

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

Might the fact that left is now assigned to the value that's gone through tilde_observe!! cause any changes? Maybe something about copies or allocations? I'm guessing not, just thinking out loud.

@mhauru mhauru added this pull request to the merge queue Dec 16, 2025
Merged via the queue into main with commit c68fbf8 Dec 16, 2025
21 checks passed
@mhauru mhauru deleted the py/immutable branch December 16, 2025 14:06
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.

NamedTuple field as data

3 participants