Implement jvp for cumulative logsumexp#3711
Conversation
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.
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.
cba7502 to
715e141
Compare
|
Good observation. The positive/negative split is taken directly from the existing I checked, and the current vjp is also not phase-correct for complex tangents (the holomorphic adjoint |
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