probcomp / Gen.jl

A general-purpose probabilistic programming system with programmable inference

Home Page:https://gen.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

_fill_array! can't handle DynamicChoiceMaps with tuple and symbol addresses

Joaoloula opened this issue · comments

Minimal example:

@gen function model()
    x ~ normal(0, 1)
    {(1, 1)} ~ normal(0, 1)
end

trace = simulate(model, ())
hmc(trace, selectall())

throws the following error:

MethodError: no method matching isless(::Symbol, ::Tuple{Int64, Int64})
Closest candidates are:
  isless(::Tuple{}, ::Tuple) at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/tuple.jl:454
  isless(::Tuple, ::Tuple) at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/tuple.jl:462
  isless(::Any, ::Missing) at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/missing.jl:88
  ...

Stacktrace:
  [1] lt(o::Base.Order.ForwardOrdering, a::Symbol, b::Tuple{Int64, Int64})
    @ Base.Order ./ordering.jl:109
  [2] sort!(v::Vector{Any}, lo::Int64, hi::Int64, #unused#::Base.Sort.InsertionSortAlg, o::Base.Order.ForwardOrdering)
    @ Base.Sort ./sort.jl:507
  [3] sort!(v::Vector{Any}, lo::Int64, hi::Int64, a::Base.Sort.MergeSortAlg, o::Base.Order.ForwardOrdering, t::Vector{Any})
    @ Base.Sort ./sort.jl:592
  [4] sort!
    @ ./sort.jl:591 [inlined]
  [5] sort!
    @ ./sort.jl:664 [inlined]
  [6] #sort!#8
    @ ./sort.jl:725 [inlined]
  [7] sort!
    @ ./sort.jl:713 [inlined]
  [8] #sort#9
    @ ./sort.jl:772 [inlined]
  [9] sort
    @ ./sort.jl:772 [inlined]
 [10] _fill_array!(choices::DynamicChoiceMap, arr::Vector{Float64}, start_idx::Int64)
    @ Gen ~/.julia/packages/Gen/Dne3u/src/choice_map.jl:799
 [11] to_array(choices::DynamicChoiceMap, #unused#::Type{Float64})
    @ Gen ~/.julia/packages/Gen/Dne3u/src/choice_map.jl:174
 [12] hmc(trace::Gen.DynamicDSLTrace{DynamicDSLFunction{Any}}, selection::AllSelection; L::Int64, eps::Float64, check::Bool, observations::EmptyChoiceMap)
    @ Gen ~/.julia/packages/Gen/Dne3u/src/inference/hmc.jl:36
 [13] hmc(trace::Gen.DynamicDSLTrace{DynamicDSLFunction{Any}}, selection::AllSelection)
    @ Gen ~/.julia/packages/Gen/Dne3u/src/inference/hmc.jl:28
 [14] top-level scope
    @ In[1585]:7
 [15] eval
    @ ./boot.jl:373 [inlined]
 [16] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
    @ Base ./loading.jl:1196

The "sort" operation in "_fill_array!" for DynamicChoiceMaps being the problem. I can think of two fixes:

  • If sorting isn't necessary, rewrite the function to look like the StaticChoiceMap version.
  • If sorting is necessary, define isless(a::Symbol, b::Tuple).

Happy to make a PR once you guys have decided on the best fix!