Skip to content

Turing.Inference.Transition may fail for varinfos with non-default accumulators #2703

@penelopeysm

Description

@penelopeysm

In here:

function Transition(
model::DynamicPPL.Model, vi::AbstractVarInfo, stats; reevaluate=true
)
# Avoid mutating vi as it may be used later e.g. when constructing
# sampler states.
vi = deepcopy(vi)
if reevaluate
vi = DynamicPPL.setaccs!!(
vi,
(
DynamicPPL.ValuesAsInModelAccumulator(true),
DynamicPPL.LogPriorAccumulator(),
DynamicPPL.LogLikelihoodAccumulator(),
),
)
_, vi = DynamicPPL.evaluate!!(model, vi)
end
# Extract all the information we need
vals_as_in_model = DynamicPPL.getacc(vi, Val(:ValuesAsInModel)).values
logprior = DynamicPPL.getlogprior(vi)
loglikelihood = DynamicPPL.getloglikelihood(vi)

getlogprior(vi) will fail if vi does not have a LogPriorAccumulator.

This cannot happen right now because vi is always constructed by Turing (there is no way to change what varinfo is used for sampling .... welll .... technically it is possible with default_varinfo(); but generally no). So it is not possible for this error to ever occur.

However, it's not future proof, and it's generally not ideal. For example, in principle one might want to sample with Prior() but without a likelihood accumulator (after all it's just prior sampling). But Prior then uses reevaluate=false, which means that if the varinfo doesn't have a likelihood acc, it will fail at getloglikelihood.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions