Skip to content
Open
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
8 changes: 8 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ When rebuilding a `VarNamedTuple` from its skeleton, each nested array now keeps

`templated_setindex!!` accepts another `VarNamedTuple`'s storage as a template when a slice of it, such as `z[1:2]`, holds a `VarNamedTuple`; it previously threw ([#1539](https://github.com/TuringLang/DynamicPPL.jl/pull/1539)).

Tilde statements on zero-dimensional arrays, such as `x = fill(0.0); x[] ~ Normal()`, no longer throw when values are recorded, as in `rand(model)` or `LogDensityFunction(model)`.

Tilde statements with keyword indices, such as `x[X=2] ~ Normal()` on a `DimArray`, keep the array type; previously `x` became its parent array.

`hash` now agrees with `isequal` for transformed values, transform strategies, `ParamsWithStats`, `VarNamedTuple` and log-probability accumulators, so `Set` and `Dict` no longer keep equal copies as separate keys ([#1543](https://github.com/TuringLang/DynamicPPL.jl/pull/1543)).

Growable-array creation warnings use a logging limit of one per array dimension and logger ([#1538](https://github.com/TuringLang/DynamicPPL.jl/pull/1538)).
Expand Down Expand Up @@ -50,6 +54,10 @@ Added `evaluate!!(model, context, vi)` to evaluate with an explicit leaf context

`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()`.

A tilde assignment that widens storage, such as a `Float64` draw into `Union{Missing,Int}[2^53 + 1, missing]`, throws an `ArgumentError` when the promotion would change a retained element; previously the element was rounded. Arrays of a plain isbits element type, such as `Vector{Int}`, are not checked, because their unset entries hold arbitrary bits.

Tuple tilde writes promote all elements: `(1, 0)` becomes `(1.0, draw)` after a floating-point draw into its second element, so a later tilde into the first element no longer throws when values are recorded. Lossy promotion, and writes that leave a tuple with mixed element types, throw an `ArgumentError`; use storage with one element type that represents both retained values and draws. This includes tuple arguments: for `@model f(x) = (x[1] ~ Normal(); x)`, `f((missing, missing))` now throws; pass `Union{Missing,Float64}[missing, missing]` or a tuple of the draw's type instead.

`VarNamedTuple` lookups and model evaluation throw `ArgumentError` for indices with a different number of dimensions, indices using `begin`, `end` or `:`, or Boolean masks of a different shape, in values set by index without a template; previously these could return the wrong element or be treated as missing. Supply a template or the whole array, or pass the `VarNamedTuple` itself rather than a `Dict` of its pairs ([#1544](https://github.com/TuringLang/DynamicPPL.jl/pull/1544)).

`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).
Expand Down
6 changes: 6 additions & 0 deletions ext/DynamicPPLMooncakeExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ function Mooncake.rrule!!(
return output, pullback
end

# Sound: `_check_widening` only throws or returns `nothing`. Mooncake cannot differentiate
# the try/catch in `isassigned` on non-`Array` storage.
Mooncake.@zero_derivative Mooncake.DefaultCtx Tuple{
typeof(DynamicPPL._check_widening),Vararg
}

# These are purely optimisations (although quite significant ones sometimes, especially for
# _get_range_and_transform).
Mooncake.@zero_derivative Mooncake.DefaultCtx Tuple{typeof(is_transformed),Vararg}
Expand Down
81 changes: 80 additions & 1 deletion src/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,85 @@ function generate_tilde_literal(left, right)
end

function set_tilde_value!!(::AbstractVarInfo, object, value, vn::VarName)
return Accessors.set(object, AbstractPPL.with_mutation(AbstractPPL.getoptic(vn)), value)
return _set_lhs(object, AbstractPPL.getoptic(vn), value, vn)
end

function _set_lhs(object, optic, value, vn)
(object === missing || object === nothing) && throw(
ArgumentError(
"Cannot assign latent LHS variable `$vn`: `$(AbstractPPL.getsym(vn))` is `$object` " *
"and has no storage; supply a concrete argument such as `f(zeros(n))` or create the " *
"storage in the model body before the tilde.",
),
)
return _set_at_optic(object, optic, value, vn)
end
_set_at_optic(object, ::AbstractPPL.Iden, value, vn) = value
function _set_at_optic(object, optic::AbstractPPL.Property{S}, value, vn) where {S}
child = _set_at_optic(getproperty(object, S), optic.child, value, vn)
return BangBang.setproperty!!(object, S, child)
end
function _set_at_optic(object, optic::AbstractPPL.Index, value, vn)
optic = AbstractPPL.concretize_top_level(optic, object)
child = if optic.child isa AbstractPPL.Iden
value
else
_set_at_optic(getindex(object, optic.ix...; optic.kw...), optic.child, value, vn)
end
if !isempty(optic.kw)
setindex!(object, child, optic.ix...; optic.kw...)
return object
end
new = _setindex!!(object, child, optic.ix...)
_check_widening(object, new, optic.ix, child, vn)
return new
end

# `_setindex!!` widens by `promote_type` and converts every retained element, which can
# round them (`2^53 + 1` as `Float64`); such a write must throw rather than change data.
_check_widening(old, new, ix, value, vn) = nothing
function _check_widening(old::Union{AbstractArray,Tuple}, new, ix, value, vn)
eltype(old) <: eltype(new) && return nothing
_isbits_array(old) && return nothing
indices = LinearIndices(axes(old))
written = view(_index_array(old, ix), ix...)
for i in indices
_isassigned(old, i) || continue
o, n = old[i], new[i]
i in written || _retains(o, n) || _throw_widening(o, n, value, vn)
end
return nothing
end
# Must skip undefined entries: nested storage such as `[Vector{BigInt}(undef, 1)]` holds
# them, and `isequal` throws on reading one.
_retains(o, n) = isequal(o, n)
function _retains(o::AbstractArray, n::AbstractArray)
axes(o) == axes(n) || return false
_isbits_array(o) && return true
return all(i -> !_isassigned(o, i) || _retains(o[i], n[i]), LinearIndices(o))
end
# Never check isbits arrays, at any depth: unset entries of `Vector{Int}(undef, n)` hold
# arbitrary bits, which widening may round and no check can tell from retained values.
_isbits_array(x) = x isa AbstractArray && isbitstype(eltype(x))
_index_array(old, ::_PositionalIndices) = LinearIndices(axes(old))
# `similar(old, Int)` keeps `old`'s wrapper and axes, so `view` resolves wrapper indices:
# `ComponentArray` symbols, or DimensionalData selectors, on which `to_indices` throws.
_index_array(old, _) = copyto!(similar(old, Int), LinearIndices(axes(old)))
function _check_widening(old::AbstractDict, new, ix, value, vn)
valtype(old) <: valtype(new) && return nothing
for (k, o) in old
isequal(k, only(ix)) || _retains(o, new[k]) || _throw_widening(o, new[k], value, vn)
end
return nothing
end
function _throw_widening(o, n, value, vn)
throw(
ArgumentError(
"Assigning a `$(typeof(value))` to `$vn` changes the retained element " *
"$(repr(o)) to $(repr(n)). Use an element or value type that holds both " *
"values, such as `Union{Missing,Real}` for an array with `missing` entries.",
),
)
end

assign_or_set!!(lhs::Symbol, rhs, vn) = drop_escape(:($lhs = $rhs))
Expand Down Expand Up @@ -584,6 +662,7 @@ function generate_tilde_assume(left, right, vn)
__varinfo__,
)
$expr
$(VarNamedTuples._reject_mixed_tuple)($template, $(AbstractPPL.getoptic)($vn))
$value
end
end
Expand Down
41 changes: 41 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,44 @@
# Use DynamicPPL._setindex!! to add write methods without pirating BangBang.
function _setindex!!(tuple::Tuple{T,Vararg{T}}, value, index::Int) where {T}
S = _promote_elt(T, typeof(value))
return Base.setindex(convert(NTuple{length(tuple),S}, tuple), convert(S, value), index)
end
# Promote tuple and named-tuple fields pairwise: `promote_type` widens them as a whole, so
# `Tuple{Int,Int}` and `Tuple{Float64,Float64}` give `Tuple{Real,Real}`, leaving them mixed.
_promote_elt(::Type{T}, ::Type{V}) where {T,V} = promote_type(T, V)
function _promote_elt(::Type{T}, ::Type{V}) where {N,T<:NTuple{N,Any},V<:NTuple{N,Any}}
return Tuple{map(_promote_elt, fieldtypes(T), fieldtypes(V))...}
end
function _promote_elt(::Type{NamedTuple{K,T}}, ::Type{NamedTuple{K,V}}) where {K,T,V}
return NamedTuple{K,_promote_elt(T, V)}
end
_setindex!!(array, value, indices...) = BangBang.setindex!!(array, value, indices...)
# BangBang applies slice checks to array-valued scalar writes whose type differs
# from the eltype; remove this path when its applicability check handles them.
function _setindex!!(
array::AbstractArray,
value,
i::Union{Integer,CartesianIndex},
ix::Union{Integer,CartesianIndex}...,
)
value isa eltype(array) && BangBang.implements(setindex!, typeof(array)) ||
return BangBang.setindex!!(array, value, i, ix...)
array[i, ix...] = value
return array
end
# BangBang's dimension reduction needs a nonempty index tuple for scalar array writes.
function _setindex!!(array::AbstractArray, value)
return BangBang.setindex!!(array, value, CartesianIndex())
end

_isassigned(x, indices...) = isassigned(x, indices...)
_isassigned(x::Tuple, i) = i in keys(x)
const _PositionalIndices = Tuple{
Vararg{
Union{Integer,Colon,CartesianIndex,AbstractArray{<:Union{Integer,CartesianIndex}}}
},
}

# subset is defined here to avoid circular dependencies between files. Methods for it are
# defined in other files.
function subset end
Expand Down
19 changes: 18 additions & 1 deletion src/varnamedtuple/getset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ function _setindex_optic!!(
)
perms isa MustNotOverwrite && throw(MustNotOverwriteError(perms))
coptic = AbstractPPL.concretize_top_level(optic, arr)
return BangBang.setindex!!(arr, value, coptic.ix...; coptic.kw...)
return DynamicPPL._setindex!!(arr, value, coptic.ix...; coptic.kw...)
end
function _setindex_optic!!(
nt::NamedTuple{names},
Expand Down Expand Up @@ -396,6 +396,23 @@ function _reject_mixed_tuple(::Tuple)
ArgumentError("same-type tuples only; give the elements one type or use an array")
)
end
const _SameTypeTuple = Tuple{T,Vararg{T}} where {T}
# Checks the containers on a tilde's path after the write, which can mix a same-type tuple:
# a draw into `(missing, missing)` gives `(draw, missing)`.
_reject_mixed_tuple(x, ::AbstractPPL.Iden) = nothing
function _reject_mixed_tuple(x, optic::AbstractPPL.AbstractOptic)
if x isa Tuple{Any,Vararg} && !(x isa _SameTypeTuple)
throw(
ArgumentError(
"this tilde assignment would leave a tuple with mixed element types " *
"($(typeof(x))); use array storage, such as " *
"`Union{Missing,Float64}[missing, missing]`, or a tuple of the draw's type",
),
)
end
optic.child isa AbstractPPL.Iden && return nothing
return _reject_mixed_tuple(AbstractPPL.ohead(optic)(x), optic.child)
end
function make_leaf(value, optic::AbstractPPL.Index, template::Tuple)
_reject_mixed_tuple(template)
return make_leaf(value, optic, collect(template))
Expand Down
2 changes: 1 addition & 1 deletion src/varnamedtuple/partial_array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ function _setindex_partial_array_data!!(data, mask, value, inds...; kw...)
setindex!(data, value, inds...; kw...)
return data
end
return BangBang.setindex!!(data, value, inds...; kw...)
return DynamicPPL._setindex!!(data, value, inds...; kw...)
end

function Base.copy(pa::PartialArray)
Expand Down
124 changes: 122 additions & 2 deletions test/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ __now__ = now()

using Distributions
using DynamicPPL
using ADTypes: AutoForwardDiff
using ComponentArrays: ComponentArrays as CA
using DimensionalData: DimensionalData as DD
using ForwardDiff: ForwardDiff
using REPL: REPL
using LinearAlgebra: I
using Random: Random
using LogDensityProblems: logdensity_and_gradient
using Random: Random, Xoshiro
using Test

macro custom(expr)
Expand Down Expand Up @@ -220,7 +224,7 @@ end
x[1] ~ Bernoulli(0.5)
return x
end
@test_throws MethodError testmodel_missing2(missing)()
@test_throws ArgumentError testmodel_missing2(missing)()

# Test use of internal names
@model function testmodel_missing3(x)
Expand Down Expand Up @@ -945,6 +949,122 @@ end
@test only(DynamicPPL.getindex_internal(vi, @varname(m))) isa Real
end

@testset "widening tilde storage keeps retained elements" begin
@model function widen(x)
x[2] ~ Normal()
return x
end
@model function widen_field(a)
x = (; b=Union{Missing,Int}[a, 0])
x.b[2] ~ Normal()
return x
end
@model function widen_int(a)
x = [a, 0]
x[2] ~ Normal()
return x
end
@model function widen_int_nested(a)
x = [[a, 0], [a, 0]]
x[1][2] ~ Normal()
return x
end
@model function widen_tuple(a)
x = (a, 0)
x[2] ~ Normal()
return x
end
@model function widen_dict(a)
x = Dict(:a => a, :b => 0)
x[:b] ~ Normal()
return x
end
@model function widen_ca(a)
x = CA.ComponentVector(Union{Missing,Int}[a, 0], CA.Axis(; a=1, b=2))
x[:b] ~ Normal()
return x
end
@model function widen_dim(a)
x = DD.DimArray(Union{Missing,Int}[a, 0], DD.X([10, 20]))
x[DD.X(DD.At(20))] ~ Normal()
return x
end
@model function write_tuple(x)
x[1] ~ Normal()
return x
end
@model function widen_nested(a)
x = ((a, a), (a + 1, a + 1))
x[1][2] ~ Normal()
return x
end
@model function widen_named(a)
x = ((u=a, v=a), (u=a + 1, v=a + 1))
x[1].u ~ Normal()
return x
end
@model function widen_nested_undef()
x = [Vector{BigInt}(undef, 1) for _ in 1:2]
x[1][1] ~ Normal()
return x
end
@model widen_sub() = Vector{BigFloat}(undef, 1)
@model function widen_written()
x = [[big(1)], [big(2)]]
x[1] ~ to_submodel(widen_sub(), false)
return x
end
@model function widen_undef()
x = Vector{Vector{Int}}(undef, 2)
x[1] ~ MvNormal(zeros(2), I)
return x
end
rng = Xoshiro(468)
n = 2^53 + 1
msg = "changes the retained element"
@test widen_tuple(1)(rng)[1] === 1.0
@test widen_ca(1)(rng).a === 1.0
@test widen_dim(1)(rng)[1] === 1.0
@test widen_nested(0)(rng)[2] === (1.0, 1.0)
ldf = LogDensityFunction(widen_nested(0.0); adtype=AutoForwardDiff())
@test logdensity_and_gradient(ldf, [0.3]) == (logpdf(Normal(), 0.3), [-0.3])
ldf = LogDensityFunction(widen_named(0.0); adtype=AutoForwardDiff())
@test logdensity_and_gradient(ldf, [0.3]) == (logpdf(Normal(), 0.3), [-0.3])
@test !isassigned(widen_undef()(rng), 2)
@test !isassigned(widen_nested_undef()(rng)[2], 1)
@test !isassigned(widen_written()(rng)[1], 1)
# Plain isbits arrays are not checked, since unset entries hold arbitrary bits.
@test widen_int(n)(rng)[1] === Float64(n)
@test widen_int_nested(n)(rng)[2][1] === Float64(n)
@test_throws msg widen(Union{Missing,Int}[n, missing])(rng)
@test_throws msg widen_field(n)(rng)
@test_throws msg widen_tuple(n)(rng)
@test_throws msg widen_dict(n)(rng)
@test_throws "mixed element types" write_tuple((missing, missing))(rng)
end

@testset "tilde writes keep storage" begin
@model function alias_any()
x = Any[0.0, 0.0]
y = x
x[CartesianIndex(1)] ~ MvNormal(zeros(2), I)
return y[1]
end
@model function zero_dim()
x = fill(0.0)
x[] ~ Normal()
return x
end
@model function dim_keyword()
x = DD.DimArray([0.0, 0.0], DD.X([10, 20]))
x[X=2] ~ Normal()
return x
end
@test alias_any()(Xoshiro(1)) isa Vector{Float64}
@test rand(Xoshiro(1), zero_dim())[@varname(x[])] == rand(Xoshiro(1), Normal())
@test dim_keyword()(Xoshiro(1)) isa DD.DimArray
end

@testset "convert_model_argument" begin
tdual = ForwardDiff.Dual{Nothing,Float64,0}
# no-op
Expand Down
19 changes: 17 additions & 2 deletions test/ext/DynamicPPLMooncakeExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ using Dates: now
@info "Testing $(@__FILE__)..."
__now__ = now()

using ADTypes: AutoMooncake
using ComponentArrays: ComponentArrays as CA
using Distributions: Normal, logpdf
using LogDensityProblems: logdensity_and_gradient
using Mooncake: Mooncake
using StableRNGs: StableRNG
using DynamicPPL: is_transformed, VarInfo, VectorValueAccumulator
using Test: @testset
using DynamicPPL:
@model, is_transformed, LogDensityFunction, VarInfo, VectorValueAccumulator
using Test: @testset, @test

@testset "DynamicPPLMooncakeExt" begin
Mooncake.TestUtils.test_rule(
Expand All @@ -17,6 +22,16 @@ using Test: @testset
unsafe_perturb=true,
interface_only=true,
)

@testset "widening tilde write" begin
@model function widen_ca()
x = CA.ComponentVector(Union{Missing,Int}[1, 0], CA.Axis(; a=1, b=2))
x[:b] ~ Normal()
return x
end
ldf = LogDensityFunction(widen_ca(); adtype=AutoMooncake())
@test logdensity_and_gradient(ldf, [0.3]) == (logpdf(Normal(), 0.3), [-0.3])
end
end

@info "Completed $(@__FILE__) in $(now() - __now__)."
Expand Down
Loading