Skip to content

# prefix(model, @varname(session[i])) warns about creating a growable array, though the model has no array there #1535

Closed

Description

@PTWaade

Dear Turing; thank you for your incredible work!
Below is a Claude-reduced and Claude-reported problem (an annoying repeated warning) I ran into while using Turing. I have read through it, but do not know enough about your internals to be of much help. I hope it's useful, and not an already known error - let me know if I can do anything.

--- Claude ---
A submodel under an indexed prefix, such as prefix(inner(), @varname(session[i])), warns "Creating a growable Base.Array of dimension 1 to store values. This may not match the actual type or size of the actual AbstractArray that will be used inside the DynamicPPL model." There is no such array in the model: session is only a name. The warning comes once for every indexed level that a prefix creates, in every evaluation that stores values: rand, VarInfo, and each draw of Turing's sample, predict and returned. logjoint at given values doesn't warn.

PrefixContext passes the variable's template on as SkipTemplate{n}(template), so that it is used n levels down. When the variable has no template, SkipTemplate{N}(::NoTemplate) returns NoTemplate() (src/varnamedtuple.jl, line 51), so the index levels that the prefix adds look like an array without a template, and make_leaf_singleindex warns (src/varnamedtuple/getset.jl, line 409).

It came up in models with one submodel per session, prefixed session[i], and one per timestep, prefixed timestep[t], where missing data are imputed as latent variables. A model of 20 sessions, each with a latent under session[i] and one under session[i].timestep[4], prints 21 warnings per rand, 483 for sample(model, NUTS(), 20) with Turing 0.49.0, and 420 each for predict and returned on those 20 draws.

Versions

  • Julia 1.13.3, macOS 26.6.2 on Apple silicon
  • DynamicPPL 0.42.14, AbstractPPL 0.15.7, Distributions 0.25.134
  • A new environment with only DynamicPPL and Distributions in it

Reproducer

using DynamicPPL, Distributions

@model function inner()
    a ~ Normal()
end

# One latent per session, under the prefix session[i]
@model function sessions(n)
    for i in 1:n
        a ~ to_submodel(prefix(inner(), @varname(session[i])), false)
    end
end

draw = rand(sessions(3))
println(collect(keys(draw)))

What happens

┌ Warning: Creating a growable `Base.Array` of dimension 1 to store values. This may not match the actual type or size of the actual `AbstractArray` that will be used inside the DynamicPPL model.
│ 
│  If this is not the type or size that you expect, please see: https://turinglang.org/docs/uri/growablearray
└ @ DynamicPPL.VarNamedTuples ~/.julia/packages/DynamicPPL/K7ixO/src/varnamedtuple/partial_array.jl:835
VarName[session[1].a, session[2].a, session[3].a]

What I expected

No warning. The levels that a prefix adds have no array in the model that the stored values could fail to match, and a GrowableArray is the right storage for them.

