Skip to content

Mark all methods with DynamicPPL.Model as produceable#2780

Merged
penelopeysm merged 5 commits intomainfrom
py/submodel-libtask-alternative
Mar 2, 2026
Merged

Mark all methods with DynamicPPL.Model as produceable#2780
penelopeysm merged 5 commits intomainfrom
py/submodel-libtask-alternative

Conversation

@penelopeysm
Copy link
Member

@penelopeysm penelopeysm commented Mar 2, 2026

This needs a Libtask release and version bump, which I'll handle once JuliaRegistrator does its things.

This essentially implements the plan described in TuringLang/Libtask.jl#217. A lot of the issues stemming from Libtask not picking up model evaluators either with keyword arguments, or in submodels, can be fixed by simply declaring that every method that dispatches on DynamicPPL.Model is produceable. The mechanism for this is implemented in TuringLang/Libtask.jl#218, and this PR makes use of that.

For the end-user, this means that we guarantee correctness where models either have submodels or where models have keyword arguments. The user no longer has to mark models with keyword arguments as @might_produce.

I tested performance, and there is no regression — in fact there is a small speedup (although that is probably benchmark noise):

Submodel case

This was the issue #2772 where non-inlined submodels were not correctly picked up.

#2778 fixed this with a strategy that was similar to that in this PR, but was slightly more limited (this PR handles both submodels and keyword arguments together).

using Turing, StableRNGs, Test

@model function inner(y, x)
    @noinline
    y ~ Normal(x)
end
@model function nested(y)
    x ~ Normal()
    a ~ to_submodel(inner(y, x))
end
m1 = nested(1.0)
@time sample(StableRNG(468), m1, PG(10), 2000; chain_type=Any, progress=false);

# 2.585299 seconds on #2778
# 2.523017 seconds on this PR

Keyword argument case

This was the long-standing issue where models with keyword arguments were originally not picked up by Libtask, and since v0.42.5, could be, but relied on the user themselves manually declaring Libtask.@might_produce.

@model function withkw(; y=0.0)
    x ~ Normal()
    y ~ Normal(x)
end
m1 = withkw(y=10.0);
# Turing.@might_produce(withkw)
@time sample(StableRNG(468), m1, PG(10), 2000; chain_type=Any, progress=false);

# withkw case
# 4.741234 seconds on main (requiring @might_produce)
# 4.441797 seconds on this PR (and not requiring @might_produce)

@penelopeysm penelopeysm force-pushed the py/submodel-libtask-alternative branch 2 times, most recently from 3a21368 to 8795e6b Compare March 2, 2026 13:42
@penelopeysm penelopeysm force-pushed the py/submodel-libtask-alternative branch from 8795e6b to 3d14cd9 Compare March 2, 2026 13:48
@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2026

Turing.jl documentation for PR #2780 is available at:
https://TuringLang.github.io/Turing.jl/previews/PR2780/

@codecov
Copy link

codecov bot commented Mar 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.03%. Comparing base (9be3ed5) to head (a4c2671).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2780      +/-   ##
==========================================
- Coverage   87.08%   87.03%   -0.06%     
==========================================
  Files          20       20              
  Lines        1309     1296      -13     
==========================================
- Hits         1140     1128      -12     
+ Misses        169      168       -1     

☔ 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 7072ff1 into main Mar 2, 2026
24 of 27 checks passed
@penelopeysm penelopeysm deleted the py/submodel-libtask-alternative branch March 2, 2026 15:33
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