probcomp / Gen.jl

A general-purpose probabilistic programming system with programmable inference
https://gen.dev
Apache License 2.0
1.79k stars 160 forks source link

Involution DSL appears unable to read ":continuous" and ":discrete" annotation through variables #485

Open fsaad opened 1 year ago

fsaad commented 1 year ago
julia> @transform f (model_in, aux_in) to (model_out, aux_out) begin
           z = :continuous
           @write(model_out[:x], 1, z)
       end

ERROR: LoadError: MethodError: no method matching var"@write"(::LineNumberNode, ::Module, ::Expr, ::Int64, ::Symbol)
Closest candidates are:
  var"@write"(::LineNumberNode, ::Module, ::Any, ::Any, ::QuoteNode) at ~/.julia/dev/Gen/src/inference/trace_translators.jl:202
ztangent commented 1 year ago

Seems like the issue is that we're hardcoding how the :continuous annotation gets parsed by the macro:

https://github.com/probcomp/Gen.jl/blob/b959223ff31a5dd254e5256ae5d83275931b2055/src/inference/trace_translators.jl#L182-L194

https://github.com/probcomp/Gen.jl/blob/b959223ff31a5dd254e5256ae5d83275931b2055/src/inference/trace_translators.jl#L169-L180

The fixed definition would be something like:

macro read(src, ann)
    _typed = GlobalRef(Gen, :typed)
    return quote read($(esc(bij_state)), $(esc(src)), $(_typed)($(esc(ann.value)))) end
end

The downside is that it would lead to a somewhat slower dynamic call to the typed function at run-time, instead of handling that logic at compile time. I'm not sure how much slower, but perhaps it's worth not making the switch and just clearly documenting that the user has to directly specify whether they want something to be :continuous or :discrete.