Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 = "SliceSampling"
uuid = "43f4d3e8-9711-4a8c-bd1b-03ac73a255cf"
version = "0.7.7"
version = "0.7.8"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand All @@ -21,7 +21,7 @@ Distributions = "0.25"
LinearAlgebra = "1"
LogDensityProblems = "2"
Random = "1"
Turing = "0.39.5"
Turing = "0.40"
julia = "1.10"

[extras]
Expand Down
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ Random = "1"
SliceSampling = "0.7.1"
StableRNGs = "1"
Statistics = "1"
Turing = "0.37, 0.38, 0.39"
Turing = "0.37, 0.38, 0.39, 0.40"
julia = "1.10"
33 changes: 20 additions & 13 deletions ext/SliceSamplingTuringExt.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

module SliceSamplingTuringExt

using LogDensityProblems
using Random
using SliceSampling
using Turing
Expand Down Expand Up @@ -32,32 +33,38 @@ const SliceSamplingStates = Union{
function Turing.Inference.getparams(::Turing.DynamicPPL.Model, sample::SliceSamplingStates)
return sample.transition.params
end

function Turing.Inference.getlogp_external(
::Turing.DynamicPPL.Model, t::SliceSampling.Transition, state
)
return t.lp
end
# end

function SliceSampling.initial_sample(rng::Random.AbstractRNG, ℓ::Turing.LogDensityFunction)
n_max_attempts = 1000

model = ℓ.model
vi = Turing.DynamicPPL.VarInfo(rng, model, Turing.SampleFromUniform())
vi_spl = last(Turing.DynamicPPL.evaluate!!(model, rng, vi, Turing.SampleFromUniform()))
vi_spl = last(Turing.DynamicPPL.evaluate_and_sample!!(rng, model, vi, Turing.SampleFromUniform()))
Copy link
Member

Choose a reason for hiding this comment

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

Hmmm this will only work on DynamicPPL 0.37, so only Turing 0.40, so I think it might be better to restrict the compat to only 0.40.

Alternatively to be explicit you could make both Turing and DynamicPPL triggers for the Turing extension, then that way you can control both compat entries. I think that's the best way although it is a bit of boilerplate.

Otherwise LGTM :)

Copy link
Member Author

@Red-Portal Red-Portal Aug 13, 2025

Choose a reason for hiding this comment

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

Ah okay. Hmm let me just restrict to Turing 0.40 to keep things simple. Thanks for taking a look!

θ = vi_spl[:]
ℓp = LogDensityProblems.logdensity(ℓ, θ)

init_attempt_count = 1
while !all(isfinite.(θ))
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"
for attempts in 1:n_max_attempts
if attempts == 10
@warn "Failed to find valid initial parameters after $(init_attempt_count) attempts; consider providing explicit initial parameters using the `initial_params` keyword"
end

# NOTE: This will sample in the unconstrained space.
vi_spl = last(Turing.DynamicPPL.evaluate!!(model, rng, vi, Turing.SampleFromUniform()))
θ = vi_spl[:]
vi_spl = last(
Turing.DynamicPPL.evaluate_and_sample!!(
rng, model, vi, Turing.SampleFromUniform()
),
)
θ = vi_spl[:]
ℓp = LogDensityProblems.logdensity(ℓ, θ)

init_attempt_count += 1
if all(isfinite.(θ)) && isfinite(ℓp)
return θ
end
end

@error "Failed to find valid initial parameters after $(n_max_attempts) attempts; consider providing explicit initial parameters using the `initial_params` keyword"
return θ
end

Expand Down
2 changes: 1 addition & 1 deletion src/SliceSampling.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ls

module SliceSampling

using AbstractMCMC
Expand Down
2 changes: 1 addition & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ MCMCTesting = "0.3"
Random = "1"
StableRNGs = "1"
Test = "1"
Turing = "0.37, 0.38, 0.39"
Turing = "0.37, 0.38, 0.39, 0.40"
julia = "1.10"
19 changes: 19 additions & 0 deletions test/turing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,30 @@
return nothing
end

@model function illbehavedmodel()
@addlogprob! -Inf
return nothing
end

@model function logp_check()
a ~ Normal()
return b ~ Normal()
end

rng = Random.default_rng()
@test begin
init = SliceSampling.initial_sample(rng, LogDensityFunction(demo()))
all(isfinite.(init))
end

@test_warn "Warning: Failed" SliceSampling.initial_sample(
rng, LogDensityFunction(illbehavedmodel())
)

@test_warn "Error: Failed" SliceSampling.initial_sample(
rng, LogDensityFunction(illbehavedmodel())
)

n_samples = 1000
model = demo()

Expand Down
Loading