Move external sampler interface to AbstractMCMC - #2704
Merged
Conversation
penelopeysm
marked this pull request as draft
October 24, 2025 13:39
Contributor
|
Turing.jl documentation for PR #2704 is available at: |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## breaking #2704 +/- ##
============================================
+ Coverage 86.45% 86.52% +0.06%
============================================
Files 21 21
Lines 1418 1410 -8
============================================
- Hits 1226 1220 -6
+ Misses 192 190 -2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
penelopeysm
commented
Oct 24, 2025
| @@ -1,5 +1,5 @@ | |||
| """ | |||
| ExternalSampler{S<:AbstractSampler,AD<:ADTypes.AbstractADType,Unconstrained} | |||
| ExternalSampler{Unconstrained,S<:AbstractSampler,AD<:ADTypes.AbstractADType} | |||
Contributor
Author
There was a problem hiding this comment.
Moved this type parameter earlier so that we can dispatch on it more easily.
penelopeysm
marked this pull request as ready for review
November 4, 2025 15:47
penelopeysm
commented
Nov 4, 2025
Comment on lines
17
to
28
|
|
||
| 1. Directly implement the `AbstractMCMC.step` methods for `DynamicPPL.Model`. This is the | ||
| most powerful option and is what Turing.jl's in-house samplers do. Implementing this | ||
| means that you can directly call `sample(model, MySampler(), N)`. | ||
| 1. Directly implement the `AbstractMCMC.step` methods for `DynamicPPL.Model`. That is to | ||
| say, implement `AbstractMCMC.step(rng::Random.AbstractRNG, model::DynamicPPL.Model, | ||
| sampler::MySampler; kwargs...)` and related methods. This is the most powerful option and | ||
| is what Turing.jl's in-house samplers do. Implementing this means that you can directly | ||
| call `sample(model, MySampler(), N)`. | ||
|
|
||
| 2. Implement a generic `AbstractMCMC.step` method for `AbstractMCMC.LogDensityModel`. This | ||
| struct wraps an object that obeys the LogDensityProblems.jl interface, so your `step` | ||
| 2. Implement a generic `AbstractMCMC.step` method for `AbstractMCMC.LogDensityModel` (the | ||
| same signature as above except that `model::AbstractMCMC.LogDensityModel`). This struct | ||
| wraps an object that obeys the LogDensityProblems.jl interface, so your `step` | ||
| implementation does not need to know anything about Turing.jl or DynamicPPL.jl. To use | ||
| this with Turing.jl, you will need to wrap your sampler: `sample(model, | ||
| externalsampler(MySampler()), N)`. |
Contributor
Author
There was a problem hiding this comment.
I'll write proper docs about this separately (in the docs repo).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Following on from:
AbstractMCMC.getstatsandAbstractMCMC.requires_unconstrained_spaceAbstractMCMC.jl#182 Adding the interface functionAbstractMCMC.getstatsAbstractMCMC.getstatsAdvancedHMC.jl#471 and implementAbstractMCMC.getstatsAdvancedMH.jl#119 Implementing them for AdvancedHMC and AdvancedMHthis PR changes Turing's external sampler interface to exclusively use AbstractMCMC functions. With this PR, anyone who defines an external sampler will only need to depend on AbstractMCMC (which they presumably already do, because it is a sampler) and LogDensityProblems (which is already a dep of AbstractMCMC). No need for a Turing extension.
To be precise, it makes the following changes:
Turing.Inference.getparams, now one has to defineAbstractMCMC.getparams.AbstractMCMC.getstats.Turing.Inference.isgibbscomponentis changed totrue, so that external sampler packages don't need to override it (unless absolutely necessary).As an example implementation, this PR contains a test mock (note how it doesn't require a Turing dep):
Turing.jl/test/mcmc/external_sampler.jl
Lines 20 to 74 in 06752c4