You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
logsumexp gives the wrong derivative at exact ties under Enzyme and Mooncake #128
Enzyme and Mooncake give the wrong derivative for logsumexp when the two terms being added are equal:
using Enzyme, ForwardDiff, LogExpFunctions
# Both terms equal 1 at t = 1, with derivatives 1 and 2, so the answer is 1.5f_tuple(t) =logsumexp((t, 2t -1))
f_vector(t) =logsumexp([t, 2t -1])
f_generator(t) =logsumexp(x for x in (t, 2t -1))
f_plain(t) =log(exp(t) +exp(2t -1))
for (name, f) in (("tuple", f_tuple), ("vector", f_vector), ("generator", f_generator), ("plain", f_plain))
println(rpad(name, 10),
" Enzyme.Forward=", only(autodiff(Forward, f, Duplicated(1.0, 1.0))),
" Enzyme.Reverse=", only(only(autodiff(Reverse, f, Active, Active(1.0)))),
" ForwardDiff=", ForwardDiff.derivative(f, 1.0))
end
The value is unchanged, but the derivative of the difference is lost. ForwardDiff never takes this branch: since #60 (the fix for #59) it runs only when neither > nor < holds, and Dual comparison breaks ties using the partials.
The constant is there so that x1 = x2 = ±Inf gives 0 rather than Inf - Inf = NaN. Replacing it only when the difference is NaN handles both cases:
d = x1 - x2
x2, ifelse(isnan(d), zero(d), d)
With the same change in all three methods, every case above gives 1.5 in both Enzyme modes. logsumexp((Inf, Inf)), logsumexp((-Inf, -Inf)) and logsumexp([Inf, Inf, 1.0]) still return Inf, -Inf and Inf.
Thanks for investigating this. I agree with the approach you suggested. Would you have time to make a PR, with tests for Enzyme and/or Mooncake for this corner case?
Activity
tpapp commentedon Sep 25, 2026
Thanks for investigating this. I agree with the approach you suggested. Would you have time to make a PR, with tests for Enzyme and/or Mooncake for this corner case?
Keep derivatives and imaginary parts at ties in logsumexp (JuliaStats…
compat: admit LogExpFunctions 1.0.2+ besides 0.3 (0.2.13)