Skip to content

Commit 39751b1

Browse files
Fix test error with Turing >= 0.30 (#570)
* Fix test error with Turing >= 0.30 * Fix format Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Fix doctests and format * Fix LKJ tests * Remove `setchunksize` --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent c6e3b6b commit 39751b1

File tree

9 files changed

+59
-50
lines changed

9 files changed

+59
-50
lines changed

benchmarks/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
To run the benchmarks, simply do:
2+
23
```sh
34
julia --project -e 'using DynamicPPLBenchmarks; weave_benchmarks();'
45
```

benchmarks/benchmark_body.jmd

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ results["evaluation_typed"]
2020
```
2121

2222
```julia; echo=false; results="hidden";
23-
BenchmarkTools.save(joinpath("results", WEAVE_ARGS[:name], "$(nameof(m))_benchmarks.json"), results)
23+
BenchmarkTools.save(
24+
joinpath("results", WEAVE_ARGS[:name], "$(nameof(m))_benchmarks.json"), results
25+
)
2426
```
2527

2628
```julia; wrap=false
@@ -32,15 +34,16 @@ end
3234
```julia; echo=false; results="hidden"
3335
if WEAVE_ARGS[:include_typed_code]
3436
# Serialize the output of `typed_code` so we can compare later.
35-
haskey(WEAVE_ARGS, :name) && serialize(joinpath("results", WEAVE_ARGS[:name],"$(nameof(m)).jls"), string(typed));
37+
haskey(WEAVE_ARGS, :name) &&
38+
serialize(joinpath("results", WEAVE_ARGS[:name], "$(nameof(m)).jls"), string(typed))
3639
end
3740
```
3841

3942
```julia; wrap=false; echo=false;
4043
if haskey(WEAVE_ARGS, :name_old)
4144
# We want to compare the generated code to the previous version.
42-
import DiffUtils
43-
typed_old = deserialize(joinpath("results", WEAVE_ARGS[:name_old], "$(nameof(m)).jls"));
44-
DiffUtils.diff(typed_old, string(typed), width=130)
45+
using DiffUtils: DiffUtils
46+
typed_old = deserialize(joinpath("results", WEAVE_ARGS[:name_old], "$(nameof(m)).jls"))
47+
DiffUtils.diff(typed_old, string(typed); width=130)
4548
end
4649
```

benchmarks/benchmarks.jmd

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@ import DynamicPPLBenchmarks: time_model_def, make_suite, typed_code, weave_child
1919
m ~ Normal()
2020
x ~ Normal(m, 1)
2121

22-
return (m = m, x = x)
22+
return (m=m, x=x)
2323
end
2424

2525
model_def = demo1;
2626
data = 1.0;
2727
```
2828

2929
```julia; results="markup"; echo=false
30-
weave_child(WEAVE_ARGS[:benchmarkbody], mod = @__MODULE__, args = WEAVE_ARGS)
30+
weave_child(WEAVE_ARGS[:benchmarkbody]; mod=@__MODULE__, args=WEAVE_ARGS)
3131
```
3232

3333
### `demo2`
3434

3535
```julia
36-
@model function demo2(y)
36+
@model function demo2(y)
3737
# Our prior belief about the probability of heads in a coin.
3838
p ~ Beta(1, 1)
3939

@@ -50,7 +50,7 @@ data = rand(0:1, 10);
5050
```
5151

5252
```julia; results="markup"; echo=false
53-
weave_child(WEAVE_ARGS[:benchmarkbody], mod = @__MODULE__, args = WEAVE_ARGS)
53+
weave_child(WEAVE_ARGS[:benchmarkbody]; mod=@__MODULE__, args=WEAVE_ARGS)
5454
```
5555

5656
### `demo3`
@@ -74,7 +74,7 @@ weave_child(WEAVE_ARGS[:benchmarkbody], mod = @__MODULE__, args = WEAVE_ARGS)
7474
k = Vector{Int}(undef, N)
7575
for i in 1:N
7676
k[i] ~ Categorical(w)
77-
x[:,i] ~ MvNormal([μ[k[i]], μ[k[i]]], 1.)
77+
x[:, i] ~ MvNormal([μ[k[i]], μ[k[i]]], 1.0)
7878
end
7979
return k
8080
end
@@ -88,11 +88,11 @@ N = 30
8888
μs = [-3.5, 0.0]
8989

9090
# Construct the data points.
91-
data = mapreduce(c -> rand(MvNormal([μs[c], μs[c]], 1.), N), hcat, 1:2);
91+
data = mapreduce(c -> rand(MvNormal([μs[c], μs[c]], 1.0), N), hcat, 1:2);
9292
```
9393

9494
```julia; echo=false
95-
weave_child(WEAVE_ARGS[:benchmarkbody], mod = @__MODULE__, args = WEAVE_ARGS)
95+
weave_child(WEAVE_ARGS[:benchmarkbody]; mod=@__MODULE__, args=WEAVE_ARGS)
9696
```
9797

9898
### `demo4`: loads of indexing
@@ -107,24 +107,24 @@ weave_child(WEAVE_ARGS[:benchmarkbody], mod = @__MODULE__, args = WEAVE_ARGS)
107107
end
108108

109109
model_def = demo4
110-
data = (100_000, );
110+
data = (100_000,);
111111
```
112112

113113
```julia; echo=false
114-
weave_child(WEAVE_ARGS[:benchmarkbody], mod = @__MODULE__, args = WEAVE_ARGS)
114+
weave_child(WEAVE_ARGS[:benchmarkbody]; mod=@__MODULE__, args=WEAVE_ARGS)
115115
```
116116

117117
```julia
118118
@model function demo4_dotted(n, ::Type{TV}=Vector{Float64}) where {TV}
119119
m ~ Normal()
120120
x = TV(undef, n)
121-
x .~ Normal(m, 1.0)
121+
return x .~ Normal(m, 1.0)
122122
end
123123

124124
model_def = demo4_dotted
125-
data = (100_000, );
125+
data = (100_000,);
126126
```
127127

128128
```julia; echo=false
129-
weave_child(WEAVE_ARGS[:benchmarkbody], mod = @__MODULE__, args = WEAVE_ARGS)
129+
weave_child(WEAVE_ARGS[:benchmarkbody]; mod=@__MODULE__, args=WEAVE_ARGS)
130130
```

docs/src/index.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
# DynamicPPL.jl
22

33
A domain-specific language and backend for probabilistic programming languages, used by [Turing.jl](https://github.com/TuringLang/Turing.jl).
4-

docs/src/tutorials/prob-interface.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ nothing # hide
4747
```
4848

4949
We can also decondition only some of the variables:
50+
5051
```@example probinterface
5152
decondition(model, :μ)
5253
nothing # hide

src/submodel_macro.jl

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -120,49 +120,49 @@ julia> @model inner() = x ~ Normal()
120120
inner (generic function with 2 methods)
121121
122122
julia> # When `prefix` is unspecified, no prefix is used.
123-
@model outer() = @submodel a = inner()
124-
outer (generic function with 2 methods)
123+
@model submodel_noprefix() = @submodel a = inner()
124+
submodel_noprefix (generic function with 2 methods)
125125
126-
julia> @varname(x) in keys(VarInfo(outer()))
126+
julia> @varname(x) in keys(VarInfo(submodel_noprefix()))
127127
true
128128
129129
julia> # Explicitely don't use any prefix.
130-
@model outer() = @submodel prefix=false a = inner()
131-
outer (generic function with 2 methods)
130+
@model submodel_prefix_false() = @submodel prefix=false a = inner()
131+
submodel_prefix_false (generic function with 2 methods)
132132
133-
julia> @varname(x) in keys(VarInfo(outer()))
133+
julia> @varname(x) in keys(VarInfo(submodel_prefix_false()))
134134
true
135135
136136
julia> # Automatically determined from `a`.
137-
@model outer() = @submodel prefix=true a = inner()
138-
outer (generic function with 2 methods)
137+
@model submodel_prefix_true() = @submodel prefix=true a = inner()
138+
submodel_prefix_true (generic function with 2 methods)
139139
140-
julia> @varname(var"a.x") in keys(VarInfo(outer()))
140+
julia> @varname(var"a.x") in keys(VarInfo(submodel_prefix_true()))
141141
true
142142
143143
julia> # Using a static string.
144-
@model outer() = @submodel prefix="my prefix" a = inner()
145-
outer (generic function with 2 methods)
144+
@model submodel_prefix_string() = @submodel prefix="my prefix" a = inner()
145+
submodel_prefix_string (generic function with 2 methods)
146146
147-
julia> @varname(var"my prefix.x") in keys(VarInfo(outer()))
147+
julia> @varname(var"my prefix.x") in keys(VarInfo(submodel_prefix_string()))
148148
true
149149
150150
julia> # Using string interpolation.
151-
@model outer() = @submodel prefix="\$(nameof(inner()))" a = inner()
152-
outer (generic function with 2 methods)
151+
@model submodel_prefix_interpolation() = @submodel prefix="\$(nameof(inner()))" a = inner()
152+
submodel_prefix_interpolation (generic function with 2 methods)
153153
154-
julia> @varname(var"inner.x") in keys(VarInfo(outer()))
154+
julia> @varname(var"inner.x") in keys(VarInfo(submodel_prefix_interpolation()))
155155
true
156156
157157
julia> # Or using some arbitrary expression.
158-
@model outer() = @submodel prefix=1 + 2 a = inner()
159-
outer (generic function with 2 methods)
158+
@model submodel_prefix_expr() = @submodel prefix=1 + 2 a = inner()
159+
submodel_prefix_expr (generic function with 2 methods)
160160
161-
julia> @varname(var"3.x") in keys(VarInfo(outer()))
161+
julia> @varname(var"3.x") in keys(VarInfo(submodel_prefix_expr()))
162162
true
163163
164164
julia> # (×) Automatic prefixing without a left-hand side expression does not work!
165-
@model outer() = @submodel prefix=true inner()
165+
@model submodel_prefix_error() = @submodel prefix=true inner()
166166
ERROR: LoadError: cannot automatically prefix with no left-hand side
167167
[...]
168168
```

test/lkj.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
using Bijectors: pd_from_upper, pd_from_lower
2-
31
function pd_from_triangular(X::AbstractMatrix, uplo::Char)
4-
return uplo == 'U' ? pd_from_upper(X) : pd_from_lower(X)
2+
# Pre-allocation fixes a problem with abstract element types in Julia 1.10
3+
# Ref https://github.com/TuringLang/DynamicPPL.jl/pull/570#issue-2092729916
4+
out = similar(X, Base.promote_op(*, eltype(X), eltype(X)))
5+
if uplo === 'U'
6+
mul!(out, UpperTriangular(X)', UpperTriangular(X))
7+
else
8+
mul!(out, LowerTriangular(X), LowerTriangular(X)')
9+
end
10+
return out
511
end
612

713
@model lkj_prior_demo() = x ~ LKJ(2, 1)

test/turing/Project.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
66
Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"
77

88
[compat]
9-
DynamicPPL = "0.20, 0.21, 0.22, 0.23, 0.24"
10-
Turing = "0.21 - 0.30"
11-
julia = "1.6"
9+
DynamicPPL = "0.24"
10+
Turing = "0.30"
11+
julia = "1.7"

test/turing/compiler.jl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@
110110
end
111111

112112
chain = sample(
113-
newinterface(obs), HMC{Turing.ForwardDiffAD{2}}(0.75, 3, :p, :x), 100
113+
newinterface(obs),
114+
HMC(0.75, 3, :p, :x; adtype=AutoForwardDiff(; chunksize=2)),
115+
100,
114116
)
115117
end
116118
@testset "no return" begin
@@ -186,8 +188,7 @@
186188

187189
# Vector assumptions
188190
N = 10
189-
setchunksize(N)
190-
alg = HMC(0.2, 4)
191+
alg = HMC(0.2, 4; adtype=AutoForwardDiff(; chunksize=N))
191192

192193
@model function vdemo3()
193194
x = Vector{Real}(undef, N)
@@ -254,8 +255,7 @@
254255

255256
# Vector assumptions
256257
N = 10
257-
setchunksize(N)
258-
alg = HMC(0.2, 4)
258+
alg = HMC(0.2, 4; adtype=AutoForwardDiff(; chunksize=N))
259259

260260
@model function vdemo3()
261261
x = Vector{Real}(undef, N)
@@ -300,8 +300,7 @@
300300
end
301301
@testset "Type parameters" begin
302302
N = 10
303-
setchunksize(N)
304-
alg = HMC(0.01, 5)
303+
alg = HMC(0.01, 5; adtype=AutoForwardDiff(; chunksize=N))
305304
x = randn(1000)
306305
@model function vdemo1(::Type{T}=Float64) where {T}
307306
x = Vector{T}(undef, N)

0 commit comments

Comments
 (0)