From 15ffdc1c35de13ae72fb21c30b74c53695012640 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Thu, 23 May 2024 11:15:35 +0100 Subject: [PATCH 01/29] Remove unused test util function --- test/test_utils/testing_functions.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/test_utils/testing_functions.jl b/test/test_utils/testing_functions.jl index 4f00d7793..f73de928f 100644 --- a/test/test_utils/testing_functions.jl +++ b/test/test_utils/testing_functions.jl @@ -20,7 +20,3 @@ function randr(vi::Turing.VarInfo, return vi[vn] end end - -function insdelim(c, deli=",") - return reduce((e, res) -> append!(e, [res, deli]), c; init = [])[1:end-1] -end From 91956d5d2ffe71f3b9a146eb8cb30677fe1dfaf2 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Thu, 23 May 2024 12:06:02 +0100 Subject: [PATCH 02/29] Refactoring ad and optiminterface tests to modules --- test/essential/ad.jl | 26 ++++++++++++++-- test/optimisation/OptimInterface.jl | 20 +++++++++++- test/runtests.jl | 48 ++--------------------------- 3 files changed, 45 insertions(+), 49 deletions(-) diff --git a/test/essential/ad.jl b/test/essential/ad.jl index 09655ad54..788bf5c8d 100644 --- a/test/essential/ad.jl +++ b/test/essential/ad.jl @@ -1,5 +1,24 @@ +module AdTests + +using LinearAlgebra +using Test: @testset, @test + +using ForwardDiff +using ReverseDiff +using Distributions: logpdf +import LogDensityProblems +import LogDensityProblemsAD +using Zygote + +using Turing +using DynamicPPL: getval, getlogp +using Turing: SampleFromPrior + +include(pkgdir(Turing)*"/test/test_utils/models.jl") +include(pkgdir(Turing)*"/test/test_utils/ad_utils.jl") + @testset "ad.jl" begin - @turing_testset "adr" begin + @testset "adr" begin ad_test_f = gdemo_default vi = Turing.VarInfo(ad_test_f) ad_test_f(vi, SampleFromPrior()) @@ -50,7 +69,8 @@ ∇E2 = LogDensityProblems.logdensity_and_gradient(zygoteℓ, x)[2] @test sort(∇E2) ≈ grad_FWAD atol = 1e-9 end - @turing_testset "general AD tests" begin + + @testset "general AD tests" begin # Tests gdemo gradient. function logp1(x::Vector) dist_s = InverseGamma(2, 3) @@ -179,3 +199,5 @@ @test ℓ_grad == ℓ_grad_compiled end end + +end diff --git a/test/optimisation/OptimInterface.jl b/test/optimisation/OptimInterface.jl index ba9e2f78e..ae5f1365a 100644 --- a/test/optimisation/OptimInterface.jl +++ b/test/optimisation/OptimInterface.jl @@ -1,3 +1,19 @@ +module OptimInterfaceTests + +using LinearAlgebra: I +using Random: Random +using Test: @testset, @test + +using Optim: Optim +using Optim: optimize, NelderMead, LBFGS +using StatsBase: StatsBase +using StatsBase: vcov, coef, coeftable, coefnames, informationmatrix, stderror +using Distributions.FillArrays: Zeros + +using Turing + +include(pkgdir(Turing)*"/test/test_utils/models.jl") + # Used for testing how well it works with nested contexts. struct OverrideContext{C,T1,T2} <: DynamicPPL.AbstractContext context::C @@ -22,7 +38,7 @@ function DynamicPPL.tilde_observe(context::OverrideContext, right, left, vi) return context.loglikelihood_weight, vi end -@numerical_testset "OptimInterface.jl" begin +@testset "OptimInterface.jl" begin @testset "MLE" begin Random.seed!(222) true_value = [0.0625, 1.75] @@ -245,3 +261,5 @@ end @test result.values ≈ mode(Dirichlet(2 * ones(3))) atol=0.2 end end + +end diff --git a/test/runtests.jl b/test/runtests.jl index 8f9db1d1d..deac16ad4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,54 +1,10 @@ -using AbstractMCMC -using AdvancedMH -using AdvancedPS -using Clustering -using Distributions -using Distributions.FillArrays -using DistributionsAD -using FiniteDifferences -using ForwardDiff -using MCMCChains -using NamedArrays -using Optim -using Optimization -using OptimizationOptimJL -using PDMats -using ReverseDiff -using SpecialFunctions -using StatsBase -using StatsFuns -using HypothesisTests -using Tracker -using Turing -using Turing.Inference -using Turing.RandomMeasures -using Zygote - -using LinearAlgebra using Pkg -using Random using Test -using StableRNGs - -using AdvancedPS: ResampleWithESSThreshold, resample_systematic, resample_multinomial -using AdvancedVI: TruncatedADAGrad, DecayedADAGrad, apply! -using Distributions: Binomial, logpdf -using DynamicPPL: getval, getlogp -using ForwardDiff: Dual -using MCMCChains: Chains -using StatsFuns: binomlogpdf, logistic, logsumexp using TimerOutputs: TimerOutputs, @timeit -using Turing: BinomialLogit, Sampler, SampleFromPrior, NUTS, - Variational, getspace -using Turing.Essential: TuringDenseMvNormal, TuringDiagMvNormal -using Turing.Variational: TruncatedADAGrad, DecayedADAGrad, AdvancedVI - -import LogDensityProblems -import LogDensityProblemsAD -setprogress!(false) +using Turing: Turing -include(pkgdir(Turing)*"/test/test_utils/AllUtils.jl") +Turing.setprogress!(false) # Collect timing and allocations information to show in a clear way. const TIMEROUTPUT = TimerOutputs.TimerOutput() From 58c65615a87dedad1dc20fba46f7503981185eb9 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Tue, 28 May 2024 12:05:14 +0100 Subject: [PATCH 03/29] Add SelectiveTests.jl --- test/runtests.jl | 18 ++++++++++++++++-- test/test_utils/SelectiveTests.jl | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 test/test_utils/SelectiveTests.jl diff --git a/test/runtests.jl b/test/runtests.jl index deac16ad4..d8ad6454c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -6,9 +6,23 @@ using Turing: Turing Turing.setprogress!(false) -# Collect timing and allocations information to show in a clear way. +include("test_utils/SelectiveTests.jl") +using .SelectiveTests: parse_args, isincluded + +included_paths, excluded_paths = parse_args(ARGS) + +# Filter which tests to run and collect timing and allocations information to show in a +# clear way. const TIMEROUTPUT = TimerOutputs.TimerOutput() -macro timeit_include(path::AbstractString) :(@timeit TIMEROUTPUT $path include($path)) end +macro timeit_include(path::AbstractString) + return quote + if isincluded($path, included_paths, excluded_paths) + @timeit TIMEROUTPUT $path include($path) + else + println("Skipping tests in $($path)") + end + end +end @testset "Turing" begin @testset "essential" begin diff --git a/test/test_utils/SelectiveTests.jl b/test/test_utils/SelectiveTests.jl new file mode 100644 index 000000000..c9726f845 --- /dev/null +++ b/test/test_utils/SelectiveTests.jl @@ -0,0 +1,27 @@ +module SelectiveTests + +function parse_args(args) + included_paths = Vector{String}() + excluded_paths = Vector{String}() + for (i, arg) in enumerate(args) + if arg == "--skip" + append!(excluded_paths, args[i+1:end]) + break + else + push!(included_paths, arg) + end + end + return included_paths, excluded_paths +end + +function isincluded(filepath, included_paths, excluded_paths) + if any(excl -> occursin(lowercase(excl), lowercase(filepath)), excluded_paths) + return false + end + if any(incl -> occursin(lowercase(incl), lowercase(filepath)), included_paths) + return true + end + return isempty(included_paths) +end + +end From 3097f497bec4f6b13d69b123bc9d5589e19beb12 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Wed, 29 May 2024 14:23:28 +0100 Subject: [PATCH 04/29] Rework tests CI GA --- .github/workflows/TuringCI.yml | 39 ++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/.github/workflows/TuringCI.yml b/.github/workflows/TuringCI.yml index 88cc27bcb..c230b105b 100644 --- a/.github/workflows/TuringCI.yml +++ b/.github/workflows/TuringCI.yml @@ -1,4 +1,4 @@ -name: Turing-CI +name: Tests on: push: @@ -10,8 +10,18 @@ jobs: test: runs-on: ${{ matrix.os }} continue-on-error: ${{ matrix.version == 'nightly' }} + strategy: matrix: + test-args: + # Run some of the slower test files individually. The last one catches everything + # not included in the others. + - "mcmc/gibbs.jl" + - "mcmc/hmc.jl" + - "mcmc/Inference.jl" + - "experimental/gibbs.jl" + - "mcmc/ess.jl" + - "--skip mcmc/gibbs.jl mcmc/hmc.jl mcmc/Inference.jl experimental/gibbs.jl mcmc/ess.jl" version: - '1.7' - '1' @@ -33,15 +43,19 @@ jobs: num_threads: 2 - version: '1.7' os: macOS-latest - arch: x64 + arch: aarch64 num_threads: 2 + + env: + DO_COVERAGE: ${{ matrix.version == '1' && matrix.os == 'ubuntu-latest' && matrix.num_threads == 1 }} + steps: - - uses: actions/checkout@v2 - - uses: julia-actions/setup-julia@v1 + - uses: actions/checkout@v4 + - uses: julia-actions/setup-julia@v2 with: version: ${{ matrix.version }} arch: ${{ matrix.arch }} - - uses: actions/cache@v1 + - uses: actions/cache@v4 env: cache-name: cache-artifacts with: @@ -52,19 +66,16 @@ jobs: ${{ runner.os }}-test- ${{ runner.os }}- - uses: julia-actions/julia-buildpkg@latest - - uses: julia-actions/julia-runtest@latest - with: - coverage: ${{ matrix.version == '1.6' && matrix.os == 'ubuntu-latest' && matrix.num_threads == 1 }} - env: - JULIA_NUM_THREADS: ${{ matrix.num_threads }} + - name: Call Pkg.test + run: julia --color=yes --depwarn=yes --check-bounds=yes --threads=${{ matrix.num_threads }} --project=@. -e 'import Pkg; coverage=parse(Bool, "${{ env.DO_COVERAGE }}"); Pkg.test(; coverage=coverage)' - uses: julia-actions/julia-processcoverage@v1 - if: matrix.version == '1.7' && matrix.os == 'ubuntu-latest' && matrix.num_threads == 1 - - uses: codecov/codecov-action@v1 - if: matrix.version == '1.7' && matrix.os == 'ubuntu-latest' && matrix.num_threads == 1 + if: env.DO_COVERAGE + - uses: codecov/codecov-action@v3 + if: env.DO_COVERAGE with: file: lcov.info - uses: coverallsapp/github-action@master - if: matrix.version == '1.7' && matrix.os == 'ubuntu-latest' && matrix.num_threads == 1 + if: env.DO_COVERAGE with: github-token: ${{ secrets.GITHUB_TOKEN }} path-to-lcov: lcov.info From 00bd2e0bd3a75e6a63ef2c934d4c4fc24445d3b8 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Wed, 29 May 2024 14:32:03 +0100 Subject: [PATCH 05/29] Switch test CI on Mac back to x64 --- .github/workflows/TuringCI.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/TuringCI.yml b/.github/workflows/TuringCI.yml index c230b105b..a69013228 100644 --- a/.github/workflows/TuringCI.yml +++ b/.github/workflows/TuringCI.yml @@ -43,7 +43,7 @@ jobs: num_threads: 2 - version: '1.7' os: macOS-latest - arch: aarch64 + arch: x64 num_threads: 2 env: @@ -53,7 +53,7 @@ jobs: - uses: actions/checkout@v4 - uses: julia-actions/setup-julia@v2 with: - version: ${{ matrix.version }} + version: '${{ matrix.version }}' arch: ${{ matrix.arch }} - uses: actions/cache@v4 env: From e40456d081eaccb7034fe4bdbe41c80270547e92 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Wed, 29 May 2024 15:28:17 +0100 Subject: [PATCH 06/29] Remove coverage from CI. Improve matrix. --- .github/workflows/TuringCI.yml | 50 ++++++++++++++++------------------ 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/.github/workflows/TuringCI.yml b/.github/workflows/TuringCI.yml index a69013228..650e69b1e 100644 --- a/.github/workflows/TuringCI.yml +++ b/.github/workflows/TuringCI.yml @@ -27,27 +27,34 @@ jobs: - '1' os: - ubuntu-latest + - windows-latest + - macOS-latest arch: - x64 + - x86 num_threads: - 1 - 2 - include: - - version: '1.7' - os: ubuntu-latest + exclude: + # With Windows and macOS, only run Julia 1.7, x64, 2 threads. We just want to see + # some combination work on OSes other than Ubuntu. + - os: windows-latest + version: '1.0' + - os: macOS-latest + version: '1.0' + - os: windows-latest + arch: x86 + - os: macOS-latest + arch: x86 + - os: windows-latest + num_threads: 1 + - os: macOS-latest + num_threads: 1 + # It's sufficient to test x86 with one version of Julia and one thread. + - version: '1.0' + arch: x86 + - num_threads: 2 arch: x86 - num_threads: 2 - - version: '1.7' - os: windows-latest - arch: x64 - num_threads: 2 - - version: '1.7' - os: macOS-latest - arch: x64 - num_threads: 2 - - env: - DO_COVERAGE: ${{ matrix.version == '1' && matrix.os == 'ubuntu-latest' && matrix.num_threads == 1 }} steps: - uses: actions/checkout@v4 @@ -67,15 +74,4 @@ jobs: ${{ runner.os }}- - uses: julia-actions/julia-buildpkg@latest - name: Call Pkg.test - run: julia --color=yes --depwarn=yes --check-bounds=yes --threads=${{ matrix.num_threads }} --project=@. -e 'import Pkg; coverage=parse(Bool, "${{ env.DO_COVERAGE }}"); Pkg.test(; coverage=coverage)' - - uses: julia-actions/julia-processcoverage@v1 - if: env.DO_COVERAGE - - uses: codecov/codecov-action@v3 - if: env.DO_COVERAGE - with: - file: lcov.info - - uses: coverallsapp/github-action@master - if: env.DO_COVERAGE - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - path-to-lcov: lcov.info + run: julia --color=yes --depwarn=yes --check-bounds=yes --threads=${{ matrix.num_threads }} --project=@. -e 'import Pkg; Pkg.test(; test_args="${{ matrix.test-args }}")' From b9c94bea954ce43a14201dbbc7d1f5e170279137 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Wed, 29 May 2024 15:29:34 +0100 Subject: [PATCH 07/29] Wrap all tests in modules. --- test/experimental/gibbs.jl | 14 +++++++- test/ext/dynamichmc.jl | 18 +++++++++- test/mcmc/Inference.jl | 19 +++++++++++ test/mcmc/abstractmcmc.jl | 21 ++++++++++-- test/mcmc/emcee.jl | 15 ++++++++ test/mcmc/ess.jl | 20 +++++++++-- test/mcmc/gibbs.jl | 24 ++++++++++--- test/mcmc/gibbs_conditional.jl | 26 ++++++++++++-- test/mcmc/hmc.jl | 57 +++++++++++++++++++++---------- test/mcmc/is.jl | 18 ++++++++-- test/mcmc/mh.jl | 40 +++++++++++++++++----- test/mcmc/particle_mcmc.jl | 32 ++++++++++++----- test/mcmc/sghmc.jl | 23 ++++++++++--- test/mcmc/utilities.jl | 16 +++++++++ test/stdlib/RandomMeasures.jl | 13 +++++++ test/stdlib/distributions.jl | 26 +++++++++++--- test/test_utils/SelectiveTests.jl | 34 +++++++++++++++++- test/variational/optimisers.jl | 15 ++++++++ 18 files changed, 370 insertions(+), 61 deletions(-) diff --git a/test/experimental/gibbs.jl b/test/experimental/gibbs.jl index 1fb27e705..bae49ae97 100644 --- a/test/experimental/gibbs.jl +++ b/test/experimental/gibbs.jl @@ -1,4 +1,14 @@ -using Test, Random, Turing, DynamicPPL +module ExperimentalGibbsTests + +using Random +using Test + +using DynamicPPL + +using Turing + +include(pkgdir(Turing)*"/test/test_utils/models.jl") +include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") function check_transition_varnames( transition::Turing.Inference.Transition, @@ -187,3 +197,5 @@ end check_MoGtest_default_z_vector(chain, atol = 0.2) end end + +end diff --git a/test/ext/dynamichmc.jl b/test/ext/dynamichmc.jl index 61027a196..1264c9cd6 100644 --- a/test/ext/dynamichmc.jl +++ b/test/ext/dynamichmc.jl @@ -1,5 +1,19 @@ +module DynamicHMCTests + +using Test: @testset, @test +using Random: Random + +using Distributions: sample +using DynamicPPL: DynamicPPL +using DynamicPPL: Sampler +using DynamicHMC: DynamicHMC + +using Turing + +include(pkgdir(Turing)*"/test/test_utils/models.jl") +include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") + @testset "TuringDynamicHMCExt" begin - import DynamicHMC Random.seed!(100) @test DynamicPPL.alg_str(Sampler(externalsampler(DynamicHMC.NUTS()))) == "DynamicNUTS" @@ -8,3 +22,5 @@ chn = sample(gdemo_default, spl, 10_000) check_gdemo(chn) end + +end diff --git a/test/mcmc/Inference.jl b/test/mcmc/Inference.jl index e1d0f9e96..12ce5d7c7 100644 --- a/test/mcmc/Inference.jl +++ b/test/mcmc/Inference.jl @@ -1,3 +1,20 @@ +module InferenceTests + +using LinearAlgebra: I +using Test: @testset, @test, @test_throws +using Random: Random + +using Distributions: sample +using Distributions: Bernoulli, Beta, Normal, InverseGamma +using DynamicPPL: DynamicPPL +using DynamicPPL: Sampler, getlogp +using MCMCChains: MCMCChains + +using Turing + +include(pkgdir(Turing)*"/test/test_utils/models.jl") +include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") + @testset "Testing inference.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false)) # Only test threading if 1.3+. if VERSION > v"1.2" @@ -544,3 +561,5 @@ @test all(xs[:, 2] .=== [missing, 2, 4]) end end + +end diff --git a/test/mcmc/abstractmcmc.jl b/test/mcmc/abstractmcmc.jl index 378b22fba..db58c1262 100644 --- a/test/mcmc/abstractmcmc.jl +++ b/test/mcmc/abstractmcmc.jl @@ -1,3 +1,18 @@ +module AbstractMCMCTests + +using LinearAlgebra: I +using Test: @testset, @test, @test_throws +using Random: Random + +using Distributions: sample +using Distributions.FillArrays: Zeros +using StableRNGs: StableRNG +using LogDensityProblems: LogDensityProblems +using LogDensityProblemsAD: LogDensityProblemsAD +using AdvancedMH: AdvancedMH +using DynamicPPL: DynamicPPL + +using Turing using Turing.Inference: AdvancedHMC function initialize_nuts(model::Turing.Model) @@ -83,7 +98,7 @@ function test_initial_params(model, sampler, initial_params=DynamicPPL.VarInfo(m end @testset "External samplers" begin - @turing_testset "AdvancedHMC.jl" begin + @testset "AdvancedHMC.jl" begin # Try a few different AD backends. @testset "adtype=$adtype" for adtype in [AutoForwardDiff(), AutoReverseDiff()] @testset "$(model.f)" for model in DynamicPPL.TestUtils.DEMO_MODELS @@ -135,7 +150,7 @@ end end end - @turing_testset "AdvancedMH.jl" begin + @testset "AdvancedMH.jl" begin @testset "RWMH" begin @testset "$(model.f)" for model in DynamicPPL.TestUtils.DEMO_MODELS # Need some functionality to initialize the sampler. @@ -181,3 +196,5 @@ end # end end end + +end diff --git a/test/mcmc/emcee.jl b/test/mcmc/emcee.jl index 929506f95..a1aa2fea4 100644 --- a/test/mcmc/emcee.jl +++ b/test/mcmc/emcee.jl @@ -1,3 +1,16 @@ +module EmceeTests + +using Test: @testset, @test, @test_throws +using Random: Random +using DynamicPPL: DynamicPPL +using DynamicPPL: Sampler +using Distributions: sample + +using Turing + +include(pkgdir(Turing)*"/test/test_utils/models.jl") +include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") + @testset "emcee.jl" begin @testset "gdemo" begin Random.seed!(9876) @@ -41,3 +54,5 @@ @test chain[:m] == fill(1.0, 1, nwalkers) end end + +end diff --git a/test/mcmc/ess.jl b/test/mcmc/ess.jl index a11068656..e1ae64e38 100644 --- a/test/mcmc/ess.jl +++ b/test/mcmc/ess.jl @@ -1,3 +1,17 @@ +module ESSTests + +using Test: @testset, @test +using Distributions: Normal +using Distributions: sample +using Random: Random +using DynamicPPL: DynamicPPL +using DynamicPPL: Sampler + +using Turing + +include(pkgdir(Turing)*"/test/test_utils/models.jl") +include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") + @testset "ESS" begin @model function demo(x) m ~ Normal() @@ -12,7 +26,7 @@ end demodot_default = demodot(1.0) - @turing_testset "ESS constructor" begin + @testset "ESS constructor" begin Random.seed!(0) N = 500 @@ -31,7 +45,7 @@ c5 = sample(gdemo_default, s3, N) end - @numerical_testset "ESS inference" begin + @testset "ESS inference" begin Random.seed!(1) chain = sample(demo_default, ESS(), 5_000) check_numerical(chain, [:m], [0.8], atol = 0.1) @@ -71,3 +85,5 @@ ) end end + +end diff --git a/test/mcmc/gibbs.jl b/test/mcmc/gibbs.jl index ef2299dca..a14600df3 100644 --- a/test/mcmc/gibbs.jl +++ b/test/mcmc/gibbs.jl @@ -1,5 +1,19 @@ +module GibbsTests + +using Test: @testset, @test +using Distributions: InverseGamma, Normal +using Distributions: sample +using Random: Random + +using Turing +using Turing: Inference +using Turing.RandomMeasures: DirichletProcess, ChineseRestaurantProcess + +include(pkgdir(Turing)*"/test/test_utils/models.jl") +include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") + @testset "Testing gibbs.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false)) - @turing_testset "gibbs constructor" begin + @testset "gibbs constructor" begin N = 500 s1 = Gibbs(HMC(0.1, 5, :s, :m; adtype=adbackend)) s2 = Gibbs(PG(10, :s, :m)) @@ -30,7 +44,7 @@ # it should return a Chains object @test sample(gdemo_default, g, N) isa MCMCChains.Chains end - @numerical_testset "gibbs inference" begin + @testset "gibbs inference" begin Random.seed!(100) alg = Gibbs(CSMC(15, :s), HMC(0.2, 4, :m; adtype=adbackend)) chain = sample(gdemo(1.5, 2.0), alg, 10_000) @@ -65,7 +79,7 @@ end end - @turing_testset "transitions" begin + @testset "transitions" begin @model function gdemo_copy() s ~ InverseGamma(2, 3) m ~ Normal(0, sqrt(s)) @@ -96,7 +110,7 @@ alg = Gibbs(MH(:s), HMC(0.2, 4, :m; adtype=adbackend)) sample(model, alg, 100; callback = callback) end - @turing_testset "dynamic model" begin + @testset "dynamic model" begin @model function imm(y, alpha, ::Type{M}=Vector{Float64}) where {M} N = length(y) rpm = DirichletProcess(alpha) @@ -122,3 +136,5 @@ sample(model, Gibbs(PG(10, :z), HMC(0.01, 4, :m; adtype=adbackend)), 100) end end + +end diff --git a/test/mcmc/gibbs_conditional.jl b/test/mcmc/gibbs_conditional.jl index d7752da64..1f72bd570 100644 --- a/test/mcmc/gibbs_conditional.jl +++ b/test/mcmc/gibbs_conditional.jl @@ -1,7 +1,25 @@ -@turing_testset "Testing gibbs conditionals.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false)) +module GibbsConditionalTests + +using LinearAlgebra: Diagonal, I +using Random: Random +using Test: @testset, @test + +using Distributions: Categorical, InverseGamma, Normal +using Distributions: sample +using StatsFuns: StatsFuns +using StatsBase: counts +using Clustering: Clustering +using StableRNGs: StableRNG + +using Turing + +include(pkgdir(Turing)*"/test/test_utils/models.jl") +include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") + +@testset "Testing gibbs conditionals.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false)) Random.seed!(1000); rng = StableRNG(123) - @turing_testset "gdemo" begin + @testset "gdemo" begin # We consider the model # ```math # s ~ InverseGamma(2, 3) @@ -61,7 +79,7 @@ check_gdemo(chain) end - @turing_testset "GMM" begin + @testset "GMM" begin Random.seed!(1000); rng = StableRNG(123) # We consider the model # ```math @@ -145,3 +163,5 @@ end end end + +end diff --git a/test/mcmc/hmc.jl b/test/mcmc/hmc.jl index 755fa4b45..fad763628 100644 --- a/test/mcmc/hmc.jl +++ b/test/mcmc/hmc.jl @@ -1,7 +1,26 @@ +module HMCTests + +using LinearAlgebra: I, dot, vec +using Test: @testset, @test, @test_logs +using Random: Random + +using Distributions: sample +using Distributions: Bernoulli, Beta, Categorical, Dirichlet, Normal, Wishart +using StableRNGs: StableRNG +using StatsFuns: logistic +using HypothesisTests: ApproximateTwoSampleKSTest, pvalue +using DynamicPPL: DynamicPPL +using DynamicPPL: Sampler + +using Turing + +include(pkgdir(Turing)*"/test/test_utils/models.jl") +include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") + @testset "Testing hmc.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false)) # Set a seed rng = StableRNG(123) - @numerical_testset "constrained bounded" begin + @testset "constrained bounded" begin obs = [0,1,0,1,1,1,1,1,1,1] @model function constrained_test(obs) @@ -20,7 +39,7 @@ check_numerical(chain, [:p], [10/14], atol=0.1) end - @numerical_testset "constrained simplex" begin + @testset "constrained simplex" begin obs12 = [1,2,1,2,2,2,2,2,2,2] @model function constrained_simplex_test(obs12) @@ -40,12 +59,12 @@ check_numerical(chain, ["ps[1]", "ps[2]"], [5/16, 11/16], atol=0.015) end - @numerical_testset "hmc reverse diff" begin + @testset "hmc reverse diff" begin alg = HMC(0.1, 10; adtype=adbackend) res = sample(rng, gdemo_default, alg, 4000) check_gdemo(res, rtol=0.1) end - @turing_testset "matrix support" begin + @testset "matrix support" begin @model function hmcmatrixsup() v ~ Wishart(7, [1 0.5; 0.5 1]) end @@ -60,7 +79,7 @@ @test maximum(abs, mean(vs) - (7 * [1 0.5; 0.5 1])) <= 0.5 end - @turing_testset "multivariate support" begin + @testset "multivariate support" begin # Define NN flow function nn(x, b1, w11, w12, w13, bo, wo) h = tanh.([w11 w12 w13]' * x .+ b1) @@ -106,7 +125,7 @@ chain = sample(rng, bnn(ts), HMC(0.1, 5; adtype=adbackend), 10) end - @numerical_testset "hmcda inference" begin + @testset "hmcda inference" begin alg1 = HMCDA(500, 0.8, 0.015; adtype=adbackend) # alg2 = Gibbs(HMCDA(200, 0.8, 0.35, :m; adtype=adbackend), HMC(0.25, 3, :s; adtype=adbackend)) @@ -122,7 +141,7 @@ # @test mean(res2[:m]) ≈ 7/6 atol=0.2 end - @numerical_testset "hmcda+gibbs inference" begin + @testset "hmcda+gibbs inference" begin rng = StableRNG(123) Random.seed!(12345) # particle samplers do not support user-provided `rng` yet alg3 = Gibbs(PG(20, :s), HMCDA(500, 0.8, 0.25, :m; init_ϵ=0.05, adtype=adbackend)) @@ -131,7 +150,7 @@ check_gdemo(res3) end - @turing_testset "hmcda constructor" begin + @testset "hmcda constructor" begin alg = HMCDA(0.8, 0.75; adtype=adbackend) println(alg) sampler = Sampler(alg, gdemo_default) @@ -150,12 +169,12 @@ @test isa(alg, HMCDA) @test isa(sampler, Sampler{<:Turing.Hamiltonian}) end - @numerical_testset "nuts inference" begin + @testset "nuts inference" begin alg = NUTS(1000, 0.8; adtype=adbackend) res = sample(rng, gdemo_default, alg, 6000) check_gdemo(res) end - @turing_testset "nuts constructor" begin + @testset "nuts constructor" begin alg = NUTS(200, 0.65; adtype=adbackend) sampler = Sampler(alg, gdemo_default) @test DynamicPPL.alg_str(sampler) == "NUTS" @@ -168,7 +187,7 @@ sampler = Sampler(alg, gdemo_default) @test DynamicPPL.alg_str(sampler) == "NUTS" end - @turing_testset "check discard" begin + @testset "check discard" begin alg = NUTS(100, 0.8; adtype=adbackend) c1 = sample(rng, gdemo_default, alg, 500, discard_adapt=true) @@ -177,7 +196,7 @@ @test size(c1, 1) == 500 @test size(c2, 1) == 500 end - @turing_testset "AHMC resize" begin + @testset "AHMC resize" begin alg1 = Gibbs(PG(10, :m), NUTS(100, 0.65, :s; adtype=adbackend)) alg2 = Gibbs(PG(10, :m), HMC(0.1, 3, :s; adtype=adbackend)) alg3 = Gibbs(PG(10, :m), HMCDA(100, 0.65, 0.3, :s; adtype=adbackend)) @@ -186,7 +205,7 @@ @test sample(rng, gdemo_default, alg3, 300) isa Chains end - @turing_testset "Regression tests" begin + @testset "Regression tests" begin # https://github.com/TuringLang/DynamicPPL.jl/issues/27 @model function mwe1(::Type{T}=Float64) where {T<:Real} m = Matrix{T}(undef, 2, 3) @@ -209,7 +228,7 @@ end # issue #1923 - @turing_testset "reproducibility" begin + @testset "reproducibility" begin alg = NUTS(1000, 0.8; adtype=adbackend) res1 = sample(StableRNG(123), gdemo_default, alg, 1000) res2 = sample(StableRNG(123), gdemo_default, alg, 1000) @@ -217,7 +236,7 @@ @test Array(res1) == Array(res2) == Array(res3) end - @turing_testset "prior" begin + @testset "prior" begin @model function demo_hmc_prior() # NOTE: Used to use `InverseGamma(2, 3)` but this has infinite variance # which means that it's _very_ difficult to find a good tolerance in the test below:) @@ -230,7 +249,7 @@ check_numerical(chain, [:s, :m], [mean(truncated(Normal(3, 1); lower=0)), 0], atol=0.2) end - @turing_testset "warning for difficult init params" begin + @testset "warning for difficult init params" begin attempt = 0 @model function demo_warn_initial_params() x ~ Normal() @@ -250,7 +269,7 @@ # Disable on Julia <1.8 due to https://github.com/TuringLang/Turing.jl/pull/2197. # TODO: Remove this block once https://github.com/JuliaFolds2/BangBang.jl/pull/22 has been released. if VERSION ≥ v"1.8" - @turing_testset "(partially) issue: #2095" begin + @testset "(partially) issue: #2095" begin @model function vector_of_dirichlet(::Type{TV}=Vector{Float64}) where {TV} xs = Vector{TV}(undef, 2) xs[1] ~ Dirichlet(ones(5)) @@ -262,7 +281,7 @@ end end - @turing_testset "issue: #2195" begin + @testset "issue: #2195" begin @model function buggy_model() lb ~ Uniform(0, 1) ub ~ Uniform(1.5, 2) @@ -304,3 +323,5 @@ @test pvalue(ApproximateTwoSampleKSTest(vec(results), vec(results_prior))) > 0.01 end end + +end diff --git a/test/mcmc/is.jl b/test/mcmc/is.jl index e0bf80356..7e6bfe005 100644 --- a/test/mcmc/is.jl +++ b/test/mcmc/is.jl @@ -1,4 +1,16 @@ -@turing_testset "is.jl" begin +module ISTests + +using Test: @testset, @test +using Distributions: Normal +using Distributions: sample +using Random: Random +using StatsFuns: logsumexp +using DynamicPPL: logpdf + +using Turing + + +@testset "is.jl" begin function reference(n) as = Vector{Float64}(undef, n) bs = Vector{Float64}(undef, n) @@ -46,7 +58,7 @@ @test chain.logevidence == ref.logevidence end - @turing_testset "logevidence" begin + @testset "logevidence" begin Random.seed!(100) @model function test() @@ -65,3 +77,5 @@ @test chains.logevidence ≈ - 2 * log(2) end end + +end diff --git a/test/mcmc/mh.jl b/test/mcmc/mh.jl index e902aba47..cbb0139e9 100644 --- a/test/mcmc/mh.jl +++ b/test/mcmc/mh.jl @@ -1,5 +1,25 @@ +module MHTests + +using LinearAlgebra: I +using Test: @testset, @test +using Random: Random + +using Distributions: InverseGamma, Normal, MvNormal, Dirichlet, Exponential, LogNormal, Bernoulli +using Distributions: sample +using StableRNGs: StableRNG +using AdvancedMH: AdvancedMH +using DynamicPPL: DynamicPPL +using DynamicPPL: Sampler + +using Turing +using Turing.Inference: Inference + +include(pkgdir(Turing)*"/test/test_utils/testing_functions.jl") +include(pkgdir(Turing)*"/test/test_utils/models.jl") +include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") + @testset "mh.jl" begin - @turing_testset "mh constructor" begin + @testset "mh constructor" begin Random.seed!(10) N = 500 s1 = MH( @@ -26,7 +46,7 @@ # s6 = externalsampler(MH(gdemo_default, proposal_type=AdvancedMH.StaticProposal)) # c6 = sample(gdemo_default, s6, N) end - @numerical_testset "mh inference" begin + @testset "mh inference" begin Random.seed!(125) alg = MH() chain = sample(gdemo_default, alg, 10_000) @@ -57,7 +77,7 @@ end # Test MH shape passing. - @turing_testset "shape" begin + @testset "shape" begin @model function M(mu, sigma, observable) z ~ MvNormal(mu, sigma) @@ -94,7 +114,7 @@ @test chain isa MCMCChains.Chains end - @turing_testset "proposal matrix" begin + @testset "proposal matrix" begin Random.seed!(100) mat = [1.0 -0.05; -0.05 1.0] @@ -117,7 +137,7 @@ check_gdemo(chain2) end - @turing_testset "gibbs MH proposal matrix" begin + @testset "gibbs MH proposal matrix" begin # https://github.com/TuringLang/Turing.jl/issues/1556 # generate data @@ -167,7 +187,7 @@ # Disable on Julia <1.8 due to https://github.com/TuringLang/Turing.jl/pull/2197. # TODO: Remove this block once https://github.com/JuliaFolds2/BangBang.jl/pull/22 has been released. if VERSION ≥ v"1.8" - @turing_testset "vector of multivariate distributions" begin + @testset "vector of multivariate distributions" begin @model function test(k) T = Vector{Vector{Float64}}(undef, k) for i in 1:k @@ -189,7 +209,7 @@ end end - @turing_testset "MH link/invlink" begin + @testset "MH link/invlink" begin vi_base = DynamicPPL.VarInfo(gdemo_default) # Don't link when no proposals are given since we're using priors @@ -229,7 +249,7 @@ @test !DynamicPPL.islinked(vi, spl) end - @turing_testset "prior" begin + @testset "prior" begin # HACK: MH can be so bad for this prior model for some reason that it's difficult to # find a non-trivial `atol` where the tests will pass for all seeds. Hence we fix it :/ rng = StableRNG(10) @@ -241,7 +261,7 @@ check_numerical(chain, [:s, :m], [mean(InverseGamma(2, 3)), 0], atol=0.3) end - @turing_testset "`filldist` proposal (issue #2180)" begin + @testset "`filldist` proposal (issue #2180)" begin @model demo_filldist_issue2180() = x ~ MvNormal(zeros(3), I) chain = sample( demo_filldist_issue2180(), @@ -251,3 +271,5 @@ check_numerical(chain, [Symbol("x[1]"), Symbol("x[2]"), Symbol("x[3]")], [0, 0, 0], atol=0.2) end end + +end diff --git a/test/mcmc/particle_mcmc.jl b/test/mcmc/particle_mcmc.jl index de6e65e40..1c0afa24e 100644 --- a/test/mcmc/particle_mcmc.jl +++ b/test/mcmc/particle_mcmc.jl @@ -1,5 +1,18 @@ +module ParticleMCMCTests + +using Test: @testset, @test, @test_throws +using Distributions: Bernoulli, Beta, Gamma, Normal +using Distributions: sample +using Random: Random +using AdvancedPS: ResampleWithESSThreshold, resample_systematic, resample_multinomial +using DynamicPPL: getspace + +using Turing + +include(pkgdir(Turing)*"/test/test_utils/models.jl") + @testset "SMC" begin - @turing_testset "constructor" begin + @testset "constructor" begin s = SMC() @test s.resampler == ResampleWithESSThreshold() @test getspace(s) === () @@ -45,7 +58,7 @@ @test getspace(s) === (:x,) end - @turing_testset "models" begin + @testset "models" begin @model function normal() a ~ Normal(4,5) 3 ~ Normal(a,2) @@ -70,7 +83,7 @@ @test_throws ErrorException sample(fail_smc(), SMC(), 100) end - @turing_testset "logevidence" begin + @testset "logevidence" begin Random.seed!(100) @model function test() @@ -91,7 +104,7 @@ end @testset "PG" begin - @turing_testset "constructor" begin + @testset "constructor" begin s = PG(10) @test s.nparticles == 10 @test s.resampler == ResampleWithESSThreshold() @@ -148,7 +161,7 @@ end @test getspace(s) === (:x,) end - @turing_testset "logevidence" begin + @testset "logevidence" begin Random.seed!(100) @model function test() @@ -168,21 +181,21 @@ end end # https://github.com/TuringLang/Turing.jl/issues/1598 - @turing_testset "reference particle" begin + @testset "reference particle" begin c = sample(gdemo_default, PG(1), 1_000) @test length(unique(c[:m])) == 1 @test length(unique(c[:s])) == 1 end # https://github.com/TuringLang/Turing.jl/issues/2007 - @turing_testset "keyword arguments not supported" begin + @testset "keyword arguments not supported" begin @model kwarg_demo(; x = 2) = return x @test_throws ErrorException sample(kwarg_demo(), PG(1), 10) end end # @testset "pmmh.jl" begin -# @turing_testset "pmmh constructor" begin +# @testset "pmmh constructor" begin # N = 2000 # s1 = PMMH(N, SMC(10, :s), MH(1,(:m, s -> Normal(s, sqrt(1))))) # s2 = PMMH(N, SMC(10, :s), MH(1, :m)) @@ -218,7 +231,7 @@ end # end # @testset "ipmcmc.jl" begin -# @turing_testset "ipmcmc constructor" begin +# @testset "ipmcmc constructor" begin # Random.seed!(125) # # N = 50 @@ -239,3 +252,4 @@ end # end # end +end diff --git a/test/mcmc/sghmc.jl b/test/mcmc/sghmc.jl index 4405b505a..8266a9408 100644 --- a/test/mcmc/sghmc.jl +++ b/test/mcmc/sghmc.jl @@ -1,5 +1,18 @@ +module SGHMCTests + +using Test: @testset, @test +using LinearAlgebra: dot + +using Distributions: sample +using StableRNGs: StableRNG + +using Turing + +include(pkgdir(Turing)*"/test/test_utils/models.jl") +include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") + @testset "Testing sghmc.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false)) - @turing_testset "sghmc constructor" begin + @testset "sghmc constructor" begin alg = SGHMC(; learning_rate=0.01, momentum_decay=0.1, adtype=adbackend) @test alg isa SGHMC sampler = Turing.Sampler(alg) @@ -15,7 +28,7 @@ sampler = Turing.Sampler(alg) @test sampler isa Turing.Sampler{<:SGHMC} end - @numerical_testset "sghmc inference" begin + @testset "sghmc inference" begin rng = StableRNG(123) alg = SGHMC(; learning_rate=0.02, momentum_decay=0.5, adtype=adbackend) @@ -25,7 +38,7 @@ end @testset "Testing sgld.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false)) - @turing_testset "sgld constructor" begin + @testset "sgld constructor" begin alg = SGLD(; stepsize=PolynomialStepsize(0.25), adtype=adbackend) @test alg isa SGLD sampler = Turing.Sampler(alg) @@ -41,7 +54,7 @@ end sampler = Turing.Sampler(alg) @test sampler isa Turing.Sampler{<:SGLD} end - @numerical_testset "sgld inference" begin + @testset "sgld inference" begin rng = StableRNG(1) chain = sample(rng, gdemo_default, SGLD(; stepsize = PolynomialStepsize(0.5)), 20_000) @@ -55,3 +68,5 @@ end @test m_weighted ≈ 7/6 atol=0.2 end end + +end diff --git a/test/mcmc/utilities.jl b/test/mcmc/utilities.jl index 3a3517f60..25dc1b1e7 100644 --- a/test/mcmc/utilities.jl +++ b/test/mcmc/utilities.jl @@ -1,3 +1,17 @@ +module MCMCUtilitiesTests + +using LinearAlgebra: I, vec +using Test: @testset, @test +using Random: Random +using Random: MersenneTwister + +using Distributions: sample +using Distributions: Normal, truncated + +using Turing + +include(pkgdir(Turing)*"/test/test_utils/models.jl") + @testset "predict" begin Random.seed!(100) @@ -138,3 +152,5 @@ end @test chain.info.stop_time isa Float64 @test chain.info.start_time ≤ chain.info.stop_time end + +end diff --git a/test/stdlib/RandomMeasures.jl b/test/stdlib/RandomMeasures.jl index b56504451..71f2ab624 100644 --- a/test/stdlib/RandomMeasures.jl +++ b/test/stdlib/RandomMeasures.jl @@ -1,3 +1,14 @@ +module RandomMeasuresTests + +using Test: @testset, @test +using Random: Random + +using Distributions: sample +using Distributions: Normal + +using Turing +using Turing.RandomMeasures: DirichletProcess, ChineseRestaurantProcess + @testset "RandomMeasures.jl" begin @testset "Infinite Mixture Model" begin @model function infiniteGMM(x) @@ -381,3 +392,5 @@ # @test discr < 0.2 # end end + +end diff --git a/test/stdlib/distributions.jl b/test/stdlib/distributions.jl index 9024bba9b..b2e2304df 100644 --- a/test/stdlib/distributions.jl +++ b/test/stdlib/distributions.jl @@ -1,6 +1,20 @@ +module DistributionsTests + +using LinearAlgebra: I +using Test: @testset, @test +using Random: Random + +using Distributions +using StatsFuns: logistic +using StableRNGs: StableRNG + +using Turing + +include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") + @testset "distributions.jl" begin rng = StableRNG(12345) - @turing_testset "distributions functions" begin + @testset "distributions functions" begin ns = 10 logitp = randn(rng) d1 = BinomialLogit(ns, logitp) @@ -9,7 +23,7 @@ @test logpdf(d1, k) ≈ logpdf(d2, k) end - @turing_testset "distributions functions" begin + @testset "distributions functions" begin d = OrderedLogistic(-2, [-1, 1]) n = 1_000_000 @@ -21,7 +35,7 @@ @test all(((x, y),) -> abs(x - y) < 0.001, zip(p, pmf)) end - @turing_testset "distributions functions" begin + @testset "distributions functions" begin λ = .01:.01:5 LLp = @. logpdf(Poisson(λ),1) LLlp = @. logpdf(LogPoisson(log(λ)),1) @@ -29,7 +43,7 @@ end - @numerical_testset "single distribution correctness" begin + @testset "single distribution correctness" begin rng = StableRNG(1) n_samples = 10_000 @@ -99,7 +113,7 @@ InverseWishart(7, [1.0 0.5; 0.5 1.0]), ] - @numerical_testset "Correctness test for single distributions" begin + @testset "Correctness test for single distributions" begin for (dist_set, dist_list) ∈ [ ("UnivariateDistribution", dist_uni), ("MultivariateDistribution", dist_multi), @@ -127,3 +141,5 @@ end end end + +end diff --git a/test/test_utils/SelectiveTests.jl b/test/test_utils/SelectiveTests.jl index c9726f845..d4a20bc91 100644 --- a/test/test_utils/SelectiveTests.jl +++ b/test/test_utils/SelectiveTests.jl @@ -1,5 +1,24 @@ module SelectiveTests +""" + parse_args(args) + +Parse the command line arguments to get the included and excluded test file paths. + +The arguments are expected to be in the form: +``` +a b c --skip d e f +``` +where a test file is to be included if and only if +1) the argument list is empty, in which case all files are included, +or +2) + a) it has as a substring of its path any of the strings `a`, `b`, or `c`, + and + b) it does not have as a substring of its path any of the strings `d`, `e`, or `f`. + +The substring checks are done case-insensitively. +""" function parse_args(args) included_paths = Vector{String}() excluded_paths = Vector{String}() @@ -14,7 +33,20 @@ function parse_args(args) return included_paths, excluded_paths end -function isincluded(filepath, included_paths, excluded_paths) +""" + isincluded(filepath, included_paths, excluded_paths) + +Check if a file should be included in the tests. + +`included_paths` and `excluded_paths` are the output of [`parse_args`](@ref). + +See [`parse_args`](@ref) for the logic of when a file should be included. +""" +function isincluded( + filepath::AbstractString, + included_paths::Vector{<:AbstractString}, + excluded_paths::Vector{<:AbstractString}, +) if any(excl -> occursin(lowercase(excl), lowercase(filepath)), excluded_paths) return false end diff --git a/test/variational/optimisers.jl b/test/variational/optimisers.jl index 46a81aa0d..a61adbd56 100644 --- a/test/variational/optimisers.jl +++ b/test/variational/optimisers.jl @@ -1,3 +1,16 @@ +module VariationalOptimisersTests + +using Test: @testset, @test + +using AdvancedVI: TruncatedADAGrad, DecayedADAGrad, apply! +using ForwardDiff: ForwardDiff +using ReverseDiff: ReverseDiff + +using Turing + +include(pkgdir(Turing)*"/test/test_utils/models.jl") +include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") + function test_opt(ADPack, opt) θ = randn(10, 10) θ_fit = randn(10, 10) @@ -17,3 +30,5 @@ end for opt in [TruncatedADAGrad(), DecayedADAGrad(1e-2)] test_opt(ReverseDiff, opt) end + +end From 797566e6b335a01dff3d6c2e2fcfab93f23a38e1 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Wed, 29 May 2024 15:31:59 +0100 Subject: [PATCH 08/29] Remove unused test utils --- test/test_utils/AllUtils.jl | 7 ---- test/test_utils/random_measure_utils.jl | 34 ---------------- test/test_utils/staging.jl | 52 ------------------------- 3 files changed, 93 deletions(-) delete mode 100644 test/test_utils/AllUtils.jl delete mode 100644 test/test_utils/random_measure_utils.jl delete mode 100644 test/test_utils/staging.jl diff --git a/test/test_utils/AllUtils.jl b/test/test_utils/AllUtils.jl deleted file mode 100644 index 4ca57d838..000000000 --- a/test/test_utils/AllUtils.jl +++ /dev/null @@ -1,7 +0,0 @@ -# Import utility functions and reused models. -include("staging.jl") -include("numerical_tests.jl") -include("ad_utils.jl") -include("models.jl") -include("random_measure_utils.jl") -include("testing_functions.jl") diff --git a/test/test_utils/random_measure_utils.jl b/test/test_utils/random_measure_utils.jl deleted file mode 100644 index 63c84c11a..000000000 --- a/test/test_utils/random_measure_utils.jl +++ /dev/null @@ -1,34 +0,0 @@ -function compute_log_joint(observations, partition, tau0, tau1, sigma, theta) - n = length(observations) - k = length(partition) - prob = k*log(sigma) + lgamma(theta) + lgamma(theta/sigma + k) - lgamma(theta/sigma) - lgamma(theta + n) - for cluster in partition - prob += lgamma(length(cluster) - sigma) - lgamma(1 - sigma) - prob += compute_log_conditional_observations(observations, cluster, tau0, tau1) - end - prob -end - -function compute_log_conditional_observations(observations, cluster, tau0, tau1) - nl = length(cluster) - prob = (nl/2)*log(tau1) - (nl/2)*log(2*pi) + 0.5*log(tau0) + 0.5*log(tau0+nl) - prob += -tau1/2*(sum(observations)) + 0.5*(tau0*mu_0+tau1*sum(observations[cluster]))^2/(tau0+nl*tau1) - prob -end - -# Test of similarity between distributions -function correct_posterior(empirical_probs, data, partitions, τ0, τ1, σ, θ) - true_log_probs = map(p -> compute_log_joint(data, p, τ0, τ1, σ, θ), partitions) - true_probs = exp.(true_log_probs) - true_probs /= sum(true_probs) - - empirical_probs /= sum(empirical_probs) - - # compare distribitions - # L2 - L2 = sum((empirical_probs - true_probs).^2) - - # Discrepancy - discr = maximum(abs.(empirical_probs - true_probs)) - return L2, discr -end diff --git a/test/test_utils/staging.jl b/test/test_utils/staging.jl deleted file mode 100644 index 15d5853d0..000000000 --- a/test/test_utils/staging.jl +++ /dev/null @@ -1,52 +0,0 @@ -function get_stage() - # Appveyor uses "True" for non-Ubuntu images. - if get(ENV, "APPVEYOR", "") == "True" || get(ENV, "APPVEYOR", "") == "true" - return "nonnumeric" - end - - # Handle Travis and Github Actions specially. - if get(ENV, "TRAVIS", "") == "true" || get(ENV, "GITHUB_ACTIONS", "") == "true" - if "STAGE" in keys(ENV) - return ENV["STAGE"] - else - return "all" - end - end - - return "all" -end - -function do_test(stage_str) - stg = get_stage() - - # If the tests are being run by Appveyor, don't run - # any numerical tests. - if stg == "nonnumeric" - if stage_str == "numerical" - return false - else - return true - end - end - - # Otherwise run the regular testing procedure. - if stg == "all" || stg == stage_str - return true - end - - return false -end - -macro stage_testset(stage_string::String, args...) - if do_test(stage_string) - return esc(:(@testset($(args...)))) - end -end - -macro numerical_testset(args...) - esc(:(@stage_testset "numerical" $(args...))) -end - -macro turing_testset(args...) - esc(:(@stage_testset "test" $(args...))) -end From b2ef761668f41e71bebf651b6deef9108943ba8b Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Wed, 29 May 2024 15:33:17 +0100 Subject: [PATCH 09/29] Remove GA workflows for DynamicHMC and Numerical --- .github/workflows/DynamicHMC.yml | 42 -------------------------------- .github/workflows/Numerical.yml | 41 ------------------------------- 2 files changed, 83 deletions(-) delete mode 100644 .github/workflows/DynamicHMC.yml delete mode 100644 .github/workflows/Numerical.yml diff --git a/.github/workflows/DynamicHMC.yml b/.github/workflows/DynamicHMC.yml deleted file mode 100644 index 099f70fcf..000000000 --- a/.github/workflows/DynamicHMC.yml +++ /dev/null @@ -1,42 +0,0 @@ -name: DynamicHMC-CI - -on: - push: - branches: - - master - pull_request: - -jobs: - test: - runs-on: ${{ matrix.os }} - strategy: - matrix: - version: - - '1.7' - - '1' - os: - - ubuntu-latest - arch: - - x64 - steps: - - uses: actions/checkout@v2 - - uses: julia-actions/setup-julia@v1 - with: - version: ${{ matrix.version }} - arch: ${{ matrix.arch }} - - uses: actions/cache@v1 - env: - cache-name: cache-artifacts - with: - path: ~/.julia/artifacts - key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }} - restore-keys: | - ${{ runner.os }}-test-${{ env.cache-name }}- - ${{ runner.os }}-test- - ${{ runner.os }}- - - uses: julia-actions/julia-buildpkg@latest - - uses: julia-actions/julia-runtest@latest - with: - coverage: false - env: - STAGE: dynamichmc diff --git a/.github/workflows/Numerical.yml b/.github/workflows/Numerical.yml deleted file mode 100644 index 314241fbe..000000000 --- a/.github/workflows/Numerical.yml +++ /dev/null @@ -1,41 +0,0 @@ -name: Numerical - -on: - push: - branches: - - master - pull_request: - -jobs: - test: - runs-on: ${{ matrix.os }} - strategy: - matrix: - version: - - '1.7' - - '1' - os: - - ubuntu-latest - arch: - - x64 - steps: - - uses: actions/checkout@v2 - - uses: julia-actions/setup-julia@v1 - with: - version: ${{ matrix.version }} - arch: ${{ matrix.arch }} - - uses: actions/cache@v1 - env: - cache-name: cache-artifacts - with: - path: ~/.julia/artifacts - key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }} - restore-keys: | - ${{ runner.os }}-test-${{ env.cache-name }}- - ${{ runner.os }}-test- - - uses: julia-actions/julia-buildpkg@latest - - uses: julia-actions/julia-runtest@latest - with: - coverage: false - env: - STAGE: numerical From 235256d6beccb39f736d8cbb4a3d5474893ad929 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Wed, 29 May 2024 15:33:50 +0100 Subject: [PATCH 10/29] Rename TuringCI GA to Tests --- .github/workflows/{TuringCI.yml => Tests.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{TuringCI.yml => Tests.yml} (100%) diff --git a/.github/workflows/TuringCI.yml b/.github/workflows/Tests.yml similarity index 100% rename from .github/workflows/TuringCI.yml rename to .github/workflows/Tests.yml From 185e14f930551c6b5252dc2d16a46a39817e81eb Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Wed, 29 May 2024 15:45:05 +0100 Subject: [PATCH 11/29] Fix test_args passing in CI --- .github/workflows/Tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml index 650e69b1e..a0f8d4b43 100644 --- a/.github/workflows/Tests.yml +++ b/.github/workflows/Tests.yml @@ -74,4 +74,4 @@ jobs: ${{ runner.os }}- - uses: julia-actions/julia-buildpkg@latest - name: Call Pkg.test - run: julia --color=yes --depwarn=yes --check-bounds=yes --threads=${{ matrix.num_threads }} --project=@. -e 'import Pkg; Pkg.test(; test_args="${{ matrix.test-args }}")' + run: julia --color=yes --depwarn=yes --check-bounds=yes --threads=${{ matrix.num_threads }} --project=@. -e 'import Pkg; Pkg.test(; test_args=ARGS)' -- ${{ matrix.test-args }} From 6ba68a5aa881ebe436ccf878fe0881418c7a3eec Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Wed, 29 May 2024 17:05:25 +0100 Subject: [PATCH 12/29] Fix for CI test matrix --- .github/workflows/Tests.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml index a0f8d4b43..73a9e1abb 100644 --- a/.github/workflows/Tests.yml +++ b/.github/workflows/Tests.yml @@ -39,9 +39,9 @@ jobs: # With Windows and macOS, only run Julia 1.7, x64, 2 threads. We just want to see # some combination work on OSes other than Ubuntu. - os: windows-latest - version: '1.0' + version: '1' - os: macOS-latest - version: '1.0' + version: '1' - os: windows-latest arch: x86 - os: macOS-latest @@ -51,7 +51,7 @@ jobs: - os: macOS-latest num_threads: 1 # It's sufficient to test x86 with one version of Julia and one thread. - - version: '1.0' + - version: '1' arch: x86 - num_threads: 2 arch: x86 From 3c4bed4e288ed280413296cf9260dba38ecc444c Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Wed, 29 May 2024 17:17:31 +0100 Subject: [PATCH 13/29] Fixes to various test files --- test/ext/Optimisation.jl | 14 ++++++++++++++ test/mcmc/Inference.jl | 2 ++ test/mcmc/gibbs.jl | 2 ++ test/mcmc/hmc.jl | 2 ++ test/stdlib/RandomMeasures.jl | 12 ++++++------ test/variational/advi.jl | 29 ++++++++++++++++++++++++----- 6 files changed, 50 insertions(+), 11 deletions(-) diff --git a/test/ext/Optimisation.jl b/test/ext/Optimisation.jl index 324a8a7de..bebfae76e 100644 --- a/test/ext/Optimisation.jl +++ b/test/ext/Optimisation.jl @@ -1,3 +1,15 @@ +module OptimisationTests + +using Random: Random +using Test: @testset, @test + +using Optimization +using OptimizationOptimJL: optimize, NelderMead, LBFGS, Fminbox, BFGS + +using Turing + +include(pkgdir(Turing)*"/test/test_utils/models.jl") + @testset "ext/Optimisation.jl" begin @testset "gdemo" begin @testset "MLE" begin @@ -121,3 +133,5 @@ end end end + +end diff --git a/test/mcmc/Inference.jl b/test/mcmc/Inference.jl index 12ce5d7c7..74d8f72bf 100644 --- a/test/mcmc/Inference.jl +++ b/test/mcmc/Inference.jl @@ -9,6 +9,8 @@ using Distributions: Bernoulli, Beta, Normal, InverseGamma using DynamicPPL: DynamicPPL using DynamicPPL: Sampler, getlogp using MCMCChains: MCMCChains +using ReverseDiff: ReverseDiff +using ForwardDiff: ForwardDiff using Turing diff --git a/test/mcmc/gibbs.jl b/test/mcmc/gibbs.jl index a14600df3..d6bee0090 100644 --- a/test/mcmc/gibbs.jl +++ b/test/mcmc/gibbs.jl @@ -4,6 +4,8 @@ using Test: @testset, @test using Distributions: InverseGamma, Normal using Distributions: sample using Random: Random +using ReverseDiff: ReverseDiff +using ForwardDiff: ForwardDiff using Turing using Turing: Inference diff --git a/test/mcmc/hmc.jl b/test/mcmc/hmc.jl index fad763628..6fd27e094 100644 --- a/test/mcmc/hmc.jl +++ b/test/mcmc/hmc.jl @@ -8,6 +8,8 @@ using Distributions: sample using Distributions: Bernoulli, Beta, Categorical, Dirichlet, Normal, Wishart using StableRNGs: StableRNG using StatsFuns: logistic +using ReverseDiff: ReverseDiff +using ForwardDiff: ForwardDiff using HypothesisTests: ApproximateTwoSampleKSTest, pvalue using DynamicPPL: DynamicPPL using DynamicPPL: Sampler diff --git a/test/stdlib/RandomMeasures.jl b/test/stdlib/RandomMeasures.jl index 71f2ab624..76b41e772 100644 --- a/test/stdlib/RandomMeasures.jl +++ b/test/stdlib/RandomMeasures.jl @@ -83,7 +83,7 @@ using Turing.RandomMeasures: DirichletProcess, ChineseRestaurantProcess # [[1], [2], [3, 4]], # [[1], [2], [3], [4]]] - # @turing_testset "chinese restaurant processes" begin + # @testset "chinese restaurant processes" begin # # Data # data = [-2,2,-1.5,1.5] @@ -157,7 +157,7 @@ using Turing.RandomMeasures: DirichletProcess, ChineseRestaurantProcess # @test discr < 0.2 # end # @testset "distributions" begin - # @turing_testset "Representations" begin + # @testset "Representations" begin # d = StickBreakingProcess(DirichletProcess(1.0)) # @test minimum(d) == 0 # @test maximum(d) == 1 @@ -170,7 +170,7 @@ using Turing.RandomMeasures: DirichletProcess, ChineseRestaurantProcess # @test minimum(d) == 1 # @test maximum(d) == 3 # end - # @turing_testset "Dirichlet Process" begin + # @testset "Dirichlet Process" begin # α = 0.1 # N = 10_000 @@ -198,7 +198,7 @@ using Turing.RandomMeasures: DirichletProcess, ChineseRestaurantProcess # @test p[2] ≈ q[2] atol=0.1 # @test p[3] ≈ q[3] atol=0.1 # end - # @turing_testset "Pitman-Yor Process" begin + # @testset "Pitman-Yor Process" begin # a = 0.5 # θ = 0.1 @@ -229,7 +229,7 @@ using Turing.RandomMeasures: DirichletProcess, ChineseRestaurantProcess # @test p[3] ≈ q[3] atol=0.1 # end # end - # @turing_testset "stick breaking" begin + # @testset "stick breaking" begin # # Data # data = [-2,2,-1.5,1.5] @@ -315,7 +315,7 @@ using Turing.RandomMeasures: DirichletProcess, ChineseRestaurantProcess # @test l2 < 0.1 # @test discr < 0.3 # end - # @turing_testset "size-based sampling" begin + # @testset "size-based sampling" begin # # Data # data = [-2,2,-1.5,1.5] diff --git a/test/variational/advi.jl b/test/variational/advi.jl index 62e5ac400..a7c865f0e 100644 --- a/test/variational/advi.jl +++ b/test/variational/advi.jl @@ -1,5 +1,22 @@ +module AdvancedVITests + +using LinearAlgebra: I +using Random: Random +using Test: @testset, @test + +using AdvancedVI: AdvancedVI +using AdvancedVI: TruncatedADAGrad, DecayedADAGrad +using Distributions: Dirichlet, Normal +using MCMCChains: Chains + +using Turing +using Turing.Essential: TuringDiagMvNormal + +include(pkgdir(Turing)*"/test/test_utils/models.jl") +include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") + @testset "advi.jl" begin - @turing_testset "advi constructor" begin + @testset "advi constructor" begin Random.seed!(0) N = 500 @@ -7,7 +24,7 @@ q = vi(gdemo_default, s1) c1 = rand(q, N) end - @numerical_testset "advi inference" begin + @testset "advi inference" begin @testset for opt in [TruncatedADAGrad(), DecayedADAGrad()] Random.seed!(1) N = 500 @@ -22,7 +39,7 @@ end end - @turing_testset "advi different interfaces" begin + @testset "advi different interfaces" begin Random.seed!(1234) target = MvNormal(zeros(2), I) @@ -50,7 +67,7 @@ # regression test for: # https://github.com/TuringLang/Turing.jl/issues/2065 - @turing_testset "simplex bijector" begin + @testset "simplex bijector" begin @model function dirichlet() x ~ Dirichlet([1.0,1.0]) return x @@ -72,7 +89,7 @@ end # Ref: https://github.com/TuringLang/Turing.jl/issues/2205 - @turing_testset "with `condition` (issue #2205)" begin + @testset "with `condition` (issue #2205)" begin @model function demo_issue2205() x ~ Normal() y ~ Normal(x, 1) @@ -91,3 +108,5 @@ @test var_est ≈ var_true atol=0.2 end end + +end From 7d6fde5b104462db339f04b5c1b1761c10102a7d Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Wed, 29 May 2024 18:06:51 +0100 Subject: [PATCH 14/29] Add container.jl to test suite --- test/essential/container.jl | 16 ++++++++++++++-- test/runtests.jl | 1 + 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/test/essential/container.jl b/test/essential/container.jl index 635d684b4..27a3edf41 100644 --- a/test/essential/container.jl +++ b/test/essential/container.jl @@ -1,3 +1,13 @@ +module ContainerTests + +using Test: @test, @testset + +using AdvancedPS: AdvancedPS +using Distributions: Normal, Bernoulli, Gamma, Beta +using DynamicPPL: @model, Sampler + +using Turing + @testset "container.jl" begin @model function test() a ~ Normal(0, 1) @@ -9,7 +19,7 @@ x end - @turing_testset "constructor" begin + @testset "constructor" begin vi = DynamicPPL.VarInfo() sampler = Sampler(PG(10)) model = test() @@ -29,7 +39,7 @@ @test DynamicPPL.get_num_produce(newtrace.model.f.varinfo) == 1 end - @turing_testset "fork" begin + @testset "fork" begin @model function normal() a ~ Normal(0, 1) 3 ~ Normal(a, 2) @@ -48,3 +58,5 @@ @test AdvancedPS.advance!(trace) ≈ AdvancedPS.advance!(newtrace) end end + +end diff --git a/test/runtests.jl b/test/runtests.jl index d8ad6454c..5e1b318dd 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -27,6 +27,7 @@ end @testset "Turing" begin @testset "essential" begin @timeit_include("essential/ad.jl") + @timeit_include("essential/container.jl") end @testset "samplers (without AD)" begin From 6964622e6706844f016cfa20ea68e313ed7a4096 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Fri, 31 May 2024 10:36:20 +0100 Subject: [PATCH 15/29] Fix spacing around * in test includes --- test/essential/ad.jl | 4 ++-- test/experimental/gibbs.jl | 5 ++--- test/ext/Optimisation.jl | 2 +- test/ext/dynamichmc.jl | 4 ++-- test/mcmc/Inference.jl | 4 ++-- test/mcmc/emcee.jl | 4 ++-- test/mcmc/ess.jl | 4 ++-- test/mcmc/gibbs.jl | 4 ++-- test/mcmc/gibbs_conditional.jl | 4 ++-- test/mcmc/hmc.jl | 4 ++-- test/mcmc/is.jl | 1 - test/mcmc/mh.jl | 6 +++--- test/mcmc/particle_mcmc.jl | 2 +- test/mcmc/sghmc.jl | 4 ++-- test/mcmc/utilities.jl | 2 +- test/optimisation/OptimInterface.jl | 2 +- test/stdlib/distributions.jl | 2 +- test/variational/advi.jl | 4 ++-- test/variational/optimisers.jl | 4 ++-- 19 files changed, 32 insertions(+), 34 deletions(-) diff --git a/test/essential/ad.jl b/test/essential/ad.jl index 788bf5c8d..56760c2d9 100644 --- a/test/essential/ad.jl +++ b/test/essential/ad.jl @@ -14,8 +14,8 @@ using Turing using DynamicPPL: getval, getlogp using Turing: SampleFromPrior -include(pkgdir(Turing)*"/test/test_utils/models.jl") -include(pkgdir(Turing)*"/test/test_utils/ad_utils.jl") +include(pkgdir(Turing) * "/test/test_utils/models.jl") +include(pkgdir(Turing) * "/test/test_utils/ad_utils.jl") @testset "ad.jl" begin @testset "adr" begin diff --git a/test/experimental/gibbs.jl b/test/experimental/gibbs.jl index bae49ae97..a734593a4 100644 --- a/test/experimental/gibbs.jl +++ b/test/experimental/gibbs.jl @@ -2,13 +2,12 @@ module ExperimentalGibbsTests using Random using Test - using DynamicPPL using Turing -include(pkgdir(Turing)*"/test/test_utils/models.jl") -include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") +include(pkgdir(Turing) * "/test/test_utils/models.jl") +include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") function check_transition_varnames( transition::Turing.Inference.Transition, diff --git a/test/ext/Optimisation.jl b/test/ext/Optimisation.jl index bebfae76e..5c3280575 100644 --- a/test/ext/Optimisation.jl +++ b/test/ext/Optimisation.jl @@ -8,7 +8,7 @@ using OptimizationOptimJL: optimize, NelderMead, LBFGS, Fminbox, BFGS using Turing -include(pkgdir(Turing)*"/test/test_utils/models.jl") +include(pkgdir(Turing) * "/test/test_utils/models.jl") @testset "ext/Optimisation.jl" begin @testset "gdemo" begin diff --git a/test/ext/dynamichmc.jl b/test/ext/dynamichmc.jl index 1264c9cd6..7c28ec3f9 100644 --- a/test/ext/dynamichmc.jl +++ b/test/ext/dynamichmc.jl @@ -10,8 +10,8 @@ using DynamicHMC: DynamicHMC using Turing -include(pkgdir(Turing)*"/test/test_utils/models.jl") -include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") +include(pkgdir(Turing) * "/test/test_utils/models.jl") +include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") @testset "TuringDynamicHMCExt" begin Random.seed!(100) diff --git a/test/mcmc/Inference.jl b/test/mcmc/Inference.jl index 74d8f72bf..c6d08f45e 100644 --- a/test/mcmc/Inference.jl +++ b/test/mcmc/Inference.jl @@ -14,8 +14,8 @@ using ForwardDiff: ForwardDiff using Turing -include(pkgdir(Turing)*"/test/test_utils/models.jl") -include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") +include(pkgdir(Turing) * "/test/test_utils/models.jl") +include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") @testset "Testing inference.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false)) # Only test threading if 1.3+. diff --git a/test/mcmc/emcee.jl b/test/mcmc/emcee.jl index a1aa2fea4..4220497fd 100644 --- a/test/mcmc/emcee.jl +++ b/test/mcmc/emcee.jl @@ -8,8 +8,8 @@ using Distributions: sample using Turing -include(pkgdir(Turing)*"/test/test_utils/models.jl") -include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") +include(pkgdir(Turing) * "/test/test_utils/models.jl") +include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") @testset "emcee.jl" begin @testset "gdemo" begin diff --git a/test/mcmc/ess.jl b/test/mcmc/ess.jl index e1ae64e38..d0cd58204 100644 --- a/test/mcmc/ess.jl +++ b/test/mcmc/ess.jl @@ -9,8 +9,8 @@ using DynamicPPL: Sampler using Turing -include(pkgdir(Turing)*"/test/test_utils/models.jl") -include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") +include(pkgdir(Turing) * "/test/test_utils/models.jl") +include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") @testset "ESS" begin @model function demo(x) diff --git a/test/mcmc/gibbs.jl b/test/mcmc/gibbs.jl index d6bee0090..a28f668b9 100644 --- a/test/mcmc/gibbs.jl +++ b/test/mcmc/gibbs.jl @@ -11,8 +11,8 @@ using Turing using Turing: Inference using Turing.RandomMeasures: DirichletProcess, ChineseRestaurantProcess -include(pkgdir(Turing)*"/test/test_utils/models.jl") -include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") +include(pkgdir(Turing) * "/test/test_utils/models.jl") +include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") @testset "Testing gibbs.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false)) @testset "gibbs constructor" begin diff --git a/test/mcmc/gibbs_conditional.jl b/test/mcmc/gibbs_conditional.jl index 1f72bd570..6334e0831 100644 --- a/test/mcmc/gibbs_conditional.jl +++ b/test/mcmc/gibbs_conditional.jl @@ -13,8 +13,8 @@ using StableRNGs: StableRNG using Turing -include(pkgdir(Turing)*"/test/test_utils/models.jl") -include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") +include(pkgdir(Turing) * "/test/test_utils/models.jl") +include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") @testset "Testing gibbs conditionals.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false)) Random.seed!(1000); rng = StableRNG(123) diff --git a/test/mcmc/hmc.jl b/test/mcmc/hmc.jl index 6fd27e094..5491cfbbb 100644 --- a/test/mcmc/hmc.jl +++ b/test/mcmc/hmc.jl @@ -16,8 +16,8 @@ using DynamicPPL: Sampler using Turing -include(pkgdir(Turing)*"/test/test_utils/models.jl") -include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") +include(pkgdir(Turing) * "/test/test_utils/models.jl") +include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") @testset "Testing hmc.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false)) # Set a seed diff --git a/test/mcmc/is.jl b/test/mcmc/is.jl index 7e6bfe005..cd57000ff 100644 --- a/test/mcmc/is.jl +++ b/test/mcmc/is.jl @@ -9,7 +9,6 @@ using DynamicPPL: logpdf using Turing - @testset "is.jl" begin function reference(n) as = Vector{Float64}(undef, n) diff --git a/test/mcmc/mh.jl b/test/mcmc/mh.jl index cbb0139e9..c1d957308 100644 --- a/test/mcmc/mh.jl +++ b/test/mcmc/mh.jl @@ -14,9 +14,9 @@ using DynamicPPL: Sampler using Turing using Turing.Inference: Inference -include(pkgdir(Turing)*"/test/test_utils/testing_functions.jl") -include(pkgdir(Turing)*"/test/test_utils/models.jl") -include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") +include(pkgdir(Turing) * "/test/test_utils/testing_functions.jl") +include(pkgdir(Turing) * "/test/test_utils/models.jl") +include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") @testset "mh.jl" begin @testset "mh constructor" begin diff --git a/test/mcmc/particle_mcmc.jl b/test/mcmc/particle_mcmc.jl index 1c0afa24e..20122dd12 100644 --- a/test/mcmc/particle_mcmc.jl +++ b/test/mcmc/particle_mcmc.jl @@ -9,7 +9,7 @@ using DynamicPPL: getspace using Turing -include(pkgdir(Turing)*"/test/test_utils/models.jl") +include(pkgdir(Turing) * "/test/test_utils/models.jl") @testset "SMC" begin @testset "constructor" begin diff --git a/test/mcmc/sghmc.jl b/test/mcmc/sghmc.jl index 8266a9408..57dd87702 100644 --- a/test/mcmc/sghmc.jl +++ b/test/mcmc/sghmc.jl @@ -8,8 +8,8 @@ using StableRNGs: StableRNG using Turing -include(pkgdir(Turing)*"/test/test_utils/models.jl") -include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") +include(pkgdir(Turing) * "/test/test_utils/models.jl") +include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") @testset "Testing sghmc.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false)) @testset "sghmc constructor" begin diff --git a/test/mcmc/utilities.jl b/test/mcmc/utilities.jl index 25dc1b1e7..9506d23df 100644 --- a/test/mcmc/utilities.jl +++ b/test/mcmc/utilities.jl @@ -10,7 +10,7 @@ using Distributions: Normal, truncated using Turing -include(pkgdir(Turing)*"/test/test_utils/models.jl") +include(pkgdir(Turing) * "/test/test_utils/models.jl") @testset "predict" begin Random.seed!(100) diff --git a/test/optimisation/OptimInterface.jl b/test/optimisation/OptimInterface.jl index ae5f1365a..b48038687 100644 --- a/test/optimisation/OptimInterface.jl +++ b/test/optimisation/OptimInterface.jl @@ -12,7 +12,7 @@ using Distributions.FillArrays: Zeros using Turing -include(pkgdir(Turing)*"/test/test_utils/models.jl") +include(pkgdir(Turing) * "/test/test_utils/models.jl") # Used for testing how well it works with nested contexts. struct OverrideContext{C,T1,T2} <: DynamicPPL.AbstractContext diff --git a/test/stdlib/distributions.jl b/test/stdlib/distributions.jl index b2e2304df..f4e4717d3 100644 --- a/test/stdlib/distributions.jl +++ b/test/stdlib/distributions.jl @@ -10,7 +10,7 @@ using StableRNGs: StableRNG using Turing -include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") +include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") @testset "distributions.jl" begin rng = StableRNG(12345) diff --git a/test/variational/advi.jl b/test/variational/advi.jl index a7c865f0e..d08d2b59d 100644 --- a/test/variational/advi.jl +++ b/test/variational/advi.jl @@ -12,8 +12,8 @@ using MCMCChains: Chains using Turing using Turing.Essential: TuringDiagMvNormal -include(pkgdir(Turing)*"/test/test_utils/models.jl") -include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") +include(pkgdir(Turing) * "/test/test_utils/models.jl") +include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") @testset "advi.jl" begin @testset "advi constructor" begin diff --git a/test/variational/optimisers.jl b/test/variational/optimisers.jl index a61adbd56..5163f1b7c 100644 --- a/test/variational/optimisers.jl +++ b/test/variational/optimisers.jl @@ -8,8 +8,8 @@ using ReverseDiff: ReverseDiff using Turing -include(pkgdir(Turing)*"/test/test_utils/models.jl") -include(pkgdir(Turing)*"/test/test_utils/numerical_tests.jl") +include(pkgdir(Turing) * "/test/test_utils/models.jl") +include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") function test_opt(ADPack, opt) θ = randn(10, 10) From 4cdd6b2fecd466cde4aedb2aea914e9217d93917 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Fri, 31 May 2024 10:59:52 +0100 Subject: [PATCH 16/29] Split ad.jl and abstractmcmc.jl tests to separate CI jobs --- .github/workflows/Tests.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml index 73a9e1abb..81e5b147a 100644 --- a/.github/workflows/Tests.yml +++ b/.github/workflows/Tests.yml @@ -16,12 +16,14 @@ jobs: test-args: # Run some of the slower test files individually. The last one catches everything # not included in the others. + - "essential/ad.jl" - "mcmc/gibbs.jl" - "mcmc/hmc.jl" + - "mcmc/abstractmcmc.jl" - "mcmc/Inference.jl" - "experimental/gibbs.jl" - "mcmc/ess.jl" - - "--skip mcmc/gibbs.jl mcmc/hmc.jl mcmc/Inference.jl experimental/gibbs.jl mcmc/ess.jl" + - "--skip essential/ad.jl mcmc/gibbs.jl mcmc/hmc.jl mcmc/abstractmcmc.jl mcmc/Inference.jl experimental/gibbs.jl mcmc/ess.jl" version: - '1.7' - '1' From a16204b5be019fb0fb003a58d50d473a1df44e68 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Fri, 31 May 2024 15:34:06 +0100 Subject: [PATCH 17/29] Alphabetise imports in tests --- test/essential/ad.jl | 14 ++++++-------- test/essential/container.jl | 6 ++---- test/experimental/gibbs.jl | 3 +-- test/ext/Optimisation.jl | 6 ++---- test/ext/dynamichmc.jl | 8 +++----- test/mcmc/Inference.jl | 12 +++++------- test/mcmc/abstractmcmc.jl | 14 ++++++-------- test/mcmc/emcee.jl | 7 +++---- test/mcmc/ess.jl | 8 +++----- test/mcmc/gibbs.jl | 7 +++---- test/mcmc/gibbs_conditional.jl | 13 +++++-------- test/mcmc/hmc.jl | 17 +++++++---------- test/mcmc/is.jl | 8 +++----- test/mcmc/mh.jl | 14 ++++++-------- test/mcmc/particle_mcmc.jl | 8 +++----- test/mcmc/sghmc.jl | 6 ++---- test/mcmc/utilities.jl | 7 ++----- test/optimisation/OptimInterface.jl | 12 +++++------- test/runtests.jl | 6 ++---- test/stdlib/RandomMeasures.jl | 9 +++------ test/stdlib/distributions.jl | 8 +++----- test/variational/advi.jl | 8 +++----- test/variational/optimisers.jl | 6 ++---- 23 files changed, 80 insertions(+), 127 deletions(-) diff --git a/test/essential/ad.jl b/test/essential/ad.jl index 56760c2d9..b1088b458 100644 --- a/test/essential/ad.jl +++ b/test/essential/ad.jl @@ -1,18 +1,16 @@ module AdTests -using LinearAlgebra -using Test: @testset, @test - -using ForwardDiff -using ReverseDiff using Distributions: logpdf +using DynamicPPL: getlogp, getval +using ForwardDiff +using LinearAlgebra import LogDensityProblems import LogDensityProblemsAD -using Zygote - +using ReverseDiff +using Test: @test, @testset using Turing -using DynamicPPL: getval, getlogp using Turing: SampleFromPrior +using Zygote include(pkgdir(Turing) * "/test/test_utils/models.jl") include(pkgdir(Turing) * "/test/test_utils/ad_utils.jl") diff --git a/test/essential/container.jl b/test/essential/container.jl index 27a3edf41..9ab766767 100644 --- a/test/essential/container.jl +++ b/test/essential/container.jl @@ -1,11 +1,9 @@ module ContainerTests -using Test: @test, @testset - using AdvancedPS: AdvancedPS -using Distributions: Normal, Bernoulli, Gamma, Beta +using Distributions: Bernoulli, Beta, Gamma, Normal using DynamicPPL: @model, Sampler - +using Test: @test, @testset using Turing @testset "container.jl" begin diff --git a/test/experimental/gibbs.jl b/test/experimental/gibbs.jl index a734593a4..56ef1e59a 100644 --- a/test/experimental/gibbs.jl +++ b/test/experimental/gibbs.jl @@ -1,9 +1,8 @@ module ExperimentalGibbsTests +using DynamicPPL using Random using Test -using DynamicPPL - using Turing include(pkgdir(Turing) * "/test/test_utils/models.jl") diff --git a/test/ext/Optimisation.jl b/test/ext/Optimisation.jl index 5c3280575..1bab4c369 100644 --- a/test/ext/Optimisation.jl +++ b/test/ext/Optimisation.jl @@ -1,11 +1,9 @@ module OptimisationTests using Random: Random -using Test: @testset, @test - using Optimization -using OptimizationOptimJL: optimize, NelderMead, LBFGS, Fminbox, BFGS - +using OptimizationOptimJL: BFGS, Fminbox, LBFGS, NelderMead, optimize +using Test: @test, @testset using Turing include(pkgdir(Turing) * "/test/test_utils/models.jl") diff --git a/test/ext/dynamichmc.jl b/test/ext/dynamichmc.jl index 7c28ec3f9..6f42c3c94 100644 --- a/test/ext/dynamichmc.jl +++ b/test/ext/dynamichmc.jl @@ -1,13 +1,11 @@ module DynamicHMCTests -using Test: @testset, @test -using Random: Random - +using Test: @test, @testset using Distributions: sample +using DynamicHMC: DynamicHMC using DynamicPPL: DynamicPPL using DynamicPPL: Sampler -using DynamicHMC: DynamicHMC - +using Random: Random using Turing include(pkgdir(Turing) * "/test/test_utils/models.jl") diff --git a/test/mcmc/Inference.jl b/test/mcmc/Inference.jl index c6d08f45e..e6ffda675 100644 --- a/test/mcmc/Inference.jl +++ b/test/mcmc/Inference.jl @@ -1,17 +1,15 @@ module InferenceTests -using LinearAlgebra: I -using Test: @testset, @test, @test_throws -using Random: Random - +using Distributions: Bernoulli, Beta, InverseGamma, Normal using Distributions: sample -using Distributions: Bernoulli, Beta, Normal, InverseGamma using DynamicPPL: DynamicPPL using DynamicPPL: Sampler, getlogp +using ForwardDiff: ForwardDiff +using LinearAlgebra: I using MCMCChains: MCMCChains +using Random: Random using ReverseDiff: ReverseDiff -using ForwardDiff: ForwardDiff - +using Test: @test, @test_throws, @testset using Turing include(pkgdir(Turing) * "/test/test_utils/models.jl") diff --git a/test/mcmc/abstractmcmc.jl b/test/mcmc/abstractmcmc.jl index db58c1262..4f20fe195 100644 --- a/test/mcmc/abstractmcmc.jl +++ b/test/mcmc/abstractmcmc.jl @@ -1,17 +1,15 @@ module AbstractMCMCTests -using LinearAlgebra: I -using Test: @testset, @test, @test_throws -using Random: Random - +using AdvancedMH: AdvancedMH using Distributions: sample using Distributions.FillArrays: Zeros -using StableRNGs: StableRNG +using DynamicPPL: DynamicPPL +using LinearAlgebra: I using LogDensityProblems: LogDensityProblems using LogDensityProblemsAD: LogDensityProblemsAD -using AdvancedMH: AdvancedMH -using DynamicPPL: DynamicPPL - +using Random: Random +using StableRNGs: StableRNG +using Test: @test, @test_throws, @testset using Turing using Turing.Inference: AdvancedHMC diff --git a/test/mcmc/emcee.jl b/test/mcmc/emcee.jl index 4220497fd..e9f817020 100644 --- a/test/mcmc/emcee.jl +++ b/test/mcmc/emcee.jl @@ -1,11 +1,10 @@ module EmceeTests -using Test: @testset, @test, @test_throws -using Random: Random +using Distributions: sample using DynamicPPL: DynamicPPL using DynamicPPL: Sampler -using Distributions: sample - +using Random: Random +using Test: @test, @test_throws, @testset using Turing include(pkgdir(Turing) * "/test/test_utils/models.jl") diff --git a/test/mcmc/ess.jl b/test/mcmc/ess.jl index d0cd58204..847e3995d 100644 --- a/test/mcmc/ess.jl +++ b/test/mcmc/ess.jl @@ -1,12 +1,10 @@ module ESSTests -using Test: @testset, @test -using Distributions: Normal -using Distributions: sample -using Random: Random +using Distributions: Normal, sample using DynamicPPL: DynamicPPL using DynamicPPL: Sampler - +using Random: Random +using Test: @test, @testset using Turing include(pkgdir(Turing) * "/test/test_utils/models.jl") diff --git a/test/mcmc/gibbs.jl b/test/mcmc/gibbs.jl index a28f668b9..c3c47e71c 100644 --- a/test/mcmc/gibbs.jl +++ b/test/mcmc/gibbs.jl @@ -1,15 +1,14 @@ module GibbsTests -using Test: @testset, @test using Distributions: InverseGamma, Normal using Distributions: sample +using ForwardDiff: ForwardDiff using Random: Random using ReverseDiff: ReverseDiff -using ForwardDiff: ForwardDiff - +using Test: @test, @testset using Turing using Turing: Inference -using Turing.RandomMeasures: DirichletProcess, ChineseRestaurantProcess +using Turing.RandomMeasures: ChineseRestaurantProcess, DirichletProcess include(pkgdir(Turing) * "/test/test_utils/models.jl") include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") diff --git a/test/mcmc/gibbs_conditional.jl b/test/mcmc/gibbs_conditional.jl index 6334e0831..94490d15b 100644 --- a/test/mcmc/gibbs_conditional.jl +++ b/test/mcmc/gibbs_conditional.jl @@ -1,16 +1,13 @@ module GibbsConditionalTests +using Clustering: Clustering +using Distributions: Categorical, InverseGamma, Normal, sample using LinearAlgebra: Diagonal, I using Random: Random -using Test: @testset, @test - -using Distributions: Categorical, InverseGamma, Normal -using Distributions: sample -using StatsFuns: StatsFuns -using StatsBase: counts -using Clustering: Clustering using StableRNGs: StableRNG - +using StatsBase: counts +using StatsFuns: StatsFuns +using Test: @test, @testset using Turing include(pkgdir(Turing) * "/test/test_utils/models.jl") diff --git a/test/mcmc/hmc.jl b/test/mcmc/hmc.jl index 5491cfbbb..bb1ed252b 100644 --- a/test/mcmc/hmc.jl +++ b/test/mcmc/hmc.jl @@ -1,19 +1,16 @@ module HMCTests +using Distributions: Bernoulli, Beta, Categorical, Dirichlet, Normal, Wishart, sample +using DynamicPPL: DynamicPPL +using DynamicPPL: Sampler +using ForwardDiff: ForwardDiff +using HypothesisTests: ApproximateTwoSampleKSTest, pvalue +using ReverseDiff: ReverseDiff using LinearAlgebra: I, dot, vec -using Test: @testset, @test, @test_logs using Random: Random - -using Distributions: sample -using Distributions: Bernoulli, Beta, Categorical, Dirichlet, Normal, Wishart using StableRNGs: StableRNG using StatsFuns: logistic -using ReverseDiff: ReverseDiff -using ForwardDiff: ForwardDiff -using HypothesisTests: ApproximateTwoSampleKSTest, pvalue -using DynamicPPL: DynamicPPL -using DynamicPPL: Sampler - +using Test: @test, @test_logs, @testset using Turing include(pkgdir(Turing) * "/test/test_utils/models.jl") diff --git a/test/mcmc/is.jl b/test/mcmc/is.jl index cd57000ff..cb1d885ff 100644 --- a/test/mcmc/is.jl +++ b/test/mcmc/is.jl @@ -1,12 +1,10 @@ module ISTests -using Test: @testset, @test -using Distributions: Normal -using Distributions: sample +using Distributions: Normal, sample +using DynamicPPL: logpdf using Random: Random using StatsFuns: logsumexp -using DynamicPPL: logpdf - +using Test: @test, @testset using Turing @testset "is.jl" begin diff --git a/test/mcmc/mh.jl b/test/mcmc/mh.jl index c1d957308..71d82803e 100644 --- a/test/mcmc/mh.jl +++ b/test/mcmc/mh.jl @@ -1,16 +1,14 @@ module MHTests -using LinearAlgebra: I -using Test: @testset, @test -using Random: Random - -using Distributions: InverseGamma, Normal, MvNormal, Dirichlet, Exponential, LogNormal, Bernoulli -using Distributions: sample -using StableRNGs: StableRNG using AdvancedMH: AdvancedMH +using Distributions: Bernoulli, Dirichlet, Exponential, InverseGamma, LogNormal, MvNormal, + Normal, sample using DynamicPPL: DynamicPPL using DynamicPPL: Sampler - +using LinearAlgebra: I +using Random: Random +using StableRNGs: StableRNG +using Test: @test, @testset using Turing using Turing.Inference: Inference diff --git a/test/mcmc/particle_mcmc.jl b/test/mcmc/particle_mcmc.jl index 20122dd12..ea8010647 100644 --- a/test/mcmc/particle_mcmc.jl +++ b/test/mcmc/particle_mcmc.jl @@ -1,12 +1,10 @@ module ParticleMCMCTests -using Test: @testset, @test, @test_throws -using Distributions: Bernoulli, Beta, Gamma, Normal -using Distributions: sample -using Random: Random using AdvancedPS: ResampleWithESSThreshold, resample_systematic, resample_multinomial +using Distributions: Bernoulli, Beta, Gamma, Normal, sample using DynamicPPL: getspace - +using Random: Random +using Test: @test, @test_throws, @testset using Turing include(pkgdir(Turing) * "/test/test_utils/models.jl") diff --git a/test/mcmc/sghmc.jl b/test/mcmc/sghmc.jl index 57dd87702..c805ddfe8 100644 --- a/test/mcmc/sghmc.jl +++ b/test/mcmc/sghmc.jl @@ -1,11 +1,9 @@ module SGHMCTests -using Test: @testset, @test -using LinearAlgebra: dot - using Distributions: sample +using LinearAlgebra: dot using StableRNGs: StableRNG - +using Test: @test, @testset using Turing include(pkgdir(Turing) * "/test/test_utils/models.jl") diff --git a/test/mcmc/utilities.jl b/test/mcmc/utilities.jl index 9506d23df..2bdab6e8c 100644 --- a/test/mcmc/utilities.jl +++ b/test/mcmc/utilities.jl @@ -1,13 +1,10 @@ module MCMCUtilitiesTests +using Distributions: Normal, sample, truncated using LinearAlgebra: I, vec -using Test: @testset, @test using Random: Random using Random: MersenneTwister - -using Distributions: sample -using Distributions: Normal, truncated - +using Test: @test, @testset using Turing include(pkgdir(Turing) * "/test/test_utils/models.jl") diff --git a/test/optimisation/OptimInterface.jl b/test/optimisation/OptimInterface.jl index b48038687..cb9a10ccc 100644 --- a/test/optimisation/OptimInterface.jl +++ b/test/optimisation/OptimInterface.jl @@ -1,15 +1,13 @@ module OptimInterfaceTests +using Distributions.FillArrays: Zeros using LinearAlgebra: I -using Random: Random -using Test: @testset, @test - using Optim: Optim -using Optim: optimize, NelderMead, LBFGS +using Optim: NelderMead, LBFGS, optimize +using Random: Random using StatsBase: StatsBase -using StatsBase: vcov, coef, coeftable, coefnames, informationmatrix, stderror -using Distributions.FillArrays: Zeros - +using StatsBase: coef, coefnames, coeftable, informationmatrix, stderror, vcov +using Test: @test, @testset using Turing include(pkgdir(Turing) * "/test/test_utils/models.jl") diff --git a/test/runtests.jl b/test/runtests.jl index 5e1b318dd..b67ad1af3 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,14 +1,12 @@ +include("test_utils/SelectiveTests.jl") +using .SelectiveTests: isincluded, parse_args using Pkg using Test using TimerOutputs: TimerOutputs, @timeit - using Turing: Turing Turing.setprogress!(false) -include("test_utils/SelectiveTests.jl") -using .SelectiveTests: parse_args, isincluded - included_paths, excluded_paths = parse_args(ARGS) # Filter which tests to run and collect timing and allocations information to show in a diff --git a/test/stdlib/RandomMeasures.jl b/test/stdlib/RandomMeasures.jl index 76b41e772..2695f1689 100644 --- a/test/stdlib/RandomMeasures.jl +++ b/test/stdlib/RandomMeasures.jl @@ -1,13 +1,10 @@ module RandomMeasuresTests -using Test: @testset, @test +using Distributions: Normal, sample using Random: Random - -using Distributions: sample -using Distributions: Normal - +using Test: @test, @testset using Turing -using Turing.RandomMeasures: DirichletProcess, ChineseRestaurantProcess +using Turing.RandomMeasures: ChineseRestaurantProcess, DirichletProcess @testset "RandomMeasures.jl" begin @testset "Infinite Mixture Model" begin diff --git a/test/stdlib/distributions.jl b/test/stdlib/distributions.jl index f4e4717d3..fd8264d33 100644 --- a/test/stdlib/distributions.jl +++ b/test/stdlib/distributions.jl @@ -1,13 +1,11 @@ module DistributionsTests +using Distributions using LinearAlgebra: I -using Test: @testset, @test using Random: Random - -using Distributions -using StatsFuns: logistic using StableRNGs: StableRNG - +using StatsFuns: logistic +using Test: @testset, @test using Turing include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") diff --git a/test/variational/advi.jl b/test/variational/advi.jl index d08d2b59d..98d0ecf83 100644 --- a/test/variational/advi.jl +++ b/test/variational/advi.jl @@ -1,14 +1,12 @@ module AdvancedVITests -using LinearAlgebra: I -using Random: Random -using Test: @testset, @test - using AdvancedVI: AdvancedVI using AdvancedVI: TruncatedADAGrad, DecayedADAGrad using Distributions: Dirichlet, Normal +using LinearAlgebra: I using MCMCChains: Chains - +using Random: Random +using Test: @test, @testset using Turing using Turing.Essential: TuringDiagMvNormal diff --git a/test/variational/optimisers.jl b/test/variational/optimisers.jl index 5163f1b7c..21ba4639e 100644 --- a/test/variational/optimisers.jl +++ b/test/variational/optimisers.jl @@ -1,11 +1,9 @@ module VariationalOptimisersTests -using Test: @testset, @test - -using AdvancedVI: TruncatedADAGrad, DecayedADAGrad, apply! +using AdvancedVI: DecayedADAGrad, TruncatedADAGrad, apply! using ForwardDiff: ForwardDiff using ReverseDiff: ReverseDiff - +using Test: @test, @testset using Turing include(pkgdir(Turing) * "/test/test_utils/models.jl") From 2c38e17ee40c78374e05bdbb8a84d0e8cfb9bb95 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Fri, 31 May 2024 15:39:23 +0100 Subject: [PATCH 18/29] In tests, use import X over using X: X --- test/essential/container.jl | 2 +- test/ext/Optimisation.jl | 2 +- test/ext/dynamichmc.jl | 6 +++--- test/mcmc/Inference.jl | 10 +++++----- test/mcmc/abstractmcmc.jl | 10 +++++----- test/mcmc/emcee.jl | 4 ++-- test/mcmc/ess.jl | 4 ++-- test/mcmc/gibbs.jl | 6 +++--- test/mcmc/gibbs_conditional.jl | 6 +++--- test/mcmc/hmc.jl | 8 ++++---- test/mcmc/is.jl | 2 +- test/mcmc/mh.jl | 6 +++--- test/mcmc/particle_mcmc.jl | 2 +- test/mcmc/utilities.jl | 2 +- test/optimisation/OptimInterface.jl | 6 +++--- test/runtests.jl | 2 +- test/stdlib/RandomMeasures.jl | 2 +- test/stdlib/distributions.jl | 2 +- test/variational/advi.jl | 4 ++-- test/variational/optimisers.jl | 4 ++-- 20 files changed, 45 insertions(+), 45 deletions(-) diff --git a/test/essential/container.jl b/test/essential/container.jl index 9ab766767..90e56f46e 100644 --- a/test/essential/container.jl +++ b/test/essential/container.jl @@ -1,6 +1,6 @@ module ContainerTests -using AdvancedPS: AdvancedPS +import AdvancedPS using Distributions: Bernoulli, Beta, Gamma, Normal using DynamicPPL: @model, Sampler using Test: @test, @testset diff --git a/test/ext/Optimisation.jl b/test/ext/Optimisation.jl index 1bab4c369..7b364d1bf 100644 --- a/test/ext/Optimisation.jl +++ b/test/ext/Optimisation.jl @@ -1,6 +1,6 @@ module OptimisationTests -using Random: Random +import Random using Optimization using OptimizationOptimJL: BFGS, Fminbox, LBFGS, NelderMead, optimize using Test: @test, @testset diff --git a/test/ext/dynamichmc.jl b/test/ext/dynamichmc.jl index 6f42c3c94..af3541d26 100644 --- a/test/ext/dynamichmc.jl +++ b/test/ext/dynamichmc.jl @@ -2,10 +2,10 @@ module DynamicHMCTests using Test: @test, @testset using Distributions: sample -using DynamicHMC: DynamicHMC -using DynamicPPL: DynamicPPL +import DynamicHMC +import DynamicPPL using DynamicPPL: Sampler -using Random: Random +import Random using Turing include(pkgdir(Turing) * "/test/test_utils/models.jl") diff --git a/test/mcmc/Inference.jl b/test/mcmc/Inference.jl index e6ffda675..be615c6ac 100644 --- a/test/mcmc/Inference.jl +++ b/test/mcmc/Inference.jl @@ -2,13 +2,13 @@ module InferenceTests using Distributions: Bernoulli, Beta, InverseGamma, Normal using Distributions: sample -using DynamicPPL: DynamicPPL +import DynamicPPL using DynamicPPL: Sampler, getlogp -using ForwardDiff: ForwardDiff +import ForwardDiff using LinearAlgebra: I -using MCMCChains: MCMCChains -using Random: Random -using ReverseDiff: ReverseDiff +import MCMCChains +import Random +import ReverseDiff using Test: @test, @test_throws, @testset using Turing diff --git a/test/mcmc/abstractmcmc.jl b/test/mcmc/abstractmcmc.jl index 4f20fe195..58159e748 100644 --- a/test/mcmc/abstractmcmc.jl +++ b/test/mcmc/abstractmcmc.jl @@ -1,13 +1,13 @@ module AbstractMCMCTests -using AdvancedMH: AdvancedMH +import AdvancedMH using Distributions: sample using Distributions.FillArrays: Zeros -using DynamicPPL: DynamicPPL +import DynamicPPL using LinearAlgebra: I -using LogDensityProblems: LogDensityProblems -using LogDensityProblemsAD: LogDensityProblemsAD -using Random: Random +import LogDensityProblems +import LogDensityProblemsAD +import Random using StableRNGs: StableRNG using Test: @test, @test_throws, @testset using Turing diff --git a/test/mcmc/emcee.jl b/test/mcmc/emcee.jl index e9f817020..75205d832 100644 --- a/test/mcmc/emcee.jl +++ b/test/mcmc/emcee.jl @@ -1,9 +1,9 @@ module EmceeTests using Distributions: sample -using DynamicPPL: DynamicPPL +import DynamicPPL using DynamicPPL: Sampler -using Random: Random +import Random using Test: @test, @test_throws, @testset using Turing diff --git a/test/mcmc/ess.jl b/test/mcmc/ess.jl index 847e3995d..4c9d45bf9 100644 --- a/test/mcmc/ess.jl +++ b/test/mcmc/ess.jl @@ -1,9 +1,9 @@ module ESSTests using Distributions: Normal, sample -using DynamicPPL: DynamicPPL +import DynamicPPL using DynamicPPL: Sampler -using Random: Random +import Random using Test: @test, @testset using Turing diff --git a/test/mcmc/gibbs.jl b/test/mcmc/gibbs.jl index c3c47e71c..2e1d69067 100644 --- a/test/mcmc/gibbs.jl +++ b/test/mcmc/gibbs.jl @@ -2,9 +2,9 @@ module GibbsTests using Distributions: InverseGamma, Normal using Distributions: sample -using ForwardDiff: ForwardDiff -using Random: Random -using ReverseDiff: ReverseDiff +import ForwardDiff +import Random +import ReverseDiff using Test: @test, @testset using Turing using Turing: Inference diff --git a/test/mcmc/gibbs_conditional.jl b/test/mcmc/gibbs_conditional.jl index 94490d15b..749e2870f 100644 --- a/test/mcmc/gibbs_conditional.jl +++ b/test/mcmc/gibbs_conditional.jl @@ -1,12 +1,12 @@ module GibbsConditionalTests -using Clustering: Clustering +import Clustering using Distributions: Categorical, InverseGamma, Normal, sample using LinearAlgebra: Diagonal, I -using Random: Random +import Random using StableRNGs: StableRNG using StatsBase: counts -using StatsFuns: StatsFuns +import StatsFuns using Test: @test, @testset using Turing diff --git a/test/mcmc/hmc.jl b/test/mcmc/hmc.jl index bb1ed252b..887126e6b 100644 --- a/test/mcmc/hmc.jl +++ b/test/mcmc/hmc.jl @@ -1,13 +1,13 @@ module HMCTests using Distributions: Bernoulli, Beta, Categorical, Dirichlet, Normal, Wishart, sample -using DynamicPPL: DynamicPPL +import DynamicPPL using DynamicPPL: Sampler -using ForwardDiff: ForwardDiff +import ForwardDiff using HypothesisTests: ApproximateTwoSampleKSTest, pvalue -using ReverseDiff: ReverseDiff +import ReverseDiff using LinearAlgebra: I, dot, vec -using Random: Random +import Random using StableRNGs: StableRNG using StatsFuns: logistic using Test: @test, @test_logs, @testset diff --git a/test/mcmc/is.jl b/test/mcmc/is.jl index cb1d885ff..b2e26bba7 100644 --- a/test/mcmc/is.jl +++ b/test/mcmc/is.jl @@ -2,7 +2,7 @@ module ISTests using Distributions: Normal, sample using DynamicPPL: logpdf -using Random: Random +import Random using StatsFuns: logsumexp using Test: @test, @testset using Turing diff --git a/test/mcmc/mh.jl b/test/mcmc/mh.jl index 71d82803e..0eaddfbc7 100644 --- a/test/mcmc/mh.jl +++ b/test/mcmc/mh.jl @@ -1,12 +1,12 @@ module MHTests -using AdvancedMH: AdvancedMH +import AdvancedMH using Distributions: Bernoulli, Dirichlet, Exponential, InverseGamma, LogNormal, MvNormal, Normal, sample -using DynamicPPL: DynamicPPL +import DynamicPPL using DynamicPPL: Sampler using LinearAlgebra: I -using Random: Random +import Random using StableRNGs: StableRNG using Test: @test, @testset using Turing diff --git a/test/mcmc/particle_mcmc.jl b/test/mcmc/particle_mcmc.jl index ea8010647..85509af39 100644 --- a/test/mcmc/particle_mcmc.jl +++ b/test/mcmc/particle_mcmc.jl @@ -3,7 +3,7 @@ module ParticleMCMCTests using AdvancedPS: ResampleWithESSThreshold, resample_systematic, resample_multinomial using Distributions: Bernoulli, Beta, Gamma, Normal, sample using DynamicPPL: getspace -using Random: Random +import Random using Test: @test, @test_throws, @testset using Turing diff --git a/test/mcmc/utilities.jl b/test/mcmc/utilities.jl index 2bdab6e8c..16d3b379e 100644 --- a/test/mcmc/utilities.jl +++ b/test/mcmc/utilities.jl @@ -2,7 +2,7 @@ module MCMCUtilitiesTests using Distributions: Normal, sample, truncated using LinearAlgebra: I, vec -using Random: Random +import Random using Random: MersenneTwister using Test: @test, @testset using Turing diff --git a/test/optimisation/OptimInterface.jl b/test/optimisation/OptimInterface.jl index cb9a10ccc..749ddc015 100644 --- a/test/optimisation/OptimInterface.jl +++ b/test/optimisation/OptimInterface.jl @@ -2,10 +2,10 @@ module OptimInterfaceTests using Distributions.FillArrays: Zeros using LinearAlgebra: I -using Optim: Optim +import Optim using Optim: NelderMead, LBFGS, optimize -using Random: Random -using StatsBase: StatsBase +import Random +import StatsBase using StatsBase: coef, coefnames, coeftable, informationmatrix, stderror, vcov using Test: @test, @testset using Turing diff --git a/test/runtests.jl b/test/runtests.jl index b67ad1af3..126d8b29f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -3,7 +3,7 @@ using .SelectiveTests: isincluded, parse_args using Pkg using Test using TimerOutputs: TimerOutputs, @timeit -using Turing: Turing +import Turing Turing.setprogress!(false) diff --git a/test/stdlib/RandomMeasures.jl b/test/stdlib/RandomMeasures.jl index 2695f1689..e2be8fb2d 100644 --- a/test/stdlib/RandomMeasures.jl +++ b/test/stdlib/RandomMeasures.jl @@ -1,7 +1,7 @@ module RandomMeasuresTests using Distributions: Normal, sample -using Random: Random +import Random using Test: @test, @testset using Turing using Turing.RandomMeasures: ChineseRestaurantProcess, DirichletProcess diff --git a/test/stdlib/distributions.jl b/test/stdlib/distributions.jl index fd8264d33..17d8725d0 100644 --- a/test/stdlib/distributions.jl +++ b/test/stdlib/distributions.jl @@ -2,7 +2,7 @@ module DistributionsTests using Distributions using LinearAlgebra: I -using Random: Random +import Random using StableRNGs: StableRNG using StatsFuns: logistic using Test: @testset, @test diff --git a/test/variational/advi.jl b/test/variational/advi.jl index 98d0ecf83..9cf420d9b 100644 --- a/test/variational/advi.jl +++ b/test/variational/advi.jl @@ -1,11 +1,11 @@ module AdvancedVITests -using AdvancedVI: AdvancedVI +import AdvancedVI using AdvancedVI: TruncatedADAGrad, DecayedADAGrad using Distributions: Dirichlet, Normal using LinearAlgebra: I using MCMCChains: Chains -using Random: Random +import Random using Test: @test, @testset using Turing using Turing.Essential: TuringDiagMvNormal diff --git a/test/variational/optimisers.jl b/test/variational/optimisers.jl index 21ba4639e..2a67844df 100644 --- a/test/variational/optimisers.jl +++ b/test/variational/optimisers.jl @@ -1,8 +1,8 @@ module VariationalOptimisersTests using AdvancedVI: DecayedADAGrad, TruncatedADAGrad, apply! -using ForwardDiff: ForwardDiff -using ReverseDiff: ReverseDiff +import ForwardDiff +import ReverseDiff using Test: @test, @testset using Turing From d4a728c7cfec9e81c83bc4261e07130887e7d8d5 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Fri, 31 May 2024 15:42:03 +0100 Subject: [PATCH 19/29] Add missing imports to abstractmcmc.jl tests --- test/mcmc/abstractmcmc.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/mcmc/abstractmcmc.jl b/test/mcmc/abstractmcmc.jl index 58159e748..57554c0f2 100644 --- a/test/mcmc/abstractmcmc.jl +++ b/test/mcmc/abstractmcmc.jl @@ -4,10 +4,12 @@ import AdvancedMH using Distributions: sample using Distributions.FillArrays: Zeros import DynamicPPL +import ForwardDiff using LinearAlgebra: I import LogDensityProblems import LogDensityProblemsAD import Random +import ReverseDiff using StableRNGs: StableRNG using Test: @test, @test_throws, @testset using Turing From c624f81bdc353f82793a1f393c7dc72051611748 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Fri, 31 May 2024 15:57:13 +0100 Subject: [PATCH 20/29] Add some missing imports to tests --- test/mcmc/gibbs_conditional.jl | 2 ++ test/mcmc/sghmc.jl | 2 ++ 2 files changed, 4 insertions(+) diff --git a/test/mcmc/gibbs_conditional.jl b/test/mcmc/gibbs_conditional.jl index 749e2870f..fbfdf1834 100644 --- a/test/mcmc/gibbs_conditional.jl +++ b/test/mcmc/gibbs_conditional.jl @@ -2,8 +2,10 @@ module GibbsConditionalTests import Clustering using Distributions: Categorical, InverseGamma, Normal, sample +import ForwardDiff using LinearAlgebra: Diagonal, I import Random +import ReverseDiff using StableRNGs: StableRNG using StatsBase: counts import StatsFuns diff --git a/test/mcmc/sghmc.jl b/test/mcmc/sghmc.jl index c805ddfe8..a84f005f7 100644 --- a/test/mcmc/sghmc.jl +++ b/test/mcmc/sghmc.jl @@ -1,7 +1,9 @@ module SGHMCTests using Distributions: sample +import ForwardDiff using LinearAlgebra: dot +import ReverseDiff using StableRNGs: StableRNG using Test: @test, @testset using Turing From d370259519bac7ea02a606a0cb834990e8afaf38 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Fri, 31 May 2024 16:01:19 +0100 Subject: [PATCH 21/29] Merge ad_utils.jl to ad.jl in tests --- test/essential/ad.jl | 40 +++++++++++++- test/test_utils/ad_utils.jl | 106 ------------------------------------ 2 files changed, 39 insertions(+), 107 deletions(-) delete mode 100644 test/test_utils/ad_utils.jl diff --git a/test/essential/ad.jl b/test/essential/ad.jl index b1088b458..5c7a6f23b 100644 --- a/test/essential/ad.jl +++ b/test/essential/ad.jl @@ -13,7 +13,45 @@ using Turing: SampleFromPrior using Zygote include(pkgdir(Turing) * "/test/test_utils/models.jl") -include(pkgdir(Turing) * "/test/test_utils/ad_utils.jl") + +function test_model_ad(model, f, syms::Vector{Symbol}) + # Set up VI. + vi = Turing.VarInfo(model) + + # Collect symbols. + vnms = Vector(undef, length(syms)) + vnvals = Vector{Float64}() + for i in 1:length(syms) + s = syms[i] + vnms[i] = getfield(vi.metadata, s).vns[1] + + vals = getval(vi, vnms[i]) + for i in eachindex(vals) + push!(vnvals, vals[i]) + end + end + + # Compute primal. + x = vec(vnvals) + logp = f(x) + + # Call ForwardDiff's AD directly. + grad_FWAD = sort(ForwardDiff.gradient(f, x)) + + # Compare with `logdensity_and_gradient`. + z = vi[SampleFromPrior()] + for chunksize in (0, 1, 10), standardtag in (true, false, 0, 3) + ℓ = LogDensityProblemsAD.ADgradient( + Turing.AutoForwardDiff(; chunksize=chunksize, tag=standardtag), + Turing.LogDensityFunction(vi, model, SampleFromPrior(), DynamicPPL.DefaultContext()), + ) + l, ∇E = LogDensityProblems.logdensity_and_gradient(ℓ, z) + + # Compare result + @test l ≈ logp + @test sort(∇E) ≈ grad_FWAD atol = 1e-9 + end +end @testset "ad.jl" begin @testset "adr" begin diff --git a/test/test_utils/ad_utils.jl b/test/test_utils/ad_utils.jl deleted file mode 100644 index e48a46ae2..000000000 --- a/test/test_utils/ad_utils.jl +++ /dev/null @@ -1,106 +0,0 @@ -""" - test_reverse_mode_ad(forward, f, ȳ, x...; rtol=1e-6, atol=1e-6) - -Check that the reverse-mode sensitivities produced by an AD library are correct for `f` -at `x...`, given sensitivity `ȳ` w.r.t. `y = f(x...)` up to `rtol` and `atol`. -""" -function test_reverse_mode_ad(f, ȳ, x...; rtol=1e-6, atol=1e-6) - # Perform a regular forwards-pass. - y = f(x...) - - # Use Tracker to compute reverse-mode sensitivities. - y_tracker, back_tracker = Tracker.forward(f, x...) - x̄s_tracker = back_tracker(ȳ) - - # Use Zygote to compute reverse-mode sensitivities. - y_zygote, back_zygote = Zygote.pullback(f, x...) - x̄s_zygote = back_zygote(ȳ) - - test_rd = length(x) == 1 && y isa Number - if test_rd - # Use ReverseDiff to compute reverse-mode sensitivities. - if x[1] isa Array - x̄s_rd = similar(x[1]) - tp = ReverseDiff.GradientTape(x -> f(x), x[1]) - ReverseDiff.gradient!(x̄s_rd, tp, x[1]) - x̄s_rd .*= ȳ - y_rd = ReverseDiff.value(tp.output) - @assert y_rd isa Number - else - x̄s_rd = [x[1]] - tp = ReverseDiff.GradientTape(x -> f(x[1]), [x[1]]) - ReverseDiff.gradient!(x̄s_rd, tp, [x[1]]) - y_rd = ReverseDiff.value(tp.output)[1] - x̄s_rd = x̄s_rd[1] * ȳ - @assert y_rd isa Number - end - end - - # Use finite differencing to compute reverse-mode sensitivities. - x̄s_fdm = FDM.j′vp(central_fdm(5, 1), f, ȳ, x...) - - # Check that Tracker forwards-pass produces the correct answer. - @test isapprox(y, Tracker.data(y_tracker), atol=atol, rtol=rtol) - - # Check that Zygpte forwards-pass produces the correct answer. - @test isapprox(y, y_zygote, atol=atol, rtol=rtol) - - if test_rd - # Check that ReverseDiff forwards-pass produces the correct answer. - @test isapprox(y, y_rd, atol=atol, rtol=rtol) - end - - # Check that Tracker reverse-mode sensitivities are correct. - @test all(zip(x̄s_tracker, x̄s_fdm)) do (x̄_tracker, x̄_fdm) - isapprox(Tracker.data(x̄_tracker), x̄_fdm; atol=atol, rtol=rtol) - end - - # Check that Zygote reverse-mode sensitivities are correct. - @test all(zip(x̄s_zygote, x̄s_fdm)) do (x̄_zygote, x̄_fdm) - isapprox(x̄_zygote, x̄_fdm; atol=atol, rtol=rtol) - end - - if test_rd - # Check that ReverseDiff reverse-mode sensitivities are correct. - @test isapprox(x̄s_rd, x̄s_zygote[1]; atol=atol, rtol=rtol) - end -end - -function test_model_ad(model, f, syms::Vector{Symbol}) - # Set up VI. - vi = Turing.VarInfo(model) - - # Collect symbols. - vnms = Vector(undef, length(syms)) - vnvals = Vector{Float64}() - for i in 1:length(syms) - s = syms[i] - vnms[i] = getfield(vi.metadata, s).vns[1] - - vals = getval(vi, vnms[i]) - for i in eachindex(vals) - push!(vnvals, vals[i]) - end - end - - # Compute primal. - x = vec(vnvals) - logp = f(x) - - # Call ForwardDiff's AD directly. - grad_FWAD = sort(ForwardDiff.gradient(f, x)) - - # Compare with `logdensity_and_gradient`. - z = vi[SampleFromPrior()] - for chunksize in (0, 1, 10), standardtag in (true, false, 0, 3) - ℓ = LogDensityProblemsAD.ADgradient( - Turing.AutoForwardDiff(; chunksize=chunksize, tag=standardtag), - Turing.LogDensityFunction(vi, model, SampleFromPrior(), DynamicPPL.DefaultContext()), - ) - l, ∇E = LogDensityProblems.logdensity_and_gradient(ℓ, z) - - # Compare result - @test l ≈ logp - @test sort(∇E) ≈ grad_FWAD atol = 1e-9 - end -end From 0b80dbc0cdc283f05511c9cd4e0fa24d0951697a Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Fri, 31 May 2024 16:02:44 +0100 Subject: [PATCH 22/29] Merge testing_functions.jl into mh.jl in tests --- test/mcmc/mh.jl | 3 ++- test/test_utils/testing_functions.jl | 22 ---------------------- 2 files changed, 2 insertions(+), 23 deletions(-) delete mode 100644 test/test_utils/testing_functions.jl diff --git a/test/mcmc/mh.jl b/test/mcmc/mh.jl index 0eaddfbc7..55e852474 100644 --- a/test/mcmc/mh.jl +++ b/test/mcmc/mh.jl @@ -12,10 +12,11 @@ using Test: @test, @testset using Turing using Turing.Inference: Inference -include(pkgdir(Turing) * "/test/test_utils/testing_functions.jl") include(pkgdir(Turing) * "/test/test_utils/models.jl") include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") +GKernel(var) = (x) -> Normal(x, sqrt.(var)) + @testset "mh.jl" begin @testset "mh constructor" begin Random.seed!(10) diff --git a/test/test_utils/testing_functions.jl b/test/test_utils/testing_functions.jl deleted file mode 100644 index f73de928f..000000000 --- a/test/test_utils/testing_functions.jl +++ /dev/null @@ -1,22 +0,0 @@ -GKernel(var) = (x) -> Normal(x, sqrt.(var)) - -function randr(vi::Turing.VarInfo, - vn::Turing.VarName, - dist::Distribution, - spl::Turing.Sampler, - count::Bool = false) - if ~haskey(vi, vn) - r = rand(dist) - Turing.push!(vi, vn, r, dist, spl) - return r - elseif is_flagged(vi, vn, "del") - unset_flag!(vi, vn, "del") - r = rand(dist) - Turing.RandomVariables.setval!(vi, Turing.vectorize(dist, r), vn) - return r - else - if count Turing.checkindex(vn, vi, spl) end - Turing.updategid!(vi, vn, spl) - return vi[vn] - end -end From 2d7b101f7e5e1259a63bfbe8354dfc24098958ce Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Fri, 31 May 2024 16:50:41 +0100 Subject: [PATCH 23/29] Simplify test_utils Turn all of them into modules or merge them into other files that used to `include` them. --- test/essential/ad.jl | 3 +-- test/experimental/gibbs.jl | 6 +++--- test/ext/Optimisation.jl | 3 +-- test/ext/dynamichmc.jl | 5 ++--- test/mcmc/Inference.jl | 5 ++--- test/mcmc/emcee.jl | 5 ++--- test/mcmc/ess.jl | 5 ++--- test/mcmc/gibbs.jl | 5 ++--- test/mcmc/gibbs_conditional.jl | 5 ++--- test/mcmc/hmc.jl | 6 +++--- test/mcmc/mh.jl | 4 ++-- test/mcmc/particle_mcmc.jl | 4 ++-- test/mcmc/sghmc.jl | 5 ++--- test/mcmc/utilities.jl | 3 +-- test/optimisation/OptimInterface.jl | 3 +-- test/runtests.jl | 3 +++ test/stdlib/distributions.jl | 3 +-- test/test_utils/models.jl | 11 +++++++++-- test/test_utils/numerical_tests.jl | 10 ++++++++++ test/variational/advi.jl | 5 ++--- test/variational/optimisers.jl | 3 --- 21 files changed, 53 insertions(+), 49 deletions(-) diff --git a/test/essential/ad.jl b/test/essential/ad.jl index 5c7a6f23b..b67c8bd56 100644 --- a/test/essential/ad.jl +++ b/test/essential/ad.jl @@ -1,5 +1,6 @@ module AdTests +using ..Models: gdemo_default using Distributions: logpdf using DynamicPPL: getlogp, getval using ForwardDiff @@ -12,8 +13,6 @@ using Turing using Turing: SampleFromPrior using Zygote -include(pkgdir(Turing) * "/test/test_utils/models.jl") - function test_model_ad(model, f, syms::Vector{Symbol}) # Set up VI. vi = Turing.VarInfo(model) diff --git a/test/experimental/gibbs.jl b/test/experimental/gibbs.jl index 56ef1e59a..3f7147e66 100644 --- a/test/experimental/gibbs.jl +++ b/test/experimental/gibbs.jl @@ -1,13 +1,13 @@ module ExperimentalGibbsTests +using ..Models: MoGtest_default, MoGtest_default_z_vector, gdemo +using ..NumericalTests: check_MoGtest_default, check_MoGtest_default_z_vector, check_gdemo, + check_numerical using DynamicPPL using Random using Test using Turing -include(pkgdir(Turing) * "/test/test_utils/models.jl") -include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") - function check_transition_varnames( transition::Turing.Inference.Transition, parent_varnames diff --git a/test/ext/Optimisation.jl b/test/ext/Optimisation.jl index 7b364d1bf..0efb47b35 100644 --- a/test/ext/Optimisation.jl +++ b/test/ext/Optimisation.jl @@ -1,13 +1,12 @@ module OptimisationTests +using ..Models: gdemo, gdemo_default import Random using Optimization using OptimizationOptimJL: BFGS, Fminbox, LBFGS, NelderMead, optimize using Test: @test, @testset using Turing -include(pkgdir(Turing) * "/test/test_utils/models.jl") - @testset "ext/Optimisation.jl" begin @testset "gdemo" begin @testset "MLE" begin diff --git a/test/ext/dynamichmc.jl b/test/ext/dynamichmc.jl index af3541d26..d525512ca 100644 --- a/test/ext/dynamichmc.jl +++ b/test/ext/dynamichmc.jl @@ -1,5 +1,7 @@ module DynamicHMCTests +using ..Models: gdemo_default +using ..NumericalTests: check_gdemo using Test: @test, @testset using Distributions: sample import DynamicHMC @@ -8,9 +10,6 @@ using DynamicPPL: Sampler import Random using Turing -include(pkgdir(Turing) * "/test/test_utils/models.jl") -include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") - @testset "TuringDynamicHMCExt" begin Random.seed!(100) diff --git a/test/mcmc/Inference.jl b/test/mcmc/Inference.jl index be615c6ac..f7601b2e1 100644 --- a/test/mcmc/Inference.jl +++ b/test/mcmc/Inference.jl @@ -1,5 +1,7 @@ module InferenceTests +using ..Models: gdemo_d, gdemo_default +using ..NumericalTests: check_gdemo, check_numerical using Distributions: Bernoulli, Beta, InverseGamma, Normal using Distributions: sample import DynamicPPL @@ -12,9 +14,6 @@ import ReverseDiff using Test: @test, @test_throws, @testset using Turing -include(pkgdir(Turing) * "/test/test_utils/models.jl") -include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") - @testset "Testing inference.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false)) # Only test threading if 1.3+. if VERSION > v"1.2" diff --git a/test/mcmc/emcee.jl b/test/mcmc/emcee.jl index 75205d832..04901775e 100644 --- a/test/mcmc/emcee.jl +++ b/test/mcmc/emcee.jl @@ -1,5 +1,7 @@ module EmceeTests +using ..Models: gdemo_default +using ..NumericalTests: check_gdemo using Distributions: sample import DynamicPPL using DynamicPPL: Sampler @@ -7,9 +9,6 @@ import Random using Test: @test, @test_throws, @testset using Turing -include(pkgdir(Turing) * "/test/test_utils/models.jl") -include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") - @testset "emcee.jl" begin @testset "gdemo" begin Random.seed!(9876) diff --git a/test/mcmc/ess.jl b/test/mcmc/ess.jl index 4c9d45bf9..86029e43f 100644 --- a/test/mcmc/ess.jl +++ b/test/mcmc/ess.jl @@ -1,5 +1,7 @@ module ESSTests +using ..Models: MoGtest, MoGtest_default, gdemo, gdemo_default +using ..NumericalTests: check_MoGtest_default, check_numerical using Distributions: Normal, sample import DynamicPPL using DynamicPPL: Sampler @@ -7,9 +9,6 @@ import Random using Test: @test, @testset using Turing -include(pkgdir(Turing) * "/test/test_utils/models.jl") -include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") - @testset "ESS" begin @model function demo(x) m ~ Normal() diff --git a/test/mcmc/gibbs.jl b/test/mcmc/gibbs.jl index 2e1d69067..3e096dde1 100644 --- a/test/mcmc/gibbs.jl +++ b/test/mcmc/gibbs.jl @@ -1,5 +1,7 @@ module GibbsTests +using ..Models: MoGtest_default, gdemo, gdemo_default +using ..NumericalTests: check_MoGtest_default, check_gdemo, check_numerical using Distributions: InverseGamma, Normal using Distributions: sample import ForwardDiff @@ -10,9 +12,6 @@ using Turing using Turing: Inference using Turing.RandomMeasures: ChineseRestaurantProcess, DirichletProcess -include(pkgdir(Turing) * "/test/test_utils/models.jl") -include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") - @testset "Testing gibbs.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false)) @testset "gibbs constructor" begin N = 500 diff --git a/test/mcmc/gibbs_conditional.jl b/test/mcmc/gibbs_conditional.jl index fbfdf1834..2abec68a4 100644 --- a/test/mcmc/gibbs_conditional.jl +++ b/test/mcmc/gibbs_conditional.jl @@ -1,5 +1,7 @@ module GibbsConditionalTests +using ..Models: gdemo, gdemo_default +using ..NumericalTests: check_gdemo, check_numerical import Clustering using Distributions: Categorical, InverseGamma, Normal, sample import ForwardDiff @@ -12,9 +14,6 @@ import StatsFuns using Test: @test, @testset using Turing -include(pkgdir(Turing) * "/test/test_utils/models.jl") -include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") - @testset "Testing gibbs conditionals.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false)) Random.seed!(1000); rng = StableRNG(123) diff --git a/test/mcmc/hmc.jl b/test/mcmc/hmc.jl index 887126e6b..96ad1d0e0 100644 --- a/test/mcmc/hmc.jl +++ b/test/mcmc/hmc.jl @@ -1,5 +1,8 @@ module HMCTests +using ..Models: gdemo_default +#using ..Models: gdemo +using ..NumericalTests: check_gdemo, check_numerical using Distributions: Bernoulli, Beta, Categorical, Dirichlet, Normal, Wishart, sample import DynamicPPL using DynamicPPL: Sampler @@ -13,9 +16,6 @@ using StatsFuns: logistic using Test: @test, @test_logs, @testset using Turing -include(pkgdir(Turing) * "/test/test_utils/models.jl") -include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") - @testset "Testing hmc.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false)) # Set a seed rng = StableRNG(123) diff --git a/test/mcmc/mh.jl b/test/mcmc/mh.jl index 55e852474..3b15d8069 100644 --- a/test/mcmc/mh.jl +++ b/test/mcmc/mh.jl @@ -12,8 +12,8 @@ using Test: @test, @testset using Turing using Turing.Inference: Inference -include(pkgdir(Turing) * "/test/test_utils/models.jl") -include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") +using ..Models: gdemo_default, MoGtest_default +using ..NumericalTests: check_MoGtest_default, check_gdemo, check_numerical GKernel(var) = (x) -> Normal(x, sqrt.(var)) diff --git a/test/mcmc/particle_mcmc.jl b/test/mcmc/particle_mcmc.jl index 85509af39..01510f2a8 100644 --- a/test/mcmc/particle_mcmc.jl +++ b/test/mcmc/particle_mcmc.jl @@ -1,5 +1,7 @@ module ParticleMCMCTests +using ..Models: gdemo_default +#using ..Models: MoGtest, MoGtest_default using AdvancedPS: ResampleWithESSThreshold, resample_systematic, resample_multinomial using Distributions: Bernoulli, Beta, Gamma, Normal, sample using DynamicPPL: getspace @@ -7,8 +9,6 @@ import Random using Test: @test, @test_throws, @testset using Turing -include(pkgdir(Turing) * "/test/test_utils/models.jl") - @testset "SMC" begin @testset "constructor" begin s = SMC() diff --git a/test/mcmc/sghmc.jl b/test/mcmc/sghmc.jl index a84f005f7..f65ca1c37 100644 --- a/test/mcmc/sghmc.jl +++ b/test/mcmc/sghmc.jl @@ -1,5 +1,7 @@ module SGHMCTests +using ..Models: gdemo_default +using ..NumericalTests: check_gdemo using Distributions: sample import ForwardDiff using LinearAlgebra: dot @@ -8,9 +10,6 @@ using StableRNGs: StableRNG using Test: @test, @testset using Turing -include(pkgdir(Turing) * "/test/test_utils/models.jl") -include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") - @testset "Testing sghmc.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false)) @testset "sghmc constructor" begin alg = SGHMC(; learning_rate=0.01, momentum_decay=0.1, adtype=adbackend) diff --git a/test/mcmc/utilities.jl b/test/mcmc/utilities.jl index 16d3b379e..2b5e04b2d 100644 --- a/test/mcmc/utilities.jl +++ b/test/mcmc/utilities.jl @@ -1,5 +1,6 @@ module MCMCUtilitiesTests +using ..Models: gdemo_default using Distributions: Normal, sample, truncated using LinearAlgebra: I, vec import Random @@ -7,8 +8,6 @@ using Random: MersenneTwister using Test: @test, @testset using Turing -include(pkgdir(Turing) * "/test/test_utils/models.jl") - @testset "predict" begin Random.seed!(100) diff --git a/test/optimisation/OptimInterface.jl b/test/optimisation/OptimInterface.jl index 749ddc015..d26be7f85 100644 --- a/test/optimisation/OptimInterface.jl +++ b/test/optimisation/OptimInterface.jl @@ -1,5 +1,6 @@ module OptimInterfaceTests +using ..Models: gdemo_default using Distributions.FillArrays: Zeros using LinearAlgebra: I import Optim @@ -10,8 +11,6 @@ using StatsBase: coef, coefnames, coeftable, informationmatrix, stderror, vcov using Test: @test, @testset using Turing -include(pkgdir(Turing) * "/test/test_utils/models.jl") - # Used for testing how well it works with nested contexts. struct OverrideContext{C,T1,T2} <: DynamicPPL.AbstractContext context::C diff --git a/test/runtests.jl b/test/runtests.jl index 126d8b29f..bc5215aa1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -5,6 +5,9 @@ using Test using TimerOutputs: TimerOutputs, @timeit import Turing +include(pkgdir(Turing) * "/test/test_utils/models.jl") +include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") + Turing.setprogress!(false) included_paths, excluded_paths = parse_args(ARGS) diff --git a/test/stdlib/distributions.jl b/test/stdlib/distributions.jl index 17d8725d0..f1ce701da 100644 --- a/test/stdlib/distributions.jl +++ b/test/stdlib/distributions.jl @@ -1,5 +1,6 @@ module DistributionsTests +using ..NumericalTests: check_dist_numerical using Distributions using LinearAlgebra: I import Random @@ -8,8 +9,6 @@ using StatsFuns: logistic using Test: @testset, @test using Turing -include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") - @testset "distributions.jl" begin rng = StableRNG(12345) @testset "distributions functions" begin diff --git a/test/test_utils/models.jl b/test/test_utils/models.jl index fc392b050..94790a041 100644 --- a/test/test_utils/models.jl +++ b/test/test_utils/models.jl @@ -1,3 +1,11 @@ +module Models + +export MoGtest, MoGtest_default, MoGtest_default_z_vector, MoGtest_z_vector, gdemo, gdemo_d, + gdemo_default + +using Distributions +using Turing: @model + # The old-gdemo model. @model function gdemo(x, y) s ~ InverseGamma(2, 3) @@ -83,5 +91,4 @@ end MoGtest_default_z_vector = MoGtest_z_vector([1.0 1.0 4.0 4.0]) -# Declare empty model to make the Sampler constructor work. -@model empty_model() = x = 1 +end diff --git a/test/test_utils/numerical_tests.jl b/test/test_utils/numerical_tests.jl index 333a3f14a..b55d3c3e8 100644 --- a/test/test_utils/numerical_tests.jl +++ b/test/test_utils/numerical_tests.jl @@ -1,3 +1,11 @@ +module NumericalTests + +using Distributions +using Test: @test, @testset + +export check_MoGtest_default, check_MoGtest_default_z_vector, check_dist_numerical, + check_gdemo, check_numerical + function check_dist_numerical(dist, chn; mean_tol = 0.1, var_atol = 1.0, var_tol = 0.5) @testset "numerical" begin # Extract values. @@ -71,3 +79,5 @@ function check_MoGtest_default_z_vector(chain; atol=0.2, rtol=0.0) [1.0, 1.0, 2.0, 2.0, 1.0, 4.0], atol=atol, rtol=rtol) end + +end diff --git a/test/variational/advi.jl b/test/variational/advi.jl index 9cf420d9b..0f77150c0 100644 --- a/test/variational/advi.jl +++ b/test/variational/advi.jl @@ -1,5 +1,7 @@ module AdvancedVITests +using ..Models: gdemo_default +using ..NumericalTests: check_gdemo import AdvancedVI using AdvancedVI: TruncatedADAGrad, DecayedADAGrad using Distributions: Dirichlet, Normal @@ -10,9 +12,6 @@ using Test: @test, @testset using Turing using Turing.Essential: TuringDiagMvNormal -include(pkgdir(Turing) * "/test/test_utils/models.jl") -include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") - @testset "advi.jl" begin @testset "advi constructor" begin Random.seed!(0) diff --git a/test/variational/optimisers.jl b/test/variational/optimisers.jl index 2a67844df..8063cdf2e 100644 --- a/test/variational/optimisers.jl +++ b/test/variational/optimisers.jl @@ -6,9 +6,6 @@ import ReverseDiff using Test: @test, @testset using Turing -include(pkgdir(Turing) * "/test/test_utils/models.jl") -include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl") - function test_opt(ADPack, opt) θ = randn(10, 10) θ_fit = randn(10, 10) From 3ee5a2eb873641bd700bda0510f02955e475c447 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Fri, 31 May 2024 17:06:31 +0100 Subject: [PATCH 24/29] Add missing import to numerical_tests.jl --- test/test_utils/numerical_tests.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_utils/numerical_tests.jl b/test/test_utils/numerical_tests.jl index b55d3c3e8..cb583b517 100644 --- a/test/test_utils/numerical_tests.jl +++ b/test/test_utils/numerical_tests.jl @@ -1,6 +1,7 @@ module NumericalTests using Distributions +using MCMCChains: namesingroup using Test: @test, @testset export check_MoGtest_default, check_MoGtest_default_z_vector, check_dist_numerical, From 6a015a3550a3f6320e7813908bd8795509532def Mon Sep 17 00:00:00 2001 From: Hong Ge <3279477+yebai@users.noreply.github.com> Date: Mon, 3 Jun 2024 12:31:08 +0100 Subject: [PATCH 25/29] Update Project.toml (#2244) * Update README.md * Export adtype `AutoTapir` (#2236) * Export adtype `AutoTapir` * Update Project.toml * Fix missing AutoTapir (#2242) * Update Essential.jl * Update Project.toml * Drop support for ADTypes 0.2 (#2243) ADTypes 0.2 doesn't support AutoTapir yet. * Optimization improvements (#2221) * initial work on interface * Improving the Optimization.jl interface, work in progress * More work on Optimization.jl, still in progress * Add docstrings to Optimisation.jl * Fix OptimizationOptimJL version constraint * Clean up optimisation TODO notes * Relax OptimizationOptimJL version constraints * Simplify optimization imports * Remove commented out code * Small improvements all over in optimisation * Clean up of Optimisation tests * Add a test for OptimizationBBO * Add tests using OptimizationNLopt * Rename/move the optimisation test files The files for Optimisaton.jl and OptimInterface.jl were in the wrong folders: One in `test/optimisation` the other in `test/ext`, but the wrong way around. * Relax compat bounds on OptimizationBBO and OptimizationNLopt * Split a testset to test/optimisation/OptimisationCore.jl * Import AbstractADType from ADTypes, not SciMLBase * Fix Optimization.jl depwarning * Fix seeds in more tests * Merge OptimizationCore into Optimization * In optimisation, rename init_value to initial_params * Optimisation docstring improvements * Code style adjustments in optimisation * Qualify references in optimisation * Simplify creation of ModeResults * Qualified references in optimization tests * Enforce line length in optimization * Simplify optimisation exports * Enforce line legth in Optim.jl interface * Refactor away ModeEstimationProblem * Style and docstring improvements for optimisation * Add := test to optimisation tests. * Clarify comment * Simplify generate_initial_params * Fix doc references * Rename testsets * Refactor check_success * Make initial_params a kwarg * Remove unnecessary type constrain on kwarg * Fix broken reference in tests * Fix bug in generate_initial_params * Fix qualified references in optimisation tests * Add hasstats checks to optimisation tests * Extend OptimizationOptimJL compat to 0.3 Co-authored-by: Hong Ge <3279477+yebai@users.noreply.github.com> * Change some `import`s to `using` Co-authored-by: Tor Erlend Fjelde * Change to kwargs... in docstrings * Add a two-argument method to OptimLogDensity as callable --------- Co-authored-by: Tor Erlend Fjelde Co-authored-by: Hong Ge <3279477+yebai@users.noreply.github.com> * Update Project.toml * CompatHelper: bump compat for OptimizationOptimJL to 0.3 for package test, (keep existing compat) (#2246) Co-authored-by: CompatHelper Julia --------- Co-authored-by: Markus Hauru Co-authored-by: Tor Erlend Fjelde Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: CompatHelper Julia --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index e4b1c0b62..21790a359 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "Turing" uuid = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" -version = "0.32.3" +version = "0.33" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From de75f968822e447dc42ad5dc5b9809f784896402 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Mon, 3 Jun 2024 13:07:37 +0100 Subject: [PATCH 26/29] Set fail-fast: false for CI test matrix --- .github/workflows/Tests.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml index 81e5b147a..9223ef771 100644 --- a/.github/workflows/Tests.yml +++ b/.github/workflows/Tests.yml @@ -12,6 +12,7 @@ jobs: continue-on-error: ${{ matrix.version == 'nightly' }} strategy: + fail-fast: false matrix: test-args: # Run some of the slower test files individually. The last one catches everything From 5900ac90a5abf17b2dce945eb89764e858244a4c Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Mon, 3 Jun 2024 16:20:28 +0100 Subject: [PATCH 27/29] Add a step to print matrix variables to tests Action --- .github/workflows/Tests.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml index 9223ef771..7f8e8feec 100644 --- a/.github/workflows/Tests.yml +++ b/.github/workflows/Tests.yml @@ -60,6 +60,13 @@ jobs: arch: x86 steps: + - name: Print matrix variables + run: | + echo "OS: ${{ matrix.os }}" + echo "Architecture: ${{ matrix.arch }}" + echo "Julia version: ${{ matrix.version }}" + echo "Number of threads: ${{ matrix.num_threads }}" + echo "Test arguments: ${{ matrix.test_args }}" - uses: actions/checkout@v4 - uses: julia-actions/setup-julia@v2 with: From d2bfa5db4bd8fa96151d8502efa8e6b417d58414 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Mon, 3 Jun 2024 16:22:14 +0100 Subject: [PATCH 28/29] Fix typo in tests Action --- .github/workflows/Tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml index 7f8e8feec..4f8ac4dcc 100644 --- a/.github/workflows/Tests.yml +++ b/.github/workflows/Tests.yml @@ -66,7 +66,7 @@ jobs: echo "Architecture: ${{ matrix.arch }}" echo "Julia version: ${{ matrix.version }}" echo "Number of threads: ${{ matrix.num_threads }}" - echo "Test arguments: ${{ matrix.test_args }}" + echo "Test arguments: ${{ matrix.test-args }}" - uses: actions/checkout@v4 - uses: julia-actions/setup-julia@v2 with: From b1ee9830fb450e395340e05ffd8b2511e8621495 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Wed, 5 Jun 2024 00:43:05 +0200 Subject: [PATCH 29/29] ADTypes v0.2 compatibility for test restructuring (#2253) * Restore compat with ADTypes v0.2. Make AutoTapir export conditional. * Fix AutoTapir export in Essential.jl --- Project.toml | 4 +++- src/Turing.jl | 7 ++++++- src/essential/Essential.jl | 9 +++++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Project.toml b/Project.toml index 21790a359..99e7b316b 100644 --- a/Project.toml +++ b/Project.toml @@ -12,6 +12,7 @@ AdvancedPS = "576499cb-2369-40b2-a588-c64705576edc" AdvancedVI = "b5ca4192-6429-45e5-a2d9-87aec30a685c" BangBang = "198e06fe-97b7-11e9-32a5-e1d131e6ad66" Bijectors = "76274a88-744f-5084-9051-94815aaf08c4" +Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" DistributionsAD = "ced4e74d-a319-5a8a-b0ac-84af2272839c" @@ -48,7 +49,7 @@ TuringDynamicHMCExt = "DynamicHMC" TuringOptimExt = "Optim" [compat] -ADTypes = "1" +ADTypes = "0.2, 1" AbstractMCMC = "5.2" Accessors = "0.1" AdvancedHMC = "0.3.0, 0.4.0, 0.5.2, 0.6" @@ -63,6 +64,7 @@ DistributionsAD = "0.6" DocStringExtensions = "0.8, 0.9" DynamicHMC = "3.4" DynamicPPL = "0.27.1" +Compat = "4.15.0" EllipticalSliceSampling = "0.5, 1, 2" ForwardDiff = "0.10.3" Libtask = "0.7, 0.8" diff --git a/src/Turing.jl b/src/Turing.jl index 5562830af..4aa2fef2c 100644 --- a/src/Turing.jl +++ b/src/Turing.jl @@ -5,6 +5,7 @@ using DistributionsAD, Bijectors, StatsFuns, SpecialFunctions using Statistics, LinearAlgebra using Libtask @reexport using Distributions, MCMCChains, Libtask, AbstractMCMC, Bijectors +using Compat: pkgversion import AdvancedVI using DynamicPPL: DynamicPPL, LogDensityFunction @@ -107,7 +108,6 @@ export @model, # modelling AutoReverseDiff, AutoZygote, AutoTracker, - AutoTapir, setprogress!, # debugging @@ -144,6 +144,11 @@ export @model, # modelling MAP, MLE +# AutoTapir is only supported by ADTypes v1.0 and above. +@static if VERSION >= v"1.10" && pkgversion(ADTypes) >= v"1" + export AutoTapir +end + if !isdefined(Base, :get_extension) using Requires end diff --git a/src/essential/Essential.jl b/src/essential/Essential.jl index a3b2e4092..8cef7a681 100644 --- a/src/essential/Essential.jl +++ b/src/essential/Essential.jl @@ -11,7 +11,7 @@ using Bijectors: PDMatDistribution using AdvancedVI using StatsFuns: logsumexp, softmax @reexport using DynamicPPL -using ADTypes: ADTypes, AutoForwardDiff, AutoTracker, AutoReverseDiff, AutoZygote, AutoTapir +using ADTypes: ADTypes, AutoForwardDiff, AutoTracker, AutoReverseDiff, AutoZygote import AdvancedPS @@ -39,9 +39,14 @@ export @model, AutoTracker, AutoZygote, AutoReverseDiff, - AutoTapir, value, @logprob_str, @prob_str +# AutoTapir is only supported by ADTypes v1.0 and above. +@static if VERSION >= v"1.10" && pkgversion(ADTypes) >= v"1" + using ADTypes: AutoTapir + export AutoTapir +end + end # module