-
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.
* initialize moving, still need to move tests * Move tests, tests are not fixed yet * Make `ADTypes` a direct dep * Add `ad.jl` for testing * Remove `ADTypes` ext from `require` * Put `ADgradient` code to extensions * Add testing code * Bug fix and adding tests * Update src/simple_varinfo.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * renaming a testset * add require for ReverseDiff extension * fix UUID * fix typo * Also use the original transformation * Fix 1.6 compat * Fix typo * Fix typo, again * Update test/ad.jl Co-authored-by: Tor Erlend Fjelde <[email protected]> * Fix errors * Refactor the test * Update ad.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * disable Zygote testing * Change testset description * Update test/ad.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Tor Erlend Fjelde <[email protected]> * Apply Tor's comments --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Tor Erlend Fjelde <[email protected]>
- Loading branch information
1 parent
c33eeae
commit b924a17
Showing
10 changed files
with
191 additions
and
24 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
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
b924a17
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
b924a17
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/100866
Tip: Release Notes
Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.
To add them here just re-invoke and the PR will be updated.
Tagging
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: