|
| 1 | +struct TuringState{S,F} |
| 2 | + state::S |
| 3 | + logdensity::F |
| 4 | +end |
| 5 | + |
| 6 | +struct TuringTransition{T,NT<:NamedTuple,F<:AbstractFloat} |
| 7 | + θ::T |
| 8 | + lp::F |
| 9 | + stat::NT |
| 10 | +end |
| 11 | + |
| 12 | +function TuringTransition(vi::AbstractVarInfo, t) |
| 13 | + theta = tonamedtuple(vi) |
| 14 | + lp = getlogp(vi) |
| 15 | + return TuringTransition(theta, lp, getstats(t)) |
| 16 | +end |
| 17 | + |
| 18 | +metadata(t::TuringTransition) = merge((lp = t.lp,), t.stat) |
| 19 | +DynamicPPL.getlogp(t::TuringTransition) = t.lp |
| 20 | + |
| 21 | +state_to_turing(f::DynamicPPL.LogDensityFunction, state) = TuringState(state, f) |
| 22 | +function transition_to_turing(f::DynamicPPL.LogDensityFunction, transition) |
| 23 | + θ = getparams(transition) |
| 24 | + varinfo = DynamicPPL.unflatten(f.varinfo, θ) |
| 25 | + # TODO: `deepcopy` is overkill; make more efficient. |
| 26 | + varinfo = DynamicPPL.invlink!!(deepcopy(varinfo), f.model) |
| 27 | + return TuringTransition(varinfo, transition) |
| 28 | +end |
| 29 | + |
| 30 | +# NOTE: Only thing that depends on the underlying sampler. |
| 31 | +# Something similar should be part of AbstractMCMC at some point: |
| 32 | +# https://github.com/TuringLang/AbstractMCMC.jl/pull/86 |
| 33 | +getparams(transition::AdvancedHMC.Transition) = transition.z.θ |
| 34 | +getstats(transition::AdvancedHMC.Transition) = transition.stat |
| 35 | + |
| 36 | +getparams(transition::AdvancedMH.Transition) = transition.params |
| 37 | +getstats(transition) = NamedTuple() |
| 38 | + |
| 39 | +getvarinfo(f::DynamicPPL.LogDensityFunction) = f.varinfo |
| 40 | +getvarinfo(f::LogDensityProblemsAD.ADGradientWrapper) = getvarinfo(parent(f)) |
| 41 | + |
| 42 | +setvarinfo(f::DynamicPPL.LogDensityFunction, varinfo) = Setfield.@set f.varinfo = varinfo |
| 43 | +setvarinfo(f::LogDensityProblemsAD.ADGradientWrapper, varinfo) = setvarinfo(parent(f), varinfo) |
| 44 | + |
| 45 | +# TODO: Do we also support `resume`, etc? |
| 46 | +function AbstractMCMC.step( |
| 47 | + rng::Random.AbstractRNG, |
| 48 | + model::DynamicPPL.Model, |
| 49 | + sampler_wrapper::Sampler{<:ExternalSampler}; |
| 50 | + kwargs... |
| 51 | +) |
| 52 | + sampler = sampler_wrapper.alg.sampler |
| 53 | + |
| 54 | + # Create a log-density function with an implementation of the |
| 55 | + # gradient so we ensure that we're using the same AD backend as in Turing. |
| 56 | + f = LogDensityProblemsAD.ADgradient(DynamicPPL.LogDensityFunction(model)) |
| 57 | + |
| 58 | + # Link the varinfo. |
| 59 | + f = setvarinfo(f, DynamicPPL.link!!(getvarinfo(f), model)) |
| 60 | + |
| 61 | + # Then just call `AdvancedHMC.step` with the right arguments. |
| 62 | + transition_inner, state_inner = AbstractMCMC.step( |
| 63 | + rng, AbstractMCMC.LogDensityModel(f), sampler; kwargs... |
| 64 | + ) |
| 65 | + |
| 66 | + # Update the `state` |
| 67 | + return transition_to_turing(f, transition_inner), state_to_turing(f, state_inner) |
| 68 | +end |
| 69 | + |
| 70 | +function AbstractMCMC.step( |
| 71 | + rng::Random.AbstractRNG, |
| 72 | + model::DynamicPPL.Model, |
| 73 | + sampler_wrapper::Sampler{<:ExternalSampler}, |
| 74 | + state::TuringState; |
| 75 | + kwargs... |
| 76 | +) |
| 77 | + sampler = sampler_wrapper.alg.sampler |
| 78 | + f = state.logdensity |
| 79 | + |
| 80 | + # Then just call `AdvancedHMC.step` with the right arguments. |
| 81 | + transition_inner, state_inner = AbstractMCMC.step( |
| 82 | + rng, AbstractMCMC.LogDensityModel(f), sampler, state.state; kwargs... |
| 83 | + ) |
| 84 | + |
| 85 | + # Update the `state` |
| 86 | + return transition_to_turing(f, transition_inner), state_to_turing(f, state_inner) |
| 87 | +end |
0 commit comments