Skip to content

Commit

Permalink
Implement getstepsize() for NoAdaptation samplers (#2405)
Browse files Browse the repository at this point in the history
Co-authored-by: Hong Ge <[email protected]>
  • Loading branch information
penelopeysm and yebai authored Dec 17, 2024
1 parent 365fe16 commit 700a19a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion 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.35.3"
version = "0.35.4"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
6 changes: 6 additions & 0 deletions src/mcmc/hmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,12 @@ end

getstepsize(sampler::Sampler{<:Hamiltonian}, state) = sampler.alg.ϵ
getstepsize(sampler::Sampler{<:AdaptiveHamiltonian}, state) = AHMC.getϵ(state.adaptor)
function getstepsize(
sampler::Sampler{<:AdaptiveHamiltonian},
state::HMCState{TV,TKernel,THam,PhType,AHMC.Adaptation.NoAdaptation},
) where {TV,TKernel,THam,PhType}
return state.kernel.τ.integrator.ϵ
end

gen_metric(dim::Int, spl::Sampler{<:Hamiltonian}, state) = AHMC.UnitEuclideanMetric(dim)
function gen_metric(dim::Int, spl::Sampler{<:AdaptiveHamiltonian}, state)
Expand Down
13 changes: 13 additions & 0 deletions test/mcmc/hmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,19 @@ using Turing
@test pvalue(ApproximateTwoSampleKSTest(vec(results), vec(results_prior))) > 0.001
end

@testset "getstepsize: Turing.jl#2400" begin
algs = [HMC(0.1, 10), HMCDA(0.8, 0.75), NUTS(0.5), NUTS(0, 0.5)]
@testset "$(alg)" for alg in algs
# Construct a HMC state by taking a single step
spl = Sampler(alg, gdemo_default)
hmc_state = DynamicPPL.initialstep(
Random.default_rng(), gdemo_default, spl, DynamicPPL.VarInfo(gdemo_default)
)[2]
# Check that we can obtain the current step size
@test Turing.Inference.getstepsize(spl, hmc_state) isa Float64
end
end

@testset "Check ADType" begin
alg = HMC(0.1, 10; adtype=adbackend)
m = DynamicPPL.contextualize(
Expand Down

2 comments on commit 700a19a

@penelopeysm
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/121619

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.35.4 -m "<description of version>" 700a19a3f26bc44df08998892e59181b1ab5db6e
git push origin v0.35.4

Please sign in to comment.