Skip to content

Conversation

@penelopeysm
Copy link
Member

@penelopeysm penelopeysm commented Nov 28, 2025

It turns out I actually also fixed this in #1130, so this is really just a backport to 0.38.

For an example of the problematic behaviour, run this with current Turing release + DPPL 0.38.9:

using Turing
@model function f()
    return x ~ product_distribution((; a = Normal()))
end
model = f()
chain = sample(model, NUTS(), 10)

chain[Symbol("x.a")][1:5]
#=
 -1.1115679614699299
 -1.1115679614699299
 -1.1115679614699299
 -1.1115679614699299
  0.6485348468368219
=#

rets = returned(model, chain); # will print loads of warnings
rets[1:5]
#=
 (a = 0.43617950918721293,)
 (a = -0.03530447045796848,)
 (a = -0.22527167664558378,)
 (a = -1.9301798082540629,)
 (a = 0.8968461391852851,)
=#

This PR changes it such that the call to returned errors instead of silently giving wrong results.

Note that FlexiChains works correctly (both before and after this PR):

using Turing, FlexiChains
@model function f()
    return x ~ product_distribution((; a = Normal()))
end
model = f()
chain = sample(model, NUTS(), 10; chain_type=VNChain)
chain[@varname(x.a)][1:5]
#=
  0.21193332804635567
 -0.3905216939777102
 -0.1371019215195824
 -0.1371019215195824
 -0.7884639857653091
=#
rets = returned(model, chain);
rets[1:5]
#=
 (a = 0.21193332804635567,)
 (a = -0.3905216939777102,)
 (a = -0.1371019215195824,)
 (a = -0.1371019215195824,)
 (a = -0.7884639857653091,)
=#

@github-actions
Copy link
Contributor

github-actions bot commented Nov 28, 2025

Benchmark Report

  • this PR's head: 9df4914b9df7278fc97b106fb74c8c0fa748f26b
  • base branch: 052bc1950df3a42e14b56eae51af236881092f90

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)     │
│                       │       │             │                   │        │ ──────────┬───────────┬──────── │ ──────┬─────────┬──────── │
│                 Model │   Dim │  AD Backend │           VarInfo │ Linked │      base │   this PR │ speedup │  base │ this PR │ speedup │
├───────────────────────┼───────┼─────────────┼───────────────────┼────────┼───────────┼───────────┼─────────┼───────┼─────────┼─────────┤
│               Dynamic │    10 │    mooncake │             typed │   true │    503.08 │    519.30 │    0.97 │  9.93 │    7.67 │    1.29 │
│                   LDA │    12 │ reversediff │             typed │   true │   3111.80 │   3111.50 │    1.00 │  2.02 │    2.19 │    0.92 │
│   Loop univariate 10k │ 10000 │    mooncake │             typed │   true │ 180300.68 │ 161447.63 │    1.12 │  5.36 │    5.89 │    0.91 │
│    Loop univariate 1k │  1000 │    mooncake │             typed │   true │  14745.62 │  15074.87 │    0.98 │  5.83 │    5.78 │    1.01 │
│      Multivariate 10k │ 10000 │    mooncake │             typed │   true │  34342.66 │  46839.95 │    0.73 │  9.69 │    7.61 │    1.27 │
│       Multivariate 1k │  1000 │    mooncake │             typed │   true │   3666.27 │   5043.36 │    0.73 │  9.21 │    6.97 │    1.32 │
│ Simple assume observe │     1 │ forwarddiff │             typed │  false │     18.63 │     19.05 │    0.98 │  1.88 │    1.88 │    1.00 │
│           Smorgasbord │   201 │      enzyme │             typed │   true │   2814.39 │   3018.01 │    0.93 │  4.75 │    4.88 │    0.97 │
│           Smorgasbord │   201 │ forwarddiff │       simple_dict │   true │  28396.66 │  26062.62 │    1.09 │ 23.21 │   26.69 │    0.87 │
│           Smorgasbord │   201 │ forwarddiff │ simple_namedtuple │   true │   1159.59 │   1314.68 │    0.88 │ 82.63 │   84.88 │    0.97 │
│           Smorgasbord │   201 │ forwarddiff │             typed │  false │   2752.72 │   3040.17 │    0.91 │ 91.77 │   49.27 │    1.86 │
│           Smorgasbord │   201 │ forwarddiff │      typed_vector │   true │   2817.46 │   3083.47 │    0.91 │ 43.35 │   51.23 │    0.85 │
│           Smorgasbord │   201 │ forwarddiff │           untyped │   true │   2516.98 │   2635.01 │    0.96 │ 47.48 │   48.91 │    0.97 │
│           Smorgasbord │   201 │ forwarddiff │    untyped_vector │   true │   2599.08 │   2689.81 │    0.97 │ 44.09 │   49.09 │    0.90 │
│           Smorgasbord │   201 │    mooncake │             typed │   true │   2823.78 │   3031.10 │    0.93 │  5.58 │    4.90 │    1.14 │
│           Smorgasbord │   201 │ reversediff │             typed │   true │   2925.00 │   3203.64 │    0.91 │ 56.86 │   53.55 │    1.06 │
│              Submodel │     1 │    mooncake │             typed │   true │     30.48 │     28.09 │    1.09 │  5.00 │    5.36 │    0.93 │
└───────────────────────┴───────┴─────────────┴───────────────────┴────────┴───────────┴───────────┴─────────┴───────┴─────────┴─────────┘

@codecov
Copy link

codecov bot commented Nov 28, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.58%. Comparing base (052bc19) to head (9df4914).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1159   +/-   ##
=======================================
  Coverage   81.58%   81.58%           
=======================================
  Files          40       40           
  Lines        3845     3845           
=======================================
  Hits         3137     3137           
  Misses        708      708           

☔ 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 marked this pull request as draft November 28, 2025 18:43
@penelopeysm
Copy link
Member Author

This needs tests, but I won't do it today.

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.

2 participants