Skip to content

Commit

Permalink
ADTypes Interop (#127)
Browse files Browse the repository at this point in the history
* ADTypes interop

* Bump patch
  • Loading branch information
willtebbutt authored Apr 30, 2024
1 parent d807de1 commit 0f7976c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name = "Tapir"
uuid = "07d77754-e150-4737-8c94-cd238a1fb45b"
authors = ["Will Tebbutt, Hong Ge, and contributors"]
version = "0.2.1"
version = "0.2.2"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
DiffRules = "b552c78f-8df3-52c6-915a-8e097449b14b"
DiffTests = "de460e47-3fe3-5279-bb4a-814414816d5d"
Expand All @@ -25,6 +26,7 @@ TapirLogDensityProblemsADExt = "LogDensityProblemsAD"
TapirSpecialFunctionsExt = "SpecialFunctions"

[compat]
ADTypes = "1"
BenchmarkTools = "1"
ChainRulesCore = "1"
DiffRules = "1"
Expand All @@ -40,7 +42,7 @@ Setfield = "1"
SpecialFunctions = "2"
StableRNGs = "1"
TemporalGPs = "0.6"
Turing = "0.31"
Turing = "0.31.3"
julia = "1"

[extras]
Expand Down
23 changes: 15 additions & 8 deletions ext/TapirLogDensityProblemsADExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,42 @@
module TapirLogDensityProblemsADExt

if isdefined(Base, :get_extension)
using ADTypes
using LogDensityProblemsAD: ADGradientWrapper
import LogDensityProblemsAD: ADgradient, logdensity_and_gradient, dimension, logdensity
import Tapir
else
using ADTypes
using ..LogDensityProblemsAD: ADGradientWrapper
import ..LogDensityProblemsAD: ADgradient, logdensity_and_gradient, dimension, logdensity
import ..Tapir
end

struct TapirGradientLogDensity{Trule, L} <: ADGradientWrapper
rule::Trule
l::L
::L
end

dimension(∇l::TapirGradientLogDensity) = dimension(Tapir.primal(∇l.l))
dimension(∇l::TapirGradientLogDensity) = dimension(Tapir.primal(∇l.))

function logdensity(∇l::TapirGradientLogDensity, x::Vector{Float64})
return logdensity(Tapir.primal(∇l.l), x)
return logdensity(Tapir.primal(∇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}}
function ADgradient(::Val{:Tapir}, )
primal_sig = Tuple{typeof(logdensity), typeof(), Vector{Float64}}
rule = Tapir.build_rrule(Tapir.TapirInterpreter(), primal_sig)
return TapirGradientLogDensity(rule, Tapir.uninit_fcodual(l))
return TapirGradientLogDensity(rule, Tapir.uninit_fcodual())
end

Base.show(io::IO, ∇ℓ::TapirGradientLogDensity) = print(io, "Tapir AD wrapper for ", ∇ℓ.ℓ)
function Base.show(io::IO, ∇ℓ::TapirGradientLogDensity)
return print(io, "Tapir AD wrapper for ", Tapir.primal(∇ℓ.ℓ))
end

# We only test Tapir with `Float64`s at the minute, so make strong assumptions about the
# types supported in order to prevent silent errors.
Expand All @@ -46,10 +50,13 @@ 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))
y, pb!! = ∇l.rule(Tapir.zero_fcodual(logdensity), ∇l., Tapir.CoDual(x, dx))
@assert Tapir.primal(y) isa Float64
pb!!(1.0)
return Tapir.primal(y), dx
end

# Interop with ADTypes.
ADgradient(::ADTypes.AutoTapir, ℓ) = ADgradient(Val(:Tapir), ℓ)

end
1 change: 1 addition & 0 deletions src/Tapir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Tapir
const CC = Core.Compiler

using
ADTypes,
DiffRules,
ExprTools,
Graphs,
Expand Down
4 changes: 3 additions & 1 deletion test/integration_testing/logdensityproblemsad_interop.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using LogDensityProblemsAD
using ADTypes, LogDensityProblemsAD
using LogDensityProblemsAD: logdensity_and_gradient, capabilities, dimension, logdensity

# Copied over from LogDensityProblemsAD test suite.
Expand All @@ -19,4 +19,6 @@ test_gradient(x) = -2 .* x
@test isapprox(logdensity_and_gradient(∇l, x)[1], logdensity(TestLogDensity2(), x))
@test isapprox(logdensity_and_gradient(∇l, x)[2], test_gradient(x))
end

@test ADgradient(ADTypes.AutoTapir(), l) isa typeof(∇l)
end

2 comments on commit 0f7976c

@willtebbutt
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

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/105891

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.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

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:

git tag -a v0.2.2 -m "<description of version>" 0f7976c399522026475abccd03efa87fe2544759
git push origin v0.2.2

Please sign in to comment.