-
Notifications
You must be signed in to change notification settings - Fork 35
Remove LogDensityProblemsAD; wrap adtype in LogDensityFunction #806
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0004dd0
Remove LogDensityProblemsAD
penelopeysm e8979b0
Implement LogDensityFunctionWithGrad in place of ADgradient
penelopeysm bb832ab
Dynamically decide whether to use closure vs constant
penelopeysm 74c9f12
Combine LogDensityFunction{,WithGrad} into one (#811)
penelopeysm 8d10fc1
Warn if unsupported AD type is used
penelopeysm e6513a0
Update changelog
penelopeysm 74fbad2
Update DI compat bound
penelopeysm f76bb3d
Don't store with_closure inside LogDensityFunction
penelopeysm 05f1bce
setadtype --> LogDensityFunction
penelopeysm 566257e
Re-add ForwardDiffExt (including tests)
penelopeysm 04f640d
Add more tests for coverage
penelopeysm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,40 @@ | ||
module DynamicPPLForwardDiffExt | ||
|
||
if isdefined(Base, :get_extension) | ||
using DynamicPPL: ADTypes, DynamicPPL, LogDensityProblems, LogDensityProblemsAD | ||
using ForwardDiff | ||
else | ||
using ..DynamicPPL: ADTypes, DynamicPPL, LogDensityProblems, LogDensityProblemsAD | ||
using ..ForwardDiff | ||
end | ||
|
||
getchunksize(::ADTypes.AutoForwardDiff{chunk}) where {chunk} = chunk | ||
|
||
standardtag(::ADTypes.AutoForwardDiff{<:Any,Nothing}) = true | ||
standardtag(::ADTypes.AutoForwardDiff) = false | ||
|
||
function LogDensityProblemsAD.ADgradient( | ||
ad::ADTypes.AutoForwardDiff, ℓ::DynamicPPL.LogDensityFunction | ||
) | ||
θ = DynamicPPL.getparams(ℓ) | ||
f = Base.Fix1(LogDensityProblems.logdensity, ℓ) | ||
|
||
# Define configuration for ForwardDiff. | ||
tag = if standardtag(ad) | ||
ForwardDiff.Tag(DynamicPPL.DynamicPPLTag(), eltype(θ)) | ||
using DynamicPPL: ADTypes, DynamicPPL, LogDensityProblems | ||
using ForwardDiff | ||
|
||
# check if the AD type already has a tag | ||
use_dynamicppl_tag(::ADTypes.AutoForwardDiff{<:Any,Nothing}) = true | ||
use_dynamicppl_tag(::ADTypes.AutoForwardDiff) = false | ||
|
||
function DynamicPPL.tweak_adtype( | ||
ad::ADTypes.AutoForwardDiff{chunk_size}, | ||
::DynamicPPL.Model, | ||
vi::DynamicPPL.AbstractVarInfo, | ||
::DynamicPPL.AbstractContext, | ||
) where {chunk_size} | ||
params = vi[:] | ||
|
||
# Use DynamicPPL tag to improve stack traces | ||
# https://www.stochasticlifestyle.com/improved-forwarddiff-jl-stacktraces-with-package-tags/ | ||
# NOTE: DifferentiationInterface disables tag checking if the | ||
# tag inside the AutoForwardDiff type is not nothing. See | ||
# https://github.com/JuliaDiff/DifferentiationInterface.jl/blob/1df562180bdcc3e91c885aa5f4162a0be2ced850/DifferentiationInterface/ext/DifferentiationInterfaceForwardDiffExt/onearg.jl#L338-L350. | ||
# So we don't currently need to override ForwardDiff.checktag as well. | ||
tag = if use_dynamicppl_tag(ad) | ||
ForwardDiff.Tag(DynamicPPL.DynamicPPLTag(), eltype(params)) | ||
else | ||
ForwardDiff.Tag(f, eltype(θ)) | ||
ad.tag | ||
end | ||
chunk_size = getchunksize(ad) | ||
|
||
# Optimise chunk size according to size of model | ||
chunk = if chunk_size == 0 || chunk_size === nothing | ||
ForwardDiff.Chunk(θ) | ||
ForwardDiff.Chunk(params) | ||
else | ||
ForwardDiff.Chunk(length(θ), chunk_size) | ||
ForwardDiff.Chunk(length(params), chunk_size) | ||
end | ||
|
||
return LogDensityProblemsAD.ADgradient(Val(:ForwardDiff), ℓ; chunk, tag, x=θ) | ||
end | ||
|
||
# Allow Turing tag in gradient etc. calls of the log density function | ||
function ForwardDiff.checktag( | ||
::Type{ForwardDiff.Tag{DynamicPPL.DynamicPPLTag,V}}, | ||
::DynamicPPL.LogDensityFunction, | ||
::AbstractArray{W}, | ||
) where {V,W} | ||
return true | ||
end | ||
function ForwardDiff.checktag( | ||
::Type{ForwardDiff.Tag{DynamicPPL.DynamicPPLTag,V}}, | ||
::Base.Fix1{typeof(LogDensityProblems.logdensity),<:DynamicPPL.LogDensityFunction}, | ||
::AbstractArray{W}, | ||
) where {V,W} | ||
return true | ||
return ADTypes.AutoForwardDiff(; chunksize=ForwardDiff.chunksize(chunk), tag=tag) | ||
end | ||
|
||
end # module |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.