-
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.
- Loading branch information
Showing
3 changed files
with
31 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
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,25 +1,28 @@ | ||
@testset "Testing AD by comparing gradient using ForwardDiff and ReverseDiff for model $(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 "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) | ||
@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 | ||
) | ||
θ = identity.(varinfo[:]) | ||
logp, ref_grad = LogDensityProblems.logdensity_and_gradient(ad_forwarddiff_f, θ) | ||
# 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 | ||
@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 |