forked from TuringLang/DynamicPPL.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make ZygoteRules and ChainRulesCore weak dependencies (TuringLang#564)
* Make ZygoteRules and ChainRulesCore weak dependencies * Fix format * Add another non-differentiable to CRC extension * Perform coverage analysis on all Julia versions
- Loading branch information
Showing
7 changed files
with
70 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters