-
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.
Merge branch 'master' into compathelper/new_version/2024-01-26-00-08-…
…39-774-02165247943
- Loading branch information
Showing
11 changed files
with
202 additions
and
21 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
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(θ)) | ||
else | ||
ForwardDiff.Tag(f, eltype(θ)) | ||
end | ||
chunk_size = getchunksize(ad) | ||
chunk = if chunk_size == 0 | ||
ForwardDiff.Chunk(θ) | ||
else | ||
ForwardDiff.Chunk(length(θ), 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 | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module DynamicPPLReverseDiffExt | ||
|
||
if isdefined(Base, :get_extension) | ||
using DynamicPPL: ADTypes, DynamicPPL, LogDensityProblems, LogDensityProblemsAD | ||
using ReverseDiff | ||
else | ||
using ..DynamicPPL: ADTypes, DynamicPPL, LogDensityProblems, LogDensityProblemsAD | ||
using ..ReverseDiff | ||
end | ||
|
||
function LogDensityProblemsAD.ADgradient( | ||
ad::ADTypes.AutoReverseDiff, ℓ::DynamicPPL.LogDensityFunction | ||
) | ||
return LogDensityProblemsAD.ADgradient( | ||
Val(:ReverseDiff), | ||
ℓ; | ||
compile=Val(ad.compile), | ||
# `getparams` can return `Vector{Real}`, in which case, `ReverseDiff` will initialize the gradients to Integer 0 | ||
# because at https://github.com/JuliaDiff/ReverseDiff.jl/blob/c982cde5494fc166965a9d04691f390d9e3073fd/src/tracked.jl#L473 | ||
# `zero(D)` will return 0 when D is Real. | ||
# here we use `identity` to possibly concretize the type to `Vector{Float64}` in the case of `Vector{Real}`. | ||
x=map(identity, DynamicPPL.getparams(ℓ)), | ||
) | ||
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
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
@testset "AD: ForwardDiff and ReverseDiff" begin | ||
@testset "$(m.f)" for m in DynamicPPL.TestUtils.DEMO_MODELS | ||
f = DynamicPPL.LogDensityFunction(m) | ||
rand_param_values = DynamicPPL.TestUtils.rand_prior_true(m) | ||
vns = DynamicPPL.TestUtils.varnames(m) | ||
varinfos = DynamicPPL.TestUtils.setup_varinfos(m, rand_param_values, vns) | ||
|
||
@testset "$(short_varinfo_name(varinfo))" for varinfo in varinfos | ||
f = DynamicPPL.LogDensityFunction(m, varinfo) | ||
|
||
# use ForwardDiff result as reference | ||
ad_forwarddiff_f = LogDensityProblemsAD.ADgradient( | ||
ADTypes.AutoForwardDiff(; chunksize=0), f | ||
) | ||
# convert to `Vector{Float64}` to avoid `ReverseDiff` initializing the gradients to Integer 0 | ||
# reference: https://github.com/TuringLang/DynamicPPL.jl/pull/571#issuecomment-1924304489 | ||
θ = convert(Vector{Float64}, varinfo[:]) | ||
logp, ref_grad = LogDensityProblems.logdensity_and_gradient(ad_forwarddiff_f, θ) | ||
|
||
@testset "ReverseDiff with compile=$compile" for compile in (false, true) | ||
adtype = ADTypes.AutoReverseDiff(; compile=compile) | ||
ad_f = LogDensityProblemsAD.ADgradient(adtype, f) | ||
_, grad = LogDensityProblems.logdensity_and_gradient(ad_f, θ) | ||
@test grad ≈ ref_grad | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
@testset "tag" begin | ||
for chunksize in (0, 1, 10) | ||
ad = ADTypes.AutoForwardDiff(; chunksize=chunksize) | ||
standardtag = if !isdefined(Base, :get_extension) | ||
DynamicPPL.DynamicPPLForwardDiffExt.standardtag | ||
else | ||
Base.get_extension(DynamicPPL, :DynamicPPLForwardDiffExt).standardtag | ||
end | ||
@test standardtag(ad) | ||
for tag in (false, 0, 1) | ||
@test !standardtag(AutoForwardDiff(; chunksize=chunksize, tag=tag)) | ||
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