diff --git a/benchmarks/README.md b/benchmarks/README.md index 6e6fc2264..9ddebc4bd 100644 --- a/benchmarks/README.md +++ b/benchmarks/README.md @@ -1,4 +1,5 @@ To run the benchmarks, simply do: + ```sh julia --project -e 'using DynamicPPLBenchmarks; weave_benchmarks();' ``` diff --git a/benchmarks/benchmark_body.jmd b/benchmarks/benchmark_body.jmd index 4e73f579e..ba02957e7 100644 --- a/benchmarks/benchmark_body.jmd +++ b/benchmarks/benchmark_body.jmd @@ -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 @@ -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 ``` diff --git a/benchmarks/benchmarks.jmd b/benchmarks/benchmarks.jmd index 5b86b261e..8021e4883 100644 --- a/benchmarks/benchmarks.jmd +++ b/benchmarks/benchmarks.jmd @@ -19,7 +19,7 @@ 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; @@ -27,13 +27,13 @@ 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) @@ -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` @@ -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 @@ -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 @@ -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) ``` diff --git a/docs/src/index.md b/docs/src/index.md index 5e6855c20..a9dbab75b 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -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). - diff --git a/docs/src/tutorials/prob-interface.md b/docs/src/tutorials/prob-interface.md index 9d3b5c6bd..dc9f50204 100644 --- a/docs/src/tutorials/prob-interface.md +++ b/docs/src/tutorials/prob-interface.md @@ -47,6 +47,7 @@ nothing # hide ``` We can also decondition only some of the variables: + ```@example probinterface decondition(model, :μ) nothing # hide diff --git a/src/submodel_macro.jl b/src/submodel_macro.jl index 7e7cf0686..050bf31fc 100644 --- a/src/submodel_macro.jl +++ b/src/submodel_macro.jl @@ -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 [...] ``` diff --git a/test/lkj.jl b/test/lkj.jl index 4fe8a83af..b9c20f916 100644 --- a/test/lkj.jl +++ b/test/lkj.jl @@ -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) diff --git a/test/turing/Project.toml b/test/turing/Project.toml index 03c3a2121..71c7c2263 100644 --- a/test/turing/Project.toml +++ b/test/turing/Project.toml @@ -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" diff --git a/test/turing/compiler.jl b/test/turing/compiler.jl index adda07fe9..31bd5bbbe 100644 --- a/test/turing/compiler.jl +++ b/test/turing/compiler.jl @@ -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 @@ -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) @@ -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) @@ -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)