@@ -133,8 +133,7 @@ function logprior(
133
133
# All `observe` and `dot_observe` calls are no-op in the PriorContext
134
134
135
135
# When all of model args are on the lhs of |, this is also equal to the logjoint.
136
- args, missings = get_prior_model_args (left, right, modeltype)
137
- model = Model {G, missings} (args)
136
+ model = make_prior_model (left, right, modeltype)
138
137
vi = _vi === nothing ? VarInfo (deepcopy (model), PriorContext ()) : _vi
139
138
foreach (keys (vi. metadata)) do n
140
139
@assert n in keys (left) " Variable $n is not defined."
@@ -143,35 +142,35 @@ function logprior(
143
142
return getlogp (vi)
144
143
end
145
144
146
- @generated function get_prior_model_args (
145
+ @generated function make_prior_model (
147
146
left:: NamedTuple{leftnames} ,
148
147
right:: NamedTuple{rightnames} ,
149
- modeltype:: Type{<:Model{_G , argnames}} ,
148
+ modeltype:: Type{<:Model{G , argnames}} ,
150
149
defaults:: NamedTuple{defaultnames} = getdefaults (modeltype)
151
- ) where {leftnames, rightnames, argnames, defaultnames, _G }
152
- args = []
150
+ ) where {leftnames, rightnames, G, argnames, defaultnames }
151
+ argvals = []
153
152
missings = []
154
153
warnings = []
155
154
156
155
for argname in argnames
157
156
if argname in leftnames
158
- push! (args , :($ argname = deepcopy (left.$ argname)))
157
+ push! (argvals , :(deepcopy (left.$ argname)))
159
158
push! (missings, argname)
160
159
elseif argname in rightnames
161
- push! (args , :($ argname = right.$ argname))
160
+ push! (argvals , :(right.$ argname))
162
161
elseif argname in defaultnames
163
- push! (args , :($ argname = defaults.$ argname))
162
+ push! (argvals , :(defaults.$ argname))
164
163
else
165
164
push! (warnings, :(@warn ($ (warn_msg (argname)))))
166
- push! (args , :($ argname = nothing ))
165
+ push! (argvals , :(nothing ))
167
166
end
168
167
end
169
168
170
- # `args` is spatted as a NamedTuple expression; `missings` is splatted into a tuple and
171
- # inserted as literal
169
+ # `args` is inserted as properly typed NamedTuple expression;
170
+ # `missings` is splatted into a tuple at compile time and inserted as literal
172
171
return quote
173
172
$ (warnings... )
174
- ((; $ (args ... ))) , $ ((missings... ,))
173
+ DynamicPPL . Model {G , $((missings...,))} ( $ ( to_namedtuple_expr (argnames, argvals) ))
175
174
end
176
175
end
177
176
@@ -183,9 +182,7 @@ function loglikelihood(
183
182
modeltype:: Type{<:Model{G}} ,
184
183
_vi:: Union{Nothing, VarInfo} ,
185
184
) where {G}
186
- # Pass namesl to model constructor, remaining args are missing
187
- args, missings = get_like_model_args (left, right, modeltype)
188
- model = Model {G, missings} (args)
185
+ model = make_likelihood_model (left, right, modeltype)
189
186
vi = _vi === nothing ? VarInfo (deepcopy (model)) : _vi
190
187
if isdefined (right, :chain )
191
188
# Element-wise likelihood for each value in chain
@@ -206,31 +203,31 @@ function loglikelihood(
206
203
end
207
204
end
208
205
209
- @generated function get_like_model_args (
206
+ @generated function make_likelihood_model (
210
207
left:: NamedTuple{leftnames} ,
211
208
right:: NamedTuple{rightnames} ,
212
- modeltype:: Type{<:Model{_G , argnames}} ,
209
+ modeltype:: Type{<:Model{G , argnames}} ,
213
210
defaults:: NamedTuple{defaultnames} = getdefaults (modeltype)
214
- ) where {leftnames, rightnames, argnames, defaultnames, _G }
215
- args = []
211
+ ) where {leftnames, rightnames, G, argnames, defaultnames }
212
+ argvals = []
216
213
missings = []
217
214
218
215
for argname in argnames
219
216
if argname in leftnames
220
- push! (args , :($ argname = left.$ argname))
217
+ push! (argvals , :(left.$ argname))
221
218
elseif argname in rightnames
222
- push! (args , :($ argname = right.$ argname))
219
+ push! (argvals , :(right.$ argname))
223
220
push! (missings, argname)
224
221
elseif argname in defaultnames
225
- push! (args , :($ argname = defaults.$ argname))
222
+ push! (argvals , :(defaults.$ argname))
226
223
else
227
224
throw (" This point should not be reached. Please open an issue in the DynamicPPL.jl repository." )
228
225
end
229
226
end
230
227
231
- # `args` is spatted as a NamedTuple expression; `missings` is splatted into a tuple and
232
- # inserted as literal
233
- return :((; $ (args ... )) , $ ((missings... ,)))
228
+ # `args` is inserted as properly typed NamedTuple expression;
229
+ # `missings` is splatted into a tuple at compile time and inserted as literal
230
+ return :(DynamicPPL . Model {G , $((missings...,))} ( $ ( to_namedtuple_expr (argnames, argvals) )))
234
231
end
235
232
236
233
_setval! (vi:: TypedVarInfo , c:: AbstractChains ) = _setval! (vi. metadata, vi, c)
0 commit comments