From f618024134b10764471113e5626716b406fe5ef9 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 22 May 2025 11:39:18 +0100 Subject: [PATCH 1/3] Fix getparams on empty varinfo --- HISTORY.md | 4 ++++ Project.toml | 2 +- src/mcmc/Inference.jl | 3 +++ test/mcmc/Inference.jl | 24 ++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 42415d7efc..3e9b784608 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,7 @@ +# Release 0.38.2 + +`getparams(::Model, ::AbstractVarInfo)` now returns an empty `Float64` if the VarInfo contains no parameters. + # Release 0.38.1 The method `Bijectors.bijector(::DynamicPPL.Model)` was moved to DynamicPPL.jl. diff --git a/Project.toml b/Project.toml index acffce201e..522d352471 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "Turing" uuid = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" -version = "0.38.1" +version = "0.38.2" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" diff --git a/src/mcmc/Inference.jl b/src/mcmc/Inference.jl index 0cbb45b48f..cdc9e2570c 100644 --- a/src/mcmc/Inference.jl +++ b/src/mcmc/Inference.jl @@ -358,6 +358,9 @@ function getparams(model::DynamicPPL.Model, vi::DynamicPPL.VarInfo) # Materialize the iterators and concatenate. return mapreduce(collect, vcat, iters) end +function getparams(::DynamicPPL.Model, ::DynamicPPL.VarInfo{NamedTuple{(),Tuple{}}}) + return float(Real)[] +end function _params_to_array(model::DynamicPPL.Model, ts::Vector) names_set = OrderedSet{VarName}() diff --git a/test/mcmc/Inference.jl b/test/mcmc/Inference.jl index 5966589090..f994a36af3 100644 --- a/test/mcmc/Inference.jl +++ b/test/mcmc/Inference.jl @@ -631,6 +631,30 @@ using Turing StableRNG(seed), demo_incorrect_missing([missing]), NUTS(), 10; check_model=true ) end + + @testset "getparams" begin + @model function e(x=1.0) + return x ~ Normal() + end + evi = Turing.VarInfo(e()) + @test isempty(Turing.Inference.getparams(e(), evi)) + + @model function f() + return x ~ Normal() + end + fvi = Turing.VarInfo(f()) + @test only(Turing.Inference.getparams(f(), fvi)) == (@varname(x), fvi[@varname(x)]) + + @model function g() + x ~ Normal() + return y ~ Poisson() + end + gvi = Turing.VarInfo(g()) + gparams = Turing.Inference.getparams(g(), gvi) + @test gparams[1] == (@varname(x), gvi[@varname(x)]) + @test gparams[2] == (@varname(y), gvi[@varname(y)]) + @test length(gparams) == 2 + end end end From 83a0ebf3750a5290c61475f5f2b6e7fc61fd0934 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 22 May 2025 11:57:46 +0100 Subject: [PATCH 2/3] Fix two methods in MH for empty tuples --- src/mcmc/mh.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mcmc/mh.jl b/src/mcmc/mh.jl index 6a03f5359f..3450cfdc74 100644 --- a/src/mcmc/mh.jl +++ b/src/mcmc/mh.jl @@ -246,6 +246,7 @@ end ] return expr end +_val_tuple(::VarInfo, ::Tuple{}) = () @generated function _dist_tuple( props::NamedTuple{propnames}, vi::VarInfo, vns::NamedTuple{names} @@ -267,6 +268,7 @@ end ] return expr end +_dist_tuple(::@NamedTuple{}, ::VarInfo, ::Tuple{}) = () # Utility functions to link should_link(varinfo, sampler, proposal) = false From 632fef61993f7fca1218b713ccf5747e54e8b7aa Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 22 May 2025 11:57:59 +0100 Subject: [PATCH 3/3] Add a test to sample an empty model --- test/mcmc/Inference.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/mcmc/Inference.jl b/test/mcmc/Inference.jl index f994a36af3..55d989b154 100644 --- a/test/mcmc/Inference.jl +++ b/test/mcmc/Inference.jl @@ -655,6 +655,15 @@ using Turing @test gparams[2] == (@varname(y), gvi[@varname(y)]) @test length(gparams) == 2 end + + @testset "empty model" begin + @model function e(x=1.0) + return x ~ Normal() + end + # Can't test with HMC/NUTS because some AD backends error; see + # https://github.com/JuliaDiff/DifferentiationInterface.jl/issues/802 + @test sample(e(), IS(), 100) isa MCMCChains.Chains + end end end