Skip to content

Add tests for threaded assume statements#1340

Merged
penelopeysm merged 2 commits intomainfrom
py/threaded-assume
Mar 30, 2026
Merged

Add tests for threaded assume statements#1340
penelopeysm merged 2 commits intomainfrom
py/threaded-assume

Conversation

@penelopeysm
Copy link
Copy Markdown
Member

Since accumulators are threadsafe (or to be precise, DynamicPPL can make them threadsafe when the user calls setthreadsafe), any functionality in DynamicPPL that only uses accumulators is automatically threadsafe. This means that, for example, you can sample random variables in parallel:

@model function threaded_assume()
    x = zeros(10)
    Threads.@threads for i in eachindex(x)
        x[i] ~ Normal()
    end
end
model = setthreadsafe(threaded_assume(), true)

julia> rand(model)
VarNamedTuple
└─ x => PartialArray size=(10,) data::Vector{Float64}
        ├─ (1,) => 0.7076777209105218
        ├─ (2,) => -1.0664996275655731 (7 more set indices)
        └─ (10,) => 0.6552123596028697

This functionality has always existed (since #1284). I didn't realise at the time that that meant that even LogDensityFunction is now fully threadsafe. This PR just adds tests to verify that.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 30, 2026

Benchmark Report

  • this PR's head: 22bacda9c65a976495f454157f2d19d864f153da
  • base branch: 61eda053d9b0918345e7959691b66eaba046d6a4

Computer Information

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 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 │ Linked │     base │  this PR │ speedup │   base │ this PR │ speedup │      base │   this PR │ speedup │
├───────────────────────┼───────┼─────────────┼────────┼──────────┼──────────┼─────────┼────────┼─────────┼─────────┼───────────┼───────────┼─────────┤
│               Dynamic │    10 │    mooncake │   true │   285.46 │   279.33 │    1.02 │   8.84 │    8.60 │    1.03 │   2522.35 │   2400.91 │    1.05 │
│                   LDA │    12 │ reversediff │   true │  2510.31 │  2489.37 │    1.01 │   2.08 │    2.12 │    0.98 │   5227.54 │   5272.72 │    0.99 │
│   Loop univariate 10k │ 10000 │    mooncake │   true │ 32113.36 │ 31220.68 │    1.03 │   6.17 │    7.17 │    0.86 │ 198063.46 │ 223698.57 │    0.89 │
├───────────────────────┼───────┼─────────────┼────────┼──────────┼──────────┼─────────┼────────┼─────────┼─────────┼───────────┼───────────┼─────────┤
│    Loop univariate 1k │  1000 │    mooncake │   true │  3194.89 │  3162.65 │    1.01 │   6.22 │    6.96 │    0.89 │  19883.67 │  22009.95 │    0.90 │
│      Multivariate 10k │ 10000 │    mooncake │   true │ 32403.24 │ 31184.61 │    1.04 │  15.09 │    9.97 │    1.51 │ 489087.92 │ 310978.21 │    1.57 │
│       Multivariate 1k │  1000 │    mooncake │   true │  3436.04 │  3416.06 │    1.01 │   9.21 │    9.24 │    1.00 │  31636.37 │  31555.07 │    1.00 │
├───────────────────────┼───────┼─────────────┼────────┼──────────┼──────────┼─────────┼────────┼─────────┼─────────┼───────────┼───────────┼─────────┤
│ Simple assume observe │     1 │ forwarddiff │  false │     0.88 │     0.88 │    1.00 │  10.48 │   10.53 │    1.00 │      9.19 │      9.24 │    0.99 │
│           Smorgasbord │   201 │ forwarddiff │  false │   957.12 │   970.66 │    0.99 │  71.26 │   70.87 │    1.01 │  68202.76 │  68793.60 │    0.99 │
│           Smorgasbord │   201 │      enzyme │   true │  1300.13 │  1300.14 │    1.00 │   4.87 │    4.88 │    1.00 │   6334.46 │   6341.91 │    1.00 │
├───────────────────────┼───────┼─────────────┼────────┼──────────┼──────────┼─────────┼────────┼─────────┼─────────┼───────────┼───────────┼─────────┤
│           Smorgasbord │   201 │ forwarddiff │   true │  1302.62 │  1312.17 │    0.99 │  67.75 │   69.83 │    0.97 │  88250.58 │  91626.95 │    0.96 │
│           Smorgasbord │   201 │    mooncake │   true │  1324.10 │  1332.68 │    0.99 │   4.62 │    5.20 │    0.89 │   6117.09 │   6931.21 │    0.88 │
│           Smorgasbord │   201 │ reversediff │   true │  1313.09 │  1303.19 │    1.01 │ 126.94 │  128.52 │    0.99 │ 166685.81 │ 167486.78 │    1.00 │
├───────────────────────┼───────┼─────────────┼────────┼──────────┼──────────┼─────────┼────────┼─────────┼─────────┼───────────┼───────────┼─────────┤
│              Submodel │     1 │    mooncake │   true │     0.88 │     0.88 │    1.00 │  26.97 │   27.20 │    0.99 │     23.65 │     23.88 │    0.99 │
└───────────────────────┴───────┴─────────────┴────────┴──────────┴──────────┴─────────┴────────┴─────────┴─────────┴───────────┴───────────┴─────────┘

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.41%. Comparing base (61eda05) to head (22bacda).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1340      +/-   ##
==========================================
+ Coverage   78.13%   78.41%   +0.27%     
==========================================
  Files          50       50              
  Lines        3590     3590              
==========================================
+ Hits         2805     2815      +10     
+ Misses        785      775      -10     

☔ 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 merged commit 23bd87c into main Mar 30, 2026
21 of 22 checks passed
@penelopeysm penelopeysm deleted the py/threaded-assume branch March 30, 2026 17:09
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