Skip to content

Test Enzyme on demo models #813

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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 test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
DistributionsAD = "ced4e74d-a319-5a8a-b0ac-84af2272839c"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
EnzymeCore = "f151be2c-9106-41f4-ab19-57ee4f262869"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
Expand Down
227 changes: 115 additions & 112 deletions test/ad.jl
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
using DynamicPPL: LogDensityFunction
using EnzymeCore: set_runtime_activity, Forward, Reverse

@testset "Automatic differentiation" begin
# Used as the ground truth that others are compared against.
ref_adtype = AutoForwardDiff()
test_adtypes = [
AutoReverseDiff(; compile=false),
AutoReverseDiff(; compile=true),
AutoMooncake(; config=nothing),
# AutoReverseDiff(; compile=false),
# AutoReverseDiff(; compile=true),
# AutoMooncake(; config=nothing),
AutoEnzyme(; mode=set_runtime_activity(Forward, true)),
AutoEnzyme(; mode=set_runtime_activity(Reverse, true)),
]

@testset "Unsupported backends" begin
@model demo() = x ~ Normal()
@test_logs (:warn, r"not officially supported") LogDensityFunction(
demo(); adtype=AutoZygote()
)
end
# @testset "Unsupported backends" begin
# @model demo() = x ~ Normal()
# @test_logs (:warn, r"not officially supported") LogDensityFunction(
# demo(); adtype=AutoZygote()
# )
# end

@testset "Correctness: ForwardDiff, ReverseDiff, and Mooncake" begin
@testset "Correctness on supported AD backends" begin
@testset "$(m.f)" for m in DynamicPPL.TestUtils.DEMO_MODELS
rand_param_values = DynamicPPL.TestUtils.rand_prior_true(m)
vns = DynamicPPL.TestUtils.varnames(m)
Expand Down Expand Up @@ -66,106 +69,106 @@ using DynamicPPL: LogDensityFunction
end
end

@testset "Turing#2151: ReverseDiff compilation & eltype(vi, spl)" begin
# Failing model
t = 1:0.05:8
σ = 0.3
y = @. rand(sin(t) + Normal(0, σ))
@model function state_space(y, TT, ::Type{T}=Float64) where {T}
# Priors
α ~ Normal(y[1], 0.001)
τ ~ Exponential(1)
η ~ filldist(Normal(0, 1), TT - 1)
σ ~ Exponential(1)
# create latent variable
x = Vector{T}(undef, TT)
x[1] = α
for t in 2:TT
x[t] = x[t - 1] + η[t - 1] * τ
end
# measurement model
y ~ MvNormal(x, σ^2 * I)
return x
end
model = state_space(y, length(t))

# Dummy sampling algorithm for testing. The test case can only be replicated
# with a custom sampler, it doesn't work with SampleFromPrior(). We need to
# overload assume so that model evaluation doesn't fail due to a lack
# of implementation
struct MyEmptyAlg end
DynamicPPL.assume(
::Random.AbstractRNG, ::DynamicPPL.Sampler{MyEmptyAlg}, dist, vn, vi
) = DynamicPPL.assume(dist, vn, vi)

# Compiling the ReverseDiff tape used to fail here
spl = Sampler(MyEmptyAlg())
vi = VarInfo(model)
ldf = LogDensityFunction(
model, vi, SamplingContext(spl); adtype=AutoReverseDiff(; compile=true)
)
@test LogDensityProblems.logdensity_and_gradient(ldf, vi[:]) isa Any
end

# Test that various different ways of specifying array types as arguments work with all
# ADTypes.
@testset "Array argument types" begin
test_m = randn(2, 3)

function eval_logp_and_grad(model, m, adtype)
ldf = LogDensityFunction(model(); adtype=adtype)
return LogDensityProblems.logdensity_and_gradient(ldf, m[:])
end

@model function scalar_matrix_model(::Type{T}=Float64) where {T<:Real}
m = Matrix{T}(undef, 2, 3)
return m ~ filldist(MvNormal(zeros(2), I), 3)
end

scalar_matrix_model_reference = eval_logp_and_grad(
scalar_matrix_model, test_m, ref_adtype
)

@model function matrix_model(::Type{T}=Matrix{Float64}) where {T}
m = T(undef, 2, 3)
return m ~ filldist(MvNormal(zeros(2), I), 3)
end

matrix_model_reference = eval_logp_and_grad(matrix_model, test_m, ref_adtype)

@model function scalar_array_model(::Type{T}=Float64) where {T<:Real}
m = Array{T}(undef, 2, 3)
return m ~ filldist(MvNormal(zeros(2), I), 3)
end

scalar_array_model_reference = eval_logp_and_grad(
scalar_array_model, test_m, ref_adtype
)

@model function array_model(::Type{T}=Array{Float64}) where {T}
m = T(undef, 2, 3)
return m ~ filldist(MvNormal(zeros(2), I), 3)
end

