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

(WIP) Add ExtendingTraceTranslator. #370

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
69 changes: 65 additions & 4 deletions src/inference/trace_translators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -764,20 +764,20 @@ end
argdiffs::Tuple = (),
new_obs::ChoiceMap = EmptyChoiceMap(),
q_fwd::GenerativeFunction,
q_fwd_args::Tuple = ())
q_fwd_args::Tuple = ())

Constructor for a simple extending trace translator.

Run the translator with:

(output_trace, log_weight) = translator(input_trace)
"""
@with_kw struct SimpleExtendingTraceTranslator
@with_kw struct SimpleExtendingTraceTranslator
p_new_args::Tuple = ()
argdiffs::Tuple = ()
new_obs::ChoiceMap = EmptyChoiceMap()
q_fwd::GenerativeFunction
q_fwd_args::Tuple = ()
q_fwd_args::Tuple = ()
end

function (translator::SimpleExtendingTraceTranslator)(prev_model_trace::Trace)
Expand All @@ -801,6 +801,67 @@ function (translator::SimpleExtendingTraceTranslator)(prev_model_trace::Trace)
return (new_model_trace, log_weight)
end

##################################
# ExtendingTraceTranslator #
##################################

"""
translator = ExtendingTraceTranslator(;
p_new_args::Tuple = (),
argdiffs::Tuple = (),
new_obs::ChoiceMap = EmptyChoiceMap(),
q_fwd::GenerativeFunction,
q_fwd_args::Tuple = (),
f::Union{TraceTransformDSLProgram,Nothing} = nothing)

Constructor for a extending trace translator.

Run the translator with:

(output_trace, log_weight) = translator(input_trace)
"""
@with_kw struct ExtendingTraceTranslator
p_new_args::Tuple = ()
argdiffs::Tuple = ()
new_obs::ChoiceMap = EmptyChoiceMap()
q_fwd::GenerativeFunction
q_fwd_args::Tuple = ()
f::Union{TraceTransformDSLProgram,Nothing} = nothing # a bijection
end

function (translator::ExtendingTraceTranslator)(prev_model_trace::Trace)

# simulate from auxiliary program
forward_proposal_trace = simulate(translator.q_fwd, (prev_model_trace, translator.q_fwd_args...,))
forward_proposal_score = get_score(forward_proposal_trace)

# transform forward proposal
if translator.f === nothing
constraints = get_choices(forward_proposal_trace)
else
first_pass_results =
run_first_pass(translator.f, forward_proposal_trace, nothing)
log_abs_determinant =
jacobian_correction(translator.f, forward_proposal_trace,
nothing, first_pass_results, nothing)
constraints = first_pass_results.constraints
end

# computing the new trace via update
constraints = merge(constraints, translator.new_obs)
(new_model_trace, log_model_weight, _, discard) = update(
prev_model_trace, translator.p_new_args,
translator.argdiffs, constraints)

if !isempty(discard)
@error("can only extend the trace with random choices, cannot remove random choices")
error("Invalid ExtendingTraceTranslator")
end

log_weight = log_model_weight - forward_proposal_score + log_abs_determinant
return (new_model_trace, log_weight)
end

############################
# SymmetricTraceTranslator #
############################
Expand Down Expand Up @@ -884,7 +945,7 @@ function (translator::SymmetricTraceTranslator{<:Function})(
forward_retval = get_retval(forward_trace)
(new_model_trace, backward_choices, log_weight) = translator.involution(
prev_model_trace, forward_choices, forward_retval, translator.q_args)
(backward_score, backward_retval) = assess(translator.q, (new_model_trace, translator.q_args...), backward_choices)
(backward_score, backward_retval) = assess(translator.q, (new_model_trace, translator.q_args...), backward_choices)

log_weight += (backward_score - forward_score)

Expand Down