Skip to content

Commit b482dd0

Browse files
committed
chains: make sampling output bundling opt-in
Preserve the raw-sample fallback for other chain types. Conversion from `SamplingOutput` remains available explicitly after sampling. Assisted-by: Codex <codex@openai.com>
1 parent dc16cb7 commit b482dd0

4 files changed

Lines changed: 14 additions & 9 deletions

File tree

‎history.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# History
2+
3+
## 5.17.0
4+
5+
- Add `SamplingOutput` to retain draws, iteration indices, timing, and optional sampler states in a common container that chain packages can convert to their own formats ([#218](https://github.com/TuringLang/AbstractMCMC.jl/pull/218)).

‎src/chains.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function _bundle_samples(
7171
discard_initial=0,
7272
thinning=1,
7373
kwargs...,
74-
) where {C<:AbstractChains}
74+
) where {C<:SamplingOutput}
7575
samples = samples isa Array ? samples : collect(samples)
7676
samples = samples isa Vector ? reshape(samples, :, 1) : samples
7777
chain = SamplingOutput(

‎src/interface.jl‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ Bundle all `samples` that were sampled from the `model` with the given `sampler`
2727
The final `state` of the `sampler` can be included in the chain. The type of the chain can
2828
be specified with the `chain_type` argument.
2929
30-
For an `AbstractChains` output type, the default `_bundle_samples` collects a vector or matrix
31-
of samples and its metadata in a [`SamplingOutput`](@ref), AbstractMCMC's internal chain
32-
representation, then calls `convert(chain_type, chain)`. Chain packages can implement
33-
`Base.convert(::Type{MyChains}, output::SamplingOutput)` and use this default without
34-
overloading `bundle_samples` or its internal helper `_bundle_samples`.
30+
For a [`SamplingOutput`](@ref) output type, the default `_bundle_samples` collects a vector
31+
or matrix of samples and its metadata in that representation. Chain packages can implement
32+
`Base.convert(::Type{MyChains}, output::SamplingOutput)` for explicit conversion after
33+
sampling with `chain_type=SamplingOutput`. Other chain types retain their existing bundling
34+
behavior; defining `convert` alone does not opt them into this helper.
3535
Otherwise, this method returns `samples` (or converts its entries for `Vector{T}` output).
3636
"""
3737
function bundle_samples(

‎test/chains.jl‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ Base.convert(::Type{ConvertedChain}, chain::SamplingOutput) = ConvertedChain(cha
5555
@test_throws ArgumentError chainscat(chain, SamplingOutput(samples; iterations=2:3))
5656

5757
converted = sample(
58-
Xoshiro(1), Model(), Sampler(), 3; chain_type=ConvertedChain, opts...
58+
Xoshiro(1), Model(), Sampler(), 3; chain_type=SamplingOutput, opts...
5959
)
60-
converted = converted.chain
60+
converted = convert(ConvertedChain, converted).chain
6161
@test converted.iterations == 3:3:9
6262
@test converted.sampler_states == [9]
6363
@test only(converted.sampling_stats).duration >= 0
6464
@test_throws ArgumentError from_samples(typeof(converted), converted.samples)
65-
for chain_type in (Any, Vector{NamedTuple})
65+
for chain_type in (Any, Vector{NamedTuple}, ConvertedChain, AbstractMCMC.AbstractChains)
6666
raw = sample(Xoshiro(1), Model(), Sampler(), 3; chain_type, progress=false)
6767
@test raw isa Vector{<:NamedTuple}
6868
end

0 commit comments

Comments
 (0)