Skip to content

Conversation

@ztangent
Copy link
Member

Given a static generative function like this:

@gen (static) function datum(x::Float64, (grad)(params::Params))
    is_outlier = @trace(bernoulli(params.prob_outlier), :z)
    std = is_outlier ? params.outlier_std : params.inlier_std
    y = @trace(normal(x * params.slope + params.intercept, std), :y)
    return y
end

This PR makes it so that the IR is automatically pretty-printed like this:

julia> Gen.get_ir(datum)
== Static IR ==
Arguments: (x, params)
  ##.#445 = #15(params)
  ##z#442 = @trace(Gen.Bernoulli()(##.#445), :z)
  std = #16(params, ##z#442)
  ##call#451 = #17(params, x)
  ##y#448 = @trace(Gen.Normal()(##call#451, std), :y)
  return ##y#448

Should come in handy for debugging domain-specific compilers to IR, as well as any future improvements made to the IR or static function compilation.

params::Dict{Symbol,Any}
end
(gen_fn::$gen_fn_type_name)(args...) = $(GlobalRef(Gen, :propose))(gen_fn, args)[3]
$(GlobalRef(Gen, :get_ir))(::$gen_fn_type_name) = $(QuoteNode(ir))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reasonable, but it's making me wonder whether there was any reason we didn't have this in the first place. I guess the answer is probably that get_ir was mostly used from @generated functions, which only had access to the type of the GF, not the GF itself.

@ztangent ztangent merged commit 7a458b8 into master Feb 16, 2021
@ztangent ztangent deleted the 20200216_ztangent_irprettyprint branch July 13, 2021 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants