Skip to content

eltype for empty VarInfo always results in Any #386

@torfjelde

Description

@torfjelde

The bug and the fix:

julia> using Revise, DynamicPPL, Distributions

julia> @model demo() = x ~ Normal()
demo (generic function with 2 methods)

julia> model = demo() | (x = 1.0, )
Model{typeof(demo), (), (), (), Tuple{}, Tuple{}, ConditionContext{(:x,), NamedTuple{(:x,), Tuple{Float64}}, DefaultContext}}(:demo, demo, NamedTuple(), NamedTuple(), ConditionContext((x = 1.0,), DefaultContext()))

julia> varinfo = VarInfo(model)
TypedVarInfo{NamedTuple{(), Tuple{}}, Float64}(NamedTuple(), Base.RefValue{Float64}(-1.4189385332046727), Base.RefValue{Int64}(1))

julia> eltype(varinfo, SampleFromPrior())
Any

julia> # Overload "empty" `TypedVarInfo`.
       function Base.eltype(
           vi::DynamicPPL.VarInfo{NamedTuple{(),Tuple{}}},
           spl::Union{DynamicPPL.AbstractSampler,DynamicPPL.SampleFromPrior}
       )
           return eltype(DynamicPPL.getlogp(vi))
       end

julia> eltype(varinfo, SampleFromPrior())
Float64

This can be pretty annoying as it leads to downstream issues such as StackOverflowError when used with MvNormal.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions