Skip to content

varnamedtuple: reject lookups in another index form - #1544

Merged
yebai merged 1 commit into
mainfrom
index-form
Nov 20, 2026
Merged

yebai merged 1 commit into
mainfrom
index-form

Conversation

@yebai

@yebai yebai commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Split of #1529 (part 4.1 of 12; part 4 comes as ten PRs).

A VarNamedTuple built from indexed keys without a template infers its array's shape from those keys. A lookup in another index form is then resolved against that inferred shape rather than the model's, and can return the wrong element. For y = zeros(3, 3); y[4] ~ Normal(), values keyed y[1, 2] and y[2, 2] imply a 2×2 array, so y[4] received the value of y[2, 2]. Indices that depend on the shape go wrong in the same way. For y = zeros(3); y[end] ~ Normal(), values keyed y[1] and y[2] gave y[3] the value of y[2]; and a Boolean mask sized for the model's array did not fit, so the supplied values were ignored.

Such lookups now throw an ArgumentError: indices with a different number of dimensions, indices using begin, end or :, and Boolean masks of a different shape. The check is in haskey and getindex on the VarNamedTuple, so it also applies to InitFromParams (logjoint, returned, predict), condition and fix. Values with a template, whole arrays, and values a model produces (rand, VarInfo, chains) are unaffected.

predict compares the supplied parameters with the model's own variables. It now reads them only at the model's keys, re-keyed with the model's templates, so its lookups use the model's index form and it never reads values the model does not use.

HISTORY lists the change as breaking. Remedies: supply a template or the whole array, or pass the VarNamedTuple itself rather than a Dict of its pairs.

@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.30%. Comparing base (702b8c0) to head (cb097eb).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1544      +/-   ##
==========================================
+ Coverage   85.12%   85.30%   +0.17%     
==========================================
  Files          52       52              
  Lines        4671     4694      +23     
==========================================
+ Hits         3976     4004      +28     
+ Misses        695      690       -5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

Copy link
Copy Markdown
Contributor

DynamicPPL.jl documentation for PR #1544 is available at:
https://TuringLang.github.io/DynamicPPL.jl/previews/PR1544/

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Benchmarks

Performance Ratio: gradient time divided by log-density time.

For very small models these ratios are noisy across runs and machines; raw primal and gradient timings are more reliable. The benchmarks are aimed at DynamicPPL developers and mainly catch obvious allocation or type-stability regressions. See benchmark notes for details.

Primal times: main 702b8c0, PR cb097eb

================================================================================================================================
                                                                     primal                            gradient                 
                                                             ----------------------  -------------------------------------------
