Skip to content

Conversation

ysiraichi
Copy link
Collaborator

This PR refactors the trace operation implementation by improving its error message, and returning a status type value.

Key Changes:

  • Make tensor_methods::trace return StatusOr<XLATensorPtr>
  • Improve error messages and error handling
    • Renamed CheckMMInputIsMatrix to CheckInputIsMatrix
    • Added a new parameter for specifying the operation name, so as to build a better error message

Example

a = torch.rand(2, 2, 2, device="xla")
torch.trace(a)

Before:

Traceback (most recent call last):
  File "examples/trace.py", line 5, in <module>
    torch.trace(a)
RuntimeError: Check failed: (*input_shape_ref).dimensions_size() == 2 (3 vs. 2)invalid argument for trace: expected a matrix (at torch_xla/csrc/tensor_methods.cpp:3579)

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

After:

Traceback (most recent call last):
  File "examples/trace.py", line 5, in <module>
    torch.trace(a)
RuntimeError: trace(): expected the input tensor f32[2,2,2] to be a matrix (i.e. a 2D tensor).

Status Propagation Trace:
    From: CheckInputIsMatrix at torch_xla/csrc/tensor_methods.cpp:491 (error: trace(): expected the input tensor f32[2,2,2] to be a matrix (i.e. a 2D tensor).)
    From: trace at torch_xla/csrc/tensor_methods.cpp:3596
    From: trace at torch_xla/csrc/aten_xla_type.cpp:3874

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

@ysiraichi ysiraichi force-pushed the ysiraichi/better-error-trace branch 2 times, most recently from 3496526 to 6c3e64f Compare September 12, 2025 19:19
@ysiraichi

This comment was marked as outdated.

@ysiraichi ysiraichi force-pushed the ysiraichi/better-error-trace branch from 6c3e64f to e013a25 Compare September 17, 2025 17:09
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