Skip to content

Make index autodiff errors explicitly recommend stop_gradient#3820

Open
dogukanveziroglu wants to merge 6 commits into
ml-explore:mainfrom
dogukanveziroglu:fix-index-vjp-stop-gradient-errors
Open

Make index autodiff errors explicitly recommend stop_gradient#3820
dogukanveziroglu wants to merge 6 commits into
ml-explore:mainfrom
dogukanveziroglu:fix-index-vjp-stop-gradient-errors

Conversation

@dogukanveziroglu

Copy link
Copy Markdown
Contributor

Based on the discussion in #3612 and the closing note in #1439, this makes gather match the other index-based ops, if gradients reach index arguments, users now get an explicit error asking them to use mx.stop_gradient instead of getting a silent zero VJP.

I also updated the related error messages and added small regression tests for both the error case and the intended workaround. (Tests might be unnecessary so I can trim them if you want)

This actually happens on mask VJP error messages too. I didn't add them on this PR but I can add a similar stop_gradient on there too.

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks! I think it makes sense also adding the error message to masked ops.

Comment thread mlx/primitives.cpp Outdated

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@dogukanveziroglu dogukanveziroglu force-pushed the fix-index-vjp-stop-gradient-errors branch from 6868aa9 to 4715c89 Compare July 9, 2026 09:18
@dogukanveziroglu

Copy link
Copy Markdown
Contributor Author

Force pushed for fixing my author signature. Plus fixing the CI issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants