Fix Nx.gather error message for scalar indices#1713
Merged
polvalente merged 1 commit intoelixir-nx:mainfrom Mar 20, 2026
Merged
Fix Nx.gather error message for scalar indices#1713polvalente merged 1 commit intoelixir-nx:mainfrom
Nx.gather error message for scalar indices#1713polvalente merged 1 commit intoelixir-nx:mainfrom
Conversation
Nx.gather(tensor, scalar) gave an unhelpful Erlang error because
indexed_axes tried to access elem({}, -1) before the shape validation
in Nx.Shape.gather could fire. Moved the scalar check earlier.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
b60d75a to
1d319b4
Compare
polvalente
approved these changes
Mar 20, 2026
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.
Nx.gather(tensor, scalar)gives an unhelpful Erlang error because indexed_axes tries to accesselem({}, -1)before the shape validation in Nx.Shape.gather can fire. Moved the scalar check earlier to return a more helpful Nx error.So if you try:
You'll get a 1st argument out of range error (Argument Error)
There is validation in
Nx.Shape.gather(line 1641-1643 innx/lib/nx/shape.ex). It just isn't reached becauseindexed_axes(tensor, indices, opts)runs first, which doeselem(indices.shape, tuple_size(indices.shape) -1) -> ends up withelem({}, -1). So Erlang crashes before there's a graceful error.The fix is to move the same check the gather would do, just earlier, before indexed_axes.
Running
gather_scalar_error_test.exson mainoriginally part of the closed fuzz test edge case PR #1707