Skip to content

Commit

Permalink
Fix for type instability when using LKJChol (#548)
Browse files Browse the repository at this point in the history
* concretize reshape in `reconstruct` for `LKJCholesky` to avoid type-instabilities

* added tests for `demo_lkjchol`

* bumped patch versionion
  • Loading branch information
torfjelde authored Oct 23, 2023
1 parent efd9da3 commit 12e7c27
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DynamicPPL"
uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8"
version = "0.23.19"
version = "0.23.20"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand Down
2 changes: 1 addition & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ reconstruct(::MatrixDistribution, val::AbstractMatrix{<:Real}) = copy(val)
reconstruct(::Inverse{Bijectors.VecCorrBijector}, ::LKJ, val::AbstractVector) = copy(val)

function reconstruct(dist::LKJCholesky, val::AbstractVector{<:Real})
return reconstruct(dist, reshape(val, size(dist)))
return reconstruct(dist, Matrix(reshape(val, size(dist))))
end
function reconstruct(dist::LKJCholesky, val::AbstractMatrix{<:Real})
return Cholesky(val, dist.uplo, 0)
Expand Down
32 changes: 32 additions & 0 deletions test/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ function innermost_distribution_type(d::Distributions.Product)
return dists[1]
end

is_typed_varinfo(::DynamicPPL.AbstractVarInfo) = false
is_typed_varinfo(varinfo::DynamicPPL.TypedVarInfo) = true
is_typed_varinfo(varinfo::DynamicPPL.SimpleVarInfo{<:NamedTuple}) = true

@testset "model.jl" begin
@testset "convenience functions" begin
model = gdemo_default # defined in test/test_util.jl
Expand Down Expand Up @@ -329,4 +333,32 @@ end
@test x_true.UL == result.x.UL
end
end

@testset "Type stability of models" begin
models_to_test = [
# FIXME: Fix issues with type-stability in `DEMO_MODELS`.
# DynamicPPL.TestUtils.DEMO_MODELS...,
DynamicPPL.TestUtils.demo_lkjchol(2),
]
@testset "$(model.f)" for model in models_to_test
vns = DynamicPPL.TestUtils.varnames(model)
example_values = DynamicPPL.TestUtils.rand(model)
varinfos = filter(
is_typed_varinfo,
DynamicPPL.TestUtils.setup_varinfos(model, example_values, vns),
)
@testset "$(short_varinfo_name(varinfo))" for varinfo in varinfos
@test (@inferred(DynamicPPL.evaluate!!(model, varinfo, DefaultContext()));
true)

varinfo_linked = DynamicPPL.link(varinfo, model)
@test (
@inferred(
DynamicPPL.evaluate!!(model, varinfo_linked, DefaultContext())
);
true
)
end
end
end
end

2 comments on commit 12e7c27

@torfjelde
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/93982

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.23.20 -m "<description of version>" 12e7c27a2d9cbcbc1fa1c3eb2d84704357f26a59
git push origin v0.23.20

Please sign in to comment.