Fix mx.prod vjp for complex types#3433
Merged
angeloskath merged 1 commit intoml-explore:mainfrom Apr 22, 2026
Merged
Conversation
angeloskath
approved these changes
Apr 21, 2026
Member
angeloskath
left a comment
There was a problem hiding this comment.
Thanks that's a great catch!
1 task
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.
Fix bug in VJP for Product Reduction
I was running some experiments and noticed some strange gradient behavior. After some digging, it turned out the issue was with how
mx.prodhandles complex numbers. My understanding is that MLX uses the conjugate-transpose formulation as seen inMatmul::vjphere:And also in
Multiply::vjphere:This PR fixes
Reduce::vjpto use the same convention by conjugatingexclusive_prodbefore multiplying it with the cotangent.I added a test which checks that the updated vjp agrees both with a hand-computed version as well as with the vjp implementation of
multiply, since they should always agree. I also tested these changes in a toy example and in a tiny experiment and the gradient divergence issues I was seeing before are gone now.To my knowledge, no documentation needs to be updated.
One thing to note is that there may be other operations in MLX which also fail to follow this convention. I have checked
fft,ifft, andmultiplysince those are the other ops I was using at the time, and those appear to be fine.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes