From b36c8b2396d9ec300d88127c9e7ddd2f091e3fa3 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 25 Sep 2024 12:49:43 +0100 Subject: [PATCH 01/33] CompatHelper: bump compat for DynamicPPL to 0.29, (keep existing compat) (#2339) Co-authored-by: CompatHelper Julia --- Project.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 5f5c86b04..2b454bd8d 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "Turing" uuid = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" -version = "0.34.1" +version = "0.34.2" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" @@ -58,13 +58,13 @@ AdvancedPS = "0.6.0" AdvancedVI = "0.2" BangBang = "0.4" Bijectors = "0.13.6" +Compat = "4.15.0" DataStructures = "0.18" Distributions = "0.23.3, 0.24, 0.25" DistributionsAD = "0.6" DocStringExtensions = "0.8, 0.9" DynamicHMC = "3.4" -DynamicPPL = "0.28.2" -Compat = "4.15.0" +DynamicPPL = "0.29" EllipticalSliceSampling = "0.5, 1, 2" ForwardDiff = "0.10.3" Libtask = "0.7, 0.8" From 356864946a232f6dc3697b4de88d1804dc570881 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 25 Sep 2024 12:50:00 +0100 Subject: [PATCH 02/33] CompatHelper: bump compat for DynamicPPL to 0.29 for package test, (keep existing compat) (#2340) Co-authored-by: CompatHelper Julia --- test/Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Project.toml b/test/Project.toml index 67292d2af..81f519610 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -45,7 +45,7 @@ Clustering = "0.14, 0.15" Distributions = "0.25" DistributionsAD = "0.6.3" DynamicHMC = "2.1.6, 3.0" -DynamicPPL = "0.28" +DynamicPPL = "0.28, 0.29" FiniteDifferences = "0.10.8, 0.11, 0.12" ForwardDiff = "0.10.12 - 0.10.32, 0.10" HypothesisTests = "0.11" From f43e57d741fbca5430c3ac287523f239f722dc6d Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Wed, 25 Sep 2024 14:20:44 +0100 Subject: [PATCH 03/33] Replace vectorize(d, r) -> DynamicPPL.tovec(r) vectorize was removed in DynamicPPL 0.29.0 --- src/mcmc/Inference.jl | 2 +- src/mcmc/ess.jl | 2 +- src/mcmc/particle_mcmc.jl | 2 +- test/Project.toml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mcmc/Inference.jl b/src/mcmc/Inference.jl index b7bdf206b..b89fbd2ff 100644 --- a/src/mcmc/Inference.jl +++ b/src/mcmc/Inference.jl @@ -5,7 +5,7 @@ using DynamicPPL: Metadata, VarInfo, TypedVarInfo, islinked, invlink!, link!, setindex!!, push!!, setlogp!!, getlogp, - VarName, getsym, vectorize, + VarName, getsym, _getvns, getdist, Model, Sampler, SampleFromPrior, SampleFromUniform, DefaultContext, PriorContext, diff --git a/src/mcmc/ess.jl b/src/mcmc/ess.jl index 2910a7efd..395456ee5 100644 --- a/src/mcmc/ess.jl +++ b/src/mcmc/ess.jl @@ -85,7 +85,7 @@ struct ESSPrior{M<:Model,S<:Sampler{<:ESS},V<:AbstractVarInfo,T} dist = getdist(varinfo, vn) EllipticalSliceSampling.isgaussian(typeof(dist)) || error("[ESS] only supports Gaussian prior distributions") - vectorize(dist, mean(dist)) + DynamicPPL.tovec(mean(dist)) end return new{M,S,V,typeof(μ)}(model, sampler, varinfo, μ) end diff --git a/src/mcmc/particle_mcmc.jl b/src/mcmc/particle_mcmc.jl index 02a53766e..991bb2977 100644 --- a/src/mcmc/particle_mcmc.jl +++ b/src/mcmc/particle_mcmc.jl @@ -380,7 +380,7 @@ function DynamicPPL.assume( elseif is_flagged(vi, vn, "del") unset_flag!(vi, vn, "del") # Reference particle parent r = rand(trng, dist) - vi[vn] = vectorize(dist, r) + vi[vn] = DynamicPPL.tovec(r) DynamicPPL.setgid!(vi, spl.selector, vn) setorder!(vi, vn, get_num_produce(vi)) else diff --git a/test/Project.toml b/test/Project.toml index 81f519610..150eb5df5 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -45,7 +45,7 @@ Clustering = "0.14, 0.15" Distributions = "0.25" DistributionsAD = "0.6.3" DynamicHMC = "2.1.6, 3.0" -DynamicPPL = "0.28, 0.29" +DynamicPPL = "0.29" FiniteDifferences = "0.10.8, 0.11, 0.12" ForwardDiff = "0.10.12 - 0.10.32, 0.10" HypothesisTests = "0.11" From c8e2337fef98f60282c04b696a853475497b9fd5 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 26 Sep 2024 23:24:08 +0100 Subject: [PATCH 04/33] Require Julia >= 1.9 --- .github/workflows/Tests.yml | 43 ++++++++++++++++++++----------------- Project.toml | 2 +- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml index 8de296e5e..cde61050f 100644 --- a/.github/workflows/Tests.yml +++ b/.github/workflows/Tests.yml @@ -8,25 +8,34 @@ on: jobs: test: + # Use matrix.test.name here to avoid it taking up the entire window width + name: test ${{matrix.test.name}} (${{ matrix.os }}, ${{ matrix.version }}, ${{ matrix.arch }}, ${{ matrix.num_threads }}) runs-on: ${{ matrix.os }} continue-on-error: ${{ matrix.version == 'nightly' }} strategy: fail-fast: false matrix: - test-args: + test: # 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 essential/ad.jl mcmc/gibbs.jl mcmc/hmc.jl mcmc/abstractmcmc.jl mcmc/Inference.jl experimental/gibbs.jl mcmc/ess.jl" + - name: "essential/ad" + args: "essential/ad.jl" + - name: "mcmc/gibbs" + args: "mcmc/gibbs.jl" + - name: "mcmc/hmc" + args: "mcmc/hmc.jl" + - name: "mcmc/abstractmcmc" + args: "mcmc/abstractmcmc.jl" + - name: "mcmc/Inference" + args: "mcmc/Inference.jl" + - name: "experimental/gibbs" + args: "experimental/gibbs.jl" + - name: "mcmc/ess" + args: "mcmc/ess.jl" + - name: "everything else" + args: "--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' os: - ubuntu-latest @@ -39,12 +48,8 @@ jobs: - 1 - 2 exclude: - # With Windows and macOS, only run Julia 1.7, x64, 2 threads. We just want to see + # With Windows and macOS, only run x64, 2 threads. We just want to see # some combination work on OSes other than Ubuntu. - - os: windows-latest - version: '1' - - os: macOS-latest - version: '1' - os: windows-latest arch: x86 - os: macOS-latest @@ -53,9 +58,7 @@ jobs: 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' - arch: x86 + # It's sufficient to test x86 with one thread. - num_threads: 2 arch: x86 @@ -66,7 +69,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 }}" - name: (De)activate coverage analysis run: echo "COVERAGE=${{ matrix.version == '1' && matrix.os == 'ubuntu-latest' && matrix.num_threads == 2 }}" >> "$GITHUB_ENV" shell: bash @@ -81,7 +84,7 @@ jobs: # Custom calls of Pkg.test tend to miss features such as e.g. adjustments for CompatHelper PRs # Ref https://github.com/julia-actions/julia-runtest/pull/73 - name: Call Pkg.test - run: julia --color=yes --inline=yes --depwarn=yes --check-bounds=yes --threads=${{ matrix.num_threads }} --project=@. -e 'import Pkg; Pkg.test(; coverage=parse(Bool, ENV["COVERAGE"]), test_args=ARGS)' -- ${{ matrix.test-args }} + run: julia --color=yes --inline=yes --depwarn=yes --check-bounds=yes --threads=${{ matrix.num_threads }} --project=@. -e 'import Pkg; Pkg.test(; coverage=parse(Bool, ENV["COVERAGE"]), test_args=ARGS)' -- ${{ matrix.test.args }} - uses: julia-actions/julia-processcoverage@v1 if: ${{ env.COVERAGE }} - uses: codecov/codecov-action@v4 diff --git a/Project.toml b/Project.toml index 2b454bd8d..9cfb99d00 100644 --- a/Project.toml +++ b/Project.toml @@ -87,7 +87,7 @@ Statistics = "1.6" StatsAPI = "1.6" StatsBase = "0.32, 0.33, 0.34" StatsFuns = "0.8, 0.9, 1" -julia = "1.7" +julia = "1.9" [extras] DynamicHMC = "bbc10e6e-7c05-544b-b16e-64fede858acb" From 97d6869e3d44d077c9df47cd83338d93e1258154 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Wed, 2 Oct 2024 20:52:04 +0100 Subject: [PATCH 05/33] Add Julia 1.9 tests back --- .github/workflows/Tests.yml | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml index cde61050f..b8319ca11 100644 --- a/.github/workflows/Tests.yml +++ b/.github/workflows/Tests.yml @@ -36,6 +36,7 @@ jobs: - name: "everything else" args: "--skip essential/ad.jl mcmc/gibbs.jl mcmc/hmc.jl mcmc/abstractmcmc.jl mcmc/Inference.jl experimental/gibbs.jl mcmc/ess.jl" version: + - '1.9' - '1' os: - ubuntu-latest @@ -50,6 +51,10 @@ jobs: exclude: # With Windows and macOS, only run x64, 2 threads. We just want to see # some combination work on OSes other than Ubuntu. + - os: windows-latest + version: '1' + - os: macOS-latest + version: '1' - os: windows-latest arch: x86 - os: macOS-latest @@ -58,9 +63,11 @@ jobs: num_threads: 1 - os: macOS-latest num_threads: 1 - # It's sufficient to test x86 with one thread. - - num_threads: 2 - arch: x86 + # It's sufficient to test x86 with only Julia 1.9 and 1 thread. + - arch: x86 + version: '1' + - arch: x86 + num_threads: 2 steps: - name: Print matrix variables From aac4628d5f97432596c924541d2a3b8b2e10e50a Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Wed, 2 Oct 2024 23:44:24 +0100 Subject: [PATCH 06/33] Fix essential/ad tests --- test/essential/ad.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/essential/ad.jl b/test/essential/ad.jl index 943b4eadc..5f29b8b5b 100644 --- a/test/essential/ad.jl +++ b/test/essential/ad.jl @@ -2,7 +2,7 @@ module AdTests using ..Models: gdemo_default using Distributions: logpdf -using DynamicPPL: getlogp, getval +using DynamicPPL: getlogp, getindex_internal using ForwardDiff using LinearAlgebra using LogDensityProblems: LogDensityProblems @@ -24,7 +24,7 @@ function test_model_ad(model, f, syms::Vector{Symbol}) s = syms[i] vnms[i] = getfield(vi.metadata, s).vns[1] - vals = getval(vi, vnms[i]) + vals = getindex_internal(vi, vnms[i]) for i in eachindex(vals) push!(vnvals, vals[i]) end @@ -61,8 +61,8 @@ end ad_test_f(vi, SampleFromPrior()) svn = vi.metadata.s.vns[1] mvn = vi.metadata.m.vns[1] - _s = getval(vi, svn)[1] - _m = getval(vi, mvn)[1] + _s = getindex_internal(vi, svn)[1] + _m = getindex_internal(vi, mvn)[1] dist_s = InverseGamma(2, 3) From 36e4651d8fe90aaf31e79b29dcc377db95dc01a7 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 3 Oct 2024 01:14:27 +0100 Subject: [PATCH 07/33] Fix reconstruct calls in MH --- src/mcmc/mh.jl | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/mcmc/mh.jl b/src/mcmc/mh.jl index ffc064eb1..2d2d5b099 100644 --- a/src/mcmc/mh.jl +++ b/src/mcmc/mh.jl @@ -285,10 +285,8 @@ end unvectorize(dists::AbstractVector) = length(dists) == 1 ? first(dists) : dists # possibly unpack and reshape samples according to the prior distribution -reconstruct(dist::Distribution, val::AbstractVector) = DynamicPPL.reconstruct(dist, val) -function reconstruct(dist::AbstractVector{<:UnivariateDistribution}, val::AbstractVector) - return val -end +reconstruct(dist::Distribution, val::AbstractVector) = DynamicPPL.from_vec_transform(dist)(val) +reconstruct(dist::AbstractVector{<:UnivariateDistribution}, val::AbstractVector) = val function reconstruct(dist::AbstractVector{<:MultivariateDistribution}, val::AbstractVector) offset = 0 return map(dist) do d @@ -322,7 +320,7 @@ end :( $name = reconstruct( unvectorize(DynamicPPL.getdist.(Ref(vi), vns.$name)), - DynamicPPL.getval(vi, vns.$name), + DynamicPPL.getindex_internal(vi, vns.$name), ) ) for name in names ] From 29957915a8affd5d41ae99eb339031da9691be6c Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 3 Oct 2024 01:17:35 +0100 Subject: [PATCH 08/33] Update src/mcmc/mh.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/mcmc/mh.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/mcmc/mh.jl b/src/mcmc/mh.jl index 2d2d5b099..0fb920b81 100644 --- a/src/mcmc/mh.jl +++ b/src/mcmc/mh.jl @@ -285,7 +285,9 @@ end unvectorize(dists::AbstractVector) = length(dists) == 1 ? first(dists) : dists # possibly unpack and reshape samples according to the prior distribution -reconstruct(dist::Distribution, val::AbstractVector) = DynamicPPL.from_vec_transform(dist)(val) +function reconstruct(dist::Distribution, val::AbstractVector) + return DynamicPPL.from_vec_transform(dist)(val) +end reconstruct(dist::AbstractVector{<:UnivariateDistribution}, val::AbstractVector) = val function reconstruct(dist::AbstractVector{<:MultivariateDistribution}, val::AbstractVector) offset = 0 From 8387f5095a91f13214f278499811de8a7f544876 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 3 Oct 2024 01:23:01 +0100 Subject: [PATCH 09/33] Require Julia 1.10 --- .github/workflows/Tests.yml | 4 ++-- Project.toml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml index b8319ca11..c1f752471 100644 --- a/.github/workflows/Tests.yml +++ b/.github/workflows/Tests.yml @@ -36,7 +36,7 @@ jobs: - name: "everything else" args: "--skip essential/ad.jl mcmc/gibbs.jl mcmc/hmc.jl mcmc/abstractmcmc.jl mcmc/Inference.jl experimental/gibbs.jl mcmc/ess.jl" version: - - '1.9' + - '1.10' - '1' os: - ubuntu-latest @@ -63,7 +63,7 @@ jobs: num_threads: 1 - os: macOS-latest num_threads: 1 - # It's sufficient to test x86 with only Julia 1.9 and 1 thread. + # It's sufficient to test x86 with only Julia 1.10 and 1 thread. - arch: x86 version: '1' - arch: x86 diff --git a/Project.toml b/Project.toml index 9cfb99d00..e2dfc3137 100644 --- a/Project.toml +++ b/Project.toml @@ -81,13 +81,13 @@ Random = "1" Optim = "1" Reexport = "0.2, 1" Requires = "0.5, 1.0" -SciMLBase = "1.92.1, 2" +SciMLBase = "2" SpecialFunctions = "0.7.2, 0.8, 0.9, 0.10, 1, 2" Statistics = "1.6" StatsAPI = "1.6" StatsBase = "0.32, 0.33, 0.34" StatsFuns = "0.8, 0.9, 1" -julia = "1.9" +julia = "1.10" [extras] DynamicHMC = "bbc10e6e-7c05-544b-b16e-64fede858acb" From 410d98e2cd0ff55d84747abe2590252952807bbf Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 3 Oct 2024 01:33:15 +0100 Subject: [PATCH 10/33] Change 1 -> I in MvNormal() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit setting a real number as σ is deprecated --- test/mcmc/Inference.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mcmc/Inference.jl b/test/mcmc/Inference.jl index 15ec6149c..492329ab4 100644 --- a/test/mcmc/Inference.jl +++ b/test/mcmc/Inference.jl @@ -578,7 +578,7 @@ ADUtils.install_tapir && import Tapir ); true) @model function demo_incorrect_missing(y) - y[1:1] ~ MvNormal(zeros(1), 1) + y[1:1] ~ MvNormal(zeros(1), I) end @test_throws ErrorException sample( demo_incorrect_missing([missing]), NUTS(), 1000; check_model=true From ed17b7e9b2776887f405b7ccd8f9961a9be875c6 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 3 Oct 2024 13:01:48 +0100 Subject: [PATCH 11/33] Simplify tests as we no longer support Julia <1.8 --- Project.toml | 2 +- test/mcmc/hmc.jl | 20 ++++++++------------ test/mcmc/mh.jl | 34 +++++++++++++++------------------- 3 files changed, 24 insertions(+), 32 deletions(-) diff --git a/Project.toml b/Project.toml index e2dfc3137..8554521d7 100644 --- a/Project.toml +++ b/Project.toml @@ -56,7 +56,7 @@ AdvancedHMC = "0.3.0, 0.4.0, 0.5.2, 0.6" AdvancedMH = "0.8" AdvancedPS = "0.6.0" AdvancedVI = "0.2" -BangBang = "0.4" +BangBang = "0.4.2" Bijectors = "0.13.6" Compat = "4.15.0" DataStructures = "0.18" diff --git a/test/mcmc/hmc.jl b/test/mcmc/hmc.jl index dde977a6f..220c36928 100644 --- a/test/mcmc/hmc.jl +++ b/test/mcmc/hmc.jl @@ -269,19 +269,15 @@ ADUtils.install_tapir && import Tapir end end - # 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" - @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)) - xs[2] ~ Dirichlet(ones(5)) - end - model = vector_of_dirichlet() - chain = sample(model, NUTS(), 1000) - @test mean(Array(chain)) ≈ 0.2 + @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)) + xs[2] ~ Dirichlet(ones(5)) end + model = vector_of_dirichlet() + chain = sample(model, NUTS(), 1000) + @test mean(Array(chain)) ≈ 0.2 end @testset "issue: #2195" begin diff --git a/test/mcmc/mh.jl b/test/mcmc/mh.jl index a01d3dc25..2c6e7a6fe 100644 --- a/test/mcmc/mh.jl +++ b/test/mcmc/mh.jl @@ -185,28 +185,24 @@ GKernel(var) = (x) -> Normal(x, sqrt.(var)) # @test v1 < v2 end - # 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" - @testset "vector of multivariate distributions" begin - @model function test(k) - T = Vector{Vector{Float64}}(undef, k) - for i in 1:k - T[i] ~ Dirichlet(5, 1.0) - end + @testset "vector of multivariate distributions" begin + @model function test(k) + T = Vector{Vector{Float64}}(undef, k) + for i in 1:k + T[i] ~ Dirichlet(5, 1.0) end + end - Random.seed!(100) - chain = sample(test(1), MH(), 5_000) - for i in 1:5 - @test mean(chain, "T[1][$i]") ≈ 0.2 atol = 0.01 - end + Random.seed!(100) + chain = sample(test(1), MH(), 5_000) + for i in 1:5 + @test mean(chain, "T[1][$i]") ≈ 0.2 atol = 0.01 + end - Random.seed!(100) - chain = sample(test(10), MH(), 5_000) - for j in 1:10, i in 1:5 - @test mean(chain, "T[$j][$i]") ≈ 0.2 atol = 0.01 - end + Random.seed!(100) + chain = sample(test(10), MH(), 5_000) + for j in 1:10, i in 1:5 + @test mean(chain, "T[$j][$i]") ≈ 0.2 atol = 0.01 end end From 97ed363cfc4b0037df53c2b91102114fefbead46 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 3 Oct 2024 13:03:08 +0100 Subject: [PATCH 12/33] Simplify `set_namedtuple!` --- src/mcmc/mh.jl | 45 +++++++++++---------------------------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/src/mcmc/mh.jl b/src/mcmc/mh.jl index 0fb920b81..cfbbc70eb 100644 --- a/src/mcmc/mh.jl +++ b/src/mcmc/mh.jl @@ -212,43 +212,20 @@ end Places the values of a `NamedTuple` into the relevant places of a `VarInfo`. """ function set_namedtuple!(vi::DynamicPPL.VarInfoOrThreadSafeVarInfo, nt::NamedTuple) - # TODO: Replace this with something like - # for vn in keys(vi) - # vi = DynamicPPL.setindex!!(vi, get(nt, vn)) - # end for (n, vals) in pairs(nt) vns = vi.metadata[n].vns - nvns = length(vns) - - # if there is a single variable only - if nvns == 1 - # assign the unpacked values - if length(vals) == 1 - vi[vns[1]] = [vals[1];] - # otherwise just assign the values - else - vi[vns[1]] = [vals;] - end - # if there are multiple variables - elseif vals isa AbstractArray - nvals = length(vals) - # if values are provided as an array with a single element - if nvals == 1 - # iterate over variables and unpacked values - for (vn, val) in zip(vns, vals[1]) - vi[vn] = [val;] - end - # otherwise number of variables and number of values have to be equal - elseif nvals == nvns - # iterate over variables and values - for (vn, val) in zip(vns, vals) - vi[vn] = [val;] - end - else - error("Cannot assign `NamedTuple` to `VarInfo`") - end + if vals isa AbstractVector + vals = unvectorize(vals) + end + if length(vns) == 1 + # Only one variable, assign the values to it + DynamicPPL.setindex!(vi, vals, vns[1]) else - error("Cannot assign `NamedTuple` to `VarInfo`") + # Spread the values across the variables + length(vns) == length(vals) || error("Unequal number of variables and values") + for (vn, val) in zip(vns, vals) + DynamicPPL.setindex!(vi, val, vn) + end end end end From b9b68c4bdf31b679636a37549dd3732dec540859 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 3 Oct 2024 13:40:33 +0100 Subject: [PATCH 13/33] Remove conditional loading/exporting of Tapir --- Project.toml | 3 +-- src/Turing.jl | 21 +-------------------- src/essential/Essential.jl | 9 ++------- test/Project.toml | 2 ++ test/mcmc/Inference.jl | 3 +-- test/mcmc/abstractmcmc.jl | 3 +-- test/mcmc/gibbs.jl | 3 +-- test/mcmc/gibbs_conditional.jl | 3 +-- test/mcmc/hmc.jl | 4 +--- test/mcmc/sghmc.jl | 3 +-- test/test_utils/ad_utils.jl | 16 +++++----------- 11 files changed, 17 insertions(+), 53 deletions(-) diff --git a/Project.toml b/Project.toml index 8554521d7..3390af1bb 100644 --- a/Project.toml +++ b/Project.toml @@ -73,14 +73,13 @@ LogDensityProblems = "2" LogDensityProblemsAD = "1.7.0" MCMCChains = "5, 6" NamedArrays = "0.9, 0.10" +Optim = "1" Optimization = "3" OptimizationOptimJL = "0.1, 0.2, 0.3" OrderedCollections = "1" Printf = "1" Random = "1" -Optim = "1" Reexport = "0.2, 1" -Requires = "0.5, 1.0" SciMLBase = "2" SpecialFunctions = "0.7.2, 0.8, 0.9, 0.10, 1, 2" Statistics = "1.6" diff --git a/src/Turing.jl b/src/Turing.jl index 8dfb8df28..01529d449 100644 --- a/src/Turing.jl +++ b/src/Turing.jl @@ -107,6 +107,7 @@ export @model, # modelling AutoReverseDiff, AutoZygote, AutoTracker, + AutoTapir, setprogress!, # debugging Flat, FlatPos, @@ -136,24 +137,4 @@ 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 - -function __init__() - @static if !isdefined(Base, :get_extension) - @require Optim = "429524aa-4258-5aef-a3af-852621145aeb" include( - "../ext/TuringOptimExt.jl" - ) - @require DynamicHMC = "bbc10e6e-7c05-544b-b16e-64fede858acb" include( - "../ext/TuringDynamicHMCExt.jl" - ) - end -end - end diff --git a/src/essential/Essential.jl b/src/essential/Essential.jl index 778e2a62f..7b6794b91 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 +using ADTypes: ADTypes, AutoForwardDiff, AutoTracker, AutoReverseDiff, AutoZygote, AutoTapir using AdvancedPS: AdvancedPS @@ -23,13 +23,8 @@ export @model, AutoTracker, AutoZygote, AutoReverseDiff, + AutoTapir, @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 diff --git a/test/Project.toml b/test/Project.toml index 150eb5df5..3938580ff 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -30,6 +30,7 @@ SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b" StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3" StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c" +Tapir = "07d77754-e150-4737-8c94-cd238a1fb45b" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f" Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c" @@ -67,6 +68,7 @@ SpecialFunctions = "0.10.3, 1, 2" StableRNGs = "1" StatsBase = "0.33, 0.34" StatsFuns = "0.9.5, 1" +Tapir = "0.2.45" TimerOutputs = "0.5" Tracker = "0.2.11" Zygote = "0.5.4, 0.6" diff --git a/test/mcmc/Inference.jl b/test/mcmc/Inference.jl index 492329ab4..534198f75 100644 --- a/test/mcmc/Inference.jl +++ b/test/mcmc/Inference.jl @@ -12,11 +12,10 @@ using LinearAlgebra: I import MCMCChains import Random import ReverseDiff +import Tapir using Test: @test, @test_throws, @testset using Turing -ADUtils.install_tapir && import Tapir - @testset "Testing inference.jl with $adbackend" for adbackend in ADUtils.adbackends # Only test threading if 1.3+. if VERSION > v"1.2" diff --git a/test/mcmc/abstractmcmc.jl b/test/mcmc/abstractmcmc.jl index 43e3966a9..544fb57c5 100644 --- a/test/mcmc/abstractmcmc.jl +++ b/test/mcmc/abstractmcmc.jl @@ -12,12 +12,11 @@ using LogDensityProblemsAD: LogDensityProblemsAD using Random: Random using ReverseDiff: ReverseDiff using StableRNGs: StableRNG +import Tapir using Test: @test, @test_throws, @testset using Turing using Turing.Inference: AdvancedHMC -ADUtils.install_tapir && import Tapir - function initialize_nuts(model::Turing.Model) # Create a log-density function with an implementation of the # gradient so we ensure that we're using the same AD backend as in Turing. diff --git a/test/mcmc/gibbs.jl b/test/mcmc/gibbs.jl index 6868cb5e8..ebb6795a4 100644 --- a/test/mcmc/gibbs.jl +++ b/test/mcmc/gibbs.jl @@ -8,13 +8,12 @@ using Distributions: sample using ForwardDiff: ForwardDiff using Random: Random using ReverseDiff: ReverseDiff +import Tapir using Test: @test, @testset using Turing using Turing: Inference using Turing.RandomMeasures: ChineseRestaurantProcess, DirichletProcess -ADUtils.install_tapir && import Tapir - @testset "Testing gibbs.jl with $adbackend" for adbackend in ADUtils.adbackends @testset "gibbs constructor" begin N = 500 diff --git a/test/mcmc/gibbs_conditional.jl b/test/mcmc/gibbs_conditional.jl index 3f02c7594..ba2a4a04e 100644 --- a/test/mcmc/gibbs_conditional.jl +++ b/test/mcmc/gibbs_conditional.jl @@ -12,11 +12,10 @@ using ReverseDiff: ReverseDiff using StableRNGs: StableRNG using StatsBase: counts using StatsFuns: StatsFuns +import Tapir using Test: @test, @testset using Turing -ADUtils.install_tapir && import Tapir - @testset "Testing gibbs conditionals.jl with $adbackend" for adbackend in ADUtils.adbackends Random.seed!(1000) rng = StableRNG(123) diff --git a/test/mcmc/hmc.jl b/test/mcmc/hmc.jl index 220c36928..7fd89b100 100644 --- a/test/mcmc/hmc.jl +++ b/test/mcmc/hmc.jl @@ -2,7 +2,6 @@ module HMCTests using ..Models: gdemo_default using ..ADUtils: ADTypeCheckContext -#using ..Models: gdemo using ..NumericalTests: check_gdemo, check_numerical import ..ADUtils using Distributions: Bernoulli, Beta, Categorical, Dirichlet, Normal, Wishart, sample @@ -15,11 +14,10 @@ using LinearAlgebra: I, dot, vec import Random using StableRNGs: StableRNG using StatsFuns: logistic +import Tapir using Test: @test, @test_logs, @testset using Turing -ADUtils.install_tapir && import Tapir - @testset "Testing hmc.jl with $adbackend" for adbackend in ADUtils.adbackends # Set a seed rng = StableRNG(123) diff --git a/test/mcmc/sghmc.jl b/test/mcmc/sghmc.jl index 95b3bc543..a390cf742 100644 --- a/test/mcmc/sghmc.jl +++ b/test/mcmc/sghmc.jl @@ -8,11 +8,10 @@ import ForwardDiff using LinearAlgebra: dot import ReverseDiff using StableRNGs: StableRNG +import Tapir using Test: @test, @testset using Turing -ADUtils.install_tapir && import Tapir - @testset "Testing sghmc.jl with $adbackend" for adbackend in ADUtils.adbackends @testset "sghmc constructor" begin alg = SGHMC(; learning_rate=0.01, momentum_decay=0.1, adtype=adbackend) diff --git a/test/test_utils/ad_utils.jl b/test/test_utils/ad_utils.jl index e900a8f69..c4a0fa06e 100644 --- a/test/test_utils/ad_utils.jl +++ b/test/test_utils/ad_utils.jl @@ -4,6 +4,7 @@ using ForwardDiff: ForwardDiff using Pkg: Pkg using Random: Random using ReverseDiff: ReverseDiff +using Tapir: Tapir using Test: Test using Tracker: Tracker using Turing: Turing @@ -42,6 +43,7 @@ const eltypes_by_adtype = Dict( Tracker.TrackedVecOrMat, Tracker.TrackedVector, ), + Turing.AutoTapir => (Tapir.CoDual,), ) """ @@ -281,17 +283,9 @@ end All the ADTypes on which we want to run the tests. """ adbackends = [ - Turing.AutoForwardDiff(; chunksize=0), Turing.AutoReverseDiff(; compile=false) + Turing.AutoForwardDiff(; chunksize=0), + Turing.AutoReverseDiff(; compile=false), + Turing.AutoTapir(false), ] -# Tapir isn't supported for older Julia versions, hence the check. -install_tapir = isdefined(Turing, :AutoTapir) -if install_tapir - # TODO(mhauru) Is there a better way to install optional dependencies like this? - Pkg.add("Tapir") - using Tapir - push!(adbackends, Turing.AutoTapir(false)) - push!(eltypes_by_adtype, Turing.AutoTapir => (Tapir.CoDual,)) -end - end From f42d3d8f2d6591e6a52247d3a05282d90ffb674b Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 3 Oct 2024 15:46:42 +0100 Subject: [PATCH 14/33] Tapir -> Mooncake --- src/Turing.jl | 2 +- src/essential/Essential.jl | 4 ++-- test/Project.toml | 3 +-- test/mcmc/Inference.jl | 2 +- test/mcmc/abstractmcmc.jl | 4 ++-- test/mcmc/gibbs.jl | 2 +- test/mcmc/gibbs_conditional.jl | 2 +- test/mcmc/hmc.jl | 2 +- test/mcmc/sghmc.jl | 2 +- test/test_utils/ad_utils.jl | 6 +++--- 10 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/Turing.jl b/src/Turing.jl index 01529d449..d5545b276 100644 --- a/src/Turing.jl +++ b/src/Turing.jl @@ -107,7 +107,7 @@ export @model, # modelling AutoReverseDiff, AutoZygote, AutoTracker, - AutoTapir, + AutoMooncake, setprogress!, # debugging Flat, FlatPos, diff --git a/src/essential/Essential.jl b/src/essential/Essential.jl index 7b6794b91..b3709df66 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, AutoMooncake using AdvancedPS: AdvancedPS @@ -23,7 +23,7 @@ export @model, AutoTracker, AutoZygote, AutoReverseDiff, - AutoTapir, + AutoMooncake, @logprob_str, @prob_str diff --git a/test/Project.toml b/test/Project.toml index 3938580ff..d3acf0bad 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -16,6 +16,7 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" LogDensityProblems = "6fdf6af0-433a-55f7-b3ed-c6c6e0b8df7c" LogDensityProblemsAD = "996a588d-648d-4e1f-a8f0-a84b347e47b1" MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d" +Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6" NamedArrays = "86f7a689-2022-50b4-a561-43c23ac3c673" Optim = "429524aa-4258-5aef-a3af-852621145aeb" Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba" @@ -30,7 +31,6 @@ SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b" StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3" StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c" -Tapir = "07d77754-e150-4737-8c94-cd238a1fb45b" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f" Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c" @@ -68,7 +68,6 @@ SpecialFunctions = "0.10.3, 1, 2" StableRNGs = "1" StatsBase = "0.33, 0.34" StatsFuns = "0.9.5, 1" -Tapir = "0.2.45" TimerOutputs = "0.5" Tracker = "0.2.11" Zygote = "0.5.4, 0.6" diff --git a/test/mcmc/Inference.jl b/test/mcmc/Inference.jl index 534198f75..9ffcd4014 100644 --- a/test/mcmc/Inference.jl +++ b/test/mcmc/Inference.jl @@ -12,7 +12,7 @@ using LinearAlgebra: I import MCMCChains import Random import ReverseDiff -import Tapir +import Mooncake using Test: @test, @test_throws, @testset using Turing diff --git a/test/mcmc/abstractmcmc.jl b/test/mcmc/abstractmcmc.jl index 544fb57c5..0f58b218f 100644 --- a/test/mcmc/abstractmcmc.jl +++ b/test/mcmc/abstractmcmc.jl @@ -12,7 +12,7 @@ using LogDensityProblemsAD: LogDensityProblemsAD using Random: Random using ReverseDiff: ReverseDiff using StableRNGs: StableRNG -import Tapir +import Mooncake using Test: @test, @test_throws, @testset using Turing using Turing.Inference: AdvancedHMC @@ -116,7 +116,7 @@ end @testset "External samplers" begin @testset "AdvancedHMC.jl" begin - # TODO(mhauru) The below tests fail with Tapir, see + # TODO(mhauru) The below tests fail with Mooncake, see # https://github.com/TuringLang/Turing.jl/pull/2289. # Once that is fixed, this should say `for adtype in ADUtils.adbackends`. @testset "adtype=$adtype" for adtype in [AutoForwardDiff(), AutoReverseDiff()] diff --git a/test/mcmc/gibbs.jl b/test/mcmc/gibbs.jl index ebb6795a4..cd044910b 100644 --- a/test/mcmc/gibbs.jl +++ b/test/mcmc/gibbs.jl @@ -8,7 +8,7 @@ using Distributions: sample using ForwardDiff: ForwardDiff using Random: Random using ReverseDiff: ReverseDiff -import Tapir +import Mooncake using Test: @test, @testset using Turing using Turing: Inference diff --git a/test/mcmc/gibbs_conditional.jl b/test/mcmc/gibbs_conditional.jl index ba2a4a04e..d6d81cbe0 100644 --- a/test/mcmc/gibbs_conditional.jl +++ b/test/mcmc/gibbs_conditional.jl @@ -12,7 +12,7 @@ using ReverseDiff: ReverseDiff using StableRNGs: StableRNG using StatsBase: counts using StatsFuns: StatsFuns -import Tapir +import Mooncake using Test: @test, @testset using Turing diff --git a/test/mcmc/hmc.jl b/test/mcmc/hmc.jl index 7fd89b100..7404dbf43 100644 --- a/test/mcmc/hmc.jl +++ b/test/mcmc/hmc.jl @@ -14,7 +14,7 @@ using LinearAlgebra: I, dot, vec import Random using StableRNGs: StableRNG using StatsFuns: logistic -import Tapir +import Mooncake using Test: @test, @test_logs, @testset using Turing diff --git a/test/mcmc/sghmc.jl b/test/mcmc/sghmc.jl index a390cf742..1f8179503 100644 --- a/test/mcmc/sghmc.jl +++ b/test/mcmc/sghmc.jl @@ -8,7 +8,7 @@ import ForwardDiff using LinearAlgebra: dot import ReverseDiff using StableRNGs: StableRNG -import Tapir +import Mooncake using Test: @test, @testset using Turing diff --git a/test/test_utils/ad_utils.jl b/test/test_utils/ad_utils.jl index c4a0fa06e..5fc05dd9c 100644 --- a/test/test_utils/ad_utils.jl +++ b/test/test_utils/ad_utils.jl @@ -4,7 +4,7 @@ using ForwardDiff: ForwardDiff using Pkg: Pkg using Random: Random using ReverseDiff: ReverseDiff -using Tapir: Tapir +using Mooncake: Mooncake using Test: Test using Tracker: Tracker using Turing: Turing @@ -43,7 +43,7 @@ const eltypes_by_adtype = Dict( Tracker.TrackedVecOrMat, Tracker.TrackedVector, ), - Turing.AutoTapir => (Tapir.CoDual,), + Turing.AutoMooncake => (Mooncake.CoDual,), ) """ @@ -285,7 +285,7 @@ All the ADTypes on which we want to run the tests. adbackends = [ Turing.AutoForwardDiff(; chunksize=0), Turing.AutoReverseDiff(; compile=false), - Turing.AutoTapir(false), + Turing.AutoMooncake(; config=Mooncake.Config()), ] end From 7b2257089c7d174f2fefd67138cc7f13a5983b75 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 3 Oct 2024 17:31:23 +0100 Subject: [PATCH 15/33] Update src/essential/Essential.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/essential/Essential.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/essential/Essential.jl b/src/essential/Essential.jl index b3709df66..82b67ccac 100644 --- a/src/essential/Essential.jl +++ b/src/essential/Essential.jl @@ -11,7 +11,8 @@ using Bijectors: PDMatDistribution using AdvancedVI using StatsFuns: logsumexp, softmax @reexport using DynamicPPL -using ADTypes: ADTypes, AutoForwardDiff, AutoTracker, AutoReverseDiff, AutoZygote, AutoMooncake +using ADTypes: + ADTypes, AutoForwardDiff, AutoTracker, AutoReverseDiff, AutoZygote, AutoMooncake using AdvancedPS: AdvancedPS From 5e56f09aab7443fd1067df8e725bf3d550f442c3 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 3 Oct 2024 16:49:36 +0100 Subject: [PATCH 16/33] Remove Requires from Project.toml --- Project.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Project.toml b/Project.toml index 3390af1bb..0b3788905 100644 --- a/Project.toml +++ b/Project.toml @@ -32,7 +32,6 @@ OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" -Requires = "ae029012-a4dd-5104-9daa-d747884805df" SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462" SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" From d6d0d2166cc19c1e08453e29a1d3849caa615595 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Fri, 4 Oct 2024 00:31:28 +0100 Subject: [PATCH 17/33] Bump minor version instead --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 0b3788905..64bfa7687 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "Turing" uuid = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" -version = "0.34.2" +version = "0.35.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From e9e20dc7496087e20f3fd39e99cae381e889ce27 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Fri, 4 Oct 2024 00:32:38 +0100 Subject: [PATCH 18/33] Restrict ADTypes to 1.9.0 for AutoMooncake() --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 64bfa7687..3ba0f9e5b 100644 --- a/Project.toml +++ b/Project.toml @@ -48,7 +48,7 @@ TuringDynamicHMCExt = "DynamicHMC" TuringOptimExt = "Optim" [compat] -ADTypes = "0.2, 1" +ADTypes = "1.9" AbstractMCMC = "5.2" Accessors = "0.1" AdvancedHMC = "0.3.0, 0.4.0, 0.5.2, 0.6" From 452d0d09dee04031a253402ec0b1a36c623d6b48 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Fri, 4 Oct 2024 00:47:38 +0100 Subject: [PATCH 19/33] Re-enable Mooncake tests in mcmc/abstractmcmc --- test/mcmc/abstractmcmc.jl | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/mcmc/abstractmcmc.jl b/test/mcmc/abstractmcmc.jl index 0f58b218f..449b43b71 100644 --- a/test/mcmc/abstractmcmc.jl +++ b/test/mcmc/abstractmcmc.jl @@ -116,10 +116,7 @@ end @testset "External samplers" begin @testset "AdvancedHMC.jl" begin - # TODO(mhauru) The below tests fail with Mooncake, see - # https://github.com/TuringLang/Turing.jl/pull/2289. - # Once that is fixed, this should say `for adtype in ADUtils.adbackends`. - @testset "adtype=$adtype" for adtype in [AutoForwardDiff(), AutoReverseDiff()] + @testset "adtype=$adtype" for adtype in ADUtils.adbackends @testset "$(model.f)" for model in DynamicPPL.TestUtils.DEMO_MODELS # Need some functionality to initialize the sampler. # TODO: Remove this once the constructors in the respective packages become "lazy". From b0bb31e26c7a577d0aa273a87e9faf072937bd0a Mon Sep 17 00:00:00 2001 From: Tor Erlend Fjelde Date: Mon, 7 Oct 2024 12:10:42 +0200 Subject: [PATCH 20/33] Update the currently buggy and incorrect tilde overloads in `mh.jl` (#2360) --- src/mcmc/mh.jl | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/mcmc/mh.jl b/src/mcmc/mh.jl index cfbbc70eb..67c7e3612 100644 --- a/src/mcmc/mh.jl +++ b/src/mcmc/mh.jl @@ -442,42 +442,45 @@ end #### #### Compiler interface, i.e. tilde operators. #### -function DynamicPPL.assume(rng, spl::Sampler{<:MH}, dist::Distribution, vn::VarName, vi) +function DynamicPPL.assume( + rng::Random.AbstractRNG, spl::Sampler{<:MH}, dist::Distribution, vn::VarName, vi +) + # Just defer to `SampleFromPrior`. + retval = DynamicPPL.assume(rng, SampleFromPrior(), dist, vn, vi) + # Update the Gibbs IDs because they might have been assigned in the `SampleFromPrior` call. DynamicPPL.updategid!(vi, vn, spl) - r = vi[vn] - return r, logpdf_with_trans(dist, r, istrans(vi, vn)), vi + # Return. + return retval end function DynamicPPL.dot_assume( rng, spl::Sampler{<:MH}, dist::MultivariateDistribution, - vn::VarName, + vns::AbstractVector{<:VarName}, var::AbstractMatrix, - vi, + vi::AbstractVarInfo, ) - @assert dim(dist) == size(var, 1) - getvn = i -> VarName(vn, vn.indexing * "[:,$i]") - vns = getvn.(1:size(var, 2)) - DynamicPPL.updategid!.(Ref(vi), vns, Ref(spl)) - r = vi[vns] - var .= r - return var, sum(logpdf_with_trans(dist, r, istrans(vi, vns[1]))), vi + # Just defer to `SampleFromPrior`. + retval = DynamicPPL.dot_assume(rng, SampleFromPrior(), dist, vns[1], var, vi) + # Update the Gibbs IDs because they might have been assigned in the `SampleFromPrior` call. + DynamicPPL.updategid!.((vi,), vns, (spl,)) + # Return. + return retval end function DynamicPPL.dot_assume( rng, spl::Sampler{<:MH}, dists::Union{Distribution,AbstractArray{<:Distribution}}, - vn::VarName, + vns::AbstractArray{<:VarName}, var::AbstractArray, - vi, + vi::AbstractVarInfo, ) - getvn = ind -> VarName(vn, vn.indexing * "[" * join(Tuple(ind), ",") * "]") - vns = getvn.(CartesianIndices(var)) - DynamicPPL.updategid!.(Ref(vi), vns, Ref(spl)) - r = reshape(vi[vec(vns)], size(var)) - var .= r - return var, sum(logpdf_with_trans.(dists, r, istrans(vi, vns[1]))), vi + # Just defer to `SampleFromPrior`. + retval = DynamicPPL.dot_assume(rng, SampleFromPrior(), dists, vns, var, vi) + # Update the Gibbs IDs because they might have been assigned in the `SampleFromPrior` call. + DynamicPPL.updategid!.((vi,), vns, (spl,)) + return retval end function DynamicPPL.observe(spl::Sampler{<:MH}, d::Distribution, value, vi) From 2e178d781ab497b798a8ab1534795dfbf10d02f2 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Fri, 4 Oct 2024 15:33:24 +0200 Subject: [PATCH 21/33] More autoformatting (#2359) * Bring more files into autoformatting fold * Run JuliaFormatter * Remove trailing whitespace * Update comment in .JuliaFormatter.toml --- .JuliaFormatter.toml | 9 +- .github/ISSUE_TEMPLATE/01-bug-report.yml | 6 +- .github/workflows/Tests.yml | 2 +- src/mcmc/Inference.jl | 270 +++++++++++++---------- test/mcmc/Inference.jl | 67 +++--- test/test_utils/numerical_tests.jl | 75 ++++--- 6 files changed, 232 insertions(+), 197 deletions(-) diff --git a/.JuliaFormatter.toml b/.JuliaFormatter.toml index d0e00b45f..15ecbc5c3 100644 --- a/.JuliaFormatter.toml +++ b/.JuliaFormatter.toml @@ -5,17 +5,10 @@ import_to_using = false # We ignore these files because when formatting was first put in place they were being worked on. # These ignores should be removed once the relevant PRs are merged/closed. ignore = [ - # https://github.com/TuringLang/Turing.jl/pull/2231/files + # https://github.com/TuringLang/Turing.jl/pull/2328/files "src/experimental/gibbs.jl", - "src/mcmc/abstractmcmc.jl", "test/experimental/gibbs.jl", - "test/test_utils/numerical_tests.jl", - # https://github.com/TuringLang/Turing.jl/pull/2218/files - "src/mcmc/Inference.jl", - "test/mcmc/Inference.jl", # https://github.com/TuringLang/Turing.jl/pull/1887 # Enzyme PR - "test/mcmc/Inference.jl", "test/mcmc/hmc.jl", "test/mcmc/sghmc.jl", - "test/runtests.jl", ] diff --git a/.github/ISSUE_TEMPLATE/01-bug-report.yml b/.github/ISSUE_TEMPLATE/01-bug-report.yml index 598371476..cc271e419 100644 --- a/.github/ISSUE_TEMPLATE/01-bug-report.yml +++ b/.github/ISSUE_TEMPLATE/01-bug-report.yml @@ -8,7 +8,7 @@ body: attributes: value: | Thank you for submitting a bug report to Turing.jl! - + To make sure we can pinpoint the issue and fix it as quickly as possible, we ask you to provide some information about the bug you encountered. Please fill out the form below. - type: textarea @@ -35,7 +35,7 @@ body: description: Paste the output of `versioninfo()` between the triple backticks value: |
versioninfo() - + ``` (Paste here) ``` @@ -50,7 +50,7 @@ body: description: Paste the output of `]st --manifest` between the triple backticks. value: |
]st --manifest - + ``` (Paste here) ``` diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml index c1f752471..7ef2ad36f 100644 --- a/.github/workflows/Tests.yml +++ b/.github/workflows/Tests.yml @@ -89,7 +89,7 @@ jobs: - uses: julia-actions/julia-buildpkg@v1 # TODO: Use julia-actions/julia-runtest when test_args are supported # Custom calls of Pkg.test tend to miss features such as e.g. adjustments for CompatHelper PRs - # Ref https://github.com/julia-actions/julia-runtest/pull/73 + # Ref https://github.com/julia-actions/julia-runtest/pull/73 - name: Call Pkg.test run: julia --color=yes --inline=yes --depwarn=yes --check-bounds=yes --threads=${{ matrix.num_threads }} --project=@. -e 'import Pkg; Pkg.test(; coverage=parse(Bool, ENV["COVERAGE"]), test_args=ARGS)' -- ${{ matrix.test.args }} - uses: julia-actions/julia-processcoverage@v1 diff --git a/src/mcmc/Inference.jl b/src/mcmc/Inference.jl index b89fbd2ff..118b16264 100644 --- a/src/mcmc/Inference.jl +++ b/src/mcmc/Inference.jl @@ -1,16 +1,32 @@ module Inference using ..Essential -using DynamicPPL: Metadata, VarInfo, TypedVarInfo, - islinked, invlink!, link!, - setindex!!, push!!, - setlogp!!, getlogp, - VarName, getsym, - _getvns, getdist, - Model, Sampler, SampleFromPrior, SampleFromUniform, - DefaultContext, PriorContext, - LikelihoodContext, set_flag!, unset_flag!, - getspace, inspace +using DynamicPPL: + Metadata, + VarInfo, + TypedVarInfo, + islinked, + invlink!, + link!, + setindex!!, + push!!, + setlogp!!, + getlogp, + VarName, + getsym, + _getvns, + getdist, + Model, + Sampler, + SampleFromPrior, + SampleFromUniform, + DefaultContext, + PriorContext, + LikelihoodContext, + set_flag!, + unset_flag!, + getspace, + inspace using Distributions, Libtask, Bijectors using DistributionsAD: VectorOfMultivariate using LinearAlgebra @@ -25,8 +41,10 @@ using Accessors: Accessors import ADTypes import AbstractMCMC -import AdvancedHMC; const AHMC = AdvancedHMC -import AdvancedMH; const AMH = AdvancedMH +import AdvancedHMC +const AHMC = AdvancedHMC +import AdvancedMH +const AMH = AdvancedMH import AdvancedPS import Accessors import EllipticalSliceSampling @@ -36,35 +54,35 @@ import Random import MCMCChains import StatsBase: predict -export InferenceAlgorithm, - Hamiltonian, - StaticHamiltonian, - AdaptiveHamiltonian, - SampleFromUniform, - SampleFromPrior, - MH, - ESS, - Emcee, - Gibbs, # classic sampling - GibbsConditional, - HMC, - SGLD, - PolynomialStepsize, - SGHMC, - HMCDA, - NUTS, # Hamiltonian-like sampling - IS, - SMC, - CSMC, - PG, - Prior, - assume, - dot_assume, - observe, - dot_observe, - predict, - isgibbscomponent, - externalsampler +export InferenceAlgorithm, + Hamiltonian, + StaticHamiltonian, + AdaptiveHamiltonian, + SampleFromUniform, + SampleFromPrior, + MH, + ESS, + Emcee, + Gibbs, # classic sampling + GibbsConditional, + HMC, + SGLD, + PolynomialStepsize, + SGHMC, + HMCDA, + NUTS, # Hamiltonian-like sampling + IS, + SMC, + CSMC, + PG, + Prior, + assume, + dot_assume, + observe, + dot_observe, + predict, + isgibbscomponent, + externalsampler ####################### # Sampler abstraction # @@ -86,7 +104,8 @@ The `Unconstrained` type-parameter is to indicate whether the sampler requires u # Fields $(TYPEDFIELDS) """ -struct ExternalSampler{S<:AbstractSampler,AD<:ADTypes.AbstractADType,Unconstrained} <: InferenceAlgorithm +struct ExternalSampler{S<:AbstractSampler,AD<:ADTypes.AbstractADType,Unconstrained} <: + InferenceAlgorithm "the sampler to wrap" sampler::S "the automatic differentiation (AD) backend to use" @@ -105,10 +124,12 @@ struct ExternalSampler{S<:AbstractSampler,AD<:ADTypes.AbstractADType,Unconstrain function ExternalSampler( sampler::AbstractSampler, adtype::ADTypes.AbstractADType, - ::Val{unconstrained}=Val(true) + ::Val{unconstrained}=Val(true), ) where {unconstrained} if !(unconstrained isa Bool) - throw(ArgumentError("Expected Val{true} or Val{false}, got Val{$unconstrained}")) + throw( + ArgumentError("Expected Val{true} or Val{false}, got Val{$unconstrained}") + ) end return new{typeof(sampler),typeof(adtype),unconstrained}(sampler, adtype) end @@ -121,7 +142,9 @@ DynamicPPL.getspace(::ExternalSampler) = () Return `true` if the sampler requires unconstrained space, and `false` otherwise. """ -requires_unconstrained_space(::ExternalSampler{<:Any,<:Any,Unconstrained}) where {Unconstrained} = Unconstrained +requires_unconstrained_space( + ::ExternalSampler{<:Any,<:Any,Unconstrained} +) where {Unconstrained} = Unconstrained """ externalsampler(sampler::AbstractSampler; adtype=AutoForwardDiff(), unconstrained=true) @@ -135,18 +158,21 @@ Wrap a sampler so it can be used as an inference algorithm. - `adtype::ADTypes.AbstractADType=ADTypes.AutoForwardDiff()`: The automatic differentiation (AD) backend to use. - `unconstrained::Bool=true`: Whether the sampler requires unconstrained space. """ -function externalsampler(sampler::AbstractSampler; adtype=Turing.DEFAULT_ADTYPE, unconstrained::Bool=true) +function externalsampler( + sampler::AbstractSampler; adtype=Turing.DEFAULT_ADTYPE, unconstrained::Bool=true +) return ExternalSampler(sampler, adtype, Val(unconstrained)) end - getADType(spl::Sampler) = getADType(spl.alg) getADType(::SampleFromPrior) = Turing.DEFAULT_ADTYPE getADType(ctx::DynamicPPL.SamplingContext) = getADType(ctx.sampler) getADType(ctx::DynamicPPL.AbstractContext) = getADType(DynamicPPL.NodeTrait(ctx), ctx) getADType(::DynamicPPL.IsLeaf, ctx::DynamicPPL.AbstractContext) = Turing.DEFAULT_ADTYPE -getADType(::DynamicPPL.IsParent, ctx::DynamicPPL.AbstractContext) = getADType(DynamicPPL.childcontext(ctx)) +function getADType(::DynamicPPL.IsParent, ctx::DynamicPPL.AbstractContext) + return getADType(DynamicPPL.childcontext(ctx)) +end getADType(alg::Hamiltonian) = alg.adtype @@ -156,7 +182,7 @@ end function LogDensityProblems.logdensity( f::Turing.LogDensityFunction{<:AbstractVarInfo,<:Model,<:DynamicPPL.DefaultContext}, - x::NamedTuple + x::NamedTuple, ) return DynamicPPL.logjoint(f.model, DynamicPPL.unflatten(f.varinfo, x)) end @@ -166,7 +192,9 @@ function DynamicPPL.unflatten(vi::TypedVarInfo, θ::NamedTuple) set_namedtuple!(deepcopy(vi), θ) return vi end -DynamicPPL.unflatten(vi::SimpleVarInfo, θ::NamedTuple) = SimpleVarInfo(θ, vi.logp, vi.transformation) +function DynamicPPL.unflatten(vi::SimpleVarInfo, θ::NamedTuple) + return SimpleVarInfo(θ, vi.logp, vi.transformation) +end # Algorithm for sampling from the prior struct Prior <: InferenceAlgorithm end @@ -178,13 +206,13 @@ function AbstractMCMC.step( state=nothing; kwargs..., ) - vi = last(DynamicPPL.evaluate!!( - model, - VarInfo(), - SamplingContext( - rng, DynamicPPL.SampleFromPrior(), DynamicPPL.PriorContext() - ) - )) + vi = last( + DynamicPPL.evaluate!!( + model, + VarInfo(), + SamplingContext(rng, DynamicPPL.SampleFromPrior(), DynamicPPL.PriorContext()), + ), + ) return vi, nothing end @@ -215,10 +243,10 @@ getstats(t) = nothing abstract type AbstractTransition end -struct Transition{T, F<:AbstractFloat, S<:Union{NamedTuple, Nothing}} <: AbstractTransition - θ :: T - lp :: F # TODO: merge `lp` with `stat` - stat :: S +struct Transition{T,F<:AbstractFloat,S<:Union{NamedTuple,Nothing}} <: AbstractTransition + θ::T + lp::F # TODO: merge `lp` with `stat` + stat::S end Transition(θ, lp) = Transition(θ, lp, nothing) @@ -231,16 +259,16 @@ end function metadata(t::Transition) stat = t.stat if stat === nothing - return (lp = t.lp,) + return (lp=t.lp,) else - return merge((lp = t.lp,), stat) + return merge((lp=t.lp,), stat) end end DynamicPPL.getlogp(t::Transition) = t.lp # Metadata of VarInfo object -metadata(vi::AbstractVarInfo) = (lp = getlogp(vi),) +metadata(vi::AbstractVarInfo) = (lp=getlogp(vi),) # TODO: Implement additional checks for certain samplers, e.g. # HMC not supporting discrete parameters. @@ -256,10 +284,7 @@ end ######################################### function AbstractMCMC.sample( - model::AbstractModel, - alg::InferenceAlgorithm, - N::Integer; - kwargs... + model::AbstractModel, alg::InferenceAlgorithm, N::Integer; kwargs... ) return AbstractMCMC.sample(Random.default_rng(), model, alg, N; kwargs...) end @@ -270,7 +295,7 @@ function AbstractMCMC.sample( alg::InferenceAlgorithm, N::Integer; check_model::Bool=true, - kwargs... + kwargs..., ) check_model && _check_model(model, alg) return AbstractMCMC.sample(rng, model, Sampler(alg, model), N; kwargs...) @@ -282,10 +307,11 @@ function AbstractMCMC.sample( ensemble::AbstractMCMC.AbstractMCMCEnsemble, N::Integer, n_chains::Integer; - kwargs... + kwargs..., ) - return AbstractMCMC.sample(Random.default_rng(), model, alg, ensemble, N, n_chains; - kwargs...) + return AbstractMCMC.sample( + Random.default_rng(), model, alg, ensemble, N, n_chains; kwargs... + ) end function AbstractMCMC.sample( @@ -296,11 +322,12 @@ function AbstractMCMC.sample( N::Integer, n_chains::Integer; check_model::Bool=true, - kwargs... + kwargs..., ) check_model && _check_model(model, alg) - return AbstractMCMC.sample(rng, model, Sampler(alg, model), ensemble, N, n_chains; - kwargs...) + return AbstractMCMC.sample( + rng, model, Sampler(alg, model), ensemble, N, n_chains; kwargs... + ) end function AbstractMCMC.sample( @@ -312,10 +339,19 @@ function AbstractMCMC.sample( n_chains::Integer; chain_type=MCMCChains.Chains, progress=PROGRESS[], - kwargs... + kwargs..., ) - return AbstractMCMC.mcmcsample(rng, model, sampler, ensemble, N, n_chains; - chain_type=chain_type, progress=progress, kwargs...) + return AbstractMCMC.mcmcsample( + rng, + model, + sampler, + ensemble, + N, + n_chains; + chain_type=chain_type, + progress=progress, + kwargs..., + ) end ########################## @@ -349,7 +385,6 @@ function getparams(model::DynamicPPL.Model, vi::DynamicPPL.VarInfo) return mapreduce(collect, vcat, iters) end - function _params_to_array(model::DynamicPPL.Model, ts::Vector) names_set = OrderedSet{VarName}() # Extract the parameter names and values from each transition. @@ -364,9 +399,9 @@ function _params_to_array(model::DynamicPPL.Model, ts::Vector) return OrderedDict(zip(nms, vs)) end names = collect(names_set) - vals = [get(dicts[i], key, missing) for i in eachindex(dicts), - (j, key) in enumerate(names)] - + vals = [ + get(dicts[i], key, missing) for i in eachindex(dicts), (j, key) in enumerate(names) + ] return names, vals end @@ -382,7 +417,7 @@ function get_transition_extras(ts::AbstractVector) return names_values(extra_data) end -function names_values(extra_data::AbstractVector{<:NamedTuple{names}}) where names +function names_values(extra_data::AbstractVector{<:NamedTuple{names}}) where {names} values = [getfield(data, name) for data in extra_data, name in names] return collect(names), values end @@ -398,10 +433,7 @@ function names_values(xs::AbstractVector{<:NamedTuple}) names_unique = collect(names_set) # Extract all values as matrix. - values = [ - haskey(x, name) ? x[name] : missing - for x in xs, name in names_unique - ] + values = [haskey(x, name) ? x[name] : missing for x in xs, name in names_unique] return names_unique, values end @@ -416,13 +448,13 @@ function AbstractMCMC.bundle_samples( spl::Union{Sampler{<:InferenceAlgorithm},SampleFromPrior}, state, chain_type::Type{MCMCChains.Chains}; - save_state = false, - stats = missing, - sort_chain = false, - include_varname_to_symbol = true, - discard_initial = 0, - thinning = 1, - kwargs... + save_state=false, + stats=missing, + sort_chain=false, + include_varname_to_symbol=true, + discard_initial=0, + thinning=1, + kwargs..., ) # Convert transitions to array format. # Also retrieve the variable names. @@ -443,11 +475,11 @@ function AbstractMCMC.bundle_samples( info = NamedTuple() if include_varname_to_symbol - info = merge(info, (varname_to_symbol = OrderedDict(zip(varnames, varnames_symbol)),)) + info = merge(info, (varname_to_symbol=OrderedDict(zip(varnames, varnames_symbol)),)) end if save_state - info = merge(info, (model = model, sampler = spl, samplerstate = state)) + info = merge(info, (model=model, sampler=spl, samplerstate=state)) end # Merge in the timing info, if available @@ -462,7 +494,7 @@ function AbstractMCMC.bundle_samples( chain = MCMCChains.Chains( parray, nms, - (internals = extra_params,); + (internals=extra_params,); evidence=le, info=info, start=discard_initial + 1, @@ -479,7 +511,7 @@ function AbstractMCMC.bundle_samples( spl::Union{Sampler{<:InferenceAlgorithm},SampleFromPrior}, state, chain_type::Type{Vector{NamedTuple}}; - kwargs... + kwargs..., ) return map(ts) do t # Construct a dictionary of pairs `vn => value`. @@ -545,15 +577,13 @@ for alg in (:SMC, :PG, :MH, :IS, :ESS, :Gibbs, :Emcee) @eval DynamicPPL.getspace(::$alg{space}) where {space} = space end for alg in (:HMC, :HMCDA, :NUTS, :SGLD, :SGHMC) - @eval DynamicPPL.getspace(::$alg{<:Any, space}) where {space} = space + @eval DynamicPPL.getspace(::$alg{<:Any,space}) where {space} = space end function DynamicPPL.get_matching_type( - spl::Sampler{<:Union{PG, SMC}}, - vi, - ::Type{TV}, -) where {T, N, TV <: Array{T, N}} - return Array{T, N} + spl::Sampler{<:Union{PG,SMC}}, vi, ::Type{TV} +) where {T,N,TV<:Array{T,N}} + return Array{T,N} end ############## @@ -636,32 +666,34 @@ true function predict(model::Model, chain::MCMCChains.Chains; kwargs...) return predict(Random.default_rng(), model, chain; kwargs...) end -function predict(rng::AbstractRNG, model::Model, chain::MCMCChains.Chains; include_all = false) +function predict( + rng::AbstractRNG, model::Model, chain::MCMCChains.Chains; include_all=false +) # Don't need all the diagnostics chain_parameters = MCMCChains.get_sections(chain, :parameters) spl = DynamicPPL.SampleFromPrior() # Sample transitions using `spl` conditioned on values in `chain` - transitions = transitions_from_chain(rng, model, chain_parameters; sampler = spl) + transitions = transitions_from_chain(rng, model, chain_parameters; sampler=spl) # Let the Turing internals handle everything else for you chain_result = reduce( - MCMCChains.chainscat, [ + MCMCChains.chainscat, + [ AbstractMCMC.bundle_samples( - transitions[:, chain_idx], - model, - spl, - nothing, - MCMCChains.Chains - ) for chain_idx = 1:size(transitions, 2) - ] + transitions[:, chain_idx], model, spl, nothing, MCMCChains.Chains + ) for chain_idx in 1:size(transitions, 2) + ], ) parameter_names = if include_all names(chain_result, :parameters) else - filter(k -> ∉(k, names(chain_parameters, :parameters)), names(chain_result, :parameters)) + filter( + k -> ∉(k, names(chain_parameters, :parameters)), + names(chain_result, :parameters), + ) end return chain_result[parameter_names] @@ -716,11 +748,7 @@ julia> [first(t.θ.x) for t in transitions] # extract samples for `x` [-1.704630494695469] ``` """ -function transitions_from_chain( - model::Turing.Model, - chain::MCMCChains.Chains; - kwargs... -) +function transitions_from_chain(model::Turing.Model, chain::MCMCChains.Chains; kwargs...) return transitions_from_chain(Random.default_rng(), model, chain; kwargs...) end @@ -728,7 +756,7 @@ function transitions_from_chain( rng::Random.AbstractRNG, model::Turing.Model, chain::MCMCChains.Chains; - sampler = DynamicPPL.SampleFromPrior() + sampler=DynamicPPL.SampleFromPrior(), ) vi = Turing.VarInfo(model) diff --git a/test/mcmc/Inference.jl b/test/mcmc/Inference.jl index 9ffcd4014..1eb07f5bb 100644 --- a/test/mcmc/Inference.jl +++ b/test/mcmc/Inference.jl @@ -69,7 +69,9 @@ using Turing # Smoke test for default sample call. Random.seed!(100) - chain = sample(gdemo_default, HMC(0.1, 7; adtype=adbackend), MCMCThreads(), 1000, 4) + chain = sample( + gdemo_default, HMC(0.1, 7; adtype=adbackend), MCMCThreads(), 1000, 4 + ) check_gdemo(chain) # run sampler: progress logging should be disabled and @@ -113,7 +115,7 @@ using Turing a ~ Beta() lp1 = getlogp(__varinfo__) x[1] ~ Bernoulli(a) - global loglike = getlogp(__varinfo__) - lp1 + return global loglike = getlogp(__varinfo__) - lp1 end model = testmodel1([1.0]) varinfo = Turing.VarInfo(model) @@ -123,13 +125,17 @@ using Turing # Test MiniBatchContext @model function testmodel2(x) a ~ Beta() - x[1] ~ Bernoulli(a) + return x[1] ~ Bernoulli(a) end model = testmodel2([1.0]) varinfo1 = Turing.VarInfo(model) varinfo2 = deepcopy(varinfo1) model(varinfo1, Turing.SampleFromPrior(), Turing.LikelihoodContext()) - model(varinfo2, Turing.SampleFromPrior(), Turing.MiniBatchContext(Turing.LikelihoodContext(), 10)) + model( + varinfo2, + Turing.SampleFromPrior(), + Turing.MiniBatchContext(Turing.LikelihoodContext(), 10), + ) @test isapprox(getlogp(varinfo2) / getlogp(varinfo1), 10) end @testset "Prior" begin @@ -140,24 +146,24 @@ using Turing chains = sample(gdemo_d(), Prior(), N) @test chains isa MCMCChains.Chains @test size(chains) == (N, 3, 1) - @test mean(chains, :s) ≈ 3 atol=0.1 - @test mean(chains, :m) ≈ 0 atol=0.1 + @test mean(chains, :s) ≈ 3 atol = 0.1 + @test mean(chains, :m) ≈ 0 atol = 0.1 Random.seed!(100) chains = sample(gdemo_d(), Prior(), MCMCThreads(), N, 4) @test chains isa MCMCChains.Chains @test size(chains) == (N, 3, 4) - @test mean(chains, :s) ≈ 3 atol=0.1 - @test mean(chains, :m) ≈ 0 atol=0.1 + @test mean(chains, :s) ≈ 3 atol = 0.1 + @test mean(chains, :m) ≈ 0 atol = 0.1 Random.seed!(100) - chains = sample(gdemo_d(), Prior(), N; chain_type = Vector{NamedTuple}) + chains = sample(gdemo_d(), Prior(), N; chain_type=Vector{NamedTuple}) @test chains isa Vector{<:NamedTuple} @test length(chains) == N @test all(length(x) == 3 for x in chains) @test all(haskey(x, :lp) for x in chains) - @test mean(x[:s][1] for x in chains) ≈ 3 atol=0.1 - @test mean(x[:m][1] for x in chains) ≈ 0 atol=0.1 + @test mean(x[:s][1] for x in chains) ≈ 3 atol = 0.1 + @test mean(x[:m][1] for x in chains) ≈ 0 atol = 0.1 @testset "#2169" begin # Not exactly the same as the issue, but similar. @@ -177,10 +183,10 @@ using Turing @testset "chain ordering" begin for alg in (Prior(), Emcee(10, 2.0)) - chain_sorted = sample(gdemo_default, alg, 1, sort_chain=true) + chain_sorted = sample(gdemo_default, alg, 1; sort_chain=true) @test names(MCMCChains.get_sections(chain_sorted, :parameters)) == [:m, :s] - chain_unsorted = sample(gdemo_default, alg, 1, sort_chain=false) + chain_unsorted = sample(gdemo_default, alg, 1; sort_chain=false) @test names(MCMCChains.get_sections(chain_unsorted, :parameters)) == [:s, :m] end end @@ -292,8 +298,12 @@ using Turing @test_throws ErrorException chain = sample(gauss2(; x=x), PG(10), 10) @test_throws ErrorException chain = sample(gauss2(; x=x), SMC(), 10) - @test_throws ErrorException chain = sample(gauss2(DynamicPPL.TypeWrap{Vector{Float64}}(); x=x), PG(10), 10) - @test_throws ErrorException chain = sample(gauss2(DynamicPPL.TypeWrap{Vector{Float64}}(); x=x), SMC(), 10) + @test_throws ErrorException chain = sample( + gauss2(DynamicPPL.TypeWrap{Vector{Float64}}(); x=x), PG(10), 10 + ) + @test_throws ErrorException chain = sample( + gauss2(DynamicPPL.TypeWrap{Vector{Float64}}(); x=x), SMC(), 10 + ) @model function gauss3(x, ::Type{TV}=Vector{Float64}) where {TV} priors = TV(undef, 2) @@ -323,7 +333,9 @@ using Turing end sample( - newinterface(obs), HMC(0.75, 3, :p, :x; adtype = Turing.AutoForwardDiff(; chunksize=2)), 100 + newinterface(obs), + HMC(0.75, 3, :p, :x; adtype=Turing.AutoForwardDiff(; chunksize=2)), + 100, ) end @testset "no return" begin @@ -527,7 +539,9 @@ using Turing t_loop = @elapsed res = sample(vdemo1(DynamicPPL.TypeWrap{Float64}()), alg, 250) vdemo1kw(; T) = vdemo1(T) - t_loop = @elapsed res = sample(vdemo1kw(; T=DynamicPPL.TypeWrap{Float64}()), alg, 250) + t_loop = @elapsed res = sample( + vdemo1kw(; T=DynamicPPL.TypeWrap{Float64}()), alg, 250 + ) @model function vdemo2(::Type{T}=Float64) where {T<:Real} x = Vector{T}(undef, N) @@ -538,7 +552,9 @@ using Turing t_vec = @elapsed res = sample(vdemo2(DynamicPPL.TypeWrap{Float64}()), alg, 250) vdemo2kw(; T) = vdemo2(T) - t_vec = @elapsed res = sample(vdemo2kw(; T=DynamicPPL.TypeWrap{Float64}()), alg, 250) + t_vec = @elapsed res = sample( + vdemo2kw(; T=DynamicPPL.TypeWrap{Float64}()), alg, 250 + ) @model function vdemo3(::Type{TV}=Vector{Float64}) where {TV<:AbstractVector} x = TV(undef, N) @@ -553,11 +569,7 @@ using Turing end @testset "names_values" begin - ks, xs = Turing.Inference.names_values([ - (a=1,), - (b=2,), - (a=3, b=4) - ]) + ks, xs = Turing.Inference.names_values([(a=1,), (b=2,), (a=3, b=4)]) @test all(xs[:, 1] .=== [1, missing, 3]) @test all(xs[:, 2] .=== [missing, 2, 4]) end @@ -565,19 +577,18 @@ using Turing @testset "check model" begin @model function demo_repeated_varname() x ~ Normal(0, 1) - x ~ Normal(x, 1) + return x ~ Normal(x, 1) end @test_throws ErrorException sample( demo_repeated_varname(), NUTS(), 1000; check_model=true ) # Make sure that disabling the check also works. - @test (sample( - demo_repeated_varname(), Prior(), 10; check_model=false - ); true) + @test (sample(demo_repeated_varname(), Prior(), 10; check_model=false); + true) @model function demo_incorrect_missing(y) - y[1:1] ~ MvNormal(zeros(1), I) + return y[1:1] ~ MvNormal(zeros(1), I) end @test_throws ErrorException sample( demo_incorrect_missing([missing]), NUTS(), 1000; check_model=true diff --git a/test/test_utils/numerical_tests.jl b/test/test_utils/numerical_tests.jl index c44c502c1..97d174014 100644 --- a/test/test_utils/numerical_tests.jl +++ b/test/test_utils/numerical_tests.jl @@ -5,10 +5,10 @@ using MCMCChains: namesingroup using Test: @test, @testset using HypothesisTests: HypothesisTests -export check_MoGtest_default, check_MoGtest_default_z_vector, check_dist_numerical, - check_gdemo, check_numerical +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) +function check_dist_numerical(dist, chn; mean_tol=0.1, var_atol=1.0, var_tol=0.5) @testset "numerical" begin # Extract values. chn_xs = Array(chn[1:2:end, namesingroup(chn, :x), :]) @@ -17,14 +17,14 @@ function check_dist_numerical(dist, chn; mean_tol = 0.1, var_atol = 1.0, var_tol dist_mean = mean(dist) mean_shape = size(dist_mean) if !all(isnan, dist_mean) && !all(isinf, dist_mean) - chn_mean = vec(mean(chn_xs, dims=1)) - chn_mean = length(chn_mean) == 1 ? - chn_mean[1] : - reshape(chn_mean, mean_shape) - atol_m = length(chn_mean) > 1 ? - mean_tol * length(chn_mean) : + chn_mean = vec(mean(chn_xs; dims=1)) + chn_mean = length(chn_mean) == 1 ? chn_mean[1] : reshape(chn_mean, mean_shape) + atol_m = if length(chn_mean) > 1 + mean_tol * length(chn_mean) + else max(mean_tol, mean_tol * chn_mean) - @test chn_mean ≈ dist_mean atol=atol_m + end + @test chn_mean ≈ dist_mean atol = atol_m end # Check variances. @@ -34,52 +34,52 @@ function check_dist_numerical(dist, chn; mean_tol = 0.1, var_atol = 1.0, var_tol dist_var = var(dist) var_shape = size(dist_var) if !all(isnan, dist_var) && !all(isinf, dist_var) - chn_var = vec(var(chn_xs, dims=1)) - chn_var = length(chn_var) == 1 ? - chn_var[1] : - reshape(chn_var, var_shape) - atol_v = length(chn_mean) > 1 ? - mean_tol * length(chn_mean) : + chn_var = vec(var(chn_xs; dims=1)) + chn_var = length(chn_var) == 1 ? chn_var[1] : reshape(chn_var, var_shape) + atol_v = if length(chn_mean) > 1 + mean_tol * length(chn_mean) + else max(mean_tol, mean_tol * chn_mean) - @test chn_mean ≈ dist_mean atol=atol_v + end + @test chn_mean ≈ dist_mean atol = atol_v end end end end # Helper function for numerical tests -function check_numerical(chain, - symbols::Vector, - exact_vals::Vector; - atol=0.2, - rtol=0.0) +function check_numerical(chain, symbols::Vector, exact_vals::Vector; atol=0.2, rtol=0.0) for (sym, val) in zip(symbols, exact_vals) - E = val isa Real ? - mean(chain[sym]) : - vec(mean(chain[sym], dims=1)) + E = val isa Real ? mean(chain[sym]) : vec(mean(chain[sym]; dims=1)) @info (symbol=sym, exact=val, evaluated=E) - @test E ≈ val atol=atol rtol=rtol + @test E ≈ val atol = atol rtol = rtol end end # Wrapper function to quickly check gdemo accuracy. function check_gdemo(chain; atol=0.2, rtol=0.0) - check_numerical(chain, [:s, :m], [49/24, 7/6], atol=atol, rtol=rtol) + return check_numerical(chain, [:s, :m], [49 / 24, 7 / 6]; atol=atol, rtol=rtol) end # Wrapper function to check MoGtest. function check_MoGtest_default(chain; atol=0.2, rtol=0.0) - check_numerical(chain, + return check_numerical( + chain, [:z1, :z2, :z3, :z4, :mu1, :mu2], - [1.0, 1.0, 2.0, 2.0, 1.0, 4.0], - atol=atol, rtol=rtol) + [1.0, 1.0, 2.0, 2.0, 1.0, 4.0]; + atol=atol, + rtol=rtol, + ) end function check_MoGtest_default_z_vector(chain; atol=0.2, rtol=0.0) - check_numerical(chain, + return check_numerical( + chain, [Symbol("z[1]"), Symbol("z[2]"), Symbol("z[3]"), Symbol("z[4]"), :mu1, :mu2], - [1.0, 1.0, 2.0, 2.0, 1.0, 4.0], - atol=atol, rtol=rtol) + [1.0, 1.0, 2.0, 2.0, 1.0, 4.0]; + atol=atol, + rtol=rtol, + ) end """ @@ -104,9 +104,12 @@ function two_sample_test(xs_left, xs_right; α=1e-3, warn_on_fail=false) if HypothesisTests.pvalue(t) > α true else - warn_on_fail && @warn "Two-sample AD test failed with p-value $(HypothesisTests.pvalue(t))" - warn_on_fail && @warn "Means of the two samples: $(mean(xs_left)), $(mean(xs_right))" - warn_on_fail && @warn "Variances of the two samples: $(var(xs_left)), $(var(xs_right))" + warn_on_fail && + @warn "Two-sample AD test failed with p-value $(HypothesisTests.pvalue(t))" + warn_on_fail && + @warn "Means of the two samples: $(mean(xs_left)), $(mean(xs_right))" + warn_on_fail && + @warn "Variances of the two samples: $(var(xs_left)), $(var(xs_right))" false end end From 517e9fb5491597eb2914583caa8510efadd148dd Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Mon, 7 Oct 2024 11:44:39 +0100 Subject: [PATCH 22/33] Fix bad merge From 188bd80644e10e61b1bf793892d1213c3e697efe Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Mon, 7 Oct 2024 14:01:26 +0100 Subject: [PATCH 23/33] Restrict Mooncake to >= 0.4.9 --- test/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/test/Project.toml b/test/Project.toml index d3acf0bad..e82e05bcd 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -54,6 +54,7 @@ LinearAlgebra = "1" LogDensityProblems = "2" LogDensityProblemsAD = "1.4" MCMCChains = "5, 6" +Mooncake = "0.4.9" NamedArrays = "0.9.4, 0.10" Optim = "1" Optimization = "3" From a277c29d15b46e25fb599c65e33d00f7a923f81f Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Mon, 7 Oct 2024 14:28:02 +0100 Subject: [PATCH 24/33] Add MH test for LKJCholesky --- test/mcmc/mh.jl | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/mcmc/mh.jl b/test/mcmc/mh.jl index 2c6e7a6fe..8813834ed 100644 --- a/test/mcmc/mh.jl +++ b/test/mcmc/mh.jl @@ -206,6 +206,23 @@ GKernel(var) = (x) -> Normal(x, sqrt.(var)) end end + @testset "LKJCholesky" begin + for uplo in ['L', 'U'] + @model f() = x ~ LKJCholesky(2, 1, uplo) + Random.seed!(100) + chain = sample(f(), MH(), 5_000) + indices = [(1, 1), (2, 1), (2, 2)] + values = [1, 0, 0.785] + for ((i, j), v) in zip(indices, values) + if uplo == 'U' # Transpose + @test mean(chain, "x.$uplo[$j, $i]") ≈ v atol = 0.01 + else + @test mean(chain, "x.$uplo[$i, $j]") ≈ v atol = 0.01 + end + end + end + end + @testset "MH link/invlink" begin vi_base = DynamicPPL.VarInfo(gdemo_default) From 742364ecd0efe1a4f4b2688885fb74d6bc4f1c83 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 10 Oct 2024 10:51:18 +0100 Subject: [PATCH 25/33] Remove Tracker --- src/Turing.jl | 1 - src/essential/Essential.jl | 3 +-- test/Project.toml | 2 -- test/essential/ad.jl | 16 +--------------- test/ext/OptimInterface.jl | 3 ++- test/mcmc/Inference.jl | 1 - test/optimisation/Optimisation.jl | 18 ++++++++---------- test/test_utils/ad_utils.jl | 17 ++++------------- 8 files changed, 16 insertions(+), 45 deletions(-) diff --git a/src/Turing.jl b/src/Turing.jl index d5545b276..d38af3a97 100644 --- a/src/Turing.jl +++ b/src/Turing.jl @@ -106,7 +106,6 @@ export @model, # modelling AutoForwardDiff, # ADTypes AutoReverseDiff, AutoZygote, - AutoTracker, AutoMooncake, setprogress!, # debugging Flat, diff --git a/src/essential/Essential.jl b/src/essential/Essential.jl index 82b67ccac..872792aab 100644 --- a/src/essential/Essential.jl +++ b/src/essential/Essential.jl @@ -12,7 +12,7 @@ using AdvancedVI using StatsFuns: logsumexp, softmax @reexport using DynamicPPL using ADTypes: - ADTypes, AutoForwardDiff, AutoTracker, AutoReverseDiff, AutoZygote, AutoMooncake + ADTypes, AutoForwardDiff, AutoReverseDiff, AutoZygote, AutoMooncake using AdvancedPS: AdvancedPS @@ -21,7 +21,6 @@ include("container.jl") export @model, @varname, AutoForwardDiff, - AutoTracker, AutoZygote, AutoReverseDiff, AutoMooncake, diff --git a/test/Project.toml b/test/Project.toml index e82e05bcd..7090fc42f 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -33,7 +33,6 @@ StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f" -Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c" Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" [compat] @@ -70,6 +69,5 @@ StableRNGs = "1" StatsBase = "0.33, 0.34" StatsFuns = "0.9.5, 1" TimerOutputs = "0.5" -Tracker = "0.2.11" Zygote = "0.5.4, 0.6" julia = "1.3" diff --git a/test/essential/ad.jl b/test/essential/ad.jl index 5f29b8b5b..0c497a2cb 100644 --- a/test/essential/ad.jl +++ b/test/essential/ad.jl @@ -88,20 +88,6 @@ end ) x = map(x -> Float64(x), vi[SampleFromPrior()]) - trackerℓ = LogDensityProblemsAD.ADgradient(Turing.AutoTracker(), ℓ) - if isdefined(Base, :get_extension) - @test trackerℓ isa - Base.get_extension( - LogDensityProblemsAD, :LogDensityProblemsADTrackerExt - ).TrackerGradientLogDensity - else - @test trackerℓ isa - LogDensityProblemsAD.LogDensityProblemsADTrackerExt.TrackerGradientLogDensity - end - @test trackerℓ.ℓ === ℓ - ∇E1 = LogDensityProblems.logdensity_and_gradient(trackerℓ, x)[2] - @test sort(∇E1) ≈ grad_FWAD atol = 1e-9 - zygoteℓ = LogDensityProblemsAD.ADgradient(Turing.AutoZygote(), ℓ) if isdefined(Base, :get_extension) @test zygoteℓ isa @@ -149,7 +135,7 @@ end test_model_ad(wishart_ad(), logp3, [:v]) end - @testset "Simplex Tracker, Zygote and ReverseDiff (with and without caching) AD" begin + @testset "Simplex Zygote and ReverseDiff (with and without caching) AD" begin @model function dir() return theta ~ Dirichlet(1 ./ fill(4, 4)) end diff --git a/test/ext/OptimInterface.jl b/test/ext/OptimInterface.jl index 817d7a520..e9d16535a 100644 --- a/test/ext/OptimInterface.jl +++ b/test/ext/OptimInterface.jl @@ -112,7 +112,8 @@ using Turing @test isapprox(map1.values.array, map2.values.array) end - # FIXME: Some models doesn't work for Tracker and ReverseDiff. + # FIXME: Some models don't work for ReverseDiff. + # TODO: Check if above statement is still correct @testset "MAP for $(model.f)" for model in DynamicPPL.TestUtils.DEMO_MODELS result_true = DynamicPPL.TestUtils.posterior_optima(model) diff --git a/test/mcmc/Inference.jl b/test/mcmc/Inference.jl index 1eb07f5bb..a92f9c763 100644 --- a/test/mcmc/Inference.jl +++ b/test/mcmc/Inference.jl @@ -382,7 +382,6 @@ using Turing chn = sample(gdemo_default, alg, 1000) end @testset "vectorization @." begin - # https://github.com/FluxML/Tracker.jl/issues/119 @model function vdemo1(x) s ~ InverseGamma(2, 3) m ~ Normal(0, sqrt(s)) diff --git a/test/optimisation/Optimisation.jl b/test/optimisation/Optimisation.jl index 1ba073864..58ecd1ad2 100644 --- a/test/optimisation/Optimisation.jl +++ b/test/optimisation/Optimisation.jl @@ -1,7 +1,7 @@ module OptimisationTests using ..Models: gdemo, gdemo_default -using ..ADUtils: ADTypeCheckContext +using ..ADUtils: ADUtils using Distributions using Distributions.FillArrays: Zeros using DynamicPPL: DynamicPPL @@ -617,16 +617,14 @@ using Turing @assert get(result, :c) == (; :c => Array{Float64}[]) end - @testset "ADType" begin + @testset "ADType" for adbackend in ADUtils.adbackends Random.seed!(222) - for adbackend in (AutoReverseDiff(), AutoForwardDiff(), AutoTracker()) - m = DynamicPPL.contextualize( - gdemo_default, ADTypeCheckContext(adbackend, gdemo_default.context) - ) - # These will error if the adbackend being used is not the one set. - maximum_likelihood(m; adtype=adbackend) - maximum_a_posteriori(m; adtype=adbackend) - end + m = DynamicPPL.contextualize( + gdemo_default, ADUtils.ADTypeCheckContext(adbackend, gdemo_default.context) + ) + # These will error if the adbackend being used is not the one set. + maximum_likelihood(m; adtype=adbackend) + maximum_a_posteriori(m; adtype=adbackend) end end diff --git a/test/test_utils/ad_utils.jl b/test/test_utils/ad_utils.jl index 5fc05dd9c..f7358de75 100644 --- a/test/test_utils/ad_utils.jl +++ b/test/test_utils/ad_utils.jl @@ -6,7 +6,6 @@ using Random: Random using ReverseDiff: ReverseDiff using Mooncake: Mooncake using Test: Test -using Tracker: Tracker using Turing: Turing using Turing: DynamicPPL using Zygote: Zygote @@ -31,19 +30,10 @@ const eltypes_by_adtype = Dict( ReverseDiff.TrackedVecOrMat, ReverseDiff.TrackedVector, ), + Turing.AutoMooncake => (Mooncake.CoDual,), # Zygote.Dual is actually the same as ForwardDiff.Dual, so can't distinguish between the # two by element type. However, we have other checks for Zygote, see check_adtype. Turing.AutoZygote => (Zygote.Dual,), - Turing.AutoTracker => ( - Tracker.Tracked, - Tracker.TrackedArray, - Tracker.TrackedMatrix, - Tracker.TrackedReal, - Tracker.TrackedStyle, - Tracker.TrackedVecOrMat, - Tracker.TrackedVector, - ), - Turing.AutoMooncake => (Mooncake.CoDual,), ) """ @@ -247,7 +237,8 @@ Test.@testset "ADTypeCheckContext" begin Turing.AutoForwardDiff(), Turing.AutoReverseDiff(), Turing.AutoZygote(), - Turing.AutoTracker(), + # TODO: Mooncake + # Turing.AutoMooncake(config=nothing), ) for actual_adtype in adtypes sampler = Turing.HMC(0.1, 5; adtype=actual_adtype) @@ -285,7 +276,7 @@ All the ADTypes on which we want to run the tests. adbackends = [ Turing.AutoForwardDiff(; chunksize=0), Turing.AutoReverseDiff(; compile=false), - Turing.AutoMooncake(; config=Mooncake.Config()), + Turing.AutoMooncake(; config=nothing), ] end From f2cc18312e176565143fb5e7683fef3e67521683 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Fri, 11 Oct 2024 15:11:23 +0100 Subject: [PATCH 26/33] Format imports Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/essential/Essential.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/essential/Essential.jl b/src/essential/Essential.jl index 872792aab..c04c7e862 100644 --- a/src/essential/Essential.jl +++ b/src/essential/Essential.jl @@ -11,8 +11,7 @@ using Bijectors: PDMatDistribution using AdvancedVI using StatsFuns: logsumexp, softmax @reexport using DynamicPPL -using ADTypes: - ADTypes, AutoForwardDiff, AutoReverseDiff, AutoZygote, AutoMooncake +using ADTypes: ADTypes, AutoForwardDiff, AutoReverseDiff, AutoZygote, AutoMooncake using AdvancedPS: AdvancedPS From b60d907ff1635cd833fc08f895542cc2fad8a918 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Fri, 11 Oct 2024 17:47:06 +0100 Subject: [PATCH 27/33] Add AD imports to Optimisation test --- test/optimisation/Optimisation.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/optimisation/Optimisation.jl b/test/optimisation/Optimisation.jl index 58ecd1ad2..498852231 100644 --- a/test/optimisation/Optimisation.jl +++ b/test/optimisation/Optimisation.jl @@ -5,7 +5,9 @@ using ..ADUtils: ADUtils using Distributions using Distributions.FillArrays: Zeros using DynamicPPL: DynamicPPL +using ForwardDiff: ForwardDiff using LinearAlgebra: Diagonal, I +using Mooncake: Mooncake using Random: Random using Optimization using Optimization: Optimization From 1c039f63b2e10110e933cbe8487b60f0b7373b54 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Mon, 21 Oct 2024 23:22:03 +0100 Subject: [PATCH 28/33] Bump Mooncake compat in tests --- test/Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Project.toml b/test/Project.toml index 7090fc42f..d5d83726f 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -53,7 +53,7 @@ LinearAlgebra = "1" LogDensityProblems = "2" LogDensityProblemsAD = "1.4" MCMCChains = "5, 6" -Mooncake = "0.4.9" +Mooncake = "0.4.19" NamedArrays = "0.9.4, 0.10" Optim = "1" Optimization = "3" From cce79c0c0a61232139a6dffc8b9cfdc90c3733c2 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Tue, 22 Oct 2024 13:42:49 +0100 Subject: [PATCH 29/33] Bump Libtask version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 3ba0f9e5b..830c0f57a 100644 --- a/Project.toml +++ b/Project.toml @@ -66,7 +66,7 @@ DynamicHMC = "3.4" DynamicPPL = "0.29" EllipticalSliceSampling = "0.5, 1, 2" ForwardDiff = "0.10.3" -Libtask = "0.7, 0.8" +Libtask = "0.8.8" LinearAlgebra = "1" LogDensityProblems = "2" LogDensityProblemsAD = "1.7.0" From 0f46682d50ba9c9e71a76e2ba1ccc7f01c627d8a Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Tue, 22 Oct 2024 16:53:31 +0100 Subject: [PATCH 30/33] Get Optimization tests to pass CI --- test/optimisation/Optimisation.jl | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/test/optimisation/Optimisation.jl b/test/optimisation/Optimisation.jl index 498852231..d8afd83db 100644 --- a/test/optimisation/Optimisation.jl +++ b/test/optimisation/Optimisation.jl @@ -619,14 +619,28 @@ using Turing @assert get(result, :c) == (; :c => Array{Float64}[]) end - @testset "ADType" for adbackend in ADUtils.adbackends + @testset "ADType test with $adbackend" for adbackend in ADUtils.adbackends Random.seed!(222) m = DynamicPPL.contextualize( gdemo_default, ADUtils.ADTypeCheckContext(adbackend, gdemo_default.context) ) - # These will error if the adbackend being used is not the one set. - maximum_likelihood(m; adtype=adbackend) - maximum_a_posteriori(m; adtype=adbackend) + if adbackend isa AutoMooncake + # Optimization.jl does not support Mooncake as an AD backend, see + # https://docs.sciml.ai/Optimization/stable/API/ad/#ad + # If it ever does, then we should just run them to make sure they don't error + err_msg = "The passed automatic differentiation backend choice is not available" + @test_throws err_msg maximum_likelihood(m; adtype=adbackend) + @test_throws err_msg maximum_a_posteriori(m; adtype=adbackend) + elseif adbackend isa AutoForwardDiff + # TODO: Figure out why this is happening. + # https://github.com/TuringLang/Turing.jl/issues/2369 + @test_throws DivideError maximum_likelihood(m; adtype=adbackend) + @test_throws DivideError maximum_a_posteriori(m; adtype=adbackend) + else + # These will error if the adbackend being used is not the one set. + maximum_likelihood(m; adtype=adbackend) + maximum_a_posteriori(m; adtype=adbackend) + end end end From d9bc88a894a1bfa5bd9febd9046277660ae7dcad Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Tue, 22 Oct 2024 16:58:50 +0100 Subject: [PATCH 31/33] Update API docs --- docs/src/api.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/docs/src/api.md b/docs/src/api.md index e2b193c01..4ffcc6c34 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -85,13 +85,12 @@ See the [variational inference tutorial](https://turinglang.org/docs/tutorials/0 These are used to specify the automatic differentiation backend to use. See the [AD guide](https://turinglang.org/docs/tutorials/docs-10-using-turing-autodiff/) for more information. -| Exported symbol | Documentation | Description | -|:----------------- |:------------------------------------ |:----------------------------------------- | -| `AutoForwardDiff` | [`ADTypes.AutoForwardDiff`](@extref) | ForwardDiff.jl backend | -| `AutoReverseDiff` | [`ADTypes.AutoReverseDiff`](@extref) | ReverseDiff.jl backend | -| `AutoZygote` | [`ADTypes.AutoZygote`](@extref) | Zygote.jl backend | -| `AutoTracker` | [`ADTypes.AutoTracker`](@extref) | Tracker.jl backend | -| `AutoTapir` | [`ADTypes.AutoTapir`](@extref) | Tapir.jl backend, only for ADTypes >= 1.0 | +| Exported symbol | Documentation | Description | +|:----------------- |:------------------------------------ |:----------------------- | +| `AutoForwardDiff` | [`ADTypes.AutoForwardDiff`](@extref) | ForwardDiff.jl backend | +| `AutoReverseDiff` | [`ADTypes.AutoReverseDiff`](@extref) | ReverseDiff.jl backend | +| `AutoZygote` | [`ADTypes.AutoZygote`](@extref) | Zygote.jl backend | +| `AutoMooncake` | [`ADTypes.AutoMooncake`](@extref) | Mooncake.jl backend | ### Debugging From 5f55f309f8bb6daa164b6e50b5ff6edf8d95305c Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Tue, 22 Oct 2024 17:00:29 +0100 Subject: [PATCH 32/33] Write a changelog entry --- HISTORY.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 5b1cad0ed..3bc362d2b 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,14 @@ +# Release 0.35.0 + +## Breaking changes + +Julia 1.10 is now the minimum required version for Turing. + +Tapir.jl has been removed and replaced with its successor, Mooncake.jl. +You can use Mooncake.jl by passing `adbackend=AutoMooncake(; config=nothing)` to the relevant samplers. + +Support for Tracker.jl as an AD backend has been removed. + # Release 0.33.0 ## Breaking changes From 9fb9e955b9a0beee7701e3e6fff3d4bd2d500503 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Tue, 22 Oct 2024 19:52:29 +0100 Subject: [PATCH 33/33] Update docs/src/api.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- docs/src/api.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/src/api.md b/docs/src/api.md index 4ffcc6c34..6acda4586 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -85,12 +85,12 @@ See the [variational inference tutorial](https://turinglang.org/docs/tutorials/0 These are used to specify the automatic differentiation backend to use. See the [AD guide](https://turinglang.org/docs/tutorials/docs-10-using-turing-autodiff/) for more information. -| Exported symbol | Documentation | Description | -|:----------------- |:------------------------------------ |:----------------------- | -| `AutoForwardDiff` | [`ADTypes.AutoForwardDiff`](@extref) | ForwardDiff.jl backend | -| `AutoReverseDiff` | [`ADTypes.AutoReverseDiff`](@extref) | ReverseDiff.jl backend | -| `AutoZygote` | [`ADTypes.AutoZygote`](@extref) | Zygote.jl backend | -| `AutoMooncake` | [`ADTypes.AutoMooncake`](@extref) | Mooncake.jl backend | +| Exported symbol | Documentation | Description | +|:----------------- |:------------------------------------ |:---------------------- | +| `AutoForwardDiff` | [`ADTypes.AutoForwardDiff`](@extref) | ForwardDiff.jl backend | +| `AutoReverseDiff` | [`ADTypes.AutoReverseDiff`](@extref) | ReverseDiff.jl backend | +| `AutoZygote` | [`ADTypes.AutoZygote`](@extref) | Zygote.jl backend | +| `AutoMooncake` | [`ADTypes.AutoMooncake`](@extref) | Mooncake.jl backend | ### Debugging