Skip to content

Conversation

ysiraichi
Copy link
Collaborator

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

Key Changes:

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

Example:

dims = [0, 0, 0, 1, 2, 3, -1]
a = torch.rand(2, 2, 2, 2, device="xla")
print(torch.flip(a, dims=dims))

Before:

Traceback (most recent call last):
  File "scratch.py", line 8, in <module>
    print(torch.flip(a, dims=dims))
          ^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Check failed: unique_dims.size() == dimensions.size() (4 vs. 7) (at torch_xla/csrc/tensor_methods.cpp:1675)

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

After:

Traceback (most recent call last):
  File "scratch.py", line 8, in <module>
    print(torch.flip(a, dims=dims))
          ^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: flip(): expected each dimension to appear at most once. Found dimensions: 0 (3 times), 3 (2 times). Consider changing dims from [0, 0, 0, 1, 2, 3, -1] to [0, 1, 2, 3].

Status Propagation Trace:
    From: flip at torch_xla/csrc/tensor_methods.cpp:1704 (error: flip(): expected each dimension to appear at most once. Found dimensions: 0 (3 times), 3 (2 times). Consider changing dims from [0, 0, 0, 1, 2, 3, -1] to [0, 1, 2, 3].)

Exception raised from MaybeThrow at torch_xla/csrc/status.cpp:128 (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-flip branch from 3c61bee to a22002c Compare August 11, 2025 17:52
@ysiraichi ysiraichi force-pushed the ysiraichi/better-error-flip branch from a22002c to 3d78f32 Compare August 11, 2025 18:39
@ysiraichi ysiraichi merged commit d5b9a6d into master Aug 14, 2025
35 of 38 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