Skip to content

Commit d751976

Browse files
committed
Globally rename sample!! -> evaluate_and_sample!!, add changelog warning
1 parent 732c6c2 commit d751976

14 files changed

+55
-47
lines changed

HISTORY.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ To aid with this process, `contextualize` is now exported from DynamicPPL.
3535

3636
The main situation where one _did_ want to specify an additional evaluation context was when that context was a `SamplingContext`.
3737
Doing this would allow you to run the model and sample fresh values, instead of just using the values that existed in the VarInfo object.
38-
Thus, this release also introduces the unexported function `sample!!`.
39-
Essentially, `sample!!(rng, model, varinfo, sampler)` is a drop-in replacement for `evaluate!!(model, varinfo, SamplingContext(rng, sampler))`.
38+
Thus, this release also introduces the **unexported** function `evaluate_and_sample!!`.
39+
Essentially, `evaluate_and_sample!!(rng, model, varinfo, sampler)` is a drop-in replacement for `evaluate!!(model, varinfo, SamplingContext(rng, sampler))`.
40+
**Do note that this is an internal method**, and its name or semantics are liable to change in the future without warning.
4041

4142
There are many methods that no longer take a context argument, and listing them all would be too much.
4243
However, here are the more user-facing ones:
@@ -50,7 +51,7 @@ However, here are the more user-facing ones:
5051
And a couple of more internal changes:
5152

5253
- `evaluate!!`, `evaluate_threadsafe!!`, and `evaluate_threadunsafe!!` no longer accept context arguments
53-
- `evaluate!!` no longer takes rng and sampler (if you used this, you should use `sample!!` instead, or construct your own `SamplingContext`)
54+
- `evaluate!!` no longer takes rng and sampler (if you used this, you should use `evaluate_and_sample!!` instead, or construct your own `SamplingContext`)
5455
- The model evaluation function, `model.f` for some `model::Model`, no longer takes a context as an argument
5556

5657
## 0.36.11

docs/src/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ By default, it does not perform any actual sampling: it only evaluates the model
455455
To perform sampling, you can either wrap `model.context` in a `SamplingContext`, or use this convenience method:
456456

457457
```@docs
458-
DynamicPPL.sample!!
458+
DynamicPPL.evaluate_and_sample!!
459459
```
460460

461461
The behaviour of a model execution can be changed with evaluation contexts, which are a field of the model.

