Skip to content
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

Name clash caused by submodels is hard to debug #427

Closed
phipsgabler opened this issue Sep 13, 2022 · 4 comments
Closed

Name clash caused by submodels is hard to debug #427

phipsgabler opened this issue Sep 13, 2022 · 4 comments

Comments

@phipsgabler
Copy link
Member

Consider

@model function linear(x, θ, intercept)
    η = x * θ .+ intercept
    return y ~ product_distribution(Normal.(η, 2))
end

@model function test(n1, n2)
    x1 ~ product_distribution(Fill(Uniform(), n1))
    @submodel y1 = linear(x1, 1, 1)

    x2 ~ product_distribution(Fill(Uniform(), n2))
    @submodel y2 = linear(x2, 2, 2)
end

test(5, 5)() works, but probably erroneously. OTOH, test(5, 2)() fails with

julia> test(5, 2)()
ERROR: DimensionMismatch: inconsistent array dimensions
Stacktrace:
  [1] logpdf
    @ ~/.julia/packages/Distributions/yoypU/src/common.jl:247 [inlined]
  [2] logpdf_with_trans(d::DiagNormal, x::Vector{Real}, transform::Bool)
    @ Bijectors ~/.julia/packages/Bijectors/hEXu9/src/Bijectors.jl:136
  [3] assume(rng::Random._GLOBAL_RNG, sampler::SampleFromPrior, dist::DiagNormal, vn::VarName{:y, Setfield.IdentityLens}, vi::UntypedVarInfo{DynamicPPL.Metadata{Dict{VarName, Int64}, Vector{Distribution}, Vector{VarName}, Vector{Real}, Vector{Set{DynamicPPL.Selector}}}, Float64})
    @ DynamicPPL ~/.julia/packages/DynamicPPL/zPOYL/src/context_implementations.jl:231
  [4] tilde_assume
    @ ~/.julia/packages/DynamicPPL/zPOYL/src/context_implementations.jl:49 [inlined]
  [5] tilde_assume
    @ ~/.julia/packages/DynamicPPL/zPOYL/src/context_implementations.jl:46 [inlined]
  [6] tilde_assume
    @ ~/.julia/packages/DynamicPPL/zPOYL/src/context_implementations.jl:31 [inlined]
  [7] tilde_assume!!(context::SamplingContext{SampleFromPrior, DefaultContext, Random._GLOBAL_RNG}, right::DiagNormal, vn::VarName{:y, Setfield.IdentityLens}, vi::UntypedVarInfo{DynamicPPL.Metadata{Dict{VarName, Int64}, Vector{Distribution}, Vector{VarName}, Vector{Real}, Vector{Set{DynamicPPL.Selector}}}, Float64})
    @ DynamicPPL ~/.julia/packages/DynamicPPL/zPOYL/src/context_implementations.jl:117
  [8] macro expansion
    @ ~/.julia/packages/DynamicPPL/zPOYL/src/compiler.jl:539 [inlined]
  [9] linear(__model__::Model{typeof(linear), (:x, :θ, :intercept), (), (), Tuple{Vector{Float64}, Int64, Int64}, Tuple{}, DefaultContext}, __varinfo__::UntypedVarInfo{DynamicPPL.Metadata{Dict{VarName, Int64}, Vector{Distribution}, Vector{VarName}, Vector{Real}, Vector{Set{DynamicPPL.Selector}}}, Float64}, __context__::SamplingContext{SampleFromPrior, DefaultContext, Random._GLOBAL_RNG}, x::Vector{Float64}, θ::Int64, intercept::Int64)
    @ Main ~/git/jasa2-paper/code/semibeta.jl:50
 [10] macro expansion
    @ ~/.julia/packages/DynamicPPL/zPOYL/src/model.jl:593 [inlined]
 [11] _evaluate!!(model::Model{typeof(linear), (:x, :θ, :intercept), (), (), Tuple{Vector{Float64}, Int64, Int64}, Tuple{}, DefaultContext}, varinfo::UntypedVarInfo{DynamicPPL.Metadata{Dict{VarName, Int64}, Vector{Distribution}, Vector{VarName}, Vector{Real}, Vector{Set{DynamicPPL.Selector}}}, Float64}, context::SamplingContext{SampleFromPrior, DefaultContext, Random._GLOBAL_RNG})
    @ DynamicPPL ~/.julia/packages/DynamicPPL/zPOYL/src/model.jl:576
 [12] macro expansion
    @ ~/.julia/packages/DynamicPPL/zPOYL/src/submodel_macro.jl:244 [inlined]
 [13] macro expansion
    @ ~/.julia/packages/DynamicPPL/zPOYL/src/compiler.jl:539 [inlined]
 [14] test(__model__::Model{typeof(test), (:n1, :n2), (), (), Tuple{Int64, Int64}, Tuple{}, DefaultContext}, __varinfo__::UntypedVarInfo{DynamicPPL.Metadata{Dict{VarName, Int64}, Vector{Distribution}, Vector{VarName}, Vector{Real}, Vector{Set{DynamicPPL.Selector}}}, Float64}, __context__::SamplingContext{SampleFromPrior, DefaultContext, Random._GLOBAL_RNG}, n1::Int64, n2::Int64)
    @ Main ~/git/jasa2-paper/code/semibeta.jl:58
 [15] macro expansion
    @ ~/.julia/packages/DynamicPPL/zPOYL/src/model.jl:593 [inlined]
 [16] _evaluate!!
    @ ~/.julia/packages/DynamicPPL/zPOYL/src/model.jl:576 [inlined]
 [17] evaluate_threadunsafe!!
    @ ~/.julia/packages/DynamicPPL/zPOYL/src/model.jl:551 [inlined]
 [18] evaluate!!
    @ ~/.julia/packages/DynamicPPL/zPOYL/src/model.jl:504 [inlined]
 [19] evaluate!! (repeats 2 times)
    @ ~/.julia/packages/DynamicPPL/zPOYL/src/model.jl:515 [inlined]
 [20] evaluate!!
    @ ~/.julia/packages/DynamicPPL/zPOYL/src/model.jl:523 [inlined]
 [21] (::Model{typeof(test), (:n1, :n2), (), (), Tuple{Int64, Int64}, Tuple{}, DefaultContext})()
    @ DynamicPPL ~/.julia/packages/DynamicPPL/zPOYL/src/model.jl:475
 [22] top-level scope
    @ REPL[13]:1

