Skip to content

Commit d40d82b

Browse files
authored
Merge branch 'master' into torfjelde/gibbs-new-improv
2 parents 30ab9e0 + 142dab3 commit d40d82b

File tree

9 files changed

+91
-28
lines changed

9 files changed

+91
-28
lines changed

.JuliaFormatter.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ignore = [
1010
"src/mcmc/abstractmcmc.jl",
1111
"test/experimental/gibbs.jl",
1212
"test/test_utils/numerical_tests.jl",
13-
# https://github.com/TuringLang/Turing.jl/pull/2218/files
13+
# https://github.com/TuringLang/Turing.jl/pull/2218/files
1414
"src/mcmc/Inference.jl",
1515
"test/mcmc/Inference.jl",
1616
# https://github.com/TuringLang/Turing.jl/pull/1887 # Enzyme PR

.github/workflows/Tests.yml

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,43 @@ jobs:
6767
echo "Julia version: ${{ matrix.version }}"
6868
echo "Number of threads: ${{ matrix.num_threads }}"
6969
echo "Test arguments: ${{ matrix.test-args }}"
70+
- name: (De)activate coverage analysis
71+
run: echo "COVERAGE=${{ matrix.version == '1' && matrix.os == 'ubuntu-latest' && matrix.num_threads == 2 }}" >> "$GITHUB_ENV"
72+
shell: bash
7073
- uses: actions/checkout@v4
7174
- uses: julia-actions/setup-julia@v2
7275
with:
7376
version: '${{ matrix.version }}'
7477
arch: ${{ matrix.arch }}
75-
- uses: actions/cache@v4
76-
env:
77-
cache-name: cache-artifacts
78-
with:
79-
path: ~/.julia/artifacts
80-
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
81-
restore-keys: |
82-
${{ runner.os }}-test-${{ env.cache-name }}-
83-
${{ runner.os }}-test-
84-
${{ runner.os }}-
85-
- uses: julia-actions/julia-buildpkg@latest
78+
- uses: julia-actions/cache@v1
79+
- uses: julia-actions/julia-buildpkg@v1
80+
# TODO: Use julia-actions/julia-runtest when test_args are supported
81+
# Custom calls of Pkg.test tend to miss features such as e.g. adjustments for CompatHelper PRs
82+
# Ref https://github.com/julia-actions/julia-runtest/pull/73
8683
- name: Call Pkg.test
87-
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 }}
84+
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 }}
85+
- uses: julia-actions/julia-processcoverage@v1
86+
if: ${{ env.COVERAGE }}
87+
- uses: codecov/codecov-action@v4
88+
if: ${{ env.COVERAGE }}
89+
with:
90+
fail_ci_if_error: true
91+
token: ${{ secrets.CODECOV_TOKEN }}
92+
file: lcov.info
93+
- uses: coverallsapp/github-action@v2
94+
if: ${{ env.COVERAGE }}
95+
with:
96+
github-token: ${{ secrets.GITHUB_TOKEN }}
97+
path-to-lcov: lcov.info
98+
flag-name: run-${{ join(matrix.*, '-') }}
99+
parallel: true
100+
101+
finish:
102+
needs: test
103+
if: ${{ always() }}
104+
runs-on: ubuntu-latest
105+
steps:
106+
- name: Coveralls Finished
107+
uses: coverallsapp/github-action@v2
108+
with:
109+
parallel-finished: true

README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ https://turinglang.org/docs/
1515

