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
3 changes: 3 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
indent = 4
margin = 92
normalize_line_endings = "unix"
v2_stable_multiline_strings = true
conditional_to_if = false
pipe_to_function_call = false
style="blue"
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Adds `JuliaFormatter` testing which was forgotten (#60).

### Removed

- `DynamicPPLExt` no longer requires `ForwardDiff` as a triggering library to load.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function write_markdown(path, rows; warn_ratio, fail_ratio)
" |",
)
end
println(io)
return println(io)
end
return path
end
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/ParallelMCMCBenchmarks/scripts/new_bench.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function build_raw_deer_problem(

tape = map(1:T) do _
ξ = copyto!(similar(x0, D), randn(rng, FP, D))
ParallelMCMC.MALATapeElement(ξ, FP(rand(rng)))
return ParallelMCMC.MALATapeElement(ξ, FP(rand(rng)))
end

rec = ParallelMCMC._build_mala_deer_rec(
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/ParallelMCMCBenchmarks/scripts/prof_view.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function build_raw_deer_problem(

tape = map(1:T) do _
ξ = copyto!(similar(x0, D), randn(rng, FP, D))
ParallelMCMC.MALATapeElement(ξ, FP(rand(rng)))
return ParallelMCMC.MALATapeElement(ξ, FP(rand(rng)))
end

rec = ParallelMCMC._build_mala_deer_rec(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function make_tape(rng::Random.AbstractRNG, x0::AbstractVector, dim::Int, t_len:
FP = typeof(epsilon)
return map(1:t_len) do _
xi = copyto!(similar(x0, dim), randn(rng, FP, dim))
ParallelMCMC.MALATapeElement(xi, FP(rand(rng)))
return ParallelMCMC.MALATapeElement(xi, FP(rand(rng)))
end
end

Expand Down
4 changes: 2 additions & 2 deletions benchmarks/ParallelMCMCBenchmarks/src/pr_suite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ end
function write_results(path::AbstractString, results::Dict{String,Any})
mkpath(dirname(path))
open(path, "w") do io
TOML.print(io, results; sorted=true)
return TOML.print(io, results; sorted=true)
end
return path
end
Expand Down Expand Up @@ -312,7 +312,7 @@ function write_markdown(path::AbstractString, results::Dict{String,Any})
" B |",
)
end
println(io)
return println(io)
end
return path
end
Expand Down
8 changes: 8 additions & 0 deletions docs/src/90-contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ Otherwise, say what your proposed solution is and wait for a discussion around i

If your solution involves code, tests, benchmarks, or documentation builds, check the [developer documentation](91-developer.md).
Otherwise, you can use the GitHub interface directly to create your pull request.

## Pull request checklist

Before submitting a pull request, please make sure:

- You have added tests for your feature or fix, where applicable.
- You have formatted the code with `JuliaFormatter` using the version specified by the `test` project’s compatibility bounds.
- All tests pass.
4 changes: 2 additions & 2 deletions docs/src/assets/make_julia_deer_gif.jl
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,13 @@ function main()
animation_paths = vcat(frame_paths, fill(last(frame_paths), 8))
tmp_output = joinpath(frame_dir, "julia_deer_posterior.gif")
run(`$convert -delay 7 -loop 0 $animation_paths -layers Optimize $tmp_output`)
cp(tmp_output, output; force=true)
return cp(tmp_output, output; force=true)
end

final_error = maximum(abs.(last(iterates) .- final_trajectory))
println("wrote ", output)
println("last DEER metric: ", @sprintf("%.3g", last(metrics)))
println("max error vs sequential taped MALA: ", @sprintf("%.3g", final_error))
return println("max error vs sequential taped MALA: ", @sprintf("%.3g", final_error))
end

main()
12 changes: 7 additions & 5 deletions ext/DynamicPPLExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ chain = sample(model, AdaptiveMALASampler(0.3; n_warmup=500), 2_000;
chain_type=FlexiChains.VNChain, discard_warmup=true, progress=true)
```
"""
function ParallelMCMC.DensityModel(
turing_model::DynamicPPL.Model; ad_backend, hvp=nothing
)
function ParallelMCMC.DensityModel(turing_model::DynamicPPL.Model; ad_backend, hvp=nothing)
# Sample in linked/unconstrained space and let DynamicPPL provide the gradient.
ld = DynamicPPL.LogDensityFunction(
turing_model,
Expand Down Expand Up @@ -126,7 +124,11 @@ for (Ttrans, Tspl, Tstate) in (
chain_type::Type{SymChain};
kwargs...,
)
throw(ArgumentError("FlexiChains.SymChain is not supported for DynamicPPL models; please use VNChain instead."))
throw(
ArgumentError(
"FlexiChains.SymChain is not supported for DynamicPPL models; please use VNChain instead.",
),
)
end
end
end
Expand All @@ -140,7 +142,7 @@ function ParallelMCMC._construct_flexichain(
)
pwss = map(enumerate(eachrow(vals))) do (i, val)
stats = map(v -> v[i], internals)
DynamicPPL.ParamsWithStats(val, model.logdensity.ld, stats)
return DynamicPPL.ParamsWithStats(val, model.logdensity.ld, stats)
end
return AbstractMCMC.from_samples(VNChain, hcat(pwss))
end
Expand Down
7 changes: 3 additions & 4 deletions src/DEER/DEER.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ struct ReverseOnGrad <: HVPStrategy end
_strategy_from(::DI.ForwardOverAnything) = ForwardOnGrad()
_strategy_from(::DI.HVPMode) = ReverseOnGrad()

function _hvp_strategy(backend::Union{AbstractADType, DI.SecondOrder})
function _hvp_strategy(backend::Union{AbstractADType,DI.SecondOrder})
return _strategy_from(DI.hvp_mode(backend))
end

Expand Down Expand Up @@ -320,9 +320,8 @@ end
@inline _rademacher!(z::AbstractArray, rng::AbstractRNG, ::Nothing) = _rademacher!(z, rng)

@inline _rademacher_matrix!(Z::AbstractMatrix, rng::AbstractRNG) = _rademacher!(Z, rng)
@inline _rademacher_matrix!(Z::AbstractMatrix, rng::AbstractRNG, host) = _rademacher!(
Z, rng, host
)
@inline _rademacher_matrix!(Z::AbstractMatrix, rng::AbstractRNG, host) =
_rademacher!(Z, rng, host)

function jac_diag_via_jvps(rec::TapedRecursion, x::AbstractVector, t::Int)
D = length(x)
Expand Down
6 changes: 5 additions & 1 deletion src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,11 @@ function _construct_flexichain(
elseif n isa TKey || n isa Symbol
to_parameter(n)
else
throw(ArgumentError("param_names must be a collection of Pairs, Symbols, or $TKey, got $(typeof(n))"))
throw(
ArgumentError(
"param_names must be a collection of Pairs, Symbols, or $TKey, got $(typeof(n))",
),
)
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
FlexiChains = "4a37a8b9-6e57-4b92-8664-298d46e639f7"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
LogDensityProblems = "6fdf6af0-433a-55f7-b3ed-c6c6e0b8df7c"
LogDensityProblemsAD = "996a588d-648d-4e1f-a8f0-a84b347e47b1"
Expand All @@ -23,3 +24,6 @@ Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[extras]
CUDA_Runtime_jll = "76a88914-d11a-5bdc-97e0-2f5a05c973a2"

[compat]
JuliaFormatter = "2.12"
12 changes: 8 additions & 4 deletions test/test-AbstractMCMC-Interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ gradlogp_iface(x) = -x

@test chain isa SymChain
@test FlexiChains.parameters(chain) == [:param]
@test size(chain[:param, stack=true]) == (50, 1, 2)
@test size(chain[:param, stack = true]) == (50, 1, 2)
end

@testset "with vector-valued varnames" begin
Expand All @@ -256,15 +256,19 @@ gradlogp_iface(x) = -x

@test chain isa VNChain
@test FlexiChains.parameters(chain) == [@varname(param)]
@test size(chain[@varname(param), stack=true]) == (50, 1, 2)
@test size(chain[@varname(param), stack = true]) == (50, 1, 2)
end
end

@testset "invalid param_names throws" begin
model = DensityModel(logp_iface, gradlogp_iface, 2)
@test_throws "param_names must be a collection" sample(
model, MALASampler(0.15), 50;
chain_type=SymChain, progress=false, param_names=["mu", "sigma"],
model,
MALASampler(0.15),
50;
chain_type=SymChain,
progress=false,
param_names=["mu", "sigma"],
)
end

Expand Down
14 changes: 2 additions & 12 deletions test/test-Adaptive-MALA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,7 @@ end
sampler = AdaptiveMALASampler(0.2; n_warmup=50)

chain = sample(
MersenneTwister(1),
model,
sampler,
150;
chain_type=SymChain,
progress=false,
MersenneTwister(1), model, sampler, 150; chain_type=SymChain, progress=false
)

@test chain isa SymChain
Expand Down Expand Up @@ -219,12 +214,7 @@ end
sampler = AdaptiveMALASampler(0.1; n_warmup=n_w)

chain = sample(
MersenneTwister(3),
model,
sampler,
n_w + 50;
chain_type=VNChain,
progress=false,
MersenneTwister(3), model, sampler, n_w + 50; chain_type=VNChain, progress=false
)

# Filter by is_warmup flag to avoid off-by-one from the init transition.
Expand Down
6 changes: 5 additions & 1 deletion test/test-Code-Quality.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using Test
using Aqua, JET
using Aqua, JET, JuliaFormatter
using ParallelMCMC

@testset "Blue Formatting" begin
@test JuliaFormatter.format(ParallelMCMC; verbose=false, overwrite=false)
end

@testset "Aqua" begin
Aqua.test_all(ParallelMCMC)
end
Expand Down
16 changes: 3 additions & 13 deletions test/test-DEER-Interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,7 @@ end
sampler = ParallelMALASampler(0.05; T=16, backend=_AD)

chain = sample(
MersenneTwister(1),
model,
sampler,
100;
chain_type=SymChain,
progress=false,
MersenneTwister(1), model, sampler, 100; chain_type=SymChain, progress=false
)

@test chain isa SymChain
Expand All @@ -229,7 +224,7 @@ end
param_names = FlexiChains.parameters(chain)
@test length(param_names) == 1
name = only(param_names)
@test size(chain[name, stack=true], 3) == 2
@test size(chain[name, stack = true], 3) == 2
end

@testset "ParallelMALASampler bundle_samples fallback path (thinning)" begin
Expand Down Expand Up @@ -279,12 +274,7 @@ end
sampler = ParallelMALASampler(0.1; T=32, damping=0.5, backend=_AD)

chain = sample(
MersenneTwister(2025),
model,
sampler,
5_000;
chain_type=SymChain,
progress=false,
MersenneTwister(2025), model, sampler, 5_000; chain_type=SymChain, progress=false
)

burn = 500
Expand Down
8 changes: 4 additions & 4 deletions test/test-DEER-Turing-Logistic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ end
@test chain isa VNChain
@test FlexiChains.niters(chain) == 400
@test @varname(β) in FlexiChains.parameters(chain)
@test all(isfinite, chain[@varname(β), stack=true])
@test all(isfinite, chain[@varname(β), stack = true])
end

@testset "ParallelMALASampler Turing logistic: posterior sign correct" begin
Expand All @@ -136,7 +136,7 @@ end
progress=false,
)

post = chain[@varname(β), stack=true][201:end, :, :]
post = chain[@varname(β), stack = true][201:end, :, :]
β_mean = vec(mean(post; dims=1))

@test sign(β_mean[1]) == sign(_LR_β_true[1])
Expand Down Expand Up @@ -165,7 +165,7 @@ end
progress=false,
discard_warmup=true,
)
β_mala = vec(mean(mala_chain[:x, stack=true]; dims=1))
β_mala = vec(mean(mala_chain[:x, stack = true]; dims=1))

deer_chain = sample(
MersenneTwister(42),
Expand All @@ -183,7 +183,7 @@ end
chain_type=SymChain,
progress=false,
)
β_deer = vec(mean(deer_chain[:x, stack=true][201:end, :, :]; dims=1))
β_deer = vec(mean(deer_chain[:x, stack = true][201:end, :, :]; dims=1))

@test abs(β_deer[1] - β_mala[1]) < 0.25
@test abs(β_deer[2] - β_mala[2]) < 0.25
Expand Down
7 changes: 3 additions & 4 deletions test/test-GPU-AD-HVP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ end
if !_ADHVP_GPU_AVAILABLE
@info "GPU AD-HVP test: CUDA not functional — skipping"
else

#=
Multivariate Gaussian target with X'X/N perturbation:
logp(β) = -0.5 (||β||^2 + ||Xβ||^2 / N)
Expand All @@ -39,7 +38,7 @@ else
function _logp_single(β, X)
Xβ = pmcmc_matmul(X, β)
N = oftype(zero(eltype(β)), size(X, 1))
-oftype(zero(eltype(β)), 0.5) * (sum(abs2, β) + sum(abs2, Xβ) / N)
return -oftype(zero(eltype(β)), 0.5) * (sum(abs2, β) + sum(abs2, Xβ) / N)
end

function _gradlogp_single(β, X)
Expand All @@ -54,8 +53,8 @@ else
function _logp_batch(B, X)
XB = pmcmc_matmul(X, B)
N = oftype(zero(eltype(B)), size(X, 1))
-oftype(zero(eltype(B)), 0.5) .*
(vec(sum(abs2, B; dims=1)) .+ vec(sum(abs2, XB; dims=1)) ./ N)
return -oftype(zero(eltype(B)), 0.5) .*
(vec(sum(abs2, B; dims=1)) .+ vec(sum(abs2, XB; dims=1)) ./ N)
end

function _gradlogp_batch(B, X)
Expand Down
1 change: 0 additions & 1 deletion test/test-GPU-Performance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ end
if !_PERF_GPU_AVAILABLE
@info "GPU performance test: CUDA not functional — skipping"
else

#=
Multivariate Gaussian target — well-conditioned, optimal MALA acceptance from
any start. Lets ε be set analytically so the chain actually moves and DEER
Expand Down
6 changes: 3 additions & 3 deletions test/test-Jacobian-Estimator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ Base.axes(x::TaggedVector) = axes(x.data)
Base.getindex(x::TaggedVector, i::Int) = x.data[i]
Base.setindex!(x::TaggedVector, v, i::Int) = setindex!(x.data, v, i)
function Base.similar(x::TaggedVector, ::Type{T}, dims::Dims{1}) where {T}
TaggedVector(Vector{T}(undef, dims[1]))
return TaggedVector(Vector{T}(undef, dims[1]))
end
function Base.similar(x::TaggedVector, ::Type{T}, n::Int) where {T}
TaggedVector(Vector{T}(undef, n))
return TaggedVector(Vector{T}(undef, n))
end
function Base.similar(x::TaggedVector, dims::Dims{1})
TaggedVector(Vector{eltype(x)}(undef, dims[1]))
return TaggedVector(Vector{eltype(x)}(undef, dims[1]))
end
Base.similar(x::TaggedVector, n::Int) = TaggedVector(Vector{eltype(x)}(undef, n))
Base.copy(x::TaggedVector) = TaggedVector(copy(x.data))
Expand Down
Loading
Loading