Skip to content

Commit

Permalink
Don't get stuck in an infinite loop if HMC can't find an initial point (
Browse files Browse the repository at this point in the history
#2392)

* Error after 1000 attempts at finding initial parameters

* Add a test

* Fix missing import

* Bump Project.toml
  • Loading branch information
penelopeysm authored Nov 7, 2024
1 parent 470f447 commit 5b24ceb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/mcmc/hmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ function DynamicPPL.initialstep(
if init_attempt_count == 10
@warn "failed to find valid initial parameters in $(init_attempt_count) tries; consider providing explicit initial parameters using the `initial_params` keyword"
end
if init_attempt_count == 1000
error(
"failed to find valid initial parameters in $(init_attempt_count) tries. This may indicate an error with the model or AD backend; please open an issue at https://github.com/TuringLang/Turing.jl/issues",
)
end

# NOTE: This will sample in the unconstrained space.
vi = last(DynamicPPL.evaluate!!(model, rng, vi, SampleFromUniform()))
Expand Down
11 changes: 10 additions & 1 deletion test/mcmc/hmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Random
using StableRNGs: StableRNG
using StatsFuns: logistic
import Mooncake
using Test: @test, @test_logs, @testset
using Test: @test, @test_logs, @testset, @test_throws
using Turing

@testset "Testing hmc.jl with $adbackend" for adbackend in ADUtils.adbackends
Expand Down Expand Up @@ -272,6 +272,15 @@ using Turing
end
end

@testset "error for impossible model" begin
@model function demo_impossible()
x ~ Normal()
Turing.@addlogprob! -Inf
end

@test_throws ErrorException sample(demo_impossible(), NUTS(; adtype=adbackend), 5)
end

@testset "(partially) issue: #2095" begin
@model function vector_of_dirichlet(::Type{TV}=Vector{Float64}) where {TV}
xs = Vector{TV}(undef, 2)
Expand Down

2 comments on commit 5b24ceb

@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/118898

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.2 -m "<description of version>" 5b24cebe773922e0f3d5c4cb7f53162eb758b04d
git push origin v0.35.2

Please sign in to comment.