1616
See [releases](https://github.com/TuringLang/Turing.jl/releases).
1717

18-
## Want to contribute?
19-
20-
Turing was originally created and is now managed by Hong Ge. Current and past Turing team members include [Hong Ge](http://mlg.eng.cam.ac.uk/hong/), [Kai Xu](http://mlg.eng.cam.ac.uk/?portfolio=kai-xu), [Martin Trapp](http://martint.blog), [Mohamed Tarek](https://github.com/mohamed82008), [Cameron Pfiffer](https://business.uoregon.edu/faculty/cameron-pfiffer), [Tor Fjelde](http://retiredparkingguard.com/about.html).
21-
You can see the complete list on Github: https://github.com/TuringLang/Turing.jl/graphs/contributors.
22-
23-
Turing is an open source project so if you feel you have some relevant skills and are interested in contributing, please get in touch. See the [Contributing](https://turinglang.org/dev/docs/contributing/guide) page for details on the process. You can contribute by opening issues on Github, implementing things yourself, and making a pull request. We would also appreciate example models written using Turing.
24-
2518
## Issues and Discussions
2619

2720
Issues related to bugs and feature requests are welcome on the [issues page](https://github.com/TuringLang/Turing.jl/issues), while discussions and questions about statistical applications and theory should place on the [Discussions page](https://github.com/TuringLang/Turing.jl/discussions) or [our channel](https://julialang.slack.com/messages/turing/) (`#turing`) in the Julia Slack chat. If you do not have an invitation to Julia's Slack, you can get one by going [here](https://julialang.org/slack/).

src/mcmc/Inference.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,15 @@ DynamicPPL.getlogp(t::Transition) = t.lp
240240
# Metadata of VarInfo object
241241
metadata(vi::AbstractVarInfo) = (lp = getlogp(vi),)
242242

243+
# TODO: Implement additional checks for certain samplers, e.g.
244+
# HMC not supporting discrete parameters.
245+
function _check_model(model::DynamicPPL.Model)
246+
return DynamicPPL.check_model(model; error_on_failure=true)
247+
end
248+
function _check_model(model::DynamicPPL.Model, alg::InferenceAlgorithm)
249+
return _check_model(model)
250+
end
251+
243252
#########################################
244253
# Default definitions for the interface #
245254
#########################################
@@ -258,8 +267,10 @@ function AbstractMCMC.sample(
258267
model::AbstractModel,
259268
alg::InferenceAlgorithm,
260269
N::Integer;
270+
check_model::Bool=true,
261271
kwargs...
262272
)
273+
check_model && _check_model(model, alg)
263274
return AbstractMCMC.sample(rng, model, Sampler(alg, model), N; kwargs...)
264275
end
265276

@@ -282,8 +293,10 @@ function AbstractMCMC.sample(
282293
ensemble::AbstractMCMC.AbstractMCMCEnsemble,
283294
N::Integer,
284295
n_chains::Integer;
296+
check_model::Bool=true,
285297
kwargs...
286298
)
299+
check_model && _check_model(model, alg)
287300
return AbstractMCMC.sample(rng, model, Sampler(alg, model), ensemble, N, n_chains;
288301
kwargs...)
289302
end

test/mcmc/Inference.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,28 @@ using Turing
559559
@test all(xs[:, 1] .=== [1, missing, 3])
560560
@test all(xs[:, 2] .=== [missing, 2, 4])
561561
end
562+
563+
@testset "check model" begin
564+
@model function demo_repeated_varname()
565+
x ~ Normal(0, 1)
566+
x ~ Normal(x, 1)
567+
end
568+
569+
@test_throws ErrorException sample(
570+
demo_repeated_varname(), NUTS(), 1000; check_model=true
571+
)
572+
# Make sure that disabling the check also works.
573+
@test (sample(
574+
demo_repeated_varname(), Prior(), 10; check_model=false
575+
); true)
576+
577+
@model function demo_incorrect_missing(y)
578+
y[1:1] ~ MvNormal(zeros(1), 1)
579+
end
580+
@test_throws ErrorException sample(
581+
demo_incorrect_missing([missing]), NUTS(), 1000; check_model=true
582+
)
583+
end
562584
end
563585

564586
end

test/mcmc/gibbs.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ using Turing.RandomMeasures: ChineseRestaurantProcess, DirichletProcess
5050
Random.seed!(100)
5151
alg = Gibbs(CSMC(15, :s), HMC(0.2, 4, :m; adtype=adbackend))
5252
chain = sample(gdemo(1.5, 2.0), alg, 10_000)
53-
check_numerical(chain, [:s, :m], [49 / 24, 7 / 6]; atol=0.15)
53+
check_numerical(chain, [:m], [7 / 6]; atol=0.15)
54+
# Be more relaxed with the tolerance of the variance.
55+
check_numerical(chain, [:s], [49 / 24]; atol=0.35)
5456

5557
Random.seed!(100)
5658

test/mcmc/hmc.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ using Turing
319319

320320
# The discrepancies in the chains are in the tails, so we can't just compare the mean, etc.
321321
# KS will compare the empirical CDFs, which seems like a reasonable thing to do here.
322-
@test pvalue(ApproximateTwoSampleKSTest(vec(results), vec(results_prior))) > 0.01
322+
@test pvalue(ApproximateTwoSampleKSTest(vec(results), vec(results_prior))) > 0.001
323323
end
324324
end
325325

test/mcmc/is.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ using Turing
4646
ref = reference(n)
4747

4848
Random.seed!(seed)
49-
chain = sample(model, alg, n)
49+
chain = sample(model, alg, n; check_model=false)
5050
sampled = get(chain, [:a, :b, :lp])
5151

5252
@test vec(sampled.a) == ref.as

test/mcmc/mh.jl

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,41 @@ GKernel(var) = (x) -> Normal(x, sqrt.(var))
4444
# c6 = sample(gdemo_default, s6, N)
4545
end
4646
@testset "mh inference" begin
47+
# Set the initial parameters, because if we get unlucky with the initial state,
48+
# these chains are too short to converge to reasonable numbers.
49+
discard_initial = 1000
50+
initial_params = [1.0, 1.0]
51+
4752
Random.seed!(125)
4853
alg = MH()
49-
chain = sample(gdemo_default, alg, 10_000)
54+
chain = sample(gdemo_default, alg, 10_000; discard_initial, initial_params)
5055
check_gdemo(chain; atol=0.1)
5156

5257
Random.seed!(125)
5358
# MH with Gaussian proposal
5459
alg = MH((:s, InverseGamma(2, 3)), (:m, GKernel(1.0)))
55-
chain = sample(gdemo_default, alg, 10_000)
60+
chain = sample(gdemo_default, alg, 10_000; discard_initial, initial_params)
5661
check_gdemo(chain; atol=0.1)
5762

5863
Random.seed!(125)
5964
# MH within Gibbs
6065
alg = Gibbs(MH(:m), MH(:s))
61-
chain = sample(gdemo_default, alg, 10_000)
66+
chain = sample(gdemo_default, alg, 10_000; discard_initial, initial_params)
6267
check_gdemo(chain; atol=0.1)
6368

6469
Random.seed!(125)
6570
# MoGtest
6671
gibbs = Gibbs(
6772
CSMC(15, :z1, :z2, :z3, :z4), MH((:mu1, GKernel(1)), (:mu2, GKernel(1)))
6873
)
69-
chain = sample(MoGtest_default, gibbs, 500)
70-
check_MoGtest_default(chain; atol=0.15)
74+
chain = sample(
75+
MoGtest_default,
76+
gibbs,
77+
500;
78+
discard_initial=100,
79+
initial_params=[1.0, 1.0, 0.0, 0.0, 1.0, 4.0],
80+
)
81+
check_MoGtest_default(chain; atol=0.2)
7182
end
7283

7384
# Test MH shape passing.

0 commit comments

Comments
 (0)