Skip to content

Commit 35d0be2

Browse files
committed
Remove Sampler and initialstep
1 parent 8e3dcac commit 35d0be2

File tree

5 files changed

+19
-87
lines changed

5 files changed

+19
-87
lines changed

HISTORY.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# DynamicPPL Changelog
22

3+
## 0.38.0
4+
5+
TODO: write up InitContext changes
6+
7+
`DynamicPPL.Sampler` and `DynamicPPL.initialstep` have also been removed entirely.
8+
If you were using these, you should unwrap your sampler and implement `AbstractMCMC.step` as usual for any other `AbstractMCMC.AbstractSampler`.
9+
310
## 0.37.0
411

512
DynamicPPL 0.37 comes with a substantial reworking of its internals.

docs/src/api.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -493,23 +493,21 @@ DynamicPPL.AbstractInitStrategy
493493
DynamicPPL.init
494494
```
495495

496-
### Samplers
496+
### Sampling
497497

498-
In DynamicPPL a generic sampler for inference is implemented.
498+
`init_strategy(spl)` defines how parameters are to be initialised when performing MCMC sampling with `spl`.
499499

500500
```@docs
501-
Sampler
501+
DynamicPPL.init_strategy
502502
```
503503

504-
The default implementation of [`Sampler`](@ref) uses the following unexported functions.
504+
`loadstate` is used for resuming sampling from a previous chain.
505505

506506
```@docs
507-
DynamicPPL.initialstep
508507
DynamicPPL.loadstate
509-
DynamicPPL.init_strategy
510508
```
511509

512-
Finally, to specify which varinfo type a [`Sampler`](@ref) should use for a given [`Model`](@ref), this is specified by [`DynamicPPL.default_varinfo`](@ref) and can thus be overloaded for each `model`-`sampler` combination. This can be useful in cases where one has explicit knowledge that one type of varinfo will be more performant for the given `model` and `sampler`.
510+
Finally, to specify which varinfo type a sampler should use for a given [`Model`](@ref), this is specified by [`DynamicPPL.default_varinfo`](@ref) and can thus be overloaded for each `model`-`sampler` combination. This can be useful in cases where one has explicit knowledge that one type of varinfo will be more performant for the given `model` and `sampler`.
513511

514512
```@docs
515513
DynamicPPL.default_varinfo

src/DynamicPPL.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ export AbstractVarInfo,
9494
getargnames,
9595
extract_priors,
9696
values_as_in_model,
97-
# Samplers
98-
Sampler,
9997
# LogDensityFunction
10098
LogDensityFunction,
10199
# Contexts

src/sampler.jl