Model                                         dim    linked        main          PR     FwdDiff    RvsDiff    Mooncake    Enzyme
--------------------------------------------------------------------------------------------------------------------------------
Simple assume observe*                          1     false     5.26 ns     2.77 ns       12.63    1342.95       10.72      9.00
Simple assume observe*                          1      true     5.26 ns     2.81 ns       13.13    1356.82       10.53      9.17
Smorgasbord                                   201     false      5.4 μs     3.17 μs       93.55     128.96        5.28      1.23
Smorgasbord                                   201      true     6.42 μs     3.68 μs       90.83     145.20        5.64      1.55
Smorgasbord fixed                             100     false     2.58 μs     1.65 μs       62.23     133.23        4.93      1.22
Smorgasbord fixed                             100      true     3.04 μs     1.84 μs       64.79     163.95        6.00      1.53
NamedTuple-field LHS variable*                  1     false     5.25 ns     2.81 ns       12.24    1537.43       10.16      8.74
NamedTuple-field LHS variable*                  1      true     5.27 ns     2.75 ns       12.98    1455.32       10.89      9.28
MvNormal argument-supplied observation*         1     false     74.0 ns     53.1 ns        2.71     524.87        2.72      2.32
MvNormal argument-supplied observation*         1      true     73.6 ns     60.5 ns        2.53     503.74        2.53      2.22
Loop univariate 1k                           1000     false     17.9 μs     8.05 μs     1532.25     386.98        3.70      0.93
Loop univariate 1k                           1000      true     18.3 μs     10.5 μs     1575.72     289.79        3.12      0.70
Multivariate 1k                              1000     false     1.07 μs    882.0 ns     2967.03     392.84        1.30      2.29
Multivariate 1k                              1000      true    996.0 ns    842.0 ns     2975.61     399.92        1.33      2.50
Loop univariate 10k                         10000     false    164.0 μs     79.8 μs    24203.71     495.96        3.57      0.81
Loop univariate 10k                         10000      true    177.0 μs     99.4 μs    22125.13     396.02        2.78      0.64
Multivariate 10k                            10000     false     9.34 μs     6.83 μs    59791.95     642.79        1.20      2.06
Multivariate 10k                            10000      true     9.22 μs     7.09 μs    56527.70     625.42        1.13      2.00
Dynamic                                       err     false     1.39 μs    896.0 ns         err      46.61       11.26      4.46
Dynamic                                        10      true     1.87 μs     1.19 μs        1.64      61.55        8.96      6.88
Submodel*                                       1     false     5.26 ns     2.84 ns       12.17    1554.55       10.62      8.91
Submodel*                                       1      true     5.26 ns     2.81 ns       12.65    1599.36       11.31      9.36
LDA                                             6      true     3.71 μs     2.26 μs        2.01      13.02       48.18     65.44
================================================================================================================================
Environment
Julia Version 1.13.4
Commit c1ac32ab733 (2026-11-13 17:30 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 4 × AMD EPYC 9V45 96-Core Processor
  WORD_SIZE: 64
  LLVM: libLLVM-20.1.8 (ORCJIT, znver5)
  GC: Built with stock GC
Threads: 1 default, 1 interactive, 1 GC (on 4 virtual cores)

Comment thread src/chains.jl Outdated
Comment on lines +423 to +425
for (leaf, leaf_value) in
(whole ? ((vn, value),) : AbstractPPL.varname_and_value_leaves(vn, value))
whole || haskey(prediction.params, leaf) || continue

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default predict throws UndefRefError for a partly assigned input array even when the model only reads assigned entries.

Should we skip unassigned entries when rebuilding supplied.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More issues from agent.

using DynamicPPL, Distributions, Random

@model function g()
    x = zeros(3)
    x[2] ~ Normal()
end
# Supplied tuple values come back as predictions:
predict(Xoshiro(1), g(), VarNamedTuple(; x=(1.0, 7.0, 3.0))).params
# base: VarNamedTuple()    this PR: x[2] => 7.0

@model h() = z ~ Normal()
# Inputs the model never uses are split into elements anyway:
predict(Xoshiro(1), h(), VarNamedTuple(; junk=Vector{Vector{Float64}}(undef, 1)))
# base: predicts z    this PR: UndefRefError
predict(Xoshiro(1), h(), VarNamedTuple(; junk=zeros(10_000)))
# base: 1.1 μs, 1 KB    this PR: 2.3 ms, 3.8 MB

I think this suggest we need more logic to determine what to skip, for instance:

  • Names the model doesn't have, e.g. junk in the above example
  • Values whose shape is known

Does this leaves GrowableArrays?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks both; confirmed. On the previous head, predict threw UndefRefError where main returned the predictions, and the re-keying had the problems @sunxd3 lists, because it split every supplied value into elements. predict now reads params only at the model's own keys under each supplied name, so it never reads names the model lacks or elements it does not use. All three examples now match main: the Tuple case returns no predictions, unused junk with an #undef element predicts z, and unused junk = zeros(10_000) takes about 1.7 µs (main 0.85 µs; previous head about 2 ms). test/model.jl covers the #undef and Tuple cases.

On whether that leaves only GrowableArrays: yes, only values set by index without a template need re-keying. A version that re-keyed only such entries at the top level failed when the storage is nested: for x = (a = zeros(2, 2),); x.a[1] ~ Normal() with Dict(@varname(x.a[1]) => 1.0), it threw, whereas main returns no predictions. Detecting nested storage would take more code than reading at the model's keys, so this version keeps the latter.

Comment thread HISTORY.md Outdated

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

`InitFromParams`, and so `logjoint`, `returned` and `predict`, throw an `ArgumentError` when values set by index without a template use a different index form from the model's, such as `@varname(y[1, 1])` for `y = zeros(2, 2); y[1] ~ Normal()`; `haskey` and `getindex` on such a `VarNamedTuple` throw likewise. Previously such values were matched against the shape of their keys, which could select the wrong element: for `y = zeros(3, 3); y[4] ~ Normal()`, values keyed `y[1, 2]` and `y[2, 2]` gave `y[4]` the value of `y[2, 2]`. Pass the `VarNamedTuple` itself rather than a `Dict` of its pairs, key values as the model indexes them, give a template, or pass the whole array.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`InitFromParams`, and so `logjoint`, `returned` and `predict`, throw an `ArgumentError` when values set by index without a template use a different index form from the model's, such as `@varname(y[1, 1])` for `y = zeros(2, 2); y[1] ~ Normal()`; `haskey` and `getindex` on such a `VarNamedTuple` throw likewise. Previously such values were matched against the shape of their keys, which could select the wrong element: for `y = zeros(3, 3); y[4] ~ Normal()`, values keyed `y[1, 2]` and `y[2, 2]` gave `y[4]` the value of `y[2, 2]`. Pass the `VarNamedTuple` itself rather than a `Dict` of its pairs, key values as the model indexes them, give a template, or pass the whole array.
`VarNamedTuple` lookups and model evaluation throw `ArgumentError` for mismatched index dimensions in values set by index without a template. Supply a template or use matching indices.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks; adopted, with four changes. The entry now also covers begin, end and : (from @sunxd3's review) and Boolean masks of another shape; it says "a different number of dimensions", so it cannot be read as a mismatch in size; it notes that such lookups could previously return the wrong element; and it lists remedies that work in every case, since for y[end] no choice of keys matches. It now reads:

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

Comment thread src/varnamedtuple/partial_array.jl Outdated
Comment on lines +386 to +390
"Cannot look up `$vn` in values keyed by $(ndims(pa))-dimensional indices " *
"without a template: the element it names depends on the unknown shape of " *
"the array. If the values came from a `VarNamedTuple`, pass it rather than a " *
"`Dict` of its pairs; otherwise key the values as the model indexes the " *
"variable, give a template for its shape, or pass the whole array.",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Cannot look up `$vn` in values keyed by $(ndims(pa))-dimensional indices " *
"without a template: the element it names depends on the unknown shape of " *
"the array. If the values came from a `VarNamedTuple`, pass it rather than a " *
"`Dict` of its pairs; otherwise key the values as the model indexes the " *
"variable, give a template for its shape, or pass the whole array.",
"Cannot look up `$vn` in values set with $(ndims(pa))-dimensional indices " *
"without a template. Supply a template or use matching indices.",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks; adopted your opening sentence. The message adds a short reason, because for y[end] the number of dimensions matches and the problem is the unknown shape. It then lists the remedies from the HISTORY entry, and offers "use matching indices" only when that can help. For example:

Cannot look up y[1, 1] in values set with 1-dimensional indices without a template: the element it names depends on the array's unknown shape. Supply a template or the whole array, or pass the VarNamedTuple itself rather than a Dict of its pairs; or use matching indices.

@sunxd3 sunxd3 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good in general.

There is one corner case should probably be fixed. Rejecting this kind of lazy indexing is fine for me, if user want to write a model like this, they should use template.

using DynamicPPL, Distributions, Random

@model function f()
    y = zeros(3)
    y[end] ~ Normal()
    return y[3]
end
params = Dict(@varname(y[1]) => 1.0, @varname(y[2]) => 2.0)  # no value for y[3]

returned(f(), params)                                       # 2.0: y[3] silently gets y[2]'s value
rand(Xoshiro(1), condition(f(), params))                    # VarNamedTuple(): y[3] treated as observed, never sampled
logjoint(condition(f(), params), VarNamedTuple())           # -2.9189 == logpdf(Normal(), 2.0)

Another point on prediction in the reply message below.

Comment thread src/chains.jl Outdated
Comment on lines +423 to +425
for (leaf, leaf_value) in
(whole ? ((vn, value),) : AbstractPPL.varname_and_value_leaves(vn, value))
whole || haskey(prediction.params, leaf) || continue

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More issues from agent.

using DynamicPPL, Distributions, Random

@model function g()
    x = zeros(3)
    x[2] ~ Normal()
end
# Supplied tuple values come back as predictions:
predict(Xoshiro(1), g(), VarNamedTuple(; x=(1.0, 7.0, 3.0))).params
# base: VarNamedTuple()    this PR: x[2] => 7.0

@model h() = z ~ Normal()
# Inputs the model never uses are split into elements anyway:
predict(Xoshiro(1), h(), VarNamedTuple(; junk=Vector{Vector{Float64}}(undef, 1)))
# base: predicts z    this PR: UndefRefError
predict(Xoshiro(1), h(), VarNamedTuple(; junk=zeros(10_000)))
# base: 1.1 μs, 1 KB    this PR: 2.3 ms, 3.8 MB

I think this suggest we need more logic to determine what to skip, for instance:

  • Names the model doesn't have, e.g. junk in the above example
  • Values whose shape is known

Does this leaves GrowableArrays?

Values set by index without a template take the shape of their keys,
so a lookup in another index form could name the wrong element: with
storage keyed `y[1, 2]` and `y[2, 2]`, `y[4]` read `y[2, 2]`. Indices
resolved against that shape went wrong too: `begin`, `end` and `:`
used the inferred size, and a Boolean mask longer than the storage
reported a miss, so `condition` sampled the variable. Such lookups now
throw. `predict` re-keys parameters with the model's templates, reading
them only at the model's own keys, so that it still matches the
model's index form.

Assisted-by: Claude Code <noreply@anthropic.com>
Assisted-by: Codex <codex@openai.com>
@yebai

yebai commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

Thanks. On values set by index without a template, begin, end and :, including ranges such as 2:end, now throw an ArgumentError, so the three calls in your example throw instead of giving y[3] the value of y[2]. Values with a template, whole arrays, and values from rand, VarInfo or chains behave as before. The cost is that some inputs that used to give the right answer now throw as well, such as Dict(@varname(y[3]) => 3.0) for this model: the model does not say which index end names, so the lookup cannot know. A template or the whole array works.

A local run found the same problem with Boolean masks, also on main. For y[[true, true, false]] ~ MvNormal(zeros(2), I), values keyed y[1] and y[2] were ignored by condition, and returned reported that no value was provided. A mask that does not fit the inferred shape now throws too; masks that fit, as in the existing tests, still work.

The predict points are answered in the thread on src/chains.jl.

@sunxd3 sunxd3 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me, thank!

@yebai
yebai added this pull request to the merge queue Nov 20, 2026
Merged via the queue into main with commit 98bab95 Nov 20, 2026
23 checks passed
@yebai
yebai deleted the index-form branch November 20, 2026 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants