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

Add iszero(x) branches to xlogy and xlog1py #54

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ julia = "1"

[extras]
ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a"
FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000"
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["ChainRulesTestUtils", "OffsetArrays", "Random", "Test"]
test = ["ChainRulesTestUtils", "FiniteDifferences", "OffsetArrays", "Random", "Test"]
25 changes: 19 additions & 6 deletions src/chainrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ end
function _Ω_∂_xlogy(x::Real, y::Real)
logy = log(y)
z = x * logy
Ω = iszero(x) && !isnan(y) ? zero(z) : z
∂x = logy
∂y = x / y
w = x / y
if iszero(x) && !isnan(y)
Ω = zero(z)
∂y = zero(w)
else
Ω = z
∂y = iszero(y) ? oftype(w, NaN) : w
end
∂x = iszero(y) ? oftype(logy, NaN) : logy
Comment on lines +28 to +30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if these should be added, even if it makes the rule match finite differencing. NaN is usually not very useful as it infects everything. So eg the forward rule will always yield NaN for any of these conditions. See eg https://juliadiff.org/ChainRulesCore.jl/stable/maths/nondiff_points.html.

Copy link
Contributor Author

@simsurace simsurace Jul 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit mysterious to me. On the (seemingly equivalent) branches in the rrule for logpdf of NegativeBinomial we wanted NaN. Was this something specific to Distributions.jl?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to change this but I'm unsure about the discrepancy to Distributions.jl

return Ω, ∂x, ∂y
end
function ChainRulesCore.frule((_, Δx, Δy), ::typeof(xlogy), x::Real, y::Real)
Expand All @@ -38,9 +44,16 @@ end
function _Ω_∂_xlog1py(x::Real, y::Real)
log1py = log1p(y)
z = x * log1py
Ω = iszero(x) && !isnan(y) ? zero(z) : z
∂x = log1py
∂y = x / (1 + y)
yp1 = 1 + y
w = x / yp1
if iszero(x) && !isnan(y)
Ω = zero(z)
∂y = zero(z)
else
Ω = z
∂y = iszero(yp1) ? oftype(w, NaN) : w
end
∂x = iszero(yp1) ? oftype(log1py, NaN) : log1py
return Ω, ∂x, ∂y
end
function ChainRulesCore.frule((_, Δx, Δy), ::typeof(xlog1py), x::Real, y::Real)
Expand Down
6 changes: 6 additions & 0 deletions test/chainrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
test_frule(xlogy, x, y)
test_rrule(xlogy, x, y)

test_frule(xlogy, x, 0.0; fdm = forward_fdm(5, 1), nans = true)
test_rrule(xlogy, x, 0.0; fdm = forward_fdm(5, 1), nans = true)

for z in (-y, y)
test_frule(xlog1py, x, z)
test_rrule(xlog1py, x, z)
end

test_frule(xlog1py, x, -1.0; fdm = forward_fdm(5, 1), nans = true)
test_rrule(xlog1py, x, -1.0; fdm = forward_fdm(5, 1), nans = true)
end

@testset "xexpx" begin
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using LogExpFunctions
using ChainRulesTestUtils
using ChainRulesCore
using ChangesOfVariables
using FiniteDifferences
using InverseFunctions
using OffsetArrays

Expand Down