Implement jvp for cumulative logsumexp#3711
Open
obchain wants to merge 1 commit into
Open
Conversation
Scan::jvp only handled the Sum reduction and threw for everything else,
so forward-mode differentiation through mx.logcumsumexp raised. The jvp
is the running softmax-weighted sum of the tangents,
d/dt logcumsumexp(x)_k = sum_{i<=k} softmax(x)_i * t_i,
computed in log space by splitting the tangent into its positive and
negative parts, mirroring the existing vjp. Exclusive scans leave the
first element with no inputs (output -inf, locally constant), so its
tangent is set to zero, which also avoids an inf - inf there.
aeiwz
reviewed
Jun 18, 2026
aeiwz
left a comment
There was a problem hiding this comment.
Finding
[P2] Preserve complex tangent values — mlx/primitives.cpp (
Lines 4308 to 4316 in cba7502
The positive/negative split uses abs(t) and comparisons, which discards the phase of complex tangents. Since logcumsumexp supports complex arrays, JVPs such as tangent [1+1j, 2-1j]
return real magnitudes instead of the expected complex derivative. The implementation should either support complex tangents directly or explicitly reject them. Add a complex JVP test.
Numerically verified expected derivative:
[1+1j, 1.73106-0.462117j]
The current expression produces approximately:
[1.41421+0j, 2.01504+0j]
No other actionable findings.
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.
Proposed changes
Fixes #3710.
Scan::jvponly implemented theSumreduction and threw for everything else, so forward-mode differentiation throughmx.logcumsumexpraisedJVP is not implemented for cumulative prod/min/max. Itsvjpwas already implemented, so only forward mode was affected.The jvp of
logcumsumexpis the running softmax-weighted sum of the tangents:This is computed in log space for numerical stability by splitting the tangent into its positive and negative parts, mirroring the structure of the existing
LogAddExpvjp. Exclusive scans leave the first element with no inputs (output-inf, locally constant), so its tangent is set to zero — this also avoids aninf - infin the expression.cumprod/cummax/cumminjvps are unchanged (still not implemented) and the error message stays accurate.Before:
Added a test in
test_autograd.pythat checks the jvp against an explicit softmax-weighted reference and verifies the jvp/vjp adjoint identity for every combination of thereverse/inclusiveflags across axes.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes