Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bump compat of AdvancedHMC #2050

Merged
merged 20 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Turing"
uuid = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"
version = "0.27"
version = "0.27.1"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand Down Expand Up @@ -38,7 +38,7 @@ Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"

[compat]
AbstractMCMC = "4"
AdvancedHMC = "0.3.0, 0.4"
AdvancedHMC = "0.5.2"
devmotion marked this conversation as resolved.
Show resolved Hide resolved
AdvancedMH = "0.6.8, 0.7"
AdvancedPS = "0.4"
AdvancedVI = "0.2"
Expand Down
6 changes: 3 additions & 3 deletions src/inference/Inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ function AbstractMCMC.sample(
kwargs...
)
if resume_from === nothing
return AbstractMCMC.mcmcsample(rng, model, sampler, N;
chain_type=chain_type, progress=progress, kwargs...)
return AbstractMCMC.mcmcsample(rng, model, sampler, N + min(div(N, 10), 1_000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means users will get more than the requested N samples - I think this should be reverted?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a change in how the number of adaptions is handled in the external sampler interface. Instead of storing n_adapts in the sampling algorithm (see here), we now only pass them in the AbstractMCMC.sample call. This means there is no adaption by default if the change is reverted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can set disgard_initial = n_adapts here so we have a default adaption but will return the same number of MCMC samples. In addition, we can allow the user to pass a n_adapt argument to override the default adaption settings.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, we have to take into account the adaption steps - but changing this line here means that a call such as sample(model, NUTS(), 10) will return not 10 but 1010 samples? These discarded steps only have to be specified as keyword argument but not added to the positional argument (number of samples). AbstractMCMC will added discard_initial to these internally to the requested number of samples automatically.

Copy link
Member

@yebai yebai Jul 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out we have a typo in tests: we already explicitly pass the n_adapts argument to the sample function. Due to the typo, AHMC complains about missing n_adapts. After correcting this typo, these default values are no longer necessary. More generally, we should introduce default options for n_adapts in the AbstractMCMC package; see this PR.

n_adapts = min(div(N, 10), 1_000), chain_type=chain_type, progress=progress, kwargs...)
else
return resume(resume_from, N; chain_type=chain_type, progress=progress, kwargs...)
return resume(resume_from, N; n_adapts = 0, chain_type=chain_type, progress=progress, kwargs...)
end
end

Expand Down
3 changes: 2 additions & 1 deletion src/inference/hmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,8 @@ end
function make_ahmc_kernel(alg::HMCDA, ϵ)
return AHMC.HMCKernel(AHMC.Trajectory{AHMC.EndPointTS}(AHMC.Leapfrog(ϵ), AHMC.FixedIntegrationTime(alg.λ)))
end
make_ahmc_kernel(alg::NUTS, ϵ) = AHMC.NUTS(AHMC.Leapfrog(ϵ), alg.max_depth, alg.Δ_max)
make_ahmc_kernel(alg::NUTS, ϵ) =
AHMC.HMCKernel(AHMC.Trajectory{AHMC.MultinomialTS}(AHMC.Leapfrog(ϵ), AHMC.GeneralisedNoUTurn(alg.max_depth, alg.Δ_max)))

####
#### Compiler interface, i.e. tilde operators.
Expand Down
2 changes: 1 addition & 1 deletion test/contrib/inference/abstractmcmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function initialize_nuts(model::Turing.Model)
# - multinomial sampling scheme,
# - generalised No-U-Turn criteria, and
# - windowed adaption for step-size and diagonal mass matrix
proposal = AdvancedHMC.NUTS{AdvancedHMC.MultinomialTS,AdvancedHMC.GeneralisedNoUTurn}(integrator)
proposal = AdvancedHMC.HMCKernel(AdvancedHMC.Trajectory{AdvancedHMC.MultinomialTS}(integrator, AdvancedHMC.GeneralisedNoUTurn()))
adaptor = AdvancedHMC.StanHMCAdaptor(
AdvancedHMC.MassMatrixAdaptor(metric),
AdvancedHMC.StepSizeAdaptor(0.65, integrator)
Expand Down
Loading