Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/mcmc/hmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ function find_initial_params(

# if we failed to find valid initial parameters, error
return error(
"failed to find valid initial parameters in $(max_attempts) tries. This may indicate an error with the model or AD backend; please open an issue at https://github.com/TuringLang/Turing.jl/issues",
"failed to find valid initial parameters in $(max_attempts) tries. This may indicate an error with the model or AD backend. See https://turinglang.org/docs/usage/troubleshooting/#initial-parameters for common causes and solutions. If the issue persists, please open an issue at https://github.com/TuringLang/Turing.jl/issues",
Copy link
Member

@mhauru mhauru Jul 30, 2025

Choose a reason for hiding this comment

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

Maybe the sentence "This may indicate an error with the model or AD backend" can go? The message is quite long, and I think the link gives a more nuanced explanation of what might be wrong.

)
end

Expand Down
23 changes: 23 additions & 0 deletions test/mcmc/hmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,29 @@ using Turing
@test Turing.Inference.getstepsize(spl, hmc_state) isa Float64
end
end

@testset "improved error message for initialization failures" begin
# Model that fails to initialize due to VonMises AD issue
@model function failing_model()
μ ~ Uniform(-π, π)
κ ~ InverseGamma(2, 3)
return x ~ VonMises(μ, κ)
Copy link
Member

Choose a reason for hiding this comment

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

This model might get fixed at some point, independently of us (the issue is in AD packages/Distributions.jl). Would something like any super simple model but with @addlogprob!!(-Inf) thrown in at the end trigger the same error more reliably? @addlogprob!! is a thing you can call within a model to add any number you want to the log likelihood, and I think @addlogprob!!(-Inf) should make all samples be rejected. @addlogprob!!(NaN) might work too.

Copy link
Member

Choose a reason for hiding this comment

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

line 208 through 215 already contains the same test.

Copy link
Member

Choose a reason for hiding this comment

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

with @addlogprob! -Inf

end

# Test that error message includes troubleshooting link
err = nothing
try
sample(failing_model(), NUTS(), 10; progress=false)
catch e
err = e
end

@test err isa ErrorException
@test contains(
string(err),
"https://turinglang.org/docs/usage/troubleshooting/#initial-parameters",
)
end
Copy link
Member

Choose a reason for hiding this comment

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

This works, but check out Test.@test_throws, which can do this for for you.

end

end
Loading