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

xlogy grad wrong at 0 #1601

Closed
mhauru opened this issue Jul 2, 2024 · 1 comment
Closed

xlogy grad wrong at 0 #1601

mhauru opened this issue Jul 2, 2024 · 1 comment

Comments

@mhauru
Copy link
Contributor

mhauru commented Jul 2, 2024

MWE:

module MWE

using LogExpFunctions: xlogy
using FiniteDifferences
using Enzyme

# function xlogy(x::Number, y::Number)
#     result = x * log(y)
#     return iszero(x) && !isnan(y) ? zero(result) : result
# end

f(x) = xlogy(x[1], 23.0)

x = [0.0]

finitediff = FiniteDifferences.grad(central_fdm(5, 1), f, x)[1]
grad_forward = Enzyme.gradient(Enzyme.Forward, f, x)
grad_reverse = Enzyme.gradient(Enzyme.Reverse, f, x)
@show finitediff
@show grad_forward
@show grad_reverse

end

Commented out is the actual body of xlogy.

Output:

finitediff = [3.1354942159291497]
grad_forward = (0.0,)
grad_reverse = [0.0]

The iszero(x) throws things off. E.g. ForwardDiff.jl and ReverseDiff.jl deal with this by depending on DiffRules.jl, which defines a rule here: https://github.com/JuliaDiff/DiffRules.jl/blob/8842177391b07dcd8234ac7612b9ca8ca72d28e0/src/rules.jl#L305

Could Enzyme do the same?

@wsmoses
Copy link
Member

wsmoses commented Jul 11, 2024

Completed here: #1615

@wsmoses wsmoses closed this as completed Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants