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

Fixed externalsampler #2089

Merged
merged 8 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Turing"
uuid = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"
version = "0.29.2"
version = "0.29.3"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand Down
14 changes: 5 additions & 9 deletions src/mcmc/Inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,15 @@
"""
externalsampler(sampler::AbstractSampler) = ExternalSampler(sampler)

"""
ESLogDensityFunction

A log density function for the External sampler.

"""
const ESLogDensityFunction{M<:Model,S<:Sampler{<:ExternalSampler},V<:AbstractVarInfo} = Turing.LogDensityFunction{V,M,<:DynamicPPL.DefaultContext}
function LogDensityProblems.logdensity(f::ESLogDensityFunction, x::NamedTuple)
function LogDensityProblems.logdensity(

Check warning on line 101 in src/mcmc/Inference.jl

View check run for this annotation

Codecov / codecov/patch

src/mcmc/Inference.jl#L101

Added line #L101 was not covered by tests
f::Turing.LogDensityFunction{<:AbstractVarInfo,<:Model,<:DynamicPPL.DefaultContext},
x::NamedTuple
)
Comment on lines +101 to +104
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is type piracy (I know it was already there 😉 - and BTW DynamicPPL.unflatten below is another instance of type piracy). In DynamicPPL, we explicitly decided to not add this overload: TuringLang/DynamicPPL.jl#501 Can we just remove it completely?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeaaah I'm aware of both and I don't like them 😕

It's done to be compatible with AdvancedMH.jl which allows you to specify the proposal distribution using NamedTuple 😕 So we can't just remove it without messing with this; we'd have to do some more work for that, unfortunately.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you go to that PR, you can see that it was closed it in favour of another commit that did basically the same thing 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh haha I missed that, I just remembered that the issue had been closed 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You okay with this leaving this for now @devmotion ? This PR is meant to fix existing code, so feel like maybe we should get this merged and maybe make an issue / separate PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's address this separately.

return DynamicPPL.logjoint(f.model, DynamicPPL.unflatten(f.varinfo, x))
end

# TODO: make a nicer `set_namedtuple!` and move these functions to DynamicPPL.
function DynamicPPL.unflatten(vi::TypedVarInfo, θ::NamedTuple)
function DynamicPPL.unflatten(vi::TypedVarInfo, θ::NamedTuple)

Check warning on line 109 in src/mcmc/Inference.jl

View check run for this annotation

Codecov / codecov/patch

src/mcmc/Inference.jl#L109

Added line #L109 was not covered by tests
set_namedtuple!(deepcopy(vi), θ)
return vi
end
Expand Down
6 changes: 3 additions & 3 deletions src/mcmc/abstractmcmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
function transition_to_turing(f::DynamicPPL.LogDensityFunction, transition)
# TODO: We should probably rename this `getparams` since it returns something
# very different from `Turing.Inference.getparams`.
θ = getparams(transition)
θ = getparams(f.model, transition)

Check warning on line 10 in src/mcmc/abstractmcmc.jl

View check run for this annotation

Codecov / codecov/patch

src/mcmc/abstractmcmc.jl#L10

Added line #L10 was not covered by tests
varinfo = DynamicPPL.unflatten(f.varinfo, θ)
return Transition(f.model, varinfo, transition)
end

# NOTE: Only thing that depends on the underlying sampler.
# Something similar should be part of AbstractMCMC at some point:
# https://github.com/TuringLang/AbstractMCMC.jl/pull/86
getparams(transition::AdvancedHMC.Transition) = transition.z.θ
getparams(::DynamicPPL.Model, transition::AdvancedHMC.Transition) = transition.z.θ

Check warning on line 18 in src/mcmc/abstractmcmc.jl

View check run for this annotation

Codecov / codecov/patch

src/mcmc/abstractmcmc.jl#L18

Added line #L18 was not covered by tests
getstats(transition::AdvancedHMC.Transition) = transition.stat

getparams(transition::AdvancedMH.Transition) = transition.params
getparams(::DynamicPPL.Model, transition::AdvancedMH.Transition) = transition.params

Check warning on line 21 in src/mcmc/abstractmcmc.jl

View check run for this annotation

Codecov / codecov/patch

src/mcmc/abstractmcmc.jl#L21

Added line #L21 was not covered by tests

getvarinfo(f::DynamicPPL.LogDensityFunction) = f.varinfo
getvarinfo(f::LogDensityProblemsAD.ADGradientWrapper) = getvarinfo(parent(f))
Expand Down
Loading