Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test error with Turing >= 0.30 #570

Merged
merged 5 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
To run the benchmarks, simply do:

```sh
julia --project -e 'using DynamicPPLBenchmarks; weave_benchmarks();'
```
Expand Down
13 changes: 8 additions & 5 deletions benchmarks/benchmark_body.jmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ results["evaluation_typed"]
```

```julia; echo=false; results="hidden";
BenchmarkTools.save(joinpath("results", WEAVE_ARGS[:name], "$(nameof(m))_benchmarks.json"), results)
BenchmarkTools.save(
joinpath("results", WEAVE_ARGS[:name], "$(nameof(m))_benchmarks.json"), results
)
```

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

```julia; wrap=false; echo=false;
if haskey(WEAVE_ARGS, :name_old)
# We want to compare the generated code to the previous version.
import DiffUtils
typed_old = deserialize(joinpath("results", WEAVE_ARGS[:name_old], "$(nameof(m)).jls"));
DiffUtils.diff(typed_old, string(typed), width=130)
using DiffUtils: DiffUtils
typed_old = deserialize(joinpath("results", WEAVE_ARGS[:name_old], "$(nameof(m)).jls"))
DiffUtils.diff(typed_old, string(typed); width=130)
end
```
24 changes: 12 additions & 12 deletions benchmarks/benchmarks.jmd
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ import DynamicPPLBenchmarks: time_model_def, make_suite, typed_code, weave_child
m ~ Normal()
x ~ Normal(m, 1)

return (m = m, x = x)
return (m=m, x=x)
end

model_def = demo1;
data = 1.0;
```

```julia; results="markup"; echo=false
weave_child(WEAVE_ARGS[:benchmarkbody], mod = @__MODULE__, args = WEAVE_ARGS)
weave_child(WEAVE_ARGS[:benchmarkbody]; mod=@__MODULE__, args=WEAVE_ARGS)
```

### `demo2`

```julia
@model function demo2(y)
@model function demo2(y)
# Our prior belief about the probability of heads in a coin.
p ~ Beta(1, 1)

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

```julia; results="markup"; echo=false
weave_child(WEAVE_ARGS[:benchmarkbody], mod = @__MODULE__, args = WEAVE_ARGS)
weave_child(WEAVE_ARGS[:benchmarkbody]; mod=@__MODULE__, args=WEAVE_ARGS)
```

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

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

```julia; echo=false
weave_child(WEAVE_ARGS[:benchmarkbody], mod = @__MODULE__, args = WEAVE_ARGS)
weave_child(WEAVE_ARGS[:benchmarkbody]; mod=@__MODULE__, args=WEAVE_ARGS)
```

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

model_def = demo4
data = (100_000, );
data = (100_000,);
```

```julia; echo=false
weave_child(WEAVE_ARGS[:benchmarkbody], mod = @__MODULE__, args = WEAVE_ARGS)
weave_child(WEAVE_ARGS[:benchmarkbody]; mod=@__MODULE__, args=WEAVE_ARGS)
```

```julia
@model function demo4_dotted(n, ::Type{TV}=Vector{Float64}) where {TV}
m ~ Normal()
x = TV(undef, n)
x .~ Normal(m, 1.0)
return x .~ Normal(m, 1.0)
end

model_def = demo4_dotted
data = (100_000, );
data = (100_000,);
```

```julia; echo=false
weave_child(WEAVE_ARGS[:benchmarkbody], mod = @__MODULE__, args = WEAVE_ARGS)
weave_child(WEAVE_ARGS[:benchmarkbody]; mod=@__MODULE__, args=WEAVE_ARGS)
```
1 change: 0 additions & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# DynamicPPL.jl

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

1 change: 1 addition & 0 deletions docs/src/tutorials/prob-interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ nothing # hide
```

We can also decondition only some of the variables:

```@example probinterface
decondition(model, :μ)
nothing # hide
Expand Down
38 changes: 19 additions & 19 deletions src/submodel_macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,49 +120,49 @@ julia> @model inner() = x ~ Normal()
inner (generic function with 2 methods)

julia> # When `prefix` is unspecified, no prefix is used.
@model outer() = @submodel a = inner()
outer (generic function with 2 methods)
@model submodel_noprefix() = @submodel a = inner()
submodel_noprefix (generic function with 2 methods)

julia> @varname(x) in keys(VarInfo(outer()))
julia> @varname(x) in keys(VarInfo(submodel_noprefix()))
true

julia> # Explicitely don't use any prefix.
@model outer() = @submodel prefix=false a = inner()
outer (generic function with 2 methods)
@model submodel_prefix_false() = @submodel prefix=false a = inner()
submodel_prefix_false (generic function with 2 methods)

