|
69 | 69 | ###########
|
70 | 70 |
|
71 | 71 | """
|
72 |
| -``` |
73 |
| -struct VarInfo{Tmeta, Tlogp} <: AbstractVarInfo |
74 |
| - metadata::Tmeta |
75 |
| - logp::Base.RefValue{Tlogp} |
76 |
| - num_produce::Base.RefValue{Int} |
77 |
| -end |
78 |
| -``` |
| 72 | + struct VarInfo{Tmeta,Tlogp} <: AbstractVarInfo |
| 73 | + metadata::Tmeta |
| 74 | + logp::Base.RefValue{Tlogp} |
| 75 | + num_produce::Base.RefValue{Int} |
| 76 | + end |
| 77 | +
|
| 78 | +A light wrapper over some kind of metadata. |
79 | 79 |
|
80 |
| -A light wrapper over one or more instances of `Metadata`. Let `vi` be an instance of |
81 |
| -`VarInfo`. If `vi isa VarInfo{<:Metadata}`, then only one `Metadata` instance is used |
82 |
| -for all the sybmols. `VarInfo{<:Metadata}` is aliased `UntypedVarInfo`. If |
83 |
| -`vi isa VarInfo{<:NamedTuple}`, then `vi.metadata` is a `NamedTuple` that maps each |
84 |
| -symbol used on the LHS of `~` in the model to its `Metadata` instance. The latter allows |
85 |
| -for the type specialization of `vi` after the first sampling iteration when all the |
86 |
| -symbols have been observed. `VarInfo{<:NamedTuple}` is aliased `NTVarInfo`. |
| 80 | +The type of the metadata can be one of a number of options. It may either be a |
| 81 | +`Metadata` or a `VarNamedVector`, _or_, it may be a `NamedTuple` which maps |
| 82 | +symbols to `Metadata` or `VarNamedVector` instances. Here, a _symbol_ refers |
| 83 | +to a Julia variable and may consist of one or more `VarName`s which appear on |
| 84 | +the left-hand side of tilde statements. For example, `x[1]` and `x[2]` both |
| 85 | +have the same symbol `x`. |
87 | 86 |
|
88 |
| -Note: It is the user's responsibility to ensure that each "symbol" is visited at least |
89 |
| -once whenever the model is called, regardless of any stochastic branching. Each symbol |
90 |
| -refers to a Julia variable and can be a hierarchical array of many random variables, e.g. `x[1] ~ ...` and `x[2] ~ ...` both have the same symbol `x`. |
| 87 | +Several type aliases are provided for these forms of VarInfos: |
| 88 | +- `VarInfo{<:Metadata}` is `UntypedVarInfo` |
| 89 | +- `VarInfo{<:VarNamedVector}` is `UntypedVectorVarInfo` |
| 90 | +- `VarInfo{<:NamedTuple}` is `NTVarInfo` |
| 91 | +
|
| 92 | +The NamedTuple form, i.e. `NTVarInfo`, is useful for maintaining type stability |
| 93 | +of model evaluation. However, the element type of NamedTuples are not contained |
| 94 | +in its type itself: thus, there is no way to use the type system to determine |
| 95 | +whether the elements of the NamedTuple are `Metadata` or `VarNamedVector`. |
| 96 | +
|
| 97 | +Note that for NTVarInfo, it is the user's responsibility to ensure that each |
| 98 | +symbol is visited at least once during model evaluation, regardless of any |
| 99 | +stochastic branching. |
91 | 100 | """
|
92 | 101 | struct VarInfo{Tmeta,Tlogp} <: AbstractVarInfo
|
93 | 102 | metadata::Tmeta
|
94 | 103 | logp::Base.RefValue{Tlogp}
|
95 | 104 | num_produce::Base.RefValue{Int}
|
96 | 105 | end
|
97 | 106 | VarInfo(meta=Metadata()) = VarInfo(meta, Ref{LogProbType}(0.0), Ref(0))
|
| 107 | +""" |
| 108 | + VarInfo([rng, ]model[, sampler, context]) |
| 109 | +
|
| 110 | +Generate a `VarInfo` object for the given `model`, by evaluating it once using |
| 111 | +the given `rng`, `sampler`, and `context`. |
| 112 | +
|
| 113 | +!!! warning |
| 114 | +
|
| 115 | + This function currently returns a `VarInfo` with its metadata field set to |
| 116 | + a `NamedTuple` of `Metadata`. This is an implementation detail. In general, |
| 117 | + this function may return any kind of object that satisfies the |
| 118 | + `AbstractVarInfo` interface. If you require precise control over the type |
| 119 | + of `VarInfo` returned, use the internal functions `untyped_varinfo`, |
| 120 | + `typed_varinfo`, `untyped_vector_varinfo`, or `typed_vector_varinfo` |
| 121 | + instead. |
| 122 | +""" |
98 | 123 | function VarInfo(
|
99 | 124 | rng::Random.AbstractRNG,
|
100 | 125 | model::Model,
|
|
0 commit comments