probcomp / Genify.jl

Automatically convert Julia methods to Gen functions.
Apache License 2.0
47 stars 1 forks source link

Problem with using Genify with Gen #8

Open sdwfrost opened 3 weeks ago

sdwfrost commented 3 weeks ago

I'm trying to run the following example taken from the Genify documentation. genify runs without error on the provided function, but when I try to sample from the model, I get an error:

ERROR: MethodError: no method matching dynamo_generator(::UInt64, ::LineNumberNode, ::TypeVar, ::Type{typeof(Genify.splice)}, ::NTuple{7, DataType})

Closest candidates are:
  dynamo_generator(::UInt64, ::Any, ::Any, ::Any)
   @ IRTools ~/.julia/packages/IRTools/ntYVg/src/reflection/dynamo.jl:140`

Here is the code; is this a Gen or a Genify issue?

using Random, Distributions
using Gen, Genify
using Plots

function sir_model(T::Int, init_pop::Vector{Int},
                   β::Float64=0.5, γ::Float64=0.02)
    # Initialize population history
    pop_history = zeros(Int, 3, T)
    pop_history[:, 1] = init_pop
    tot_pop = sum(init_pop)
    for t in 2:T
        susceptible, infected, recovered = pop_history[:, t-1]
        # Sample number of individuals who are newly infected and recovered
        newly_infected = rand(Binomial(susceptible, β * infected / tot_pop))
        newly_recovered = rand(Binomial(infected, γ))
        # Update the population counts
        susceptible -= newly_infected
        infected += newly_infected - newly_recovered
        recovered += newly_recovered
        pop_history[:, t] = [susceptible, infected, recovered]
    end
    return pop_history
end;

pop_history = sir_model(40, [990, 10, 0], 0.5, 0.25)
plot(pop_history', labels=["Susceptible" "Infected" "Recovered"],
     xlabel="Day", ylabel="Population", title="SIR Model")

# Transform the `sir_model` method by providing its type signature
gen_sir_model = genify(sir_model, Int, Vector{Int}, Float64, Float64);

# Sample a trace from the model where 50 people were infected on day 10
trace, weight = Gen.generate(gen_sir_model, (40, [990, 10, 0], 0.5, 0.25),
                         choicemap((:newly_infected => 10 - 1, 100)))

# Check that trace has the right value
@assert trace[:newly_infected => 10 - 1] == 100
# Plot sampled trace
plot_sir(get_retval(trace))
ztangent commented 3 weeks ago

Based on the error, I suspect this might be an issue with IRTools.jl!

It's possible that various things have changed with that package since when we first wrote Genify. IRTools works a lot with Julia's compiler, which has changed a lot over the last few releases, so it wouldn't surprise me if that caused things to break...

(I'll also note that Genify is not actively maintained by us, so I wouldn't count on using it for anything more than a learning tool at this point -- but if you do manage to find out what the issue is, a PR is very welcome!)

ztangent commented 3 weeks ago

Also, a simple workaround here might be to use either an earlier version of Julia or older version of IRTools.jl.