DynamicPPL.jl icon indicating copy to clipboard operation
DynamicPPL.jl copied to clipboard

Error with `.~` and `rand`

Open marius311 opened this issue 3 years ago • 2 comments

The following errors with Turing v0.21.1 and DynamicPPL v0.19.1. It was suggested on Slack this is a bug and to post here:

@model function toy()
    x = Vector{Float64}(undef, 2)
    x .~ [Normal() for i=1:2]
end

toy()() # works
rand(toy()) # ERROR: type NamedTuple has no field x

marius311 avatar May 11 '22 20:05 marius311

The full stracktrace is

julia> rand(toy()) # ERROR: type NamedTuple has no field x
ERROR: type NamedTuple has no field x
Stacktrace:
  [1] getproperty
    @ ./Base.jl:42 [inlined]
  [2] get(obj::NamedTuple{(), Tuple{}}, l::Setfield.PropertyLens{:x})
    @ Setfield ~/.julia/packages/Setfield/AS2xF/src/lens.jl:117
  [3] get(obj::NamedTuple{(), Tuple{}}, lens::BangBang.SetfieldImpl.Lens!!{Setfield.PropertyLens{:x}})
    @ BangBang.SetfieldImpl ~/.julia/packages/BangBang/FKnzJ/src/setfield.jl:12
  [4] set(obj::NamedTuple{(), Tuple{}}, l::Setfield.ComposedLens{BangBang.SetfieldImpl.Lens!!{Setfield.PropertyLens{:x}}, BangBang.SetfieldImpl.Lens!!{Setfield.IndexLens{Tuple{Int64}}}}, val::Float64)
    @ Setfield ~/.julia/packages/Setfield/AS2xF/src/lens.jl:187
  [5] set(obj::NamedTuple{(), Tuple{}}, lens::BangBang.SetfieldImpl.Lens!!{Setfield.ComposedLens{Setfield.PropertyLens{:x}, Setfield.IndexLens{Tuple{Int64}}}}, value::Float64)
    @ BangBang.SetfieldImpl ~/.julia/packages/BangBang/FKnzJ/src/setfield.jl:17
  [6] set!!(obj::NamedTuple{(), Tuple{}}, vn::VarName{:x, Setfield.IndexLens{Tuple{Int64}}}, value::Float64)
    @ DynamicPPL ~/.julia/dev/DynamicPPL/src/utils.jl:271
  [7] setindex!!(vi::SimpleVarInfo{NamedTuple{(), Tuple{}}, Float64}, val::Float64, vn::VarName{:x, Setfield.IndexLens{Tuple{Int64}}})
    @ DynamicPPL ~/.julia/dev/DynamicPPL/src/simple_varinfo.jl:262
  [8] setindex!!
    @ ~/.julia/dev/DynamicPPL/src/simple_varinfo.jl:269 [inlined]
  [9] macro expansion
    @ ~/.julia/packages/Setfield/AS2xF/src/sugar.jl:197 [inlined]
 [10] setindex!!
    @ ~/.julia/dev/DynamicPPL/src/threadsafe.jl:82 [inlined]
 [11] dot_assume(rng::Random._GLOBAL_RNG, spl::SampleFromPrior, dists::Vector{Normal{Float64}}, vns::Vector{VarName{:x, Setfield.IndexLens{Tuple{Int64}}}}, var::Vector{Float64}, vi::DynamicPPL.ThreadSafeVarInfo{SimpleVarInfo{NamedTuple{(), Tuple{}}, Float64}, Vector{Base.RefValue{Float64}}})
    @ DynamicPPL ~/.julia/dev/DynamicPPL/src/simple_varinfo.jl:404
 [12] dot_tilde_assume
    @ ~/.julia/dev/DynamicPPL/src/context_implementations.jl:280 [inlined]
 [13] dot_tilde_assume
    @ ~/.julia/dev/DynamicPPL/src/context_implementations.jl:252 [inlined]
 [14] dot_tilde_assume!!
    @ ~/.julia/dev/DynamicPPL/src/context_implementations.jl:375 [inlined]
 [15] macro expansion
    @ ~/.julia/dev/DynamicPPL/src/compiler.jl:531 [inlined]
 [16] toy(__model__::Model{typeof(toy), (), (), (), Tuple{}, Tuple{}, DefaultContext}, __varinfo__::DynamicPPL.ThreadSafeVarInfo{SimpleVarInfo{NamedTuple{(), Tuple{}}, Float64}, Vector{Base.RefValue{Float64}}}, __context__::SamplingContext{SampleFromPrior, DefaultContext, Random._GLOBAL_RNG})
    @ Main ./REPL[4]:3
 [17] macro expansion
    @ ~/.julia/dev/DynamicPPL/src/model.jl:493 [inlined]
 [18] _evaluate!!
    @ ~/.julia/dev/DynamicPPL/src/model.jl:476 [inlined]
 [19] evaluate_threadsafe!!(model::Model{typeof(toy), (), (), (), Tuple{}, Tuple{}, DefaultContext}, varinfo::SimpleVarInfo{NamedTuple{(), Tuple{}}, Float64}, context::SamplingContext{SampleFromPrior, DefaultContext, Random._GLOBAL_RNG})
    @ DynamicPPL ~/.julia/dev/DynamicPPL/src/model.jl:467
 [20] evaluate!!
    @ ~/.julia/dev/DynamicPPL/src/model.jl:402 [inlined]
 [21] rand
    @ ~/.julia/dev/DynamicPPL/src/model.jl:525 [inlined]
 [22] rand(model::Model{typeof(toy), (), (), (), Tuple{}, Tuple{}, DefaultContext})
    @ DynamicPPL ~/.julia/dev/DynamicPPL/src/model.jl:538
 [23] top-level scope
    @ REPL[6]:1

I wonder if this is an upstream issue in Setfield or BangBang.

devmotion avatar May 11 '22 20:05 devmotion

The problematic line is https://github.com/jw3126/Setfield.jl/blob/25f5f5a0e3f4cbc6f8c44955fb7fb0981b961e55/src/lens.jl#L187: We try to set the first entry of x via a ComposedLens (the outer lens accesses the field :x, and the inner one the first entry) but since the empty NamedTuple does not contain a key :x yet accessing the retrieval of its value fails.

devmotion avatar May 11 '22 20:05 devmotion