Skip to content

Commit

Permalink
Tapir.jl Integration (#86)
Browse files Browse the repository at this point in the history
* Initial pass at adding Tapir jl

* Prevent Tapir from being installed on v1.7

* Using Pkg in tests

* Update ext/AdvancedVITapirExt.jl

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
willtebbutt and github-actions[bot] authored Sep 3, 2024
1 parent d015eb5 commit 87730a0
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 16 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "AdvancedVI"
uuid = "b5ca4192-6429-45e5-a2d9-87aec30a685c"
version = "0.3.0"
version = "0.3.1"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand All @@ -26,12 +26,14 @@ Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
Tapir = "07d77754-e150-4737-8c94-cd238a1fb45b"

[extensions]
AdvancedVIBijectorsExt = "Bijectors"
AdvancedVIEnzymeExt = "Enzyme"
AdvancedVIForwardDiffExt = "ForwardDiff"
AdvancedVIReverseDiffExt = "ReverseDiff"
AdvancedVITapirExt = "Tapir"
AdvancedVIZygoteExt = "Zygote"

[compat]
Expand Down
37 changes: 37 additions & 0 deletions ext/AdvancedVITapirExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module AdvancedVITapirExt

if isdefined(Base, :get_extension)
using AdvancedVI
using AdvancedVI: ADTypes, DiffResults
using Tapir
else
using ..AdvancedVI
using ..AdvancedVI: ADTypes, DiffResults
using ..Tapir
end

function AdvancedVI.value_and_gradient!(
::ADTypes.AutoTapir, f, x::AbstractVector{<:Real}, out::DiffResults.MutableDiffResult
)
rule = Tapir.build_rrule(f, x)
y, g = Tapir.value_and_gradient!!(rule, f, x)
DiffResults.value!(out, y)
DiffResults.gradient!(out, last(g))
return out
end

function AdvancedVI.value_and_gradient!(
::ADTypes.AutoTapir,
f,
x::AbstractVector{<:Real},
aux,
out::DiffResults.MutableDiffResult,
)
rule = Tapir.build_rrule(f, x, aux)
y, g = Tapir.value_and_gradient!!(rule, f, x, aux)
DiffResults.value!(out, y)
DiffResults.gradient!(out, g[2])
return out
end

end
4 changes: 4 additions & 0 deletions test/inference/repgradelbo_distributionsad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ else
)
end

if @isdefined(Tapir)
AD_distributionsad[:Tapir] = AutoTapir(; safe_mode=false)
end

@testset "inference RepGradELBO DistributionsAD" begin
@testset "$(modelname) $(objname) $(realtype) $(adbackname)" for realtype in
[Float64, Float32],
Expand Down
5 changes: 5 additions & 0 deletions test/inference/repgradelbo_locationscale.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ AD_locationscale = if VERSION >= v"1.10"
:ReverseDiff => AutoReverseDiff(),
:Zygote => AutoZygote(),
:Enzyme => AutoEnzyme(),
:Tapir => AutoTapir(; safe_mode=false),
)
else
Dict(
Expand All @@ -14,6 +15,10 @@ else
)
end

if @isdefined(Tapir)
AD_locationscale[:Tapir] = AutoTapir(; safe_mode=false)
end

@testset "inference RepGradELBO VILocationScale" begin
@testset "$(modelname) $(objname) $(realtype) $(adbackname)" for realtype in
[Float64, Float32],
Expand Down
5 changes: 5 additions & 0 deletions test/inference/repgradelbo_locationscale_bijectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ AD_locationscale_bijectors = if VERSION >= v"1.10"
:ReverseDiff => AutoReverseDiff(),
:Zygote => AutoZygote(),
:Enzyme => AutoEnzyme(),
:Tapir => AutoTapir(; safe_mode=false),
)
else
Dict(
Expand All @@ -14,6 +15,10 @@ else
)
end

if @isdefined(Tapir)
AD_locationscale_bijectors[:Tapir] = AutoTapir(; safe_mode=false)
end

@testset "inference RepGradELBO VILocationScale Bijectors" begin
@testset "$(modelname) $(objname) $(realtype) $(adbackname)" for realtype in
[Float64, Float32],
Expand Down
28 changes: 14 additions & 14 deletions test/interface/ad.jl
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@

