Skip to content

Commit

Permalink
DistributionsChainRulesCoreExt: make rrule/frule take `ChainRul…
Browse files Browse the repository at this point in the history
…esCore.RuleConfig`

This is what was happening implicitly already:
https://github.com/JuliaDiff/ChainRulesCore.jl/blob/9627bd6a949ca88ba867bf4fa177a45bf780a248/src/rules.jl#L137-L138

```
unning tests:
1 ambiguities found. To get a list, set `broken = false`.
Ambiguity #1
frule(::ChainRulesCore.RuleConfig, args...) in ChainRulesCore at /home/runner/.julia/packages/ChainRulesCore/I1EbV/src/rules.jl:64
frule(::Any, ::typeof(Distributions.logpdf), d::Distributions.Uniform, x::Real) in Distributions.DistributionsChainRulesCoreExt at /home/runner/work/Distributions.jl/Distributions.jl/ext/DistributionsChainRulesCoreExt/univariate/continuous/uniform.jl:1
Possible fix, define
  frule(::ChainRulesCore.RuleConfig, ::typeof(Distributions.logpdf), ::Distributions.Uniform, ::Real)
Aqua: Test Failed at /home/runner/.julia/packages/Aqua/tHrmY/src/ambiguities.jl:78
  Expression: iszero(num_ambiguities)
Stacktrace:
 [1] _test_ambiguities(packages::Vector{Base.PkgId}; broken::Bool, kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Aqua ~/.julia/packages/Aqua/tHrmY/src/ambiguities.jl:78
 [2] _test_ambiguities
   @ ~/.julia/packages/Aqua/tHrmY/src/ambiguities.jl:69 [inlined]
 [3] test_ambiguities(packages::Module; kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Aqua ~/.julia/packages/Aqua/tHrmY/src/ambiguities.jl:28
 [4] test_ambiguities(packages::Module)
   @ Aqua ~/.julia/packages/Aqua/tHrmY/src/ambiguities.jl:28
 [5] macro expansion
   @ ~/work/Distributions.jl/Distributions.jl/test/aqua.jl:19 [inlined]
 [6] macro expansion
   @ /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Test/src/Test.jl:1151 [inlined]
 [7] top-level scope
   @ ~/work/Distributions.jl/Distributions.jl/test/aqua.jl:9
```
  • Loading branch information
