Repository navigation
Conversation
StockerMC
force-pushed
the
fix-cross-entropy-vjp-strides
branch
from
October 15, 2026 05:09
079beac to
8514faa
Compare
This was referenced Oct 15, 2026
utsav1033
reviewed
Oct 19, 2026
utsav1033
left a comment
There was a problem hiding this comment.
the script showed that it computed gradient thrice and printed its size, correct being 34.352, which i got right everytime. and that wasnt the case on main as it was 0.0 at first and 6218.577 later.
the new test proved through a test added by this pr, the bug test failed on main and passed on this pr.
nothing else broke when i ran the related test files(test_fast, test_losses, test_autograd, test_vmap)
my setup is apple m4 on macOS 27.0.1
StockerMC
force-pushed
the
fix-cross-entropy-vjp-strides
branch
from
October 19, 2026 17:35
8514faa to
46087d0
Compare
This branch has not been deployed
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.
fixes #4686
On Metal, the gradient of
mx.fast.cross_entropycan be wrong for a non-row-contiguous view likelogits[:, :-1]once the input is big enough that the command buffer gets committed right after the VJP. It's then possible for the next buffer to read the gradient before it's written, so calls sometimes fail.As was mentioned in the issue,
ensure_row_contiguousregisters the input copy as a temporary, and the VJP then donates that copy to the output.end_encodingdrops temporaries from the encoder's outputs, so the gradient buffer isn't tracked by the fence and the next command buffer doesn't wait for it to be written.This fix no longer registers the input copy as a temporary, since the output shares its buffer, matching the CUDA VJP and
LayerNormVJP.I added a regression test with an input big enough so that the command buffer always splits after the VJP, and I built and tested on an M3 Pro.