Skip to content

Conversation

ysiraichi
Copy link
Collaborator

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

Key Changes:

  • Make tensor_methods::random_ return Status
  • Replace CheckRangeValues by CheckValueWithinTypeRange, and make it return Status
  • Refactor XLANativeFunctions::random_ overloads to handle the status values
  • Improve error messages

Example:

tensor = torch.empty(10, device=torch_xla.device())
from_ = 3
to_ = 1
tensor.random_(from_, to_)

Before:

Traceback (most recent call last):
  File "example.py", line 7, in <module>
    tensor.random_(from_, to_)
RuntimeError: Check failed: from <= to_val (3 vs. 1) (at torch_xla/csrc/aten_xla_type.cpp:3031)

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 7, in <module>
    tensor.random_(from_, to_)
RuntimeError: random_(): expected `from` (3) to be smaller than `to` (1).

Status Propagation Trace:
    From: random_ at torch_xla/csrc/tensor_methods.cpp:2870 (error: random_(): expected `from` (3) to be smaller than `to` (1).)

Exception raised from OkOrThrow 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-random_ branch 2 times, most recently from fd1e6fc to 340f841 Compare August 23, 2025 15:57
@ysiraichi ysiraichi force-pushed the ysiraichi/better-error-random_ branch from 340f841 to efbabe0 Compare August 23, 2025 17:28
@ysiraichi ysiraichi merged commit 49ac22a into master Aug 25, 2025
30 of 31 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