From 9c26048e6098a22eb6cd8842c41c02cc78e4a3f7 Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Tue, 10 Nov 2026 14:59:26 +0000 Subject: [PATCH] varinfo: require recorded values for container queries Without a `VectorValueAccumulator`, `keys`, `haskey`, `length`, `values` and `isempty` on a `VarInfo` reported an empty collection, and `is_transformed(vi)` returned `false`. They now throw an `ArgumentError`: an absent value accumulator is not an empty collection. The `subset` example gains its missing opening fence, so it now runs as a doctest. Assisted-by: Claude Code --- HISTORY.md | 2 ++ src/abstract_varinfo.jl | 7 +++++++ src/varinfo.jl | 10 +++++----- test/varinfo.jl | 11 ++++++----- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index ceeaaf6dc5..4596bb8115 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -42,6 +42,8 @@ Added `evaluate!!(model, context, vi)` to evaluate with an explicit leaf context ## Breaking changes +`keys`, `haskey`, `length`, `values`, `isempty` and `is_transformed(vi)` on a `VarInfo` without a `VectorValueAccumulator` now throw an `ArgumentError` instead of reporting no variables. Record values with `VarInfo(VectorValueAccumulator(), DynamicPPL.default_accumulators()...)` in place of `VarInfo()`. + `marginalize` and the `DynamicPPLMarginalLogDensitiesExt` extension are removed in DynamicPPL 0.43. Users requiring the existing Turing/MLD integration can remain on DynamicPPL 0.42.x with a compatible Turing release—for example, Turing 0.49.0—and MarginalLogDensities 0.4.3–0.4.x. See the [0.42 marginalisation documentation](https://turinglang.org/DynamicPPL.jl/v0.42/api/#Marginalisation). On newer DynamicPPL versions, MLD’s generic API requires a manually constructed log-density adapter; no drop-in model-level replacement is currently available. Migration of the integration into MLD is proposed in [MLD #47](https://github.com/ElOceanografo/MarginalLogDensities.jl/pull/47). diff --git a/src/abstract_varinfo.jl b/src/abstract_varinfo.jl index 27e8bd8526..7e5db90e4b 100644 --- a/src/abstract_varinfo.jl +++ b/src/abstract_varinfo.jl @@ -419,6 +419,8 @@ end keys(vi::AbstractVarInfo) Return an iterator over all `vns` in `vi`. + +Throw an `ArgumentError` if `vi` has no `VectorValueAccumulator`. """ Base.keys """ @@ -528,6 +530,8 @@ This is useful when using a sampling algorithm that assumes an empty `vi`, e.g. isempty(vi::AbstractVarInfo) Return true if `vi` is empty and false otherwise. + +Throw an `ArgumentError` if `vi` has no `VectorValueAccumulator`. """ Base.isempty """ @@ -564,6 +568,8 @@ Subset a `varinfo` to only contain the variables `vns`. The ordering of variables in the return value will be the same as in `varinfo`. # Examples + +```jldoctest varinfo-subset julia> using DynamicPPL, Distributions julia> @model function demo() @@ -675,6 +681,7 @@ Return `true` if all variables in `vi` have a transform of `DynamicLink`, and fa otherwise. If `vns` is provided, then only check if this/these varname(s) are transformed. +Throw an `ArgumentError` if `vi` has no `VectorValueAccumulator`, unless `vns` is empty. !!! warning Not all implementations of `AbstractVarInfo` support transforming only a subset of diff --git a/src/varinfo.jl b/src/varinfo.jl index da02d3febd..e6a76d9aa5 100644 --- a/src/varinfo.jl +++ b/src/varinfo.jl @@ -69,19 +69,19 @@ get_vector_values(vi::AbstractVarInfo) = getacc(vi, Val(VECTORVAL_ACCNAME)).valu get_values(vi::AbstractVarInfo) = get_vector_values(vi) function Base.keys(vi::Union{VarInfo,ThreadSafeVarInfo}) - return hasacc(vi, Val(VECTORVAL_ACCNAME)) ? keys(get_vector_values(vi)) : () + return keys(get_vector_values(vi)) end function Base.haskey(vi::Union{VarInfo,ThreadSafeVarInfo}, vn::VarName) - return hasacc(vi, Val(VECTORVAL_ACCNAME)) && haskey(get_vector_values(vi), vn) + return haskey(get_vector_values(vi), vn) end function Base.length(vi::Union{VarInfo,ThreadSafeVarInfo}) - return hasacc(vi, Val(VECTORVAL_ACCNAME)) ? length(get_vector_values(vi)) : 0 + return length(get_vector_values(vi)) end function Base.values(vi::Union{VarInfo,ThreadSafeVarInfo}) - return hasacc(vi, Val(VECTORVAL_ACCNAME)) ? values(get_vector_values(vi)) : () + return values(get_vector_values(vi)) end function Base.isempty(vi::Union{VarInfo,ThreadSafeVarInfo}) - return !hasacc(vi, Val(VECTORVAL_ACCNAME)) || isempty(get_vector_values(vi)) + return isempty(get_vector_values(vi)) end Base.empty(vi::VarInfo) = resetaccs!!(copy(vi)) BangBang.empty!!(vi::VarInfo) = resetaccs!!(vi) diff --git a/test/varinfo.jl b/test/varinfo.jl index 286e7ffa5e..789bffad24 100644 --- a/test/varinfo.jl +++ b/test/varinfo.jl @@ -70,11 +70,12 @@ end @testset "no recorded values" begin for vi in (VarInfo(), DynamicPPL.ThreadSafeVarInfo(VarInfo())) - @test @inferred isempty(vi) - @test isempty(@inferred keys(vi)) - @test isempty(@inferred values(vi)) - @test !(@inferred haskey(vi, @varname(x))) - @test (@inferred length(vi)) == 0 + @test_throws ArgumentError isempty(vi) + @test_throws ArgumentError keys(vi) + @test_throws ArgumentError values(vi) + @test_throws ArgumentError haskey(vi, @varname(x)) + @test_throws ArgumentError length(vi) + @test_throws ArgumentError is_transformed(vi) @test_throws ArgumentError get_vector_values(vi) @test_throws ArgumentError internal_values_as_vector(vi) @test_throws ArgumentError vi[:]