ext/DynamicPPLMCMCChainsExt.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function DynamicPPL.predict(
115115
iters = Iterators.product(1:size(chain, 1), 1:size(chain, 3))
116116
predictive_samples = map(iters) do (sample_idx, chain_idx)
117117
DynamicPPL.setval_and_resample!(varinfo, parameter_only_chain, sample_idx, chain_idx)
118-
varinfo = last(DynamicPPL.sample!!(rng, model, varinfo))
118+
varinfo = last(DynamicPPL.evaluate_and_sample!!(rng, model, varinfo))
119119

120120
vals = DynamicPPL.values_as_in_model(model, false, varinfo)
121121
varname_vals = mapreduce(

src/extract_priors.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function extract_priors(rng::Random.AbstractRNG, model::Model)
116116
# workaround for the fact that `order` is still hardcoded in VarInfo, and hence you
117117
# can't push new variables without knowing the num_produce. Remove this when possible.
118118
varinfo = setaccs!!(varinfo, (PriorDistributionAccumulator(), NumProduceAccumulator()))
119-
varinfo = last(sample!!(rng, model, varinfo))
119+
varinfo = last(evaluate_and_sample!!(rng, model, varinfo))
120120
return getacc(varinfo, Val(:PriorDistributionAccumulator)).priors
121121
end
122122

src/model.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ end
815815
# ^ Weird Documenter.jl bug means that we have to write the two above separately
816816
# as it can only detect the `function`-less syntax.
817817
function (model::Model)(rng::Random.AbstractRNG, varinfo::AbstractVarInfo=VarInfo())
818-
return first(sample!!(rng, model, varinfo))
818+
return first(evaluate_and_sample!!(rng, model, varinfo))
819819
end
820820

821821
"""
@@ -829,7 +829,7 @@ function use_threadsafe_eval(context::AbstractContext, varinfo::AbstractVarInfo)
829829
end
830830

831831
"""
832-
sample!!([rng::Random.AbstractRNG, ]model::Model, varinfo[, sampler])
832+
evaluate_and_sample!!([rng::Random.AbstractRNG, ]model::Model, varinfo[, sampler])
833833
834834
Evaluate the `model` with the given `varinfo`, but perform sampling during the
835835
evaluation using the given `sampler` by wrapping the model's context in a
@@ -839,7 +839,7 @@ If `sampler` is not provided, defaults to [`SampleFromPrior`](@ref).
839839
840840
Returns a tuple of the model's return value, plus the updated `varinfo` object.
841841
"""
842-
function sample!!(
842+
function evaluate_and_sample!!(
843843
rng::Random.AbstractRNG,
844844
model::Model,
845845
varinfo::AbstractVarInfo,
@@ -848,10 +848,10 @@ function sample!!(
848848
sampling_model = contextualize(model, SamplingContext(rng, sampler, model.context))
849849
return evaluate!!(sampling_model, varinfo)
850850
end
851-
function sample!!(
851+
function evaluate_and_sample!!(
852852
model::Model, varinfo::AbstractVarInfo, sampler::AbstractSampler=SampleFromPrior()
853853
)
854-
return sample!!(Random.default_rng(), model, varinfo, sampler)
854+
return evaluate_and_sample!!(Random.default_rng(), model, varinfo, sampler)
855855
end
856856

857857
"""
@@ -1038,7 +1038,7 @@ Base.nameof(model::Model{<:Function}) = nameof(model.f)
10381038
Generate a sample of type `T` from the prior distribution of the `model`.
10391039
"""
10401040
function Base.rand(rng::Random.AbstractRNG, ::Type{T}, model::Model) where {T}
1041-
x = last(sample!!(rng, model, SimpleVarInfo{Float64}(OrderedDict())))
1041+
x = last(evaluate_and_sample!!(rng, model, SimpleVarInfo{Float64}(OrderedDict())))
10421042
return values_as(x, T)
10431043
end
10441044

src/sampler.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function AbstractMCMC.step(
5858
kwargs...,
5959
)
6060
vi = VarInfo()
61-
DynamicPPL.sample!!(rng, model, vi, sampler)
61+
DynamicPPL.evaluate_and_sample!!(rng, model, vi, sampler)
6262
return vi, nothing
6363
end
6464

src/simple_varinfo.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ julia> rng = StableRNG(42);
3939
julia> # In the `NamedTuple` version we need to provide the place-holder values for
4040
# the variables which are using "containers", e.g. `Array`.
4141
# In this case, this means that we need to specify `x` but not `m`.
42-
_, vi = DynamicPPL.sample!!(rng, m, SimpleVarInfo((x = ones(2), )));
42+
_, vi = DynamicPPL.evaluate_and_sample!!(rng, m, SimpleVarInfo((x = ones(2), )));
4343
4444
julia> # (✓) Vroom, vroom! FAST!!!
4545
vi[@varname(x[1])]
@@ -57,12 +57,12 @@ julia> vi[@varname(x[1:2])]
5757
1.3736306979834252
5858
5959
julia> # (×) If we don't provide the container...
60-
_, vi = DynamicPPL.sample!!(rng, m, SimpleVarInfo()); vi
60+
_, vi = DynamicPPL.evaluate_and_sample!!(rng, m, SimpleVarInfo()); vi
6161
ERROR: type NamedTuple has no field x
6262
[...]
6363
6464
julia> # If one does not know the varnames, we can use a `OrderedDict` instead.
65-
_, vi = DynamicPPL.sample!!(rng, m, SimpleVarInfo{Float64}(OrderedDict()));
65+
_, vi = DynamicPPL.evaluate_and_sample!!(rng, m, SimpleVarInfo{Float64}(OrderedDict()));
6666
6767
julia> # (✓) Sort of fast, but only possible at runtime.
6868
vi[@varname(x[1])]
@@ -91,28 +91,28 @@ demo_constrained (generic function with 2 methods)
9191
9292
julia> m = demo_constrained();
9393
94-
julia> _, vi = DynamicPPL.sample!!(rng, m, SimpleVarInfo());
94+
julia> _, vi = DynamicPPL.evaluate_and_sample!!(rng, m, SimpleVarInfo());
9595
9696
julia> vi[@varname(x)] # (✓) 0 ≤ x < ∞
9797
1.8632965762164932
9898
99-
julia> _, vi = DynamicPPL.sample!!(rng, m, DynamicPPL.settrans!!(SimpleVarInfo(), true));
99+
julia> _, vi = DynamicPPL.evaluate_and_sample!!(rng, m, DynamicPPL.settrans!!(SimpleVarInfo(), true));
100100
101101
julia> vi[@varname(x)] # (✓) -∞ < x < ∞
102102
-0.21080155351918753
103103
104-
julia> xs = [last(DynamicPPL.sample!!(rng, m, DynamicPPL.settrans!!(SimpleVarInfo(), true)))[@varname(x)] for i = 1:10];
104+
julia> xs = [last(DynamicPPL.evaluate_and_sample!!(rng, m, DynamicPPL.settrans!!(SimpleVarInfo(), true)))[@varname(x)] for i = 1:10];
105105
106106
julia> any(xs .< 0) # (✓) Positive probability mass on negative numbers!
107107
true
108108
109109
julia> # And with `OrderedDict` of course!
110-
_, vi = DynamicPPL.sample!!(rng, m, DynamicPPL.settrans!!(SimpleVarInfo(OrderedDict()), true));
110+
_, vi = DynamicPPL.evaluate_and_sample!!(rng, m, DynamicPPL.settrans!!(SimpleVarInfo(OrderedDict()), true));
111111
112112
julia> vi[@varname(x)] # (✓) -∞ < x < ∞
113113
0.6225185067787314
114114
115-
julia> xs = [last(DynamicPPL.sample!!(rng, m, DynamicPPL.settrans!!(SimpleVarInfo(), true)))[@varname(x)] for i = 1:10];
115+
julia> xs = [last(DynamicPPL.evaluate_and_sample!!(rng, m, DynamicPPL.settrans!!(SimpleVarInfo(), true)))[@varname(x)] for i = 1:10];
116116
117117
julia> any(xs .< 0) # (✓) Positive probability mass on negative numbers!
118118
true
@@ -259,12 +259,12 @@ end
259259

260260
function untyped_simple_varinfo(model::Model)
261261
varinfo = SimpleVarInfo(OrderedDict())
262-
return last(sample!!(model, varinfo))
262+
return last(evaluate_and_sample!!(model, varinfo))
263263
end
264264

265265
function typed_simple_varinfo(model::Model)
266266
varinfo = SimpleVarInfo{Float64}()
267-
return last(sample!!(model, varinfo))
267+
return last(evaluate_and_sample!!(model, varinfo))
268268
end
269269

270270
function unflatten(svi::SimpleVarInfo, x::AbstractVector)

src/test_utils/model_interface.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ Even though it is recommended to implement this by hand for a particular `Model`
9292
a default implementation using [`SimpleVarInfo{<:Dict}`](@ref) is provided.
9393
"""
9494
function varnames(model::Model)
95-
return collect(keys(last(DynamicPPL.sample!!(model, SimpleVarInfo(Dict())))))
95+
return collect(
96+
keys(last(DynamicPPL.evaluate_and_sample!!(model, SimpleVarInfo(Dict()))))
97+
)
9698
end
9799

98100
"""

src/varinfo.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ Construct a VarInfo object for the given `model`, which has just a single
197197
function untyped_varinfo(
198198
rng::Random.AbstractRNG, model::Model, sampler::AbstractSampler=SampleFromPrior()
199199
)
200-
return last(sample!!(rng, model, VarInfo(Metadata()), sampler))
200+
return last(evaluate_and_sample!!(rng, model, VarInfo(Metadata()), sampler))
201201
end
202202
function untyped_varinfo(model::Model, sampler::AbstractSampler=SampleFromPrior())
203203
return untyped_varinfo(Random.default_rng(), model, sampler)

test/compiler.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -598,13 +598,13 @@ module Issue537 end
598598
# an attempt at a `NamedTuple` of the form `(x = 1, __varinfo__)`.
599599
@model empty_model() = return x = 1
600600
empty_vi = VarInfo()
601-
retval_and_vi = DynamicPPL.sample!!(empty_model(), empty_vi)
601+
retval_and_vi = DynamicPPL.evaluate_and_sample!!(empty_model(), empty_vi)
602602
@test retval_and_vi isa Tuple{Int,typeof(empty_vi)}
603603

604604
# Even if the return-value is `AbstractVarInfo`, we should return
605605
# a `Tuple` with `AbstractVarInfo` in the second component too.
606606
@model demo() = return __varinfo__
607-
retval, svi = DynamicPPL.sample!!(demo(), SimpleVarInfo())
607+
retval, svi = DynamicPPL.evaluate_and_sample!!(demo(), SimpleVarInfo())
608608
@test svi == SimpleVarInfo()
609609
if Threads.nthreads() > 1
610610
@test retval isa DynamicPPL.ThreadSafeVarInfo{<:SimpleVarInfo}
@@ -620,11 +620,11 @@ module Issue537 end
620620
f(x) = return x^2
621621
return f(1.0)
622622
end
623-
retval, svi = DynamicPPL.sample!!(demo(), SimpleVarInfo())
623+
retval, svi = DynamicPPL.evaluate_and_sample!!(demo(), SimpleVarInfo())
624624
@test retval isa Float64
625625

626626
@model demo() = x ~ Normal()
627-
retval, svi = DynamicPPL.sample!!(demo(), SimpleVarInfo())
627+
retval, svi = DynamicPPL.evaluate_and_sample!!(demo(), SimpleVarInfo())
628628

629629
# Return-value when using `to_submodel`
630630
@model inner() = x ~ Normal()

0 commit comments

Comments
 (0)