Support the return_sign argument in logsumexp derivatives - #809
Open
ump45nose wants to merge 1 commit into
Open
Conversation
``scipy.special.logsumexp`` returns a ``(log(abs(sum(b * exp(x)))), sign)``
pair when called with ``return_sign=True``, but the derivatives of
``logsumexp`` did not accept that argument, so both modes failed with a
TypeError instead of differentiating:
grad(lambda x: asp.logsumexp(x, b=b, return_sign=True)[0])(x)
# TypeError: make_grad_logsumexp() got an unexpected keyword argument 'return_sign'
Accept ``return_sign`` in the VJP and the JVP. The sign is piecewise
constant, so only the log-sum-exp component of the pair has a non-zero
derivative, but it does change that derivative: since the returned value is
the log of the *absolute value* of the sum, its derivative is
``sign * b * exp(x - ans)``, and the sign has to be taken into account to
get the direction right when the sum is negative.
Closes HIPS#243
agriyakhetarpal
approved these changes
Sep 10, 2026
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
scipy.special.logsumexpreturns a(log(abs(sum(b * exp(x)))), sign)pair when it iscalled with
return_sign=True, but the derivatives oflogsumexpdid not accept thatargument, so both modes raised a
TypeErrorinstead of differentiating:This implements the request in #243 (the workaround there was to reimplement
logsumexpin Python and differentiate that).
Accepting the argument is not enough on its own: the returned value is the log of the
absolute value of the sum, so its derivative is
i.e. the sign has to appear in the gradient, otherwise the gradient points the wrong way
whenever the sum is negative. The sign itself is piecewise constant, so only the
log-sum-exp component of the pair has a non-zero derivative (and the tangent of the sign
is zero in forward mode).
Both the VJP (
make_grad_logsumexp) and the JVP (fwd_grad_logsumexp) are updated; theexisting code path, where
return_signis not used, is untouched.Tests
test_logsumexp_negative_sumpins the sign handling down with a case whose sum isexactly negative (
b = [1, -3],x = [0, 0], sosign = -1andans = log(2)), andasserts the gradient equals
b * exp(x) / sum(b * exp(x))as well as checking itnumerically.
test_logsumexp_return_signruns the same shapes/axes/keepdimscombinations as theexisting logsumexp tests with
return_sign=Trueand mixed-signb, in both modes.Both fail on
mainwith theTypeErrorabove and pass with this change. The suite isotherwise unchanged:
ruffandruff-formatare clean.Closes #243