Skip to content

ErrorHandling: make GetComputationClient() return StatusOr<T> type. #9384

Merged
ysiraichi merged 15 commits intomasterfrom
ysiraichi/status-for-getcomputationclient
Jun 24, 2025
Merged

ErrorHandling: make GetComputationClient() return StatusOr<T> type. #9384
ysiraichi merged 15 commits intomasterfrom
ysiraichi/status-for-getcomputationclient

Conversation

@ysiraichi
Copy link
Copy Markdown
Collaborator

@ysiraichi ysiraichi commented Jun 18, 2025

Ref: #9339

This PR creates a new version of GetComputationClient() that returns a StatusOr<T> type for better error handling. Since this function is frequently used in the codebase, its old version remains, but annotated with ABSL_DEPRECATED() (following #9381 example) and renamed to GetComputationClientOrDie(). The new version is created with the old name. Summarizing:

  • Old GetComputationClient() is renamed to GetComputationClientOrDie()
  • New torch_xla::runtime::status::GetComputationClient() function that returns StatusOr<T> type
  • The old version remains, and calls the new function. It uses XLA_CHECK() for throwing an error on non-ok status, maintaining its old behavior
  • New ConsumeAndMaybeThrow() function for throwing errors if non-ok status reach the Python API boundary

Copy link
Copy Markdown
Collaborator

@ghpvnist ghpvnist left a comment

Choose a reason for hiding this comment

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

Thanks for the change, added a few comments.

Comment thread torch_xla/csrc/runtime/runtime.h Outdated
Comment thread torch_xla/csrc/runtime/runtime.cpp Outdated
Comment thread torch_xla/csrc/runtime/runtime.cpp Outdated
Comment thread torch_xla/csrc/init_python_bindings.cpp Outdated
@ysiraichi ysiraichi force-pushed the ysiraichi/status-for-getcomputationclient branch from 391d353 to 3344bb0 Compare June 18, 2025 19:25
@ysiraichi
Copy link
Copy Markdown
Collaborator Author

Incorporated the changes suggested by @zhanyong-wan comment.

Comment thread torch_xla/csrc/init_python_bindings.cpp
Comment thread torch_xla/csrc/runtime/runtime.h Outdated
Comment thread torch_xla/csrc/runtime/runtime.cpp Outdated
Comment thread torch_xla/csrc/runtime/runtime.cpp Outdated
Comment thread torch_xla/csrc/runtime/runtime.cpp Outdated
Copy link
Copy Markdown
Collaborator

@ghpvnist ghpvnist left a comment

Choose a reason for hiding this comment

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

Can you create a unit test for the new API? runtime_test.cpp in the same directory could work.

Comment thread torch_xla/csrc/runtime/runtime.cpp
Comment thread torch_xla/csrc/runtime/runtime.cpp Outdated
@ysiraichi
Copy link
Copy Markdown
Collaborator Author

@ghpvnist What kind of tests did you have in mind?
I think C++ tests usually go inside test/cpp directory.

@ghpvnist
Copy link
Copy Markdown
Collaborator

I think C++ tests usually go inside test/cpp directory.

This directory has precedent of tests within the same directory. I prefer to have tests close where the files are stored that way, future refactoring changes would be easier. Wdyt? @zhanyong-wan

What kind of tests did you have in mind?

I was thinking the typical unit tests for test coverage purposes. Something along the lines of:

  • Check that calls to GetComputationClient return a valid client.
  • GetComputationClientIfInitialized should return nullptr vs a valid client.
  • etc.

@ysiraichi ysiraichi force-pushed the ysiraichi/status-for-getcomputationclient branch from a351a69 to 7e0da02 Compare June 19, 2025 16:34
@zhanyong-wan
Copy link
Copy Markdown
Collaborator

I think C++ tests usually go inside test/cpp directory.

This directory has precedent of tests within the same directory. I prefer to have tests close where the files are stored that way, future refactoring changes would be easier. Wdyt? @zhanyong-wan

While I prefer to have tests in the same directory as the code too, pytorch uses the test/cpp/test_foo.cpp convention. I think we should align with that to ease upstreaming.

Comment thread torch_xla/csrc/runtime/runtime_test.cpp
Comment thread torch_xla/csrc/runtime/runtime_test.cpp Outdated
Comment thread torch_xla/csrc/runtime/runtime_test.cpp Outdated
Comment thread torch_xla/csrc/runtime/runtime_test.cpp Outdated
Comment thread torch_xla/csrc/runtime/runtime_test.cpp Outdated
Comment thread torch_xla/csrc/runtime/runtime.cpp Outdated
Comment thread torch_xla/csrc/runtime/runtime.h Outdated
Comment thread torch_xla/csrc/runtime/runtime.h Outdated
Comment thread torch_xla/csrc/runtime/runtime.h
Comment thread torch_xla/csrc/init_python_bindings.cpp
@ysiraichi
Copy link
Copy Markdown
Collaborator Author

@zhanyong-wan @ghpvnist Let me know if there's anything else you'd like to see in this PR.

@zhanyong-wan
Copy link
Copy Markdown
Collaborator

Hi @ysiraichi , I'll take a look soon. In the future, when the PR is ready for another review, could you click on the spinning-arrows icon next to the reviewer name? That'll let us know that you are done with editing and it's our turn. Thanks!

@ysiraichi
Copy link
Copy Markdown
Collaborator Author

Ah, ok. Got it. I don't think I ever noticed that feature. Thanks for the tip.

Copy link
Copy Markdown
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!

Comment thread test/cpp/test_runtime.cpp Outdated
Comment thread torch_xla/csrc/runtime/runtime.cpp Outdated
Comment thread torch_xla/csrc/runtime/runtime.cpp Outdated
Comment thread torch_xla/csrc/runtime/runtime.cpp Outdated
@ysiraichi ysiraichi requested a review from ghpvnist June 24, 2025 13:05
Comment thread torch_xla/csrc/init_python_bindings.cpp
@ysiraichi ysiraichi merged commit 10ed554 into master Jun 24, 2025
23 of 24 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.

3 participants