From a309bedff3b711444007db1953ea6c389d5f0b43 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 22 May 2025 01:08:43 +0100 Subject: [PATCH 1/7] Fix varinfo[:] for empty varinfo --- src/varinfo.jl | 3 +++ test/varinfo.jl | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/varinfo.jl b/src/varinfo.jl index 360857ef7..bc59c67a6 100644 --- a/src/varinfo.jl +++ b/src/varinfo.jl @@ -854,6 +854,9 @@ getindex_internal(vi::VarInfo, ::Colon) = getindex_internal(vi.metadata, Colon() function getindex_internal(vi::NTVarInfo, ::Colon) return reduce(vcat, map(Base.Fix2(getindex_internal, Colon()), vi.metadata)) end +function getindex_internal(vi::VarInfo{NamedTuple{(),Tuple{}}}, ::Colon) + return float(Real)[] +end function getindex_internal(md::Metadata, ::Colon) return mapreduce( Base.Fix1(getindex_internal, md), vcat, md.vns; init=similar(md.vals, 0) diff --git a/test/varinfo.jl b/test/varinfo.jl index 777917aa6..4c399972e 100644 --- a/test/varinfo.jl +++ b/test/varinfo.jl @@ -79,7 +79,7 @@ end @test hash(vn2) == hash(vn1) function test_base!!(vi_original) - vi = empty!!(vi_original) + vi = empty!!(deepcopy(vi_original)) @test getlogp(vi) == 0 @test isempty(vi[:]) @@ -97,8 +97,10 @@ end @test length(vi[vn]) == 1 @test vi[vn] == r + @test vi[:] == [r] vi = DynamicPPL.setindex!!(vi, 2 * r, vn) @test vi[vn] == 2 * r + @test vi[:] == [2 * r] # TODO(mhauru) Implement these functions for other VarInfo types too. if vi isa DynamicPPL.UntypedVectorVarInfo From 3063c535f0909959028d1d20bebfcb745ad2a57b Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 22 May 2025 01:09:43 +0100 Subject: [PATCH 2/7] Update changelog, bump patch --- HISTORY.md | 4 ++++ Project.toml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 42613d08f..00d61a1ea 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,9 @@ # DynamicPPL Changelog +## 0.36.5 + +`varinfo[:]` now returns an empty vector if `varinfo::DynamicPPL.NTVarInfo` is empty, rather than erroring. + ## 0.36.4 Added compatibility with DifferentiationInterface.jl 0.7, and also with JET.jl 0.10. diff --git a/Project.toml b/Project.toml index 6b3f445e3..b9253d2a5 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "DynamicPPL" uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8" -version = "0.36.4" +version = "0.36.5" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From 3f6629c40ea32a461eb0bb9ce0638667d2cac2cc Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 22 May 2025 01:12:25 +0100 Subject: [PATCH 3/7] Function no longer mutates, remove !! --- test/varinfo.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/varinfo.jl b/test/varinfo.jl index 4c399972e..019615ff7 100644 --- a/test/varinfo.jl +++ b/test/varinfo.jl @@ -78,7 +78,7 @@ end @test vn2 == vn1 @test hash(vn2) == hash(vn1) - function test_base!!(vi_original) + function test_base(vi_original) vi = empty!!(deepcopy(vi_original)) @test getlogp(vi) == 0 @test isempty(vi[:]) @@ -116,11 +116,11 @@ end end vi = VarInfo() - test_base!!(vi) - test_base!!(DynamicPPL.typed_varinfo(vi)) - test_base!!(SimpleVarInfo()) - test_base!!(SimpleVarInfo(Dict())) - test_base!!(SimpleVarInfo(DynamicPPL.VarNamedVector())) + test_base(vi) + test_base(DynamicPPL.typed_varinfo(vi)) + test_base(SimpleVarInfo()) + test_base(SimpleVarInfo(Dict())) + test_base(SimpleVarInfo(DynamicPPL.VarNamedVector())) end @testset "get/set/acc/resetlogp" begin From 7726365976720e3edf4566545567b66c978b0778 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 22 May 2025 01:14:45 +0100 Subject: [PATCH 4/7] No need to empty!! if it's not mutated --- test/varinfo.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/varinfo.jl b/test/varinfo.jl index 019615ff7..7b50e05c6 100644 --- a/test/varinfo.jl +++ b/test/varinfo.jl @@ -79,7 +79,7 @@ end @test hash(vn2) == hash(vn1) function test_base(vi_original) - vi = empty!!(deepcopy(vi_original)) + vi = deepcopy(vi_original) @test getlogp(vi) == 0 @test isempty(vi[:]) From a69e03a74364359e978d1a6f835c13d2e6c31679 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 22 May 2025 01:42:15 +0100 Subject: [PATCH 5/7] `vi` escapes the scope of the function... --- test/varinfo.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/varinfo.jl b/test/varinfo.jl index 7b50e05c6..7439869cf 100644 --- a/test/varinfo.jl +++ b/test/varinfo.jl @@ -115,9 +115,8 @@ end @test ~isempty(vi) end - vi = VarInfo() - test_base(vi) - test_base(DynamicPPL.typed_varinfo(vi)) + test_base(VarInfo()) + test_base(DynamicPPL.typed_varinfo(VarInfo())) test_base(SimpleVarInfo()) test_base(SimpleVarInfo(Dict())) test_base(SimpleVarInfo(DynamicPPL.VarNamedVector())) From 585027b2e48692e46b81cdd509e51fa589032e94 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 22 May 2025 11:25:34 +0100 Subject: [PATCH 6/7] Warn in check_model if model is empty --- HISTORY.md | 2 ++ src/debug_utils.jl | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 00d61a1ea..d1f8c2ba5 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -4,6 +4,8 @@ `varinfo[:]` now returns an empty vector if `varinfo::DynamicPPL.NTVarInfo` is empty, rather than erroring. +In its place, `check_model` now issues a warning if the model is empty. + ## 0.36.4 Added compatibility with DifferentiationInterface.jl 0.7, and also with JET.jl 0.10. diff --git a/src/debug_utils.jl b/src/debug_utils.jl index 15ef8fb01..68a78f589 100644 --- a/src/debug_utils.jl +++ b/src/debug_utils.jl @@ -338,6 +338,11 @@ function conditioned_varnames(context) end function check_varnames_seen(varnames_seen::AbstractDict{VarName,Int}) + if isempty(varnames_seen) + @warn "The model does not contain any parameters." + return true + end + issuccess = true for (varname, count) in varnames_seen if count == 0 From b81f10e82ac638df38a189f9f48d59e988f5c721 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 22 May 2025 11:43:14 +0100 Subject: [PATCH 7/7] Fix doctest --- src/debug_utils.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/debug_utils.jl b/src/debug_utils.jl index 68a78f589..754b344ee 100644 --- a/src/debug_utils.jl +++ b/src/debug_utils.jl @@ -421,6 +421,8 @@ julia> print(trace) assume: x ~ Normal{Float64}(μ=0.0, σ=1.0) ⟼ -0.670252 (logprob = -1.14356) julia> issuccess, trace = check_model_and_trace(rng, demo_correct() | (x = 1.0,)); +┌ Warning: The model does not contain any parameters. +└ @ DynamicPPL.DebugUtils DynamicPPL.jl/src/debug_utils.jl:342 julia> issuccess true