Lines changed: 2 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,3 @@
1-
# TODO(mhauru) Could we get rid of Sampler now that it's just a wrapper around `alg`?
2-
# (Selector has been removed).
3-
"""
4-
Sampler{T}
5-
6-
Generic sampler type for inference algorithms of type `T` in DynamicPPL.
7-
8-
`Sampler` should implement the AbstractMCMC interface, and in particular
9-
`AbstractMCMC.step`. A default implementation of the initial sampling step is
10-
provided that supports resuming sampling from a previous state and setting initial
11-
parameter values. It requires to overload [`loadstate`](@ref) and [`initialstep`](@ref)
12-
for loading previous states and actually performing the initial sampling step,
13-
respectively. Additionally, sometimes one might want to implement [`init_strategy`](@ref)
14-
that specifies how the initial parameter values are sampled if they are not provided.
15-
By default, values are sampled from the prior.
16-
"""
17-
struct Sampler{T} <: AbstractSampler
18-
alg::T
19-
end
20-
211
"""
222
default_varinfo(rng, model, sampler)
233
@@ -39,49 +19,13 @@ function default_varinfo(::Random.AbstractRNG, ::Model, ::AbstractSampler)
3919
return typed_varinfo(VarInfo())
4020
end
4121

42-
function AbstractMCMC.sample(
43-
rng::Random.AbstractRNG,
44-
model::Model,
45-
sampler::Sampler,
46-
N::Integer;
47-
chain_type=default_chain_type(sampler),
48-
resume_from=nothing,
49-
initial_state=loadstate(resume_from),
50-
kwargs...,
51-
)
52-
return AbstractMCMC.mcmcsample(
53-
rng, model, sampler, N; chain_type, initial_state, kwargs...
54-
)
55-
end
56-
5722
"""
5823
init_strategy(sampler)
5924
6025
Define the initialisation strategy used for generating initial values when
6126
sampling with `sampler`. Defaults to `PriorInit()`, but can be overridden.
6227
"""
63-
init_strategy(::Sampler) = PriorInit()
64-
65-
function AbstractMCMC.step(
66-
rng::Random.AbstractRNG,
67-
model::Model,
68-
spl::Sampler;
69-
initial_params::AbstractInitStrategy=init_strategy(spl),
70-
kwargs...,
71-
)
72-
# Generate the default varinfo (usually this just makes an empty VarInfo
73-
# with NamedTuple of Metadata).
74-
vi = default_varinfo(rng, model, spl)
75-
76-
# Fill it with initial parameters. Note that, if `ParamsInit` is used, the
77-
# parameters provided must be in unlinked space (when inserted into the
78-
# varinfo, they will be adjusted to match the linking status of the
79-
# varinfo).
80-
_, vi = init!!(rng, model, vi, initial_params)
81-
82-
# Call the actual function that does the first step.
83-
return initialstep(rng, model, spl, vi; initial_params, kwargs...)
84-
end
28+
init_strategy(::AbstractMCMC.AbstractSampler) = PriorInit()
8529

8630
"""
8731
loadstate(data)
@@ -97,14 +41,4 @@ loadstate(data) = data
9741
9842
Default type of the chain of posterior samples from `sampler`.
9943
"""
100-
default_chain_type(::Sampler) = Any
101-
102-
"""
103-
initialstep(rng, model, sampler, varinfo; kwargs...)
104-
105-
Perform the initial sampling step of the `sampler` for the `model`.
106-
107-
The `varinfo` contains the initial samples, which can be provided by the user or
108-
sampled randomly.
109-
"""
110-
function initialstep end
44+
default_chain_type(::AbstractMCMC.AbstractSampler) = Any

test/sampler.jl

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,23 @@
44
abstract type OnlyInitAlg end
55
struct OnlyInitAlgDefault <: OnlyInitAlg end
66
struct OnlyInitAlgUniform <: OnlyInitAlg end
7-
function DynamicPPL.initialstep(
8-
rng::Random.AbstractRNG,
9-
model::Model,
10-
::Sampler{<:OnlyInitAlg},
11-
vi::AbstractVarInfo;
12-
kwargs...,
7+
function AbstractMCMC.step(
8+
rng::Random.AbstractRNG, model::Model, ::OnlyInitAlg, kwargs...
139
)
1410
return vi, nothing
1511
end
1612

1713
# initial samplers
18-
DynamicPPL.init_strategy(::Sampler{OnlyInitAlgUniform}) = UniformInit()
19-
@test DynamicPPL.init_strategy(Sampler(OnlyInitAlgDefault())) == PriorInit()
14+
DynamicPPL.init_strategy(::OnlyInitAlgUniform) = UniformInit()
15+
@test DynamicPPL.init_strategy(OnlyInitAlgDefault()) == PriorInit()
2016

21-
for alg in (OnlyInitAlgDefault(), OnlyInitAlgUniform())
17+
for sampler in (OnlyInitAlgDefault(), OnlyInitAlgUniform())
2218
# model with one variable: initialization p = 0.2
2319
@model function coinflip()
2420
p ~ Beta(1, 1)
2521
return 10 ~ Binomial(25, p)
2622
end
2723
model = coinflip()
28-
sampler = Sampler(alg)
2924
lptrue = logpdf(Binomial(25, 0.2), 10)
3025
let inits = ParamsInit((; p=0.2))
3126
chain = sample(model, sampler, 1; initial_params=inits, progress=false)

0 commit comments

Comments
 (0)