julia> @varname(x) in keys(VarInfo(outer()))
julia> @varname(x) in keys(VarInfo(submodel_prefix_false()))
true

julia> # Automatically determined from `a`.
@model outer() = @submodel prefix=true a = inner()
outer (generic function with 2 methods)
@model submodel_prefix_true() = @submodel prefix=true a = inner()
submodel_prefix_true (generic function with 2 methods)

julia> @varname(var"a.x") in keys(VarInfo(outer()))
julia> @varname(var"a.x") in keys(VarInfo(submodel_prefix_true()))
true

julia> # Using a static string.
@model outer() = @submodel prefix="my prefix" a = inner()
outer (generic function with 2 methods)
@model submodel_prefix_string() = @submodel prefix="my prefix" a = inner()
submodel_prefix_string (generic function with 2 methods)

julia> @varname(var"my prefix.x") in keys(VarInfo(outer()))
julia> @varname(var"my prefix.x") in keys(VarInfo(submodel_prefix_string()))
true

julia> # Using string interpolation.
@model outer() = @submodel prefix="\$(nameof(inner()))" a = inner()
outer (generic function with 2 methods)
@model submodel_prefix_interpolation() = @submodel prefix="\$(nameof(inner()))" a = inner()
submodel_prefix_interpolation (generic function with 2 methods)

julia> @varname(var"inner.x") in keys(VarInfo(outer()))
julia> @varname(var"inner.x") in keys(VarInfo(submodel_prefix_interpolation()))
true

julia> # Or using some arbitrary expression.
@model outer() = @submodel prefix=1 + 2 a = inner()
outer (generic function with 2 methods)
@model submodel_prefix_expr() = @submodel prefix=1 + 2 a = inner()
submodel_prefix_expr (generic function with 2 methods)

julia> @varname(var"3.x") in keys(VarInfo(outer()))
julia> @varname(var"3.x") in keys(VarInfo(submodel_prefix_expr()))
true

julia> # (×) Automatic prefixing without a left-hand side expression does not work!
@model outer() = @submodel prefix=true inner()
@model submodel_prefix_error() = @submodel prefix=true inner()
ERROR: LoadError: cannot automatically prefix with no left-hand side
[...]
```
Expand Down
12 changes: 9 additions & 3 deletions test/lkj.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
using Bijectors: pd_from_upper, pd_from_lower

function pd_from_triangular(X::AbstractMatrix, uplo::Char)
return uplo == 'U' ? pd_from_upper(X) : pd_from_lower(X)
# Pre-allocation fixes a problem with abstract element types in Julia 1.10
# Ref https://github.com/TuringLang/DynamicPPL.jl/pull/570#issue-2092729916
out = similar(X, Base.promote_op(*, eltype(X), eltype(X)))
if uplo === 'U'
mul!(out, UpperTriangular(X)', UpperTriangular(X))
else
mul!(out, LowerTriangular(X), LowerTriangular(X)')
end
return out
end

@model lkj_prior_demo() = x ~ LKJ(2, 1)
Expand Down
6 changes: 3 additions & 3 deletions test/turing/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"

[compat]
DynamicPPL = "0.20, 0.21, 0.22, 0.23, 0.24"
Turing = "0.21 - 0.30"
julia = "1.6"
DynamicPPL = "0.24"
Turing = "0.30"
julia = "1.7"
torfjelde marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 6 additions & 7 deletions test/turing/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@
end

chain = sample(
newinterface(obs), HMC{Turing.ForwardDiffAD{2}}(0.75, 3, :p, :x), 100
newinterface(obs),
HMC(0.75, 3, :p, :x; adtype=AutoForwardDiff(; chunksize=2)),
100,
)
end
@testset "no return" begin
Expand Down Expand Up @@ -186,8 +188,7 @@

# Vector assumptions
N = 10
setchunksize(N)
alg = HMC(0.2, 4)
alg = HMC(0.2, 4; adtype=AutoForwardDiff(; chunksize=N))

@model function vdemo3()
x = Vector{Real}(undef, N)
Expand Down Expand Up @@ -254,8 +255,7 @@

# Vector assumptions
N = 10
setchunksize(N)
alg = HMC(0.2, 4)
alg = HMC(0.2, 4; adtype=AutoForwardDiff(; chunksize=N))

@model function vdemo3()
x = Vector{Real}(undef, N)
Expand Down Expand Up @@ -300,8 +300,7 @@
end
@testset "Type parameters" begin
N = 10
setchunksize(N)
alg = HMC(0.01, 5)
alg = HMC(0.01, 5; adtype=AutoForwardDiff(; chunksize=N))
x = randn(1000)
@model function vdemo1(::Type{T}=Float64) where {T}
x = Vector{T}(undef, N)
Expand Down
Loading