Repository navigation
Description
Dear Turing,
Here is a short report of a bug I ran into while making an extension for my package. As usual, it is a great pleasure to use your software - I hope this is useful. Let me know if I can do anything.
This is connected to the previous issue as well, on the growable array warning. If there are any good workarounds, I would also be happy to know them!
Below is a Claude-reduced MWE:
--- Claude ---
DynamicPPL.skeleton of a PartialArray whose element type is concrete takes the skeleton of the first element that is set, and fills every element with it. When the elements hold arrays of different sizes, the later ones are rebuilt too small. The round trip in the docstring of skeleton then fails, and so does FlexiChains.parameters_at on a chain of such a model.
using Turing
using Turing: DynamicPPL
using Turing.DynamicPPL: AbstractPPL
using Random: Xoshiro
@model inner() = (x ~ Normal(); return x)
# session[1] has a variable at timestep 1, session[2] one at timestep 3
@model function outer()
a ~ to_submodel(prefix(inner(), @varname(session[1].timestep[1])), false)
b ~ to_submodel(prefix(inner(), @varname(session[2].timestep[3])), false)
return nothing
end
model = outer()
# The round trip that the docstring of skeleton describes
values = rand(Xoshiro(1), model)
skel = DynamicPPL.skeleton(values)
rebuilt = DynamicPPL.VarNamedTuple()
for (vn, val) in pairs(values)
template = get(skel.data, AbstractPPL.getsym(vn), DynamicPPL.NoTemplate())
global rebuilt = DynamicPPL.templated_setindex!!(rebuilt, val, vn, template)
endERROR: BoundsError: attempt to access 1-element Vector{Nothing} at index [3]
Stacktrace:
[1] throw_boundserror(A::Vector{Nothing}, I::Tuple{Int64})
...
[4] view
@ ~/.julia/packages/DynamicPPL/K7ixO/src/varnamedtuple/partial_array.jl:71 [inlined]
[5] #_ndims#23
@ ~/.julia/packages/DynamicPPL/K7ixO/src/varnamedtuple/partial_array.jl:471 [inlined]
The skeleton of session gives session[2].timestep the length of session[1].timestep, which is 1. With the two sessions the other way round, the skeleton is too long instead, and the round trip works.
The same happens when FlexiChains.jl rebuilds a sample:
using Turing: FlexiChains
chain = sample(Xoshiro(1), model, Prior(), 10; progress=false)
FlexiChains.parameters_at(chain, 1, 1) # the same BoundsErrorThis is what stops returned and predict on a chain whenever a model asks for a variable that the chain doesn't hold under exactly that name, so that FlexiChains.jl rebuilds the sample. We meet it with models of several sessions of different lengths, where each session has variables for some of its timesteps, such as imputed missing data.
A fix
In src/varnamedtuple/skeleton.jl, the branch for a concrete element type that needs a skeleton could give each element its own, as the branch for an abstract element type already does. Since the element type is concrete, the skeletons have one type:
return quote
idx = findfirst(pa.mask)
idx === nothing && error("Unexpected PartialArray with no data")
example_skeleton = skeleton(pa.data[idx])
new_data = similar(pa.data, typeof(example_skeleton))
for i in eachindex(pa.data)
new_data[i] = pa.mask[i] ? skeleton(pa.data[i]) : example_skeleton
end
return new_data
endin place of
fill!(new_data, example_skeleton)With this change, the round trip above gives back the values, parameters_at works, and the reversed order gives each session a skeleton of its own length.
Turing 0.49.0, DynamicPPL 0.42.14, FlexiChains 0.6.44, Julia 1.13.4.
Activity
sunxd3 commentedon Nov 18, 2026
Thanks. Claude's diagnose and fix look correct to me. If you want to start a PR, I'll help review.
If you do, note that sizes aren't the only thing that can differ between sessions: two sessions of the same type can also have skeletons of different types, e.g. if a session's field is typed Any and holds a Vector in
session[1]but a Matrix insession[2]. Thennew_data[i] = …fails, andBangBang.setindex!!can help avoid it.In the meantime,
FlexiChains.parameters_at(chain, Dict; iter=1, chain=1)doesn't use the stored skeleton and should work.