Skip to content

Commit

Permalink
Merge branch 'master' into prior_optctxt
Browse files Browse the repository at this point in the history
  • Loading branch information
mhauru authored Jun 5, 2024
2 parents d46825b + 03d0e78 commit 54f6be9
Show file tree
Hide file tree
Showing 36 changed files with 621 additions and 507 deletions.
42 changes: 0 additions & 42 deletions .github/workflows/DynamicHMC.yml

This file was deleted.

41 changes: 0 additions & 41 deletions .github/workflows/Numerical.yml

This file was deleted.

87 changes: 87 additions & 0 deletions .github/workflows/Tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
name: Tests

on:
push:
branches:
- master
pull_request:

jobs:
test:
runs-on: ${{ matrix.os }}
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
# 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"
version:
- '1.7'
- '1'
os:
- ubuntu-latest
- windows-latest
- macOS-latest
arch:
- x64
- x86
num_threads:
- 1
- 2
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'
- os: macOS-latest
version: '1'
- 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'
arch: x86
- num_threads: 2
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:
version: '${{ matrix.version }}'
arch: ${{ matrix.arch }}
- uses: actions/cache@v4
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
- 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=ARGS)' -- ${{ matrix.test-args }}
70 changes: 0 additions & 70 deletions .github/workflows/TuringCI.yml

This file was deleted.

2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
61 changes: 59 additions & 2 deletions test/essential/ad.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
module AdTests

using ..Models: gdemo_default
using Distributions: logpdf
using DynamicPPL: getlogp, getval
using ForwardDiff
using LinearAlgebra
import LogDensityProblems
import LogDensityProblemsAD
using ReverseDiff
using Test: @test, @testset
using Turing
using Turing: SampleFromPrior
using Zygote

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
@turing_testset "adr" begin
@testset "adr" begin
ad_test_f = gdemo_default
vi = Turing.VarInfo(ad_test_f)
ad_test_f(vi, SampleFromPrior())
Expand Down Expand Up @@ -50,7 +104,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)
Expand Down Expand Up @@ -179,3 +234,5 @@
@test ℓ_grad == ℓ_grad_compiled
end
end

end
14 changes: 12 additions & 2 deletions test/essential/container.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module ContainerTests

import AdvancedPS
using Distributions: Bernoulli, Beta, Gamma, Normal
using DynamicPPL: @model, Sampler
using Test: @test, @testset
using Turing

@testset "container.jl" begin
@model function test()
a ~ Normal(0, 1)
Expand All @@ -9,7 +17,7 @@
x
end

@turing_testset "constructor" begin
@testset "constructor" begin
vi = DynamicPPL.VarInfo()
sampler = Sampler(PG(10))
model = test()
Expand All @@ -29,7 +37,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)
Expand All @@ -48,3 +56,5 @@
@test AdvancedPS.advance!(trace) AdvancedPS.advance!(newtrace)
end
end

end
12 changes: 11 additions & 1 deletion test/experimental/gibbs.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
using Test, Random, Turing, DynamicPPL
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

function check_transition_varnames(
transition::Turing.Inference.Transition,
Expand Down Expand Up @@ -187,3 +195,5 @@ end
check_MoGtest_default_z_vector(chain, atol = 0.2)
end
end

end
Loading

0 comments on commit 54f6be9

Please sign in to comment.