Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for LogDensityFunction #621

Merged
merged 6 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DynamicPPL"
uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8"
version = "0.27.2"
version = "0.27.3"
mhauru marked this conversation as resolved.
Show resolved Hide resolved

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
12 changes: 8 additions & 4 deletions src/logdensityfunction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct LogDensityFunction{V,M,C}
varinfo::V
"model used for evaluation"
model::M
"context used for evaluation"
"context used for evaluation; if `nothing`, `model.context` will be used when applicable"
context::C
end

Expand All @@ -66,11 +66,14 @@ end
function LogDensityFunction(
model::Model,
varinfo::AbstractVarInfo=VarInfo(model),
context::AbstractContext=model.context,
context::Union{Nothing,AbstractContext}=nothing,
)
return LogDensityFunction(varinfo, model, context)
end

# If a `context` has been specified, we use that. Otherwise we just use the leaf context of `model`.
getcontext(f::LogDensityFunction) = f.context === nothing ? leafcontext(f.model.context) : f.context
torfjelde marked this conversation as resolved.
Show resolved Hide resolved

# HACK: heavy usage of `AbstractSampler` for, well, _everything_, is being phased out. In the mean time
# we need to define these annoying methods to ensure that we stay compatible with everything.
getsampler(f::LogDensityFunction) = getsampler(f.context)
Expand All @@ -90,8 +93,9 @@ getparams(f::LogDensityFunction) = f.varinfo[_get_indexer(f.context)]

# LogDensityProblems interface
function LogDensityProblems.logdensity(f::LogDensityFunction, θ::AbstractVector)
vi_new = unflatten(f.varinfo, f.context, θ)
return getlogp(last(evaluate!!(f.model, vi_new, f.context)))
context = getcontext(f)
vi_new = unflatten(f.varinfo, context, θ)
return getlogp(last(evaluate!!(f.model, vi_new, context)))
end
function LogDensityProblems.capabilities(::Type{<:LogDensityFunction})
return LogDensityProblems.LogDensityOrder{0}()
Expand Down
Loading