array_model_reference = eval_logp_and_grad(array_model, test_m, ref_adtype)

@testset "$adtype" for adtype in test_adtypes
scalar_matrix_model_logp_and_grad = eval_logp_and_grad(
scalar_matrix_model, test_m, adtype
)
@test scalar_matrix_model_logp_and_grad[1] ≈ scalar_matrix_model_reference[1]
@test scalar_matrix_model_logp_and_grad[2] ≈ scalar_matrix_model_reference[2]
matrix_model_logp_and_grad = eval_logp_and_grad(matrix_model, test_m, adtype)
@test matrix_model_logp_and_grad[1] ≈ matrix_model_reference[1]
@test matrix_model_logp_and_grad[2] ≈ matrix_model_reference[2]
scalar_array_model_logp_and_grad = eval_logp_and_grad(
scalar_array_model, test_m, adtype
)
@test scalar_array_model_logp_and_grad[1] ≈ scalar_array_model_reference[1]
@test scalar_array_model_logp_and_grad[2] ≈ scalar_array_model_reference[2]
array_model_logp_and_grad = eval_logp_and_grad(array_model, test_m, adtype)
@test array_model_logp_and_grad[1] ≈ array_model_reference[1]
@test array_model_logp_and_grad[2] ≈ array_model_reference[2]
end
end
# @testset "Turing#2151: ReverseDiff compilation & eltype(vi, spl)" begin
# # Failing model
# t = 1:0.05:8
# σ = 0.3
# y = @. rand(sin(t) + Normal(0, σ))
# @model function state_space(y, TT, ::Type{T}=Float64) where {T}
# # Priors
# α ~ Normal(y[1], 0.001)
# τ ~ Exponential(1)
# η ~ filldist(Normal(0, 1), TT - 1)
# σ ~ Exponential(1)
# # create latent variable
# x = Vector{T}(undef, TT)
# x[1] = α
# for t in 2:TT
# x[t] = x[t - 1] + η[t - 1] * τ
# end
# # measurement model
# y ~ MvNormal(x, σ^2 * I)
# return x
# end
# model = state_space(y, length(t))
#
# # Dummy sampling algorithm for testing. The test case can only be replicated
# # with a custom sampler, it doesn't work with SampleFromPrior(). We need to
# # overload assume so that model evaluation doesn't fail due to a lack
# # of implementation
# struct MyEmptyAlg end
# DynamicPPL.assume(
# ::Random.AbstractRNG, ::DynamicPPL.Sampler{MyEmptyAlg}, dist, vn, vi
# ) = DynamicPPL.assume(dist, vn, vi)
#
# # Compiling the ReverseDiff tape used to fail here
# spl = Sampler(MyEmptyAlg())
# vi = VarInfo(model)
# ldf = LogDensityFunction(
# model, vi, SamplingContext(spl); adtype=AutoReverseDiff(; compile=true)
# )
# @test LogDensityProblems.logdensity_and_gradient(ldf, vi[:]) isa Any
# end
#
# # Test that various different ways of specifying array types as arguments work with all
# # ADTypes.
# @testset "Array argument types" begin
# test_m = randn(2, 3)
#
# function eval_logp_and_grad(model, m, adtype)
# ldf = LogDensityFunction(model(); adtype=adtype)
# return LogDensityProblems.logdensity_and_gradient(ldf, m[:])
# end
#
# @model function scalar_matrix_model(::Type{T}=Float64) where {T<:Real}
# m = Matrix{T}(undef, 2, 3)
# return m ~ filldist(MvNormal(zeros(2), I), 3)
# end
#
# scalar_matrix_model_reference = eval_logp_and_grad(
# scalar_matrix_model, test_m, ref_adtype
# )
#
# @model function matrix_model(::Type{T}=Matrix{Float64}) where {T}
# m = T(undef, 2, 3)
# return m ~ filldist(MvNormal(zeros(2), I), 3)
# end
#
# matrix_model_reference = eval_logp_and_grad(matrix_model, test_m, ref_adtype)
#
# @model function scalar_array_model(::Type{T}=Float64) where {T<:Real}
# m = Array{T}(undef, 2, 3)
# return m ~ filldist(MvNormal(zeros(2), I), 3)
# end
#
# scalar_array_model_reference = eval_logp_and_grad(
# scalar_array_model, test_m, ref_adtype
# )
#
# @model function array_model(::Type{T}=Array{Float64}) where {T}
# m = T(undef, 2, 3)
# return m ~ filldist(MvNormal(zeros(2), I), 3)
# end
#
# array_model_reference = eval_logp_and_grad(array_model, test_m, ref_adtype)
#
# @testset "$adtype" for adtype in test_adtypes
# scalar_matrix_model_logp_and_grad = eval_logp_and_grad(
# scalar_matrix_model, test_m, adtype
# )
# @test scalar_matrix_model_logp_and_grad[1] ≈ scalar_matrix_model_reference[1]
# @test scalar_matrix_model_logp_and_grad[2] ≈ scalar_matrix_model_reference[2]
# matrix_model_logp_and_grad = eval_logp_and_grad(matrix_model, test_m, adtype)
# @test matrix_model_logp_and_grad[1] ≈ matrix_model_reference[1]
# @test matrix_model_logp_and_grad[2] ≈ matrix_model_reference[2]
# scalar_array_model_logp_and_grad = eval_logp_and_grad(
# scalar_array_model, test_m, adtype
# )
# @test scalar_array_model_logp_and_grad[1] ≈ scalar_array_model_reference[1]
# @test scalar_array_model_logp_and_grad[2] ≈ scalar_array_model_reference[2]
# array_model_logp_and_grad = eval_logp_and_grad(array_model, test_m, adtype)
# @test array_model_logp_and_grad[1] ≈ array_model_reference[1]
# @test array_model_logp_and_grad[2] ≈ array_model_reference[2]
# end
# end
end
99 changes: 50 additions & 49 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using DifferentiationInterface
using Distributions
using DistributionsAD
using Documenter
using Enzyme: Enzyme
using ForwardDiff
using LogDensityProblems
using MacroTools
Expand Down Expand Up @@ -45,60 +46,60 @@ include("test_util.jl")
# groups are chosen to make both groups take roughly the same amount of
# time, but beyond that there is no particular reason for the split.
if GROUP == "All" || GROUP == "Group1"
if AQUA
include("Aqua.jl")
end
include("utils.jl")
include("compiler.jl")
include("varnamedvector.jl")
include("varinfo.jl")
include("simple_varinfo.jl")
include("model.jl")
include("sampler.jl")
include("independence.jl")
include("distribution_wrappers.jl")
include("logdensityfunction.jl")
include("linking.jl")
include("serialization.jl")
include("pointwise_logdensities.jl")
include("lkj.jl")
include("contexts.jl")
include("context_implementations.jl")
include("threadsafe.jl")
include("debug_utils.jl")
include("deprecated.jl")
# if AQUA
# include("Aqua.jl")
# end
# include("utils.jl")
# include("compiler.jl")
# include("varnamedvector.jl")
# include("varinfo.jl")
# include("simple_varinfo.jl")
# include("model.jl")
# include("sampler.jl")
# include("independence.jl")
# include("distribution_wrappers.jl")
# include("logdensityfunction.jl")
# include("linking.jl")
# include("serialization.jl")
# include("pointwise_logdensities.jl")
# include("lkj.jl")
# include("contexts.jl")
# include("context_implementations.jl")
# include("threadsafe.jl")
# include("debug_utils.jl")
# include("deprecated.jl")
end

