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

Test with Tapir #2289

Merged
merged 22 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
5 changes: 4 additions & 1 deletion test/mcmc/Inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module InferenceTests

using ..Models: gdemo_d, gdemo_default
using ..NumericalTests: check_gdemo, check_numerical
import ..ADUtils
using Distributions: Bernoulli, Beta, InverseGamma, Normal
using Distributions: sample
import DynamicPPL
Expand All @@ -14,7 +15,9 @@ import ReverseDiff
using Test: @test, @test_throws, @testset
using Turing

@testset "Testing inference.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(; compile=false))
ADUtils.install_tapir && import Tapir

@testset "Testing inference.jl with $adbackend" for adbackend in ADUtils.adbackends
# Only test threading if 1.3+.
if VERSION > v"1.2"
@testset "threaded sampling" begin
Expand Down
6 changes: 4 additions & 2 deletions test/mcmc/abstractmcmc.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module AbstractMCMCTests

import ..ADUtils
using AdvancedMH: AdvancedMH
using Distributions: sample
using Distributions.FillArrays: Zeros
Expand All @@ -15,6 +16,8 @@ using Test: @test, @test_throws, @testset
using Turing
using Turing.Inference: AdvancedHMC

ADUtils.install_tapir && import Tapir

function initialize_nuts(model::Turing.Model)
# Create a log-density function with an implementation of the
# gradient so we ensure that we're using the same AD backend as in Turing.
Expand Down Expand Up @@ -112,8 +115,7 @@ end

@testset "External samplers" begin
@testset "AdvancedHMC.jl" begin
# Try a few different AD backends.
@testset "adtype=$adtype" for adtype in [AutoForwardDiff(), AutoReverseDiff()]
@testset "adtype=$adtype" for adtype in ADUtils.adbackends
mhauru marked this conversation as resolved.
Show resolved Hide resolved
@testset "$(model.f)" for model in DynamicPPL.TestUtils.DEMO_MODELS
# Need some functionality to initialize the sampler.
# TODO: Remove this once the constructors in the respective packages become "lazy".
Expand Down
7 changes: 4 additions & 3 deletions test/mcmc/gibbs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module GibbsTests

using ..Models: MoGtest_default, gdemo, gdemo_default
using ..NumericalTests: check_MoGtest_default, check_gdemo, check_numerical
import ..ADUtils
using Distributions: InverseGamma, Normal
using Distributions: sample
using ForwardDiff: ForwardDiff
Expand All @@ -12,9 +13,9 @@ using Turing
using Turing: Inference
using Turing.RandomMeasures: ChineseRestaurantProcess, DirichletProcess

@testset "Testing gibbs.jl with $adbackend" for adbackend in (
AutoForwardDiff(; chunksize=0), AutoReverseDiff(; compile=false)
)
ADUtils.install_tapir && import Tapir

@testset "Testing gibbs.jl with $adbackend" for adbackend in ADUtils.adbackends
@testset "gibbs constructor" begin
N = 500
s1 = Gibbs(HMC(0.1, 5, :s, :m; adtype=adbackend))
Expand Down
7 changes: 4 additions & 3 deletions test/mcmc/gibbs_conditional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module GibbsConditionalTests

using ..Models: gdemo, gdemo_default
using ..NumericalTests: check_gdemo, check_numerical
import ..ADUtils
using Clustering: Clustering
using Distributions: Categorical, InverseGamma, Normal, sample
using ForwardDiff: ForwardDiff
Expand All @@ -14,9 +15,9 @@ using StatsFuns: StatsFuns
using Test: @test, @testset
using Turing

@testset "Testing gibbs conditionals.jl with $adbackend" for adbackend in (
AutoForwardDiff(; chunksize=0), AutoReverseDiff(; compile=false)
)
ADUtils.install_tapir && import Tapir

@testset "Testing gibbs conditionals.jl with $adbackend" for adbackend in ADUtils.adbackends
Random.seed!(1000)
rng = StableRNG(123)

Expand Down
5 changes: 4 additions & 1 deletion test/mcmc/hmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module HMCTests
using ..Models: gdemo_default
#using ..Models: gdemo
using ..NumericalTests: check_gdemo, check_numerical
import ..ADUtils
using Distributions: Bernoulli, Beta, Categorical, Dirichlet, Normal, Wishart, sample
import DynamicPPL
using DynamicPPL: Sampler
Expand All @@ -16,7 +17,9 @@ using StatsFuns: logistic
using Test: @test, @test_logs, @testset
using Turing

@testset "Testing hmc.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(; compile=false))
ADUtils.install_tapir && import Tapir

@testset "Testing hmc.jl with $adbackend" for adbackend in ADUtils.adbackends
# Set a seed
rng = StableRNG(123)
@testset "constrained bounded" begin
Expand Down
7 changes: 5 additions & 2 deletions test/mcmc/sghmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module SGHMCTests

using ..Models: gdemo_default
using ..NumericalTests: check_gdemo
import ..ADUtils
using Distributions: sample
import ForwardDiff
using LinearAlgebra: dot
Expand All @@ -10,7 +11,9 @@ using StableRNGs: StableRNG
using Test: @test, @testset
using Turing

@testset "Testing sghmc.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(; compile=false))
ADUtils.install_tapir && import Tapir

@testset "Testing sghmc.jl with $adbackend" for adbackend in ADUtils.adbackends
@testset "sghmc constructor" begin
alg = SGHMC(; learning_rate=0.01, momentum_decay=0.1, adtype=adbackend)
@test alg isa SGHMC
Expand All @@ -36,7 +39,7 @@ using Turing
end
end

@testset "Testing sgld.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(; compile=false))
@testset "Testing sgld.jl with $adbackend" for adbackend in ADUtils.adbackends
@testset "sgld constructor" begin
alg = SGLD(; stepsize=PolynomialStepsize(0.25), adtype=adbackend)
@test alg isa SGLD
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Turing

include(pkgdir(Turing) * "/test/test_utils/models.jl")
include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl")
include(pkgdir(Turing) * "/test/test_utils/ad_utils.jl")

Turing.setprogress!(false)

Expand Down
21 changes: 21 additions & 0 deletions test/test_utils/ad_utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module ADUtils

import Pkg
import Turing

"""
All the ADTypes on which we want to run the tests.
"""
adbackends = [
Turing.AutoForwardDiff(; chunksize=0), Turing.AutoReverseDiff(; compile=false)
mhauru marked this conversation as resolved.
Show resolved Hide resolved
]

# Tapir isn't supported for older Julia versions, hence the check.
install_tapir = isdefined(Turing, :AutoTapir)
if install_tapir
# TODO(mhauru) Is there a better way to install optional dependencies like this?
Pkg.add("Tapir")
yebai marked this conversation as resolved.
Show resolved Hide resolved
push!(adbackends, Turing.AutoTapir(false))
end

end
Loading