Repository navigation
Fix mx.sort vjp to transpose the permutation - #3700
Merged
angeloskath merged 1 commit intoJun 17, 2026
Merged
Conversation
Sort::vjp delegated to Sort::jvp, but sorting applies a permutation to the input and a permutation matrix is not symmetric. The jvp gathers the tangents along argsort (P v); the vjp must scatter the cotangents back to the original positions (P^T w) instead. Delegating to the jvp routed the cotangents the wrong way, producing incorrect gradients for any input whose sort permutation is not an involution. Compute the vjp with put_along_axis on the argsort indices, mirroring the existing Partition::vjp. Add an autograd test covering the value and the jvp/vjp adjoint identity across axes.
angeloskath
approved these changes
Jun 16, 2026
angeloskath
left a comment
Member
There was a problem hiding this comment.
Thanks for catching this!
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
Closes #3699.
Sort::vjpdelegated toSort::jvp, but sorting applies a permutationPto the input and a permutation matrix is not symmetric. The jvp gathers the tangents along the argsort indices (P v); the vjp must scatter the cotangents back to the original positions (P^T w). Delegating to the jvp routed the cotangents the wrong way, giving incorrect gradients for any input whose sort permutation is not an involution. This affects CPU and GPU (the rule is inprimitives.cpp), and there were no autograd tests forsort, so it went unnoticed.The fix computes the vjp with
put_along_axison the argsort indices, mirroring the existing, correctPartition::vjp.Adds
test_sort_gradcovering the exact gradient and the jvp/vjp adjoint identity 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