Repository navigation
varnamedtuple: reject lookups in another index form - #1544
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
|
DynamicPPL.jl documentation for PR #1544 is available at: |
BenchmarksPerformance 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: EnvironmentJulia 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) |
| for (leaf, leaf_value) in | ||
| (whole ? ((vn, value),) : AbstractPPL.varname_and_value_leaves(vn, value)) | ||
| whole || haskey(prediction.params, leaf) || continue |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 MBI think this suggest we need more logic to determine what to skip, for instance:
- Names the model doesn't have, e.g.
junkin the above example - Values whose shape is known
Does this leaves GrowableArrays?
There was a problem hiding this comment.
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.
|
|
||
| `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. |
There was a problem hiding this comment.
| `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. |
There was a problem hiding this comment.
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:
VarNamedTuplelookups and model evaluation throwArgumentErrorfor indices with a different number of dimensions, indices usingbegin,endor:, 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 theVarNamedTupleitself rather than aDictof its pairs (#1544).
| "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.", |
There was a problem hiding this comment.
| "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.", |
There was a problem hiding this comment.
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 theVarNamedTupleitself rather than aDictof its pairs; or use matching indices.
sunxd3
left a comment
There was a problem hiding this comment.
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.
| for (leaf, leaf_value) in | ||
| (whole ? ((vn, value),) : AbstractPPL.varname_and_value_leaves(vn, value)) | ||
| whole || haskey(prediction.params, leaf) || continue |
There was a problem hiding this comment.
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 MBI think this suggest we need more logic to determine what to skip, for instance:
- Names the model doesn't have, e.g.
junkin 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>
|
Thanks. On values set by index without a template, A local run found the same problem with Boolean masks, also on The |
Split of #1529 (part 4.1 of 12; part 4 comes as ten PRs).
A
VarNamedTuplebuilt 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. Fory = zeros(3, 3); y[4] ~ Normal(), values keyedy[1, 2]andy[2, 2]imply a 2×2 array, soy[4]received the value ofy[2, 2]. Indices that depend on the shape go wrong in the same way. Fory = zeros(3); y[end] ~ Normal(), values keyedy[1]andy[2]gavey[3]the value ofy[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 usingbegin,endor:, and Boolean masks of a different shape. The check is inhaskeyandgetindexon theVarNamedTuple, so it also applies toInitFromParams(logjoint,returned,predict),conditionandfix. Values with a template, whole arrays, and values a model produces (rand,VarInfo, chains) are unaffected.predictcompares 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
VarNamedTupleitself rather than aDictof its pairs.