Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tapir.jl Integration #86

Merged
merged 4 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading