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

Add acclogp_assume!! and acclogp_observe!! #565

Merged
merged 6 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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.24.2"
version = "0.24.3"
torfjelde marked this conversation as resolved.
Show resolved Hide resolved

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand Down
18 changes: 14 additions & 4 deletions src/context_implementations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ alg_str(spl::Sampler) = string(nameof(typeof(spl.alg)))
require_gradient(spl::Sampler) = false
require_particles(spl::Sampler) = false

# Allows samplers, etc. to hook into the final logp accumulation in the tilde-pipeline.
function acclogp_assume!!(context::AbstractContext, vi::AbstractVarInfo, logp)
return acclogp!!(context, vi, logp)
end

function acclogp_observe!!(context::AbstractContext, vi::AbstractVarInfo, logp)
return acclogp!!(context, vi, logp)
end

# assume
"""
tilde_assume(context::SamplingContext, right, vn, vi)
Expand Down Expand Up @@ -115,7 +124,7 @@ probability of `vi` with the returned value.
"""
function tilde_assume!!(context, right, vn, vi)
value, logp, vi = tilde_assume(context, right, vn, vi)
return value, acclogp!!(context, vi, logp)
return value, acclogp_assume!!(context, vi, logp)
end

# observe
Expand Down Expand Up @@ -181,7 +190,7 @@ probability of `vi` with the returned value.
"""
function tilde_observe!!(context, right, left, vi)
logp, vi = tilde_observe(context, right, left, vi)
return left, acclogp!!(context, vi, logp)
return left, acclogp_observe!!(context, vi, logp)
end

function assume(rng, spl::Sampler, dist)
Expand Down Expand Up @@ -383,7 +392,7 @@ Falls back to `dot_tilde_assume(context, right, left, vn, vi)`.
"""
function dot_tilde_assume!!(context, right, left, vn, vi)
value, logp, vi = dot_tilde_assume(context, right, left, vn, vi)
return value, acclogp!!(context, vi, logp), vi
return value, acclogp_assume!!(context, vi, logp), vi
end

# `dot_assume`
Expand Down Expand Up @@ -539,6 +548,7 @@ function get_and_set_val!(
if istrans(vi)
push!!.((vi,), vns, reconstruct_and_link.((vi,), vns, dists, r), dists, (spl,))
# NOTE: Need to add the correction.
# FIXME: This is not great.
acclogp!!(vi, sum(logabsdetjac.(bijector.(dists), r)))
torfjelde marked this conversation as resolved.
Show resolved Hide resolved
# `push!!` sets the trans-flag to `false` by default.
settrans!!.((vi,), true, vns)
Expand Down Expand Up @@ -634,7 +644,7 @@ Falls back to `dot_tilde_observe(context, right, left, vi)`.
"""
function dot_tilde_observe!!(context, right, left, vi)
logp, vi = dot_tilde_observe(context, right, left, vi)
return left, acclogp!!(context, vi, logp)
return left, acclogp_observe!!(context, vi, logp)
end

# Falls back to non-sampler definition.
Expand Down
Loading