LebedevRI committed Sep 13, 2024
1 parent 133a93e commit 519493b
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ext/DistributionsChainRulesCoreExt/eachvariate.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function ChainRulesCore.rrule(::Type{Distributions.EachVariate{V}}, x::AbstractArray{<:Real}) where {V}
function ChainRulesCore.rrule(::ChainRulesCore.RuleConfig, ::Type{Distributions.EachVariate{V}}, x::AbstractArray{<:Real}) where {V}
y = Distributions.EachVariate{V}(x)
size_x = size(x)
function EachVariate_pullback(Δ)
Expand Down
8 changes: 4 additions & 4 deletions ext/DistributionsChainRulesCoreExt/multivariate/dirichlet.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function ChainRulesCore.frule((_, Δalpha)::Tuple{Any,Any}, ::Type{DT}, alpha::AbstractVector{T}; check_args::Bool = true) where {T <: Real, DT <: Union{Dirichlet{T}, Dirichlet}}
function ChainRulesCore.frule(::ChainRulesCore.RuleConfig, (_, Δalpha)::Tuple{Any,Any}, ::Type{DT}, alpha::AbstractVector{T}; check_args::Bool = true) where {T <: Real, DT <: Union{Dirichlet{T}, Dirichlet}}
d = DT(alpha; check_args=check_args)
∂alpha0 = sum(Δalpha)
digamma_alpha0 = SpecialFunctions.digamma(d.alpha0)
Expand All @@ -9,7 +9,7 @@ function ChainRulesCore.frule((_, Δalpha)::Tuple{Any,Any}, ::Type{DT}, alpha::A
return d, Δd
end

function ChainRulesCore.rrule(::Type{DT}, alpha::AbstractVector{T}; check_args::Bool = true) where {T <: Real, DT <: Union{Dirichlet{T}, Dirichlet}}
function ChainRulesCore.rrule(::ChainRulesCore.RuleConfig, ::Type{DT}, alpha::AbstractVector{T}; check_args::Bool = true) where {T <: Real, DT <: Union{Dirichlet{T}, Dirichlet}}
d = DT(alpha; check_args=check_args)
digamma_alpha0 = SpecialFunctions.digamma(d.alpha0)
function Dirichlet_pullback(_Δd)
Expand All @@ -20,7 +20,7 @@ function ChainRulesCore.rrule(::Type{DT}, alpha::AbstractVector{T}; check_args::
return d, Dirichlet_pullback
end

function ChainRulesCore.frule((_, Δd, Δx)::Tuple{Any,Any,Any}, ::typeof(Distributions._logpdf), d::Dirichlet, x::AbstractVector{<:Real})
function ChainRulesCore.frule(::ChainRulesCore.RuleConfig, (_, Δd, Δx)::Tuple{Any,Any,Any}, ::typeof(Distributions._logpdf), d::Dirichlet, x::AbstractVector{<:Real})
Ω = Distributions._logpdf(d, x)
∂alpha = sum(Broadcast.instantiate(Broadcast.broadcasted(Δd.alpha, Δx, d.alpha, x) do Δalphai, Δxi, alphai, xi
StatsFuns.xlogy(Δalphai, xi) + (alphai - 1) * Δxi / xi
Expand All @@ -33,7 +33,7 @@ function ChainRulesCore.frule((_, Δd, Δx)::Tuple{Any,Any,Any}, ::typeof(Distri
return Ω, ΔΩ
end

function ChainRulesCore.rrule(::typeof(Distributions._logpdf), d::T, x::AbstractVector{<:Real}) where {T<:Dirichlet}
function ChainRulesCore.rrule(::ChainRulesCore.RuleConfig, ::typeof(Distributions._logpdf), d::T, x::AbstractVector{<:Real}) where {T<:Dirichlet}
Ω = Distributions._logpdf(d, x)
isfinite_Ω = isfinite(Ω)
alpha = d.alpha
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function ChainRulesCore.frule((_, Δd, _), ::typeof(logpdf), d::Uniform, x::Real)
function ChainRulesCore.frule(::ChainRulesCore.RuleConfig, (_, Δd, _), ::typeof(logpdf), d::Uniform, x::Real)
# Compute log probability
a, b = params(d)
insupport = a <= x <= b
Expand All @@ -12,7 +12,7 @@ function ChainRulesCore.frule((_, Δd, _), ::typeof(logpdf), d::Uniform, x::Real
return Ω, ΔΩ
end

function ChainRulesCore.rrule(::typeof(logpdf), d::Uniform, x::Real)
function ChainRulesCore.rrule(::ChainRulesCore.RuleConfig, ::typeof(logpdf), d::Uniform, x::Real)
# Compute log probability
a, b = params(d)
insupport = a <= x <= b
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function (f::LogPDFNegativeBinomialPullback{D})(Δ) where {D}
return ChainRulesCore.NoTangent(), Δd, ChainRulesCore.NoTangent()
end

function ChainRulesCore.rrule(::typeof(logpdf), d::NegativeBinomial, k::Real)
function ChainRulesCore.rrule(::ChainRulesCore.RuleConfig, ::typeof(logpdf), d::NegativeBinomial, k::Real)
# Compute log probability (as in the definition of `logpdf(d, k)` above)
r, p = params(d)
z = StatsFuns.xlogy(r, p) + StatsFuns.xlog1py(k, -p)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ for f in (:poissonbinomial_pdf, :poissonbinomial_pdf_fft)
pullback = Symbol(f, :_pullback)
@eval begin
function ChainRulesCore.frule(
(_, Δp)::Tuple{<:Any,<:AbstractVector{<:Real}}, ::typeof(Distributions.$f), p::AbstractVector{<:Real}
::ChainRulesCore.RuleConfig, (_, Δp)::Tuple{<:Any,<:AbstractVector{<:Real}}, ::typeof(Distributions.$f), p::AbstractVector{<:Real}
)
y = Distributions.$f(p)
A = Distributions.poissonbinomial_pdf_partialderivatives(p)
return y, A' * Δp
end
function ChainRulesCore.rrule(::typeof(Distributions.$f), p::AbstractVector{<:Real})
function ChainRulesCore.rrule(::ChainRulesCore.RuleConfig, ::typeof(Distributions.$f), p::AbstractVector{<:Real})
y = Distributions.$f(p)
A = Distributions.poissonbinomial_pdf_partialderivatives(p)
function $pullback(Δy)
Expand Down

0 comments on commit 519493b

Please sign in to comment.