Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
167 commits
Select commit Hold shift + click to select a range
3ab2bee
unwrap distributions and varnames at model-level
torfjelde May 31, 2021
a549d1f
removed _tilde and renamed tilde_assume and others
torfjelde May 31, 2021
e0f77bc
formatting
torfjelde May 31, 2021
8e4fa91
updated compiler for new tilde-methods
torfjelde May 31, 2021
1c9a2d5
fixed calls to dot_assume
torfjelde May 31, 2021
d70e1be
added sampling context and unwrap_childcontext
torfjelde May 31, 2021
f743990
updated tilde methods
torfjelde May 31, 2021
3d2e7e2
updated model call signature
torfjelde May 31, 2021
4f1d396
updated compiler
torfjelde May 31, 2021
b187d74
formatting
torfjelde May 31, 2021
ee99f8c
added getsym for vectors
torfjelde May 31, 2021
c4845d0
Update src/varname.jl
torfjelde May 31, 2021
a0c05f3
fixed some signatures for Model
torfjelde May 31, 2021
307cd7e
fixed a method call
torfjelde May 31, 2021
5972771
fixed method signatures
torfjelde Jun 1, 2021
c4ecd0e
sort of fixed the matchingvalue functionality for model
torfjelde Jun 1, 2021
a34b51c
formatting
torfjelde Jun 1, 2021
b89ff7e
removed redundant _tilde method
torfjelde Jun 1, 2021
6368282
Merge branch 'tor/tilde-simplification' into tor/sampler-context
torfjelde Jun 1, 2021
e4a2cf8
removed left-over acclogp! that should not be here anymore
torfjelde Jun 1, 2021
7605785
export SamplingContext
torfjelde Jun 1, 2021
354ac52
use context instead of ctx to refer to contexts
torfjelde Jun 1, 2021
b7a2b3b
formatting
torfjelde Jun 1, 2021
9e0fc9a
use context instead of ctx for variables
torfjelde Jun 1, 2021
7a4a1a3
use context instead of ctx to refer to contexts
torfjelde Jun 1, 2021
7899473
Update src/compiler.jl
torfjelde Jun 2, 2021
1630476
Update src/context_implementations.jl
torfjelde Jun 2, 2021
6892d2b
Apply suggestions from code review
torfjelde Jun 2, 2021
13da1b4
added some whitespace to some docstrings
torfjelde Jun 2, 2021
d76e5b3
deprecated tilde and dot_tilde plus exported new versions
torfjelde Jun 2, 2021
8059669
formatting
torfjelde Jun 2, 2021
4343896
Merge branch 'master' into tor/tilde-simplification
yebai Jun 2, 2021
43ef8d1
minor version bump
torfjelde Jun 2, 2021
fec00b6
Merge branch 'master' into tor/sampler-context
torfjelde Jun 3, 2021
ffb4933
Merge branch 'master' into tor/sampler-context
torfjelde Jun 4, 2021
43356a2
Merge branch 'master' into tor/tilde-simplification
torfjelde Jun 4, 2021
671e0b7
Merge branch 'master' into tor/tilde-simplification
torfjelde Jun 7, 2021
30c8345
Merge branch 'tor/tilde-simplification' into tor/sampling-context-simple
torfjelde Jun 7, 2021
1015f0e
added impl of matchingvalue for contexts
torfjelde Jun 7, 2021
23c86a7
reverted the change that makes assume always resample
torfjelde Jun 7, 2021
17f5abe
removed the inds arguments from assume and dot_assume to stay non-bre…
torfjelde Jun 7, 2021
dbd61f0
Update src/context_implementations.jl
yebai Jun 7, 2021
b10ba3f
added missing sampler arg to tilde_observe
torfjelde Jun 7, 2021
43779e8
Merge branch 'tor/sampling-context-simple' of github.com:TuringLang/D…
torfjelde Jun 7, 2021
bc5029f
added missing sampler argument in dot_tilde_observe
torfjelde Jun 7, 2021
7eac33d
fixed order of arguments in some dot_assume calls
torfjelde Jun 7, 2021
8599481
formatting
torfjelde Jun 7, 2021
90a8c45
formatting
torfjelde Jun 7, 2021
f9d4ff8
added missing sampler argument in tilde_observe for SamplingContext
torfjelde Jun 7, 2021
e424fe7
added missing word in a docstring
torfjelde Jun 7, 2021
70957d2
updated submodel macro
torfjelde Jun 8, 2021
d00cdcf
removed unwrap_childcontext and related since its not needed for this PR
torfjelde Jun 8, 2021
639fd6e
updated submodel macro
torfjelde Jun 8, 2021
c9a06fb
fixed evaluation implementations of dot_assume
torfjelde Jun 8, 2021
2fe5f40
updated pointwise_loglikelihoods and related
torfjelde Jun 8, 2021
b532ca6
added proper tests for pointwise_loglikelihoods
torfjelde Jun 8, 2021
4e2274e
updated DPPL tests to reflect recent changes
torfjelde Jun 8, 2021
ef6da43
bump minor version since this will be breaking
torfjelde Jun 8, 2021
dbb48c5
Merge branch 'tor/tilde-simplification' into tor/sampling-context-simple
torfjelde Jun 8, 2021
10899f3
formatting
torfjelde Jun 8, 2021
1f21ce4
formatting
torfjelde Jun 8, 2021
7004506
renamed mean_of_mean_models used in tests
torfjelde Jun 8, 2021
fa6c4d6
bumped dppl version in integration tests
torfjelde Jun 9, 2021
684d829
Apply suggestions from code review
torfjelde Jun 9, 2021
07bb284
Apply suggestions from code review
torfjelde Jun 9, 2021
c7c6a3c
fixed ambiguity error
torfjelde Jun 9, 2021
c259999
Merge branch 'tor/sampling-context-simple' of github.com:TuringLang/D…
torfjelde Jun 9, 2021
06d319c
Introduction of `SamplingContext`: keeping it simple (#259)
torfjelde Jun 9, 2021
cb996c6
Update src/DynamicPPL.jl
torfjelde Jun 9, 2021
300f4b6
Merge branch 'master' into tor/tilde-simplification
torfjelde Jun 9, 2021
0437d39
Merge branch 'tor/tilde-simplification' into tor/simple-varinfo-v2
torfjelde Jun 9, 2021
03c9285
added initial impl of SimpleVarInfo
torfjelde Jun 10, 2021
f91952d
remove unnecessary debug statements to be compat with Zygote
torfjelde Jun 10, 2021
4d4b489
make reconstruct slightly more generic
torfjelde Jun 10, 2021
1506773
Merge branch 'master' into tor/simple-varinfo-v2
torfjelde Jun 10, 2021
a68c045
added a couple of convenience constructors
torfjelde Jun 18, 2021
9766aec
formatting
torfjelde Jun 18, 2021
46b1c78
small fix
torfjelde Jun 18, 2021
3a645d6
return var_info from tilde-statements, allowing impl of immutable ver…
torfjelde Jun 20, 2021
a2ec0bd
allow usage of non-Ref types in SimpleVarInfo
torfjelde Jun 20, 2021
1d9bc37
update submodel-macro
torfjelde Jun 20, 2021
cfd7f21
formatting and docstring for submodel-macro
torfjelde Jun 20, 2021
c200e73
attempt at supporting implicit returns too
torfjelde Jun 20, 2021
efeb812
added a small comment
torfjelde Jun 20, 2021
14b9495
simplifed submodel macro a bit
torfjelde Jun 20, 2021
c3d9e7b
formatting
torfjelde Jun 20, 2021
416e773
fixed typo
torfjelde Jun 20, 2021
ac2b68d
Merge branch 'tor/simple-varinfo-v2' of github.com:TuringLang/Dynamic…
torfjelde Jun 20, 2021
b4b8b03
use bang-bang convention
torfjelde Jun 20, 2021
a725a27
updated PointwiseLikelihoodContext
torfjelde Jun 20, 2021
5512670
fixed issue where we unnecessarily replace the return-statement
torfjelde Jun 20, 2021
4c1ee70
check subtype in the retval
torfjelde Jun 20, 2021
26590b5
formatting
torfjelde Jun 20, 2021
42fd414
fixed type-instability in retval check
torfjelde Jun 20, 2021
f219545
introduced evaluate method for model
torfjelde Jun 20, 2021
ce13566
remove unnecessary type-requirement
torfjelde Jun 20, 2021
3556b11
make return-value check much nicer
torfjelde Jun 20, 2021
599d094
removed redundant creation of anonymous function
torfjelde Jun 20, 2021
22b170c
dont use UnionAll in return_values
torfjelde Jun 28, 2021
4606f16
updated tests for submodel to reflect new syntax
torfjelde Jun 28, 2021
68cb021
moved to using BangBang-convention for most methods
torfjelde Jun 30, 2021
cb1fd8b
remove SimpleVarInfo from this branch
torfjelde Jun 30, 2021
5936dd0
added a comment
torfjelde Jun 30, 2021
426c465
reverted submodel macro to use = rather than ~
torfjelde Jun 30, 2021
208b62c
Merge branch 'tor/immutable-varinfo-support' into tor/simple-varinfo-v2
torfjelde Jun 30, 2021
a8e55bd
updated SimpleVarInfo impl
torfjelde Jun 30, 2021
149229f
added a couple of missing deprecations
torfjelde Jun 30, 2021
809d23f
updated tests
torfjelde Jun 30, 2021
8ea80d7
Merge branch 'tor/immutable-varinfo-support' into tor/simple-varinfo-v2
torfjelde Jun 30, 2021
07f684b
updated implementations of logjoint and others
torfjelde Jun 30, 2021
d317bd8
Merge branch 'tor/immutable-varinfo-support' into tor/simple-varinfo-v2
torfjelde Jun 30, 2021
b00ae47
formatting
torfjelde Jun 30, 2021
a88f8ea
Merge branch 'tor/immutable-varinfo-support' into tor/simple-varinfo-v2
torfjelde Jun 30, 2021
bfd7c78
added eltype impl for SimpleVarInfo
torfjelde Jul 2, 2021
acb15eb
formatting
torfjelde Jul 2, 2021
4828aab
fixed eltype for SimpleVarInfo
torfjelde Jul 6, 2021
167976f
implement setindex!! in prep for allowing sampling with immutable vi
torfjelde Jul 9, 2021
b56024e
Merge branch 'tor/immutable-varinfo-support' into tor/simple-varinfo-v2
torfjelde Jul 9, 2021
e4f0ad2
formatting
torfjelde Jul 9, 2021
ccfd112
initial work on allowing sampling using SimpleVarInfo
torfjelde Jul 9, 2021
d660433
formatting
torfjelde Jul 9, 2021
c925b07
Merge branch 'master' into tor/simple-varinfo-v2
torfjelde Jul 16, 2021
3ec72c6
Merge branch 'tor/simple-varinfo-v2' of github.com:TuringLang/Dynamic…
torfjelde Jul 16, 2021
90cf754
add constructor for SimpleVarInfo using model
torfjelde Jul 16, 2021
0ab9d8b
improved leftover to_namedtuple_expr, fixing a bug when used with Zygote
torfjelde Jul 16, 2021
42ad552
bumped patch version
torfjelde Jul 16, 2021
975184d
Merge branch 'tor/allargs-construction-improvement' into tor/simple-v…
torfjelde Jul 16, 2021
a0cd0c4
Merge branch 'master' into tor/simple-varinfo-v2
torfjelde Jul 19, 2021
aa706ef
Merge branch 'master' into tor/immutable-varinfo-support
torfjelde Jul 19, 2021
744a032
Merge branch 'tor/immutable-varinfo-support' into tor/simple-varinfo-v2
torfjelde Jul 19, 2021
0f1def9
fixed set_flag!!
torfjelde Jul 20, 2021
54a1be7
Merge branch 'master' into tor/immutable-varinfo-support
torfjelde Jul 20, 2021
76daca6
Merge branch 'tor/immutable-varinfo-support' into tor/simple-varinfo-v2
torfjelde Jul 20, 2021
596d63b
Merge branch 'master' into tor/immutable-varinfo-support
torfjelde Jul 22, 2021
6f947f7
Merge branch 'tor/immutable-varinfo-support' into tor/simple-varinfo-v2
torfjelde Jul 22, 2021
53596fb
forgot the return in the replace_returns
torfjelde Jul 23, 2021
4076f63
Merge branch 'tor/immutable-varinfo-support' into tor/simple-varinfo-v2
torfjelde Jul 23, 2021
45b3a76
Merge branch 'master' into tor/immutable-varinfo-support
torfjelde Jul 29, 2021
57b5d47
bigboy update to benchmarks
torfjelde Aug 2, 2021
d0a08f6
fixed some issues and added support for usage of Dict in SimpleVarInfo
torfjelde Aug 5, 2021
0ab59eb
Merge branch 'master' into tor/immutable-varinfo-support
torfjelde Aug 5, 2021
4002318
Merge branch 'tor/immutable-varinfo-support' into tor/simple-varinfo-v2
torfjelde Aug 5, 2021
ff75ddc
added docstring and improved indexing behvaior for SimpleVarInfo
torfjelde Aug 5, 2021
d29dd8f
formatting
torfjelde Aug 5, 2021
a72594f
dont allow sampling with indexing when using SimpleVarInfo with Named…
torfjelde Aug 5, 2021
be35be0
_setval_kernel and others are only supported by VarInfo atm
torfjelde Aug 12, 2021
c3da562
Merge branch 'master' into tor/immutable-varinfo-support
torfjelde Aug 14, 2021
3f3d8d3
Merge branch 'tor/immutable-varinfo-support' into tor/simple-varinfo-v2
torfjelde Aug 14, 2021
4d4eeb3
fixed typo in comment
torfjelde Aug 14, 2021
b7862a8
added more values_as impls
torfjelde Aug 14, 2021
39619cd
removed redundant values_from_metadata
torfjelde Aug 14, 2021
294cd0f
Merge branch 'master' into tor/immutable-varinfo-support
torfjelde Aug 15, 2021
bcb1e46
Merge branch 'tor/immutable-varinfo-support' into tor/simple-varinfo-v2
torfjelde Aug 15, 2021
e67901b
fixed bug in push!! for SimpleVarInfo
torfjelde Aug 17, 2021
e7c0a76
Merge branch 'master' into tor/benchmark-update
torfjelde Aug 19, 2021
7309fb7
fixed a couple of bugs in SimpleVarInfo
torfjelde Aug 19, 2021
d7dad31
forgot which branch Im on
torfjelde Aug 19, 2021
6ec2d29
added handling of short defs in replace_returns and more docstrings
torfjelde Aug 21, 2021
dfd9dc5
fixed bug in generate_tilde introduced in a merge
torfjelde Aug 23, 2021
18ed817
fixed a bug in isfuncdef
torfjelde Aug 23, 2021
b61a9be
fixed tests
torfjelde Aug 23, 2021
8d2dc71
formatting
torfjelde Aug 23, 2021
9cff93f
uncomment mistakenly commented code
torfjelde Aug 24, 2021
6c3bd62
Merge branch 'master' into tor/simple-varinfo-v2
torfjelde Aug 24, 2021
06f23da
remove changes related to SimpleVarInfo
torfjelde Aug 24, 2021
41dc3c3
remove unnecessary BangBang adoption
torfjelde Aug 24, 2021
02b9885
also return vi from assume statements
torfjelde Aug 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
ZygoteRules = "700de1a5-db45-46bc-99cf-38207098b444"

[compat]
Expand All @@ -21,5 +22,6 @@ Bijectors = "0.5.2, 0.6, 0.7, 0.8, 0.9"
ChainRulesCore = "0.9.7, 0.10"
Distributions = "0.23.8, 0.24, 0.25"
MacroTools = "0.5.6"
Setfield = "0.7"
ZygoteRules = "0.2"
julia = "1.3"
32 changes: 32 additions & 0 deletions src/DynamicPPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ using ChainRulesCore: ChainRulesCore
using MacroTools: MacroTools
using ZygoteRules: ZygoteRules
using BangBang: BangBang
using Setfield: Setfield

using Random: Random

Expand All @@ -28,15 +29,22 @@ import Base:
keys,
haskey

import BangBang: push!!, empty!!, setindex!!

# VarInfo
export AbstractVarInfo,
VarInfo,
UntypedVarInfo,
TypedVarInfo,
push!!,
empty!!,
getlogp,
setlogp!,
acclogp!,
resetlogp!,
setlogp!!,
acclogp!!,
resetlogp!!,
get_num_produce,
set_num_produce!,
reset_num_produce!,
Expand All @@ -51,6 +59,8 @@ export AbstractVarInfo,
istrans,
link!,
invlink!,
link!!,
invlink!!,
tonamedtuple,
# VarName (reexport from AbstractPPL)
VarName,
Expand Down Expand Up @@ -134,4 +144,26 @@ include("compat/ad.jl")
include("loglikelihoods.jl")
include("submodel_macro.jl")

# Deprecations
@deprecate empty!(vi::VarInfo) empty!!(vi::VarInfo)
@deprecate push!(vi::AbstractVarInfo, vn::VarName, r, dist::Distribution) push!!(
vi::AbstractVarInfo, vn::VarName, r, dist::Distribution
)
@deprecate push!(
vi::AbstractVarInfo, vn::VarName, r, dist::Distribution, sampler::AbstractSampler
) push!!(vi::AbstractVarInfo, vn::VarName, r, dist::Distribution, sampler::AbstractSampler)
@deprecate push!(vi::AbstractVarInfo, vn::VarName, r, dist::Distribution, gid::Selector) push!!(
vi::AbstractVarInfo, vn::VarName, r, dist::Distribution, gid::Selector
)
@deprecate push!(
vi::AbstractVarInfo, vn::VarName, r, dist::Distribution, gid::Set{Selector}
) push!!(vi::AbstractVarInfo, vn::VarName, r, dist::Distribution, gid::Set{Selector})

@deprecate setlogp!(vi, logp) setlogp!!(vi, logp)
@deprecate acclogp!(vi, logp) acclogp!!(vi, logp)
@deprecate resetlogp!(vi) resetlogp!!(vi)

@deprecate link!(vi, spl) link!!(vi, spl)
@deprecate invlink!(vi, spl) invlink!!(vi, spl)

end # module
2 changes: 1 addition & 1 deletion src/compat/ad.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# See https://github.com/TuringLang/Turing.jl/issues/1199
ChainRulesCore.@non_differentiable push!(
ChainRulesCore.@non_differentiable push!!(
vi::VarInfo, vn::VarName, r, dist::Distribution, gidset::Set{Selector}
)

Expand Down
120 changes: 113 additions & 7 deletions src/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ function generate_tilde(left, right)
# If the LHS is a literal, it is always an observation
if isliteral(left)
return quote
$(DynamicPPL.tilde_observe!)(
_, __varinfo__ = $(DynamicPPL.tilde_observe!!)(
__context__, $(DynamicPPL.check_tilde_rhs)($right), $left, __varinfo__
)
end
Expand All @@ -383,7 +383,7 @@ function generate_tilde(left, right)
$inds = $(vinds(left))
$isassumption = $(DynamicPPL.isassumption(left))
if $isassumption
$left = $(DynamicPPL.tilde_assume!)(
$left, __varinfo__ = $(DynamicPPL.tilde_assume!!)(
__context__,
$(DynamicPPL.unwrap_right_vn)(
$(DynamicPPL.check_tilde_rhs)($right), $vn
Expand All @@ -397,7 +397,7 @@ function generate_tilde(left, right)
$left = $(DynamicPPL.getvalue_nested)(__context__, $vn)
end

$(DynamicPPL.tilde_observe!)(
_, __varinfo__ = $(DynamicPPL.tilde_observe!!)(
__context__,
$(DynamicPPL.check_tilde_rhs)($right),
$(maybe_view(left)),
Expand All @@ -418,7 +418,7 @@ function generate_dot_tilde(left, right)
# If the LHS is a literal, it is always an observation
if isliteral(left)
return quote
$(DynamicPPL.dot_tilde_observe!)(
_, __varinfo__ = $(DynamicPPL.dot_tilde_observe!!)(
__context__, $(DynamicPPL.check_tilde_rhs)($right), $left, __varinfo__
)
end
Expand All @@ -432,7 +432,7 @@ function generate_dot_tilde(left, right)
$inds = $(vinds(left))
$isassumption = $(DynamicPPL.isassumption(left))
if $isassumption
$left .= $(DynamicPPL.dot_tilde_assume!)(
_, __varinfo__ = $(DynamicPPL.dot_tilde_assume!!)(
__context__,
$(DynamicPPL.unwrap_right_left_vns)(
$(DynamicPPL.check_tilde_rhs)($right), $(maybe_view(left)), $vn
Expand All @@ -446,7 +446,7 @@ function generate_dot_tilde(left, right)
$left .= $(DynamicPPL.getvalue_nested)(__context__, $vn)
end

$(DynamicPPL.dot_tilde_observe!)(
_, __varinfo__ = $(DynamicPPL.dot_tilde_observe!!)(
__context__,
$(DynamicPPL.check_tilde_rhs)($right),
$(maybe_view(left)),
Expand All @@ -458,6 +458,108 @@ function generate_dot_tilde(left, right)
end
end

"""
isfuncdef(expr)

Return `true` if `expr` is any form of function definition, and `false` otherwise.
"""
function isfuncdef(e::Expr)
return if Meta.isexpr(e, :function)
# Classic `function f(...)`
true
elseif Meta.isexpr(e, :->)
# Anonymous functions/lambdas, e.g. `do` blocks or `->` defs.
true
elseif Meta.isexpr(e, :(=)) && Meta.isexpr(e.args[1], :call)
# Short function defs, e.g. `f(args...) = ...`.
true
else
false
end
end

"""
replace_returns(expr)

Return `Expr` with all `return ...` statements replaced with
`return ..., DynamicPPL.return_values(__varinfo__)`.

Note that this method will _not_ replace `return` statements within function
definitions. This is checked using [`isfuncdef`](@ref).
"""
replace_returns(e) = e
replace_returns(e::Symbol) = e
function replace_returns(e::Expr)
if isfuncdef(e)
return e
end

if Meta.isexpr(e, :return)
# NOTE: `return` always has an argument. In the case of
# `return`, the parsed expression will be `return nothing`.
# Hence we don't need any special handling for empty returns.
retval_expr = if length(e.args) > 1
Expr(:tuple, e.args...)
else
e.args[1]
end

return :(return $(DynamicPPL.return_values)($retval_expr, __varinfo__))
end

return Expr(e.head, map(replace_returns, e.args)...)
end

"""
return_values(retval, varinfo)

Return `(retval, varinfo)` if `retval` is not a `Tuple` with second
component being a `AbstractVarInfo`.

Used together with [`replace_returns`](@ref), it handles the following case.

# Example

Suppose the following is the return-value:

```julia
return x ~ Normal()
```

Without `return_values`, once expanded in [`generated_mainbody!`](@ref), this would be

```julia
return (x, __varinfo__ = tilde_assume!!(...)), __varinfo__
```

i.e. the return-value of the model would end up `(x, __varinfo__), __varinfo__`
which in turn would lead to a `(::Model)(args...)` call returning `(x, __varinfo__)`,
breaking with the expectation of the user.

In such a scenario `return_values` effectively results in the following

```julia
return x, __varinfo__ = tilde_assume!!(...)
```

preserving user expectation, as desired.
"""
return_values(retval, varinfo::AbstractVarInfo) = (retval, varinfo)
return_values(retval::Tuple{<:Any,<:AbstractVarInfo}, ::AbstractVarInfo) = retval

# If it's just a symbol, e.g. `f(x) = 1`, then we make it `f(x) = return 1`.
make_returns_explicit!(body) = Expr(:return, body)
function make_returns_explicit!(body::Expr)
# If the last statement is a return-statement, we don't do anything.
if Meta.isexpr(body.args[end], :return)
return body
end

# Otherwise we replace the last statement with a `return` statement.
body.args[end] = Expr(:return, body.args[end])
return body
end

const FloatOrArrayType = Type{<:Union{AbstractFloat,AbstractArray}}
hasmissing(T::Type{<:AbstractArray{TA}}) where {TA<:AbstractArray} = hasmissing(TA)
hasmissing(T::Type{<:AbstractArray{>:Missing}}) = true
Expand Down Expand Up @@ -489,7 +591,11 @@ function build_output(modelinfo, linenumbernode)
evaluatordef[:kwargs] = []

# Replace the user-provided function body with the version created by DynamicPPL.
evaluatordef[:body] = modelinfo[:body]
# NOTE: We need to replace statements of the form `return ...` with
# `return DynamicPPL.return_values(..., __varinfo__)` to ensure that the second
# element in the returned value is always the most up-to-date `__varinfo__`.
# See the docstrings of `replace_returns` and `return_values` for more info.
evaluatordef[:body] = replace_returns(make_returns_explicit!(modelinfo[:body]))

## Build the model function.

Expand Down
Loading