which makes it really hard to find the root cause of the error unless you know what's going on in the internals (all manual checking for whether the arrays match is going fine!).

I guess this is the same as in any place when you use the same LHS twice, but isn't there some way to fail this more informatively?

@torfjelde
Copy link
Member

I guess this is the same as in any place when you use the same LHS twice, but isn't there some way to fail this more informatively?

@submodel prefix=true ... is the way to go:

julia> @model function test(n1, n2)
           x1 ~ product_distribution(Fill(Uniform(), n1))
           @submodel prefix=true y1 = linear(x1, 1, 1)

           x2 ~ product_distribution(Fill(Uniform(), n2))
           @submodel prefix=true y2 = linear(x2, 2, 2)
       end
test (generic function with 2 methods)

julia> keys(SimpleVarInfo(test(5, 2)))
(x1, y1.y, x2, y2.y)

So one alternative is to just to make the prefix=true by default? 🤷

But improving error message seems more difficult 😕

@SamuelBrand1
Copy link

Hi guys,

We ran into the same issue around developing an epidemiological modelling/inference package (see here).

Personally, I'd support prefix=true default.

@torfjelde
Copy link
Member

Unfortunately it's a bit difficult to perform automatic prefixing in general because it's possible to put arbitrary expressions on the LHS of = in @submodel, e.g. @submodel (; x, y, z) = ... is valid.

But what we can do, is to perform a check on the model before inference occurs to make sure nothing strange has occurred, and if it has, warn the user about it or error. This would then catch issues such as repeating varnames.

@yebai
Copy link
Member

yebai commented Dec 19, 2024

submodels now have default prefixing, so it should be fine now. #696

@yebai yebai closed this as completed Dec 19, 2024
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

4 participants