using Test

const interface_ad_backends = Dict(
:ForwardDiff => AutoForwardDiff(),
:ReverseDiff => AutoReverseDiff(),
:Zygote => AutoZygote(),
:Enzyme => AutoEnzyme(),
)
if @isdefined(Tapir)
interface_ad_backends[:Tapir] = AutoTapir(; safe_mode=false)
end

@testset "ad" begin
@testset "$(adname)" for (adname, adsymbol) in Dict(
:ForwardDiff => AutoForwardDiff(),
:ReverseDiff => AutoReverseDiff(),
:Zygote => AutoZygote(),
:Enzyme => AutoEnzyme(),
)
@testset "$(adname)" for (adname, adtype) in interface_ad_backends
D = 10
A = randn(D, D)
λ = randn(D)
grad_buf = DiffResults.GradientResult(λ)
f(λ′) = λ′' * A * λ′ / 2
AdvancedVI.value_and_gradient!(adsymbol, f, λ, grad_buf)
AdvancedVI.value_and_gradient!(adtype, f, λ, grad_buf)
= DiffResults.gradient(grad_buf)
f = DiffResults.value(grad_buf)
@test (A + A') * λ / 2
@test f λ' * A * λ / 2
end

@testset "$(adname) with auxiliary input" for (adname, adsymbol) in Dict(
:ForwardDiff => AutoForwardDiff(),
:ReverseDiff => AutoReverseDiff(),
:Zygote => AutoZygote(),
:Enzyme => AutoEnzyme(),
)
@testset "$(adname) with auxiliary input" for (adname, adtype) in interface_ad_backends
D = 10
A = randn(D, D)
λ = randn(D)
b = randn(D)
grad_buf = DiffResults.GradientResult(λ)
f(λ′, aux) = λ′' * A * λ′ / 2 + dot(aux.b, λ′)
AdvancedVI.value_and_gradient!(adsymbol, f, λ, (b=b,), grad_buf)
AdvancedVI.value_and_gradient!(adtype, f, λ, (b=b,), grad_buf)
= DiffResults.gradient(grad_buf)
f = DiffResults.value(grad_buf)
@test (A + A') * λ / 2 + b
Expand Down
7 changes: 6 additions & 1 deletion test/interface/repgradelbo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ end
modelstats = normal_meanfield(rng, Float64)
@unpack model, μ_true, L_true, n_dims, is_meanfield = modelstats

@testset for ad in [
ad_backends = [
ADTypes.AutoForwardDiff(),
ADTypes.AutoReverseDiff(),
ADTypes.AutoZygote(),
ADTypes.AutoEnzyme(),
]
if @isdefined(Tapir)
push!(ad_backends, AutoTapir(; safe_mode=false))
end

@testset for ad in ad_backends
q_true = MeanFieldGaussian(
Vector{eltype(μ_true)}(μ_true), Diagonal(Vector{eltype(L_true)}(diag(L_true)))
)
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using Distributions
using FillArrays
using LinearAlgebra
using PDMats
using Pkg
using Random, StableRNGs
using SimpleUnPack: @unpack
using Statistics
Expand All @@ -22,6 +23,11 @@ using Optimisers
using ADTypes
using ForwardDiff, ReverseDiff, Zygote, Enzyme

if VERSION >= v"1.10"
Pkg.add("Tapir")
using Tapir
end

using AdvancedVI

const GROUP = get(ENV, "GROUP", "All")
Expand Down

4 comments on commit 87730a0

@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.

Error while trying to register: Register Failed
@willtebbutt, it looks like you are not a publicly listed member/owner in the parent organization (TuringLang).
If you are a member/owner, you will need to change your membership to public. See GitHub Help

@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/114397

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.3.1 -m "<description of version>" 87730a0d34dad5e4d0ccccb511828cc7061bd553
git push origin v0.3.1

Also, note the warning: Version 0.3.1 skips over 0.3.0
This can be safely ignored. However, if you want to fix this you can do so. Call register() again after making the fix. This will update the Pull request.

Please sign in to comment.