What changes it

  • Nested prefixes, prefix(prefix(inner(), @varname(timestep[t])), @varname(session[i])): one warning for session and one for each session's timestep, so 4 for 3 sessions.
  • An array for the variable inside the submodel, b = zeros(2); b[2] ~ Normal(): still one warning, for session.
  • x[i] ~ to_submodel(inner()) with a local array x = zeros(3), instead of prefix: the same warning, since the submodel doesn't get the caller's template (Get rid of PrefixContext & make submodels much better #1221).
  • logjoint(sessions(3), draw): no warning.

Proposed change

Keep the SkipTemplate around NoTemplate() while N is positive, so that the levels a prefix adds can be told apart from the variable's own levels, and don't warn when an index level's template is a SkipTemplate. SkipTemplate{0}(::NoTemplate) still returns NoTemplate(), so the variable's own levels, and VarNamedTuples set outside a model without a template, warn as before. The diff is against DynamicPPL 0.42.14, and applies to main as well.

diff --git a/src/varnamedtuple.jl b/src/varnamedtuple.jl
index 8c087ff..0430190 100644
--- a/src/varnamedtuple.jl
+++ b/src/varnamedtuple.jl
@@ -48,7 +48,7 @@ struct SkipTemplate{N,V}
 end
 SkipTemplate{N}(v) where {N} = SkipTemplate{N,typeof(v)}(v)
 SkipTemplate{0}(v) = v
-SkipTemplate{N}(::NoTemplate) where {N} = NoTemplate()
+SkipTemplate{N}(::NoTemplate) where {N} = SkipTemplate{N,NoTemplate}(NoTemplate())
 SkipTemplate{0}(::NoTemplate) = NoTemplate()
 # Decrease the skip level: used when recursing inside make_leaf.
 function decrease_skip(st::SkipTemplate{N}) where {N}
diff --git a/src/varnamedtuple/getset.jl b/src/varnamedtuple/getset.jl
index d4593d0..786b1fa 100644
--- a/src/varnamedtuple/getset.jl
+++ b/src/varnamedtuple/getset.jl
@@ -406,7 +406,7 @@ function make_leaf_singleindex(value, coptic::AbstractPPL.Index, template)
         # have to make a GrowableArray. No need to use `kw` since make_leaf already errors
         # if there are uninterpretable keyword indices.
         template_sz = get_maximum_size_from_indices(coptic.ix...)
-        _warn_growable_array_creation(template_sz)
+        template isa SkipTemplate || _warn_growable_array_creation(template_sz)
         GrowableArray(Array{pa_eltype}(undef, template_sz))
     end
     pa_mask = similar(pa_data, Bool)

What the change does

I applied it to a copy of DynamicPPL 0.42.14 and ran the block below in two new environments that differ only in DynamicPPL, with DynamicPPL, Distributions, Random and Logging in them. The draws under a seed, their names and logjoint are the same, and the warnings are gone, except where a VarNamedTuple is set without a template outside a model.

using DynamicPPL, Distributions, Random
using Logging: ConsoleLogger, with_logger

@model function inner()
    a ~ Normal()
end

# One latent per session, under session[i].timestep[t]
@model function sessions(timesteps)
    for (i, t) in enumerate(timesteps)
        a ~ to_submodel(prefix(prefix(inner(), @varname(timestep[t])), @varname(session[i])), false)
    end
end

# Runs f, and returns its result and the number of growable-array warnings it printed
function count_warnings(f)
    log = IOBuffer()
    result = with_logger(f, ConsoleLogger(log))
    return result, count("Warning: Creating a growable", String(take!(log)))
end

model = sessions([2, 1, 3])
draw, n = count_warnings(() -> rand(Xoshiro(1), model))
println("warnings from rand: ", n)
for (name, value) in pairs(draw)
    println(name, " = ", value)
end
println("logjoint = ", logjoint(model, draw))

# A VarNamedTuple set outside a model, without a template, still warns
_, n = count_warnings(() -> @vnt begin
    x[1] := 1.0
end)
println("warnings from @vnt with x[1] := 1.0: ", n)

With DynamicPPL 0.42.14:

warnings from rand: 4
session[1].timestep[2].a = -0.07058313895389791
session[2].timestep[1].a = 0.5314767537831963
session[3].timestep[3].a = -0.806852326006714
logjoint = -3.226045697263496
warnings from @vnt with x[1] := 1.0: 1

With the change:

warnings from rand: 0
session[1].timestep[2].a = -0.07058313895389791
session[2].timestep[1].a = 0.5314767537831963
session[3].timestep[3].a = -0.806852326006714
logjoint = -3.226045697263496
warnings from @vnt with x[1] := 1.0: 1

The same holds for the other cases above, compared the same way: VarInfo, logprior and loglikelihood are the same too, and the warnings are gone for a variable with its own array under the prefix, for a model with observations under the prefix, and for x[i] ~ to_submodel(inner()). Through Turing 0.49.0, for the model of 20 sessions above, rand, sample(model, NUTS(), 20), predict and returned give no warnings with the change, and the same NUTS draws, predictions and logjoint of every draw under a seed.

With the change, I ran these test files: varnamedtuple.jl, accumulators/raw_values.jl, compiler.jl, varinfo.jl, model.jl, pointwise_logdensities.jl, contexts.jl, contexts/init.jl, conditionfix.jl, context_implementations.jl, debug_utils.jl, submodels.jl and chains.jl. 63278 tests pass, one in model.jl is marked broken already, and one fails: the docstring test in compiler.jl, which fails the same way without the change on Julia 1.13.3 (string(@doc(demo)) gives the DocStr). I didn't run the rest of the suite.

One case loses a warning that may be useful: x[i] ~ to_submodel(inner()) with a local array x. There the submodel drops the caller's template (#1221), so the stored array may not match x, as #1221 shows with mixed indices into x = zeros(2, 2). With the change it doesn't warn. Passing the caller's template into the submodel, as #1221 discusses and the open PR #1531 sets out to do, would cover that case properly. On the branch of #1531, a prefix without a template still gets SkipTemplate{n}(template) (_prefixed_template in src/model.jl), and both lines above are unchanged, so I expect the same warnings there, but I haven't run it.

Activity

  1. PTWaade commented on Nov 13, 2026

    @PTWaade
    ContributorAuthor

    Here us a brief follow-up on a suggestion to define the array in the model, session = fill(nothing, n), so that DynamicPPL has a template for it: that silences the warning when the right-hand side is a distribution, but not when it is a submodel, with the automatic prefix or with prefix.

    Maybe this is therefore a question of how submodels are implemented, rather than for the warning per se!

    Again, Claude-MWE'd:

    using DynamicPPL, Distributions

    @model function inner()
    a ~ Normal()
    end

    A distribution on the right-hand side: the local array is the template

    @model function plain(n)
    session = fill(nothing, n)
    for i in 1:n
    session[i] ~ Normal()
    end
    end

    A submodel with the automatic prefix session[i]

    @model function submodel_tilde(n)
    session = fill(nothing, n)
    for i in 1:n
    session[i] ~ to_submodel(inner())
    end
    end

    A submodel with the prefix session[i] given by hand

    @model function submodel_prefix(n)
    session = fill(nothing, n)
    for i in 1:n
    a ~ to_submodel(prefix(inner(), @varname(session[i])), false)
    end
    end

    println("plain:")
    println(collect(keys(rand(plain(3)))))
    println("submodel_tilde:")
    println(collect(keys(rand(submodel_tilde(3)))))
    println("submodel_prefix:")
    println(collect(keys(rand(submodel_prefix(3)))))
    Output, with Julia 1.13.3, DynamicPPL 0.42.14 and Distributions 0.25.134, in a new environment with only those two packages:

    plain:
    VarName[session[1], session[2], session[3]]
    submodel_tilde:
    ┌ Warning: Creating a growable Base.Array of dimension 1 to store values. This may not match the actual type or size of the actual AbstractArray that will be used inside the DynamicPPL model.
    │
    │ If this is not the type or size that you expect, please see: https://turinglang.org/docs/uri/growablearray
    └ @ DynamicPPL.VarNamedTuples ~/.julia/packages/DynamicPPL/K7ixO/src/varnamedtuple/partial_array.jl:835
    VarName[session[1].a, session[2].a, session[3].a]
    submodel_prefix:
    ┌ Warning: Creating a growable Base.Array of dimension 1 to store values. This may not match the actual type or size of the actual AbstractArray that will be used inside the DynamicPPL model.
    │
    │ If this is not the type or size that you expect, please see: https://turinglang.org/docs/uri/growablearray
    └ @ DynamicPPL.VarNamedTuples ~/.julia/packages/DynamicPPL/K7ixO/src/varnamedtuple/partial_array.jl:835
    VarName[session[1].a, session[2].a, session[3].a]
    The template doesn't reach the submodel: tilde_assume!! for a Submodel (src/submodel.jl, lines 166–174) ignores its template argument, and PrefixContext has no field to carry one, so the levels that a prefix adds only ever get the SkipTemplate of the variable inside the submodel. The main branch has the same code. In the model of 20 sessions from the first post, adding session = fill(nothing, n) and timestep = fill(nothing, n_timesteps) leaves the counts unchanged: 21 warnings per rand, 483 for sample(model, NUTS(), 20) and 420 for predict.

  2. yebai commented on Nov 14, 2026

    @yebai
    Member

    Thanks, @PTWaade, for reporting this. We will look into whether the warning message can be printed once or suppressed.

  3. PTWaade commented on Nov 15, 2026

    @PTWaade
    ContributorAuthor

    Thank you - it is appreciated ! It is not urgent, on my side, since it applies only to a small subset of the models I am using

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions