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

Make ZygoteRules and ChainRulesCore weak dependencies #564

Merged
merged 5 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
ZygoteRules = "700de1a5-db45-46bc-99cf-38207098b444"

[weakdeps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d"
ZygoteRules = "700de1a5-db45-46bc-99cf-38207098b444"

[extensions]
DynamicPPLChainRulesCoreExt = ["ChainRulesCore"]
DynamicPPLMCMCChainsExt = ["MCMCChains"]
DynamicPPLZygoteRulesExt = ["ZygoteRules"]

[compat]
AbstractMCMC = "5"
Expand All @@ -51,4 +55,6 @@ Test = "1.6"
julia = "1.6"

[extras]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d"
ZygoteRules = "700de1a5-db45-46bc-99cf-38207098b444"
27 changes: 27 additions & 0 deletions ext/DynamicPPLChainRulesCoreExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module DynamicPPLChainRulesCoreExt

if isdefined(Base, :get_extension)
using DynamicPPL: DynamicPPL, BangBang, Distributions
using ChainRulesCore: ChainRulesCore
else
using ..DynamicPPL: DynamicPPL, BangBang, Distributions
using ..ChainRulesCore: ChainRulesCore
end

# See https://github.com/TuringLang/Turing.jl/issues/1199
ChainRulesCore.@non_differentiable BangBang.push!!(
vi::DynamicPPL.VarInfo,
vn::DynamicPPL.VarName,
r,
dist::Distributions.Distribution,
gidset::Set{DynamicPPL.Selector},
)

ChainRulesCore.@non_differentiable DynamicPPL.updategid!(
vi::DynamicPPL.AbstractVarInfo, vn::DynamicPPL.VarName, spl::DynamicPPL.Sampler
)

# No need + causes issues for some AD backends, e.g. Zygote.
ChainRulesCore.@non_differentiable DynamicPPL.infer_nested_eltype(x)

end # module
25 changes: 25 additions & 0 deletions ext/DynamicPPLZygoteRulesExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module DynamicPPLZygoteRulesExt

if isdefined(Base, :get_extension)
using DynamicPPL: DynamicPPL, Distributions
using ZygoteRules: ZygoteRules
else
using ..DynamicPPL: DynamicPPL, Distributions
using ..ZygoteRules: ZygoteRules
end

# https://github.com/TuringLang/Turing.jl/issues/1595
ZygoteRules.@adjoint function DynamicPPL.dot_observe(

Check warning on line 12 in ext/DynamicPPLZygoteRulesExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/DynamicPPLZygoteRulesExt.jl#L12

Added line #L12 was not covered by tests
spl::Union{DynamicPPL.SampleFromPrior,DynamicPPL.SampleFromUniform},
dists::AbstractArray{<:Distributions.Distribution},
value::AbstractArray,
vi,
)
function dot_observe_fallback(spl, dists, value, vi)
DynamicPPL.increment_num_produce!(vi)
return sum(map(Distributions.loglikelihood, dists, value)), vi
end
return ZygoteRules.pullback(__context__, dot_observe_fallback, spl, dists, value, vi)
end

end # module
9 changes: 6 additions & 3 deletions src/DynamicPPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@

using AbstractMCMC: AbstractMCMC
using BangBang: BangBang, push!!, empty!!, setindex!!
using ChainRulesCore: ChainRulesCore
using MacroTools: MacroTools
using ConstructionBase: ConstructionBase
using Setfield: Setfield
using ZygoteRules: ZygoteRules
using LogDensityProblems: LogDensityProblems

using LinearAlgebra: LinearAlgebra, Cholesky
Expand Down Expand Up @@ -171,7 +169,6 @@
include("context_implementations.jl")
include("compiler.jl")
include("prob_macro.jl")
include("compat/ad.jl")
include("loglikelihoods.jl")
include("submodel_macro.jl")
include("test_utils.jl")
Expand All @@ -186,9 +183,15 @@

@static if !isdefined(Base, :get_extension)
function __init__()
@require ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" include(

Check warning on line 186 in src/DynamicPPL.jl

View check run for this annotation

Codecov / codecov/patch

src/DynamicPPL.jl#L186

Added line #L186 was not covered by tests
"../ext/DynamicPPLChainRulesCoreExt.jl"
)
@require MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d" include(
"../ext/DynamicPPLMCMCChainsExt.jl"
)
@require ZygoteRules = "700de1a5-db45-46bc-99cf-38207098b444" include(

Check warning on line 192 in src/DynamicPPL.jl

View check run for this annotation

Codecov / codecov/patch

src/DynamicPPL.jl#L192

Added line #L192 was not covered by tests
"../ext/DynamicPPLZygoteRulesExt.jl"
)
end
end

Expand Down
22 changes: 0 additions & 22 deletions src/compat/ad.jl

This file was deleted.

3 changes: 0 additions & 3 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -883,9 +883,6 @@ end
# Handle `AbstractDict` differently since `eltype` results in a `Pair`.
infer_nested_eltype(::Type{<:AbstractDict{<:Any,ET}}) where {ET} = infer_nested_eltype(ET)

# No need + causes issues for some AD backends, e.g. Zygote.
ChainRulesCore.@non_differentiable infer_nested_eltype(x)

"""
varname_leaves(vn::VarName, val)

Expand Down
Loading