Skip to content
Merged
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
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# 0.43.0 (unreleased)

Added `AbstractPPL.of(vnt::VarNamedTuple)`, which describes a `VarNamedTuple`'s storage as an AbstractPPL binding template. It supports numeric `Array`s, nested fields and scalars whose type `of` preserves; other storage, such as values set by index without a template, throws an `ArgumentError` ([#1547](https://github.com/TuringLang/DynamicPPL.jl/pull/1547)).

When rebuilding a `VarNamedTuple` from its skeleton, each nested array now keeps its original size. Previously, reusing the first array’s size could produce incorrectly sized arrays or throw a `BoundsError` ([#1540](https://github.com/TuringLang/DynamicPPL.jl/issues/1540)).

`VarNamedTuple` records indexed random variables in same-type tuple templates ([#1539](https://github.com/TuringLang/DynamicPPL.jl/pull/1539)).
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ DynamicPPLReverseDiffExt = ["ReverseDiff"]
[compat]
ADTypes = "1"
AbstractMCMC = "5.17"
AbstractPPL = "0.15"
AbstractPPL = "0.15.3"
Accessors = "0.1"
BangBang = "0.4.1"
Bijectors = "0.16"
Expand Down
50 changes: 50 additions & 0 deletions src/varnamedtuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,56 @@ include("varnamedtuple/show.jl")
include("varnamedtuple/macro.jl")
include("varnamedtuple/skeleton.jl")

"""
AbstractPPL.of(vnt::VarNamedTuple)

Describe the storage in `vnt` as an AbstractPPL `OfNamedTuple` binding template.

Numeric `Array`s keep their element type and full size, even when partly filled; nested
namespaces become nested templates; real scalars are kept when `of` describes their type
exactly. Values are discarded. Other storage, such as custom or growable arrays, indexed
namespaces and scalars that `of` would widen, throws `ArgumentError`.

# Examples

```jldoctest
julia> using AbstractPPL: of

julia> template = of(VarNamedTuple(; y=zeros(Float32, 2)));

julia> zero(template)
(y = Float32[0.0, 0.0],)
```
"""
AbstractPPL.of(vnt::VarNamedTuple) = _storage_template(vnt, "")

# `path` is the dotted field path of the stored value, for error messages.
_storage_template(vnt::VarNamedTuple, path) = _storage_template(vnt.data, path)
function _storage_template(nt::NamedTuple{names}, path) where {names}
child(k) = isempty(path) ? string(k) : "$path.$k"
return AbstractPPL.of(
NamedTuple{names}(map(k -> _storage_template(nt[k], child(k)), names))
)
end
_storage_template(pa::PartialArray, path) = _storage_template(pa.data, path)
_storage_template(a::Array{<:Number}, _) = AbstractPPL.of(a)
function _storage_template(x::Real, path)
template = AbstractPPL.of(x)
typeof(zero(template)) === typeof(x) || throw(
ArgumentError(
"Cannot preserve scalar type $(typeof(x)) of `$path` in a binding template: `of` would widen it to $(typeof(zero(template))).",
),
)
return template
end
function _storage_template(x, path)
throw(
ArgumentError(
"Cannot describe `$path` of type $(typeof(x)) exactly in a binding template."
),
)
end

"""
NamedTuple(vnt::VarNamedTuple)

Expand Down
10 changes: 10 additions & 0 deletions test/varnamedtuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2565,6 +2565,16 @@ end
end
end

@testset "binding templates from storage" begin
pa = templated_setindex!!(VarNamedTuple(), 1.0f0, @varname(x[2]), zeros(Float32, 3))
@test AbstractPPL.of(VarNamedTuple(; a=pa, b=1.0)) ==
AbstractPPL.of((a=(x=zeros(Float32, 3),), b=1.0))
@test_throws "`a.x` of type String" AbstractPPL.of(
VarNamedTuple(; a=VarNamedTuple(; x="a"))
)
@test_throws "would widen it to Float64" AbstractPPL.of(VarNamedTuple(; x=1//2))
end

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

end
Loading