|
| 1 | +module Experimental |
| 2 | + |
| 3 | +using DynamicPPL: DynamicPPL |
| 4 | + |
| 5 | +# This file only defines the names of the functions, and their docstrings. The actual implementations are in `ext/DynamicPPLJETExt.jl`, since we don't want to depend on JET.jl other than as a weak dependency. |
| 6 | +""" |
| 7 | + is_suitable_varinfo(model::Model, context::AbstractContext, varinfo::AbstractVarInfo; kwargs...) |
| 8 | +
|
| 9 | +Check if the `model` supports evaluation using the provided `context` and `varinfo`. |
| 10 | +
|
| 11 | +!!! warning |
| 12 | + Loading JET.jl is required before calling this function. |
| 13 | +
|
| 14 | +# Arguments |
| 15 | +- `model`: The model to verify the support for. |
| 16 | +- `context`: The context to use for the model evaluation. |
| 17 | +- `varinfo`: The varinfo to verify the support for. |
| 18 | +
|
| 19 | +# Keyword Arguments |
| 20 | +- `only_ddpl`: If `true`, only consider error reports occuring in the tilde pipeline. Default: `true`. |
| 21 | +
|
| 22 | +# Returns |
| 23 | +- `issuccess`: `true` if the model supports the varinfo, otherwise `false`. |
| 24 | +- `report`: The result of `report_call` from JET.jl. |
| 25 | +""" |
| 26 | +function is_suitable_varinfo end |
| 27 | + |
| 28 | +# Internal hook for JET.jl to overload. |
| 29 | +function _determine_varinfo_jet end |
| 30 | + |
| 31 | +""" |
| 32 | + determine_suitable_varinfo(model[, context]; only_ddpl::Bool=true) |
| 33 | +
|
| 34 | +Return a suitable varinfo for the given `model`. |
| 35 | +
|
| 36 | +See also: [`DynamicPPL.Experimental.is_suitable_varinfo`](@ref). |
| 37 | +
|
| 38 | +!!! warning |
| 39 | + For full functionality, this requires JET.jl to be loaded. |
| 40 | + If JET.jl is not loaded, this function will assume the model is compatible with typed varinfo. |
| 41 | +
|
| 42 | +# Arguments |
| 43 | +- `model`: The model for which to determine the varinfo. |
| 44 | +- `context`: The context to use for the model evaluation. Default: `SamplingContext()`. |
| 45 | +
|
| 46 | +# Keyword Arguments |
| 47 | +- `only_ddpl`: If `true`, only consider error reports within DynamicPPL.jl. |
| 48 | +
|
| 49 | +# Examples |
| 50 | +
|
| 51 | +```jldoctest |
| 52 | +julia> using DynamicPPL.Experimental: determine_suitable_varinfo |
| 53 | +
|
| 54 | +julia> using JET: JET # needs to be loaded for full functionality |
| 55 | +
|
| 56 | +julia> @model function model_with_random_support() |
| 57 | + x ~ Bernoulli() |
| 58 | + if x |
| 59 | + y ~ Normal() |
| 60 | + else |
| 61 | + z ~ Normal() |
| 62 | + end |
| 63 | + end |
| 64 | +model_with_random_support (generic function with 2 methods) |
| 65 | +
|
| 66 | +julia> model = model_with_random_support(); |
| 67 | +
|
| 68 | +julia> # Typed varinfo cannot handle this random support model properly |
| 69 | + # as using a single execution of the model will not see all random variables. |
| 70 | + # Hence, this this model requires untyped varinfo. |
| 71 | + vi = determine_suitable_varinfo(model); |
| 72 | +┌ Warning: Model seems incompatible with typed varinfo. Falling back to untyped varinfo. |
| 73 | +└ @ DynamicPPLJETExt ~/.julia/dev/DynamicPPL.jl/ext/DynamicPPLJETExt.jl:48 |
| 74 | +
|
| 75 | +julia> vi isa typeof(DynamicPPL.untyped_varinfo(model)) |
| 76 | +true |
| 77 | +
|
| 78 | +julia> # In contrast, a simple model with no random support can be handled by typed varinfo. |
| 79 | + @model model_with_static_support() = x ~ Normal() |
| 80 | +model_with_static_support (generic function with 2 methods) |
| 81 | +
|
| 82 | +julia> vi = determine_suitable_varinfo(model_with_static_support()); |
| 83 | +
|
| 84 | +julia> vi isa typeof(DynamicPPL.typed_varinfo(model_with_static_support())) |
| 85 | +true |
| 86 | +``` |
| 87 | +""" |
| 88 | +function determine_suitable_varinfo( |
| 89 | + model::DynamicPPL.Model, |
| 90 | + context::DynamicPPL.AbstractContext=DynamicPPL.SamplingContext(); |
| 91 | + only_ddpl::Bool=true, |
| 92 | +) |
| 93 | + # If JET.jl has been loaded, and thus `determine_varinfo` has been defined, we use that. |
| 94 | + return if Base.get_extension(DynamicPPL, :DynamicPPLJETExt) !== nothing |
| 95 | + _determine_varinfo_jet(model, context; only_ddpl) |
| 96 | + else |
| 97 | + # Warn the user. |
| 98 | + @warn "JET.jl is not loaded. Assumes the model is compatible with typed varinfo." |
| 99 | + # Otherwise, we use the, possibly incorrect, default typed varinfo (to stay backwards compat). |
| 100 | + DynamicPPL.typed_varinfo(model, context) |
| 101 | + end |
| 102 | +end |
| 103 | + |
| 104 | +end |
0 commit comments