-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CompatHelper: bump compat for "Distributions" to "0.24" (#119)
* CompatHelper: bump compat for "Distributions" to "0.24" * Do not use Requires * Move around imports of AD backends * AD packages are always required * Check if Distributions >= 0.24 * Remove ForwardDiff definition if not needed * Remove some matrix-variate definitions of `loglikelihood` * Fix typo * Fix another typo * Mark specific test of Skellam as broken * Revert "Mark specific test of Skellam as broken" This reverts commit c63b814. * Try to fix PoissonBinomial and Skellam test errors * Bump version * Add comment Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: David Widmann <[email protected]> Co-authored-by: David Widmann <[email protected]>
- Loading branch information
1 parent
2d566ae
commit dcf9c3e
Showing
9 changed files
with
106 additions
and
116 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
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
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 |
---|---|---|
@@ -1,68 +1,20 @@ | ||
# Zygote loads ForwardDiff, so this adjoint will autmatically be loaded together | ||
# with `using Zygote`. | ||
|
||
# FIXME: This is inefficient, replace with the commented code below once Zygote supports it. | ||
# TODO: add adjoints without ForwardDiff | ||
@adjoint function poissonbinomial_pdf_fft(x::AbstractArray{T}) where T<:Real | ||
fft = poissonbinomial_pdf_fft(x) | ||
return fft, Δ -> begin | ||
((ForwardDiff.jacobian(x -> poissonbinomial_pdf_fft(x), x)::Matrix{T})' * Δ,) | ||
((ForwardDiff.jacobian(poissonbinomial_pdf_fft, x)::Matrix{T})' * Δ,) | ||
end | ||
end | ||
|
||
# The code below doesn't work because of bugs in Zygote. The above is inefficient. | ||
#= | ||
ZygoteRules.@adjoint function poissonbinomial_pdf_fft(x::AbstractArray{<:Real}) | ||
return ZygoteRules.pullback(poissonbinomial_pdf_fft_zygote, x) | ||
end | ||
function poissonbinomial_pdf_fft_zygote(p::AbstractArray{T}) where {T <: Real} | ||
n = length(p) | ||
ω = 2 * one(T) / (n + 1) | ||
lmax = ceil(Int, n/2) | ||
x1 = [one(T)/(n + 1)] | ||
x_lmaxp1 = map(1:lmax) do l | ||
logz = zero(T) | ||
argz = zero(T) | ||
for j=1:n | ||
zjl = 1 - p[j] + p[j] * cospi(ω*l) + im * p[j] * sinpi(ω * l) | ||
logz += log(abs(zjl)) | ||
argz += atan(imag(zjl), real(zjl)) | ||
end | ||
dl = exp(logz) | ||
return dl * cos(argz) / (n + 1) + dl * sin(argz) * im / (n + 1) | ||
end | ||
x_lmaxp2_end = [conj(x[l + 1]) for l in lmax:-1:1 if n + 1 - l > l] | ||
x = vcat(x1; x_lmaxp1, x_lmaxp2_end) | ||
y = [sum(x[j] * cis(-π * float(T)(2 * mod(j * k, n)) / n) for j in 1:n) for k in 1:n] | ||
return max.(0, real.(y)) | ||
end | ||
function poissonbinomial_pdf_fft_zygote2(p::AbstractArray{T}) where {T <: Real} | ||
n = length(p) | ||
ω = 2 * one(T) / (n + 1) | ||
x = Vector{Complex{T}}(undef, n+1) | ||
lmax = ceil(Int, n/2) | ||
x[1] = one(T)/(n + 1) | ||
for l=1:lmax | ||
logz = zero(T) | ||
argz = zero(T) | ||
for j=1:n | ||
zjl = 1 - p[j] + p[j] * cospi(ω*l) + im * p[j] * sinpi(ω * l) | ||
logz += log(abs(zjl)) | ||
argz += atan(imag(zjl), real(zjl)) | ||
if isdefined(Distributions, :poissonbinomial_pdf) | ||
@adjoint function Distributions.poissonbinomial_pdf(x::AbstractArray{T}) where T<:Real | ||
value = Distributions.poissonbinomial_pdf(x) | ||
function poissonbinomial_pdf_pullback(Δ) | ||
return ((ForwardDiff.jacobian(Distributions.poissonbinomial_pdf, x)::Matrix{T})' * Δ,) | ||
end | ||
dl = exp(logz) | ||
x[l + 1] = dl * cos(argz) / (n + 1) + dl * sin(argz) * im / (n + 1) | ||
if n + 1 - l > l | ||
x[n + 1 - l + 1] = conj(x[l + 1]) | ||
end | ||
end | ||
max.(0, real.(_dft_zygote(copy(x)))) | ||
end | ||
function _dft_zygote(x::Vector{T}) where T | ||
n = length(x) | ||
y = Zygote.Buffer(zeros(complex(float(T)), n)) | ||
@inbounds for j = 0:n-1, k = 0:n-1 | ||
y[k+1] += x[j+1] * cis(-π * float(T)(2 * mod(j * k, n)) / n) | ||
return value, poissonbinomial_pdf_pullback | ||
end | ||
return copy(y) | ||
end | ||
=# |
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
dcf9c3e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
dcf9c3e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/24930
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: