Skip to content

[Bug]: Retrieving posteriors of partially initialized ResizableArray tries to access uninitialized entries #578

@wouterwln

Description

@wouterwln

Description

GraphPPL can partially initialize arrays, but they will be requested by RxInfer at some point here:

getvarref(model::GraphPPL.Model, container::AbstractArray) = map(element -> getvarref(model, element), container)

We will request these elements and then it will be undefined and break.

Expected Behavior

I expect it to return a posterior

Actual Behavior

It crashes

Minimal Reproducible Example or steps to reproduce the issue

using RxInfer

@model function baby_model(y)
    μ[2] ~ Normal(mean=0, var=1)
    y ~ Normal(mean=μ[2], var=1)
end

infer(model = baby_model(), data = (y = 0.0,))

Error Message / Stack Trace

UndefRefError: access to undefined reference

Stacktrace:
  [1] getindex
    @ ./essentials.jl:917 [inlined]
  [2] recursive_getindex
    @ ~/.julia/packages/GraphPPL/IVPQ2/src/resizable_array.jl:144 [inlined]
  [3] getindex
    @ ~/.julia/packages/GraphPPL/IVPQ2/src/resizable_array.jl:136 [inlined]
  [4] iterate
    @ ~/.julia/packages/GraphPPL/IVPQ2/src/resizable_array.jl:180 [inlined]
  [5] iterate
    @ ./generator.jl:45 [inlined]
  [6] _collect(c::GraphPPL.ResizableArray{GraphPPL.NodeLabel, Vector{GraphPPL.NodeLabel}, 1}, itr::Base.Generator{GraphPPL.ResizableArray{GraphPPL.NodeLabel, Vector{GraphPPL.NodeLabel}, 1}, RxInfer.var"#91#92"{GraphPPL.Model{MetaGraphsNext.MetaGraph{Int64, Graphs.SimpleGraphs.SimpleGraph{Int64}, GraphPPL.NodeLabel, GraphPPL.NodeData, GraphPPL.EdgeLabel, GraphPPL.Context, MetaGraphsNext.var"#4#8", Float64}, GraphPPL.PluginsCollection{Tuple{GraphPPL.VariationalConstraintsPlugin{GraphPPL.NoConstraints}, GraphPPL.MetaPlugin{GraphPPL.MetaSpecification}, RxInfer.InitializationPlugin{RxInfer.NoInit}, RxInfer.ReactiveMPInferencePlugin{RxInfer.ReactiveMPInferenceOptions{Nothing, Nothing, Nothing}}}}, RxInfer.ReactiveMPGraphPPLBackend{Static.False}, String}}}, ::Base.EltypeUnknown, isz::Base.HasShape{1})
    @ Base ./array.jl:811
  [7] collect_similar
    @ ./array.jl:720 [inlined]
  [8] map
    @ ./abstractarray.jl:3371 [inlined]
  [9] getvarref
    @ ~/.julia/dev/RxInfer/src/model/plugins/reactivemp_inference.jl:283 [inlined]
 [10] (::RxInfer.var"#89#90"{GraphPPL.Model{MetaGraphsNext.MetaGraph{Int64, Graphs.SimpleGraphs.SimpleGraph{Int64}, GraphPPL.NodeLabel, GraphPPL.NodeData, GraphPPL.EdgeLabel, GraphPPL.Context, MetaGraphsNext.var"#4#8", Float64}, GraphPPL.PluginsCollection{Tuple{GraphPPL.VariationalConstraintsPlugin{GraphPPL.NoConstraints}, GraphPPL.MetaPlugin{GraphPPL.MetaSpecification}, RxInfer.InitializationPlugin{RxInfer.NoInit}, RxInfer.ReactiveMPInferencePlugin{RxInfer.ReactiveMPInferenceOptions{Nothing, Nothing, Nothing}}}}, RxInfer.ReactiveMPGraphPPLBackend{Static.False}, String}})(v::GraphPPL.ResizableArray{GraphPPL.NodeLabel, Vector{GraphPPL.NodeLabel}, 1})
    @ RxInfer ~/.julia/dev/RxInfer/src/model/plugins/reactivemp_inference.jl:279
 [11] map!(f::RxInfer.var"#89#90"{GraphPPL.Model{MetaGraphsNext.MetaGraph{Int64, Graphs.SimpleGraphs.SimpleGraph{Int64}, GraphPPL.NodeLabel, GraphPPL.NodeData, GraphPPL.EdgeLabel, GraphPPL.Context, MetaGraphsNext.var"#4#8", Float64}, GraphPPL.PluginsCollection{Tuple{GraphPPL.VariationalConstraintsPlugin{GraphPPL.NoConstraints}, GraphPPL.MetaPlugin{GraphPPL.MetaSpecification}, RxInfer.InitializationPlugin{RxInfer.NoInit}, RxInfer.ReactiveMPInferencePlugin{RxInfer.ReactiveMPInferenceOptions{Nothing, Nothing, Nothing}}}}, RxInfer.ReactiveMPGraphPPLBackend{Static.False}, String}}, out::Dictionaries.UnorderedDictionary{Symbol, Any}, d::Dictionaries.UnorderedDictionary{Symbol, Any})
    @ Dictionaries ~/.julia/packages/Dictionaries/L8pH7/src/map.jl:66
 [12] map(f::Function, d::Dictionaries.UnorderedDictionary{Symbol, Any})
    @ Dictionaries ~/.julia/packages/Dictionaries/L8pH7/src/map.jl:106
 [13] map(f::Function, vardict::GraphPPL.VarDict{Any})
    @ GraphPPL ~/.julia/packages/GraphPPL/IVPQ2/src/graph_engine.jl:615
 [14] getvardict
    @ ~/.julia/dev/RxInfer/src/model/plugins/reactivemp_inference.jl:279 [inlined]
 [15] getvardict
    @ ~/.julia/dev/RxInfer/src/model/model.jl:40 [inlined]
 [16] batch_inference(; model::GraphPPL.ModelGenerator{typeof(baby_model), @Kwargs{}, GraphPPL.PluginsCollection{Tuple{}}, RxInfer.ReactiveMPGraphPPLBackend{Static.False}, String}, data::@NamedTuple{y::Float64}, initialization::Nothing, constraints::Nothing, meta::Nothing, options::Nothing, returnvars::Nothing, predictvars::Nothing, iterations::Nothing, free_energy::Bool, free_energy_diagnostics::Tuple{RxInfer.ObjectiveDiagnosticCheckNaNs, RxInfer.ObjectiveDiagnosticCheckInfs}, allow_node_contraction::Bool, showprogress::Bool, callbacks::Nothing, addons::Nothing, postprocess::DefaultPostprocess, warn::Bool, catch_exception::Bool)
    @ RxInfer ~/.julia/dev/RxInfer/src/inference/batch.jl:209
 [17] batch_inference
    @ ~/.julia/dev/RxInfer/src/inference/batch.jl:96 [inlined]
 [18] #312
    @ ~/.julia/dev/RxInfer/src/inference/inference.jl:545 [inlined]
 [19] with_session(f::RxInfer.var"#312#314"{GraphPPL.ModelGenerator{typeof(baby_model), @Kwargs{}, GraphPPL.PluginsCollection{Tuple{}}, RxInfer.ReactiveMPGraphPPLBackend{Static.False}, String}, @NamedTuple{y::Float64}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Bool, Tuple{RxInfer.ObjectiveDiagnosticCheckNaNs, RxInfer.ObjectiveDiagnosticCheckInfs}, Bool, Bool, Bool, Nothing, Nothing, DefaultPostprocess, Nothing, Bool, Bool, Bool}, session::RxInfer.Session, label::Symbol)
    @ RxInfer ~/.julia/dev/RxInfer/src/session.jl:253
 [20] #infer#311
    @ ~/.julia/dev/RxInfer/src/inference/inference.jl:515 [inlined]
 [21] top-level scope
    @ ~/.julia/dev/RxInfer/jl_notebook_cell_df34fa98e69747e1a8f8a730347b8e2f_W0sZmlsZQ==.jl:8

Julia Version

1.11

RxInfer Version

Latest stable

Environment Information

Session ID (Optional)

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions