Skip to content

Conversation

ysiraichi
Copy link
Collaborator

This PR refactors the tensor_methods::gather implementation by improving its error message, and returning a status type value.

Key Changes:

  • Make tensor_methods::gather return StatusOr<absl_nonnull XLATensorPtr>
  • Improve error message on incompatible tensor shapes

Example:

input = torch.arange(16, device=torch_xla.device()).view(2, 2, 2, 2)
index = torch.randint(0, 2, (5, 2, 5, 2), device=torch_xla.device())
dim = 1
torch.gather(input, dim, index)

Before:

Traceback (most recent call last):
  File "example.py", line 28, in <module>
    torch.gather(input, dim, index)
RuntimeError: Check failed: index->size(dim) <= input->size(dim) (5 vs. 2) (at torch_xla/csrc/tensor_methods.cpp:1826)

Exception raised from operator& at torch_xla/csrc/runtime/tf_logging.cpp:26 (most recent call first):

After:

Traceback (most recent call last):
  File "example.py", line 28, in <module>
    torch.gather(input, dim, index)
RuntimeError: gather(): expected sizes of index [5, 2, 5, 2] to be smaller or equal those of input [2, 2, 2, 2] on all dimensions, except on dimension 1. However, that's not true on dimensions [0, 2].

Status Propagation Trace:
    From: CheckDimensionsAreCompatible at torch_xla/csrc/tensor_methods.cpp:440 (error: gather(): expected sizes of index [5, 2, 5, 2] to be smaller or equal those of input [2, 2, 2, 2] on all dimensions, except on dimension 1. However, that's not true on dimensions [0, 2].)
    From: gather at torch_xla/csrc/tensor_methods.cpp:1865

Exception raised from MaybeThrow at torch_xla/csrc/status.cpp:121 (most recent call first):

Copy link
Collaborator

@zhanyong-wan zhanyong-wan left a comment

Choose a reason for hiding this comment

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

Thanks!

@ysiraichi ysiraichi force-pushed the ysiraichi/better-error-gather branch from 3538c05 to 59c82f6 Compare August 23, 2025 13:47
@ysiraichi ysiraichi force-pushed the ysiraichi/better-error-gather branch from 59c82f6 to 6fd4738 Compare August 23, 2025 15:10
@ysiraichi ysiraichi merged commit 8243a25 into master Aug 23, 2025
41 of 42 checks passed
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