-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LogDensityProblemsAD + fix CI bug (#123)
* Make temporalgps run in CI * LogDensityProblemsAD interface * Update test/runtests.jl * Remove ADTypes test for now * Sort out ordering in Project.toml * Bump patch --------- Co-authored-by: Hong Ge <[email protected]>
- Loading branch information
1 parent
965b98b
commit fd12ee7
Showing
5 changed files
with
87 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# This file is largely copy + pasted + modified from the Zygote extension in | ||
# LogDensityProblemsAD.jl. | ||
|
||
module TapirLogDensityProblemsADExt | ||
|
||
if isdefined(Base, :get_extension) | ||
using LogDensityProblemsAD: ADGradientWrapper | ||
import LogDensityProblemsAD: ADgradient, logdensity_and_gradient, dimension, logdensity | ||
import Tapir | ||
else | ||
using ..LogDensityProblemsAD: ADGradientWrapper | ||
import ..LogDensityProblemsAD: ADgradient, logdensity_and_gradient, dimension, logdensity | ||
import ..Tapir | ||
end | ||
|
||
struct TapirGradientLogDensity{Trule, L} <: ADGradientWrapper | ||
rule::Trule | ||
l::L | ||
end | ||
|
||
dimension(∇l::TapirGradientLogDensity) = dimension(Tapir.primal(∇l.l)) | ||
|
||
function logdensity(∇l::TapirGradientLogDensity, x::Vector{Float64}) | ||
return logdensity(Tapir.primal(∇l.l), x) | ||
end | ||
|
||
""" | ||
ADgradient(Val(:Tapir), ℓ) | ||
Gradient using algorithmic/automatic differentiation via Tapir. | ||
""" | ||
function ADgradient(::Val{:Tapir}, l) | ||
primal_sig = Tuple{typeof(logdensity), typeof(l), Vector{Float64}} | ||
rule = Tapir.build_rrule(Tapir.TapirInterpreter(), primal_sig) | ||
return TapirGradientLogDensity(rule, Tapir.uninit_fcodual(l)) | ||
end | ||
|
||
Base.show(io::IO, ∇ℓ::TapirGradientLogDensity) = print(io, "Tapir AD wrapper for ", ∇ℓ.ℓ) | ||
|
||
# We only test Tapir with `Float64`s at the minute, so make strong assumptions about the | ||
# types supported in order to prevent silent errors. | ||
function logdensity_and_gradient(::TapirGradientLogDensity, ::AbstractVector) | ||
msg = "Only Vector{Float64} presently supported for logdensity_and_gradients." | ||
throw(ArgumentError(msg)) | ||
end | ||
|
||
function logdensity_and_gradient(∇l::TapirGradientLogDensity, x::Vector{Float64}) | ||
dx = zeros(length(x)) | ||
y, pb!! = ∇l.rule(Tapir.zero_fcodual(logdensity), ∇l.l, Tapir.CoDual(x, dx)) | ||
@assert Tapir.primal(y) isa Float64 | ||
pb!!(1.0) | ||
return Tapir.primal(y), dx | ||
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,22 @@ | ||
using LogDensityProblemsAD | ||
using LogDensityProblemsAD: logdensity_and_gradient, capabilities, dimension, logdensity | ||
|
||
# Copied over from LogDensityProblemsAD test suite. | ||
struct TestLogDensity2 end | ||
LogDensityProblemsAD.logdensity(::TestLogDensity2, x) = -sum(abs2, x) | ||
LogDensityProblemsAD.dimension(::TestLogDensity2) = 20 | ||
test_gradient(x) = -2 .* x | ||
|
||
@testset "AD via Zygote" begin | ||
l = TestLogDensity2() | ||
∇l = ADgradient(Val(:Tapir), l) | ||
|
||
@test dimension(∇l) == 20 | ||
@test capabilities(∇l) == LogDensityProblemsAD.LogDensityOrder(1) | ||
for _ in 1:100 | ||
x = randn(20) | ||
@test isapprox(@inferred(logdensity(∇l, x)), logdensity(l, x)) | ||
@test isapprox(logdensity_and_gradient(∇l, x)[1], logdensity(TestLogDensity2(), x)) | ||
@test isapprox(logdensity_and_gradient(∇l, x)[2], test_gradient(x)) | ||
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
fd12ee7
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()
fd12ee7
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/105847
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: