-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove LogDensityProblemsAD; wrap adtype in LogDensityFunction (#806)
* Remove LogDensityProblemsAD * Implement LogDensityFunctionWithGrad in place of ADgradient * Dynamically decide whether to use closure vs constant * Combine LogDensityFunction{,WithGrad} into one (#811) * Warn if unsupported AD type is used * Update changelog * Update DI compat bound Co-authored-by: Guillaume Dalle <[email protected]> * Don't store with_closure inside LogDensityFunction Co-authored-by: Guillaume Dalle <[email protected]> * setadtype --> LogDensityFunction * Re-add ForwardDiffExt (including tests) * Add more tests for coverage --------- Co-authored-by: Guillaume Dalle <[email protected]>
- Loading branch information
1 parent
f5e84f4
commit 90c7b26
Showing
13 changed files
with
420 additions
and
187 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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.