Skip to content

Conversation

@penelopeysm
Copy link
Member

@penelopeysm penelopeysm commented Nov 8, 2025

Performance gains in #1113, #1125, etc. are only available when not using TSVI.

TSVI gets used more than it should (#1086).

This PR provides a mechanism to opt out of TSVI, in a way that is fully backwards-compatible.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 8, 2025

Benchmark Report for Commit 9a74692

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

┌───────────────────────┬───────┬─────────────┬───────────────────┬────────┬────────────────┬─────────────────┐
│                 Model │   Dim │  AD Backend │           VarInfo │ Linked │ t(eval)/t(ref) │ t(grad)/t(eval) │
├───────────────────────┼───────┼─────────────┼───────────────────┼────────┼────────────────┼─────────────────┤
│ Simple assume observe │     1 │ forwarddiff │             typed │  false │            6.9 │             1.7 │
│           Smorgasbord │   201 │ forwarddiff │             typed │  false │          740.1 │            41.4 │
│           Smorgasbord │   201 │ forwarddiff │ simple_namedtuple │   true │          419.7 │            53.5 │
│           Smorgasbord │   201 │ forwarddiff │           untyped │   true │          780.7 │            35.5 │
│           Smorgasbord │   201 │ forwarddiff │       simple_dict │   true │         6686.5 │            27.8 │
│           Smorgasbord │   201 │ forwarddiff │      typed_vector │   true │          743.3 │            40.2 │
│           Smorgasbord │   201 │ forwarddiff │    untyped_vector │   true │          782.7 │            35.8 │
│           Smorgasbord │   201 │ reversediff │             typed │   true │          930.3 │            43.3 │
│           Smorgasbord │   201 │    mooncake │             typed │   true │          749.0 │             5.7 │
│           Smorgasbord │   201 │      enzyme │             typed │   true │          923.4 │             3.7 │
│    Loop univariate 1k │  1000 │    mooncake │             typed │   true │         3844.5 │             5.8 │
│       Multivariate 1k │  1000 │    mooncake │             typed │   true │          985.9 │             9.0 │
│   Loop univariate 10k │ 10000 │    mooncake │             typed │   true │        42510.0 │             5.3 │
│      Multivariate 10k │ 10000 │    mooncake │             typed │   true │         8341.9 │            10.2 │
│               Dynamic │    10 │    mooncake │             typed │   true │          120.1 │            13.1 │
│              Submodel │     1 │    mooncake │             typed │   true │            9.3 │            11.2 │
│                   LDA │    12 │ reversediff │             typed │   true │          990.7 │             2.1 │
└───────────────────────┴───────┴─────────────┴───────────────────┴────────┴────────────────┴─────────────────┘

@codecov
Copy link

codecov bot commented Nov 8, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.73%. Comparing base (7deaaab) to head (9a74692).
⚠️ Report is 1 commits behind head on py/fastinit.

Additional details and impacted files
@@               Coverage Diff               @@
##           py/fastinit    #1128      +/-   ##
===============================================
+ Coverage        81.72%   81.73%   +0.01%     
===============================================
  Files               41       41              
  Lines             3956     3959       +3     
===============================================
+ Hits              3233     3236       +3     
  Misses             723      723              

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

Comment on lines -866 to -874
"""
use_threadsafe_eval(context::AbstractContext, varinfo::AbstractVarInfo)
Return `true` if evaluation of a model using `context` and `varinfo` should
wrap `varinfo` in `ThreadSafeVarInfo`, i.e. threadsafe evaluation, and `false` otherwise.
"""
function use_threadsafe_eval(context::AbstractContext, varinfo::AbstractVarInfo)
return Threads.nthreads() > 1
end
Copy link
Member Author

@penelopeysm penelopeysm Nov 8, 2025

Choose a reason for hiding this comment

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

Technically, Turing overloads this function, so releasing this in a patch would cause upstream breakage. I don't feel bad about this though because this function is not exported.

On top of that, the method that Turing defines for this is bogus. It silently disables TSVI for PG/SMC, because Libtask can't handle it, and it generally doesn't work for SMC anyway because observations for all particles need to be in step. The problem is that for models that do need threadsafe eval, this will lead to silent wrong results (TuringLang/Turing.jl#2658).

The correct solution for this should be to emit an error saying:

PG/SMC can't run with models that require threadsafe eval. If you know your model doesn't need it, then you should call DynamicPPL.use_threadsafe_eval!(false).

Copy link
Member Author

Choose a reason for hiding this comment

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

Given that FastLDF is going into breaking instead, we should just rebase this on breaking.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 8, 2025

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

Base automatically changed from py/fastinit to py/fastldf November 10, 2025 19:28
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