-
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.
Re-add ForwardDiffExt (including tests)
- Loading branch information
1 parent
05f1bce
commit 566257e
Showing
5 changed files
with
87 additions
and
44 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module DynamicPPLForwardDiffExtTests | ||
|
||
using DynamicPPL | ||
using ADTypes: AutoForwardDiff | ||
using ForwardDiff: ForwardDiff | ||
using Distributions: MvNormal | ||
using LinearAlgebra: I | ||
using Test: @test, @testset | ||
|
||
# get_chunksize(ad::AutoForwardDiff{chunk}) where {chunk} = chunk | ||
|
||
@testset "ForwardDiff tweak_adtype" begin | ||
MODEL_SIZE = 10 | ||
@model f() = x ~ MvNormal(zeros(MODEL_SIZE), I) | ||
model = f() | ||
varinfo = VarInfo(model) | ||
context = DefaultContext() | ||
|
||
@testset "Chunk size setting" for chunksize in (nothing, 0) | ||
base_adtype = AutoForwardDiff(; chunksize=chunksize) | ||
new_adtype = DynamicPPL.tweak_adtype(base_adtype, model, varinfo, context) | ||
@test new_adtype isa AutoForwardDiff{MODEL_SIZE} | ||
end | ||
|
||
@testset "Tag setting" begin | ||
base_adtype = AutoForwardDiff() | ||
new_adtype = DynamicPPL.tweak_adtype(base_adtype, model, varinfo, context) | ||
@test new_adtype.tag isa ForwardDiff.Tag{DynamicPPL.DynamicPPLTag} | ||
end | ||
end | ||
|
||
end |
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