if GROUP == "All" || GROUP == "Group2"
@testset "compat" begin
include(joinpath("compat", "ad.jl"))
end
@testset "extensions" begin
include("ext/DynamicPPLMCMCChainsExt.jl")
include("ext/DynamicPPLJETExt.jl")
end
# @testset "compat" begin
# include(joinpath("compat", "ad.jl"))
# end
# @testset "extensions" begin
# include("ext/DynamicPPLMCMCChainsExt.jl")
# include("ext/DynamicPPLJETExt.jl")
# end
@testset "ad" begin
include("ext/DynamicPPLForwardDiffExt.jl")
include("ext/DynamicPPLMooncakeExt.jl")
# include("ext/DynamicPPLForwardDiffExt.jl")
# include("ext/DynamicPPLMooncakeExt.jl")
include("ad.jl")
end
@testset "prob and logprob macro" begin
@test_throws ErrorException prob"..."
@test_throws ErrorException logprob"..."
end
@testset "doctests" begin
DocMeta.setdocmeta!(
DynamicPPL,
:DocTestSetup,
:(using DynamicPPL, Distributions);
recursive=true,
)
doctestfilters = [
# Ignore the source of a warning in the doctest output, since this is dependent on host.
# This is a line that starts with "└ @ " and ends with the line number.
r"└ @ .+:[0-9]+",
]
doctest(DynamicPPL; manual=false, doctestfilters=doctestfilters)
end
# @testset "prob and logprob macro" begin
# @test_throws ErrorException prob"..."
# @test_throws ErrorException logprob"..."
# end
# @testset "doctests" begin
# DocMeta.setdocmeta!(
# DynamicPPL,
# :DocTestSetup,
# :(using DynamicPPL, Distributions);
# recursive=true,
# )
# doctestfilters = [
# # Ignore the source of a warning in the doctest output, since this is dependent on host.
# # This is a line that starts with "└ @ " and ends with the line number.
# r"└ @ .+:[0-9]+",
# ]
# doctest(DynamicPPL; manual=false, doctestfilters=doctestfilters)
# end
end
end
Loading