Skip to content

Error with .~ and rand #405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
marius311 opened this issue May 11, 2022 · 3 comments
Closed

Error with .~ and rand #405

marius311 opened this issue May 11, 2022 · 3 comments

Comments

@marius311
Copy link

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
@devmotion
Copy link
Member

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
Copy link
Member

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.

@mhauru
Copy link
Member

mhauru commented Feb 20, 2025

Closed by #804, which will be live in the next breaking release. The syntax in the OP is no longer allowed, and the two alternative syntaxes, x .~ Normal() and x ~ product_distribution([Normal() for i in 1:2]), both work.

@mhauru mhauru closed this as completed Feb 20, 2025
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

No branches or pull requests

3 participants