Skip to content

Commit 4199865

Browse files
authored
Rename MaybeThrow to OkOrThrow. (#9561)
1 parent 2c34318 commit 4199865

File tree

7 files changed

+34
-32
lines changed

7 files changed

+34
-32
lines changed

test/cpp/test_status_common.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ namespace cpp_test {
8181

8282
// Prefix of the C++ stacktrace PyTorch adds to the error message.
8383
constexpr inline char kTorchCppStacktracePrefix[] =
84-
"Exception raised from MaybeThrow at torch_xla/csrc/status.cpp:";
84+
"Exception raised from OkOrThrow at torch_xla/csrc/status.cpp:";
8585

8686
constexpr inline char kNewMessage[] = "New test error message";
8787
constexpr inline char kMessage[] = "Test error message";
@@ -100,15 +100,15 @@ inline std::string GetStatusPropagationTrace(const absl::Status& status) {
100100
: "";
101101
}
102102

103-
TEST_P(StatusTest, MaybeThrowWithOkStatus) {
103+
TEST_P(StatusTest, OkOrThrowWithOkStatus) {
104104
absl::Status ok_status = absl::OkStatus();
105-
EXPECT_NO_THROW(MaybeThrow(ok_status));
105+
EXPECT_NO_THROW(OkOrThrow(ok_status));
106106
}
107107

108-
TEST_P(StatusTest, MaybeThrowWithErrorStatus) {
108+
TEST_P(StatusTest, OkOrThrowWithErrorStatus) {
109109
try {
110110
absl::Status error_status = absl::InvalidArgumentError(kMessage);
111-
MaybeThrow(error_status);
111+
OkOrThrow(error_status);
112112
} catch (const c10::Error& error) {
113113
if (IsShowCppStacktracesMode()) {
114114
EXPECT_THAT(std::string_view(error.what()),
@@ -343,7 +343,7 @@ TEST_P(StatusTest, MacroErrorWithLocation) {
343343
}
344344
}
345345

346-
TEST_P(StatusTest, MaybeThrowWithErrorPropagationWithNewMessage) {
346+
TEST_P(StatusTest, OkOrThrowWithErrorPropagationWithNewMessage) {
347347
int32_t errline0 = __LINE__ + 2;
348348
auto innerfn = [&]() -> absl::Status {
349349
return XLA_ERROR_WITH_LOCATION(absl::InvalidArgumentError(kMessage));
@@ -362,7 +362,7 @@ TEST_P(StatusTest, MaybeThrowWithErrorPropagationWithNewMessage) {
362362
};
363363

364364
try {
365-
MaybeThrow(outerfn());
365+
OkOrThrow(outerfn());
366366
} catch (const c10::Error& error) {
367367
if (IsShowCppStacktracesMode()) {
368368
// Expected Error Message Prefix

torch_xla/csrc/aten_xla_type.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ at::Tensor XLANativeFunctions::_copy_from(const at::Tensor& self,
664664
} else {
665665
auto dst_tensor = std::move(dst_tensor_status).value();
666666
tensor_methods::copy_(dst_tensor, self_tensor_status.value());
667-
MaybeThrow(bridge::ReplaceXlaTensor(dst, dst_tensor));
667+
OkOrThrow(bridge::ReplaceXlaTensor(dst, dst_tensor));
668668
}
669669
return dst;
670670
}
@@ -3438,7 +3438,7 @@ at::Tensor& XLANativeFunctions::set_(at::Tensor& self,
34383438
const at::Tensor& source) {
34393439
TORCH_LAZY_FN_COUNTER_TIMED_TRACING("xla::");
34403440
XLATensorPtr source_tensor = GetValueOrThrow(bridge::GetXlaTensor(source));
3441-
MaybeThrow(bridge::ReplaceXlaTensor(self, source_tensor));
3441+
OkOrThrow(bridge::ReplaceXlaTensor(self, source_tensor));
34423442
return self;
34433443
}
34443444

torch_xla/csrc/dl_convertor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ DLManagedTensor* toDLPack(const at::Tensor& input) {
144144
pack->external_reference =
145145
GetValueOrThrow(pjrt_buffer->AcquireExternalReference());
146146
xla::PjRtFuture<> future = pjrt_buffer->GetReadyFuture();
147-
MaybeThrow(future.Await());
147+
OkOrThrow(future.Await());
148148
}
149149
pack->buffer_reference = pjrt_buffer;
150150

torch_xla/csrc/init_python_bindings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ void AllReduceInPlace(const std::string& reduce_type,
429429
replica_groups, pin_layout);
430430
std::vector<XLATensorPtr> new_xtensors =
431431
GetValueOrThrow(bridge::GetXlaTensors(tensors));
432-
MaybeThrow(bridge::ReplaceXlaTensor(tensors, new_xtensors));
432+
OkOrThrow(bridge::ReplaceXlaTensor(tensors, new_xtensors));
433433
}
434434

435435
at::Tensor AllReduce(const std::string& reduce_type, const at::Tensor& input,

torch_xla/csrc/runtime/tensor_source.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class TensorSource {
3131

3232
virtual std::vector<int64_t> byte_strides() const {
3333
std::vector<int64_t> byte_strides(shape().dimensions_size());
34-
MaybeThrow(
34+
OkOrThrow(
3535
xla::ShapeUtil::ByteStrides(shape(), absl::MakeSpan(byte_strides)));
3636
return byte_strides;
3737
}

torch_xla/csrc/status.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,16 @@ static std::string LineBreakIfCppStacktracesEnabled() {
117117
return torch::get_cpp_stacktraces_enabled() ? "\n" : "";
118118
}
119119

120-
void MaybeThrow(const absl::Status& status) {
120+
void OkOrThrow(const absl::Status& status) {
121121
TORCH_CHECK(status.ok(), absl::StrCat(BuildStatusErrorMessage(status),
122122
LineBreakIfCppStacktracesEnabled()));
123123
}
124124

125-
void GetValueOrThrow(const absl::Status& status) { MaybeThrow(status); }
125+
void GetValueOrThrow(const absl::Status& status) { OkOrThrow(status); }
126126

127-
void OkOrDie(const absl::Status& status, const char* file, const int32_t line,
128-
const char* function, std::string_view message) {
127+
void status_internal::OkOrDie(const absl::Status& status, const char* file,
128+
const int32_t line, const char* function,
129+
std::string_view message) {
129130
if (status.ok()) {
130131
return;
131132
}

torch_xla/csrc/status.h

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,10 @@ constexpr char kStatusPropagationTraceKey[] =
139139
// If `FnThatReturnStatus()` returns a non-ok status, this macro will
140140
// call `ABSL_CHECK()`, which will crash.
141141
//
142-
#define XLA_CHECK_OK(status, ...) \
143-
::torch_xla::OkOrDie(::torch_xla::status_internal::GetStatus(status), \
144-
__FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
142+
#define XLA_CHECK_OK(status, ...) \
143+
::torch_xla::status_internal::OkOrDie( \
144+
::torch_xla::status_internal::GetStatus(status), __FILE__, __LINE__, \
145+
__FUNCTION__, ##__VA_ARGS__)
145146

146147
namespace status_internal {
147148

@@ -190,6 +191,14 @@ absl::Status MaybeWithNewMessage(const absl::Status& status, const char* file,
190191
int32_t line, const char* function,
191192
std::string_view new_message = "");
192193

194+
// Checks that `status` is an ok status.
195+
//
196+
// Otherwise, it will create a new status instance with the given source
197+
// location information, and incorporate its message (alongside the
198+
// status propagation trace) to the crash report.
199+
void OkOrDie(const absl::Status& status, const char* file, const int32_t line,
200+
const char* function, std::string_view message = "");
201+
193202
} // namespace status_internal
194203

195204
// Builds the complete error message for the given `status`.
@@ -200,43 +209,35 @@ absl::Status MaybeWithNewMessage(const absl::Status& status, const char* file,
200209
// It doesn't add a trailing line break.
201210
std::string BuildStatusErrorMessage(const absl::Status& status);
202211

203-
// Maybe throws an exception if `status` has a non-ok code.
212+
// Throws an exception if `status` has a non-ok code.
204213
//
205214
// Ideally, this function should be used only used in the project's
206215
// boundary, e.g. when we need to throw an exception for the user to see.
207-
void MaybeThrow(const absl::Status& status);
216+
void OkOrThrow(const absl::Status& status);
208217

209218
// Either returns the value `status` holds, if it's an ok-status, or throw an
210219
// exception from its error status.
211220
template <class T>
212221
T& GetValueOrThrow(absl::StatusOr<T>& status) {
213-
MaybeThrow(status.status());
222+
OkOrThrow(status.status());
214223
return status.value();
215224
}
216225

217226
template <class T>
218227
const T& GetValueOrThrow(const absl::StatusOr<T>& status) {
219-
MaybeThrow(status.status());
228+
OkOrThrow(status.status());
220229
return status.value();
221230
}
222231

223232
template <class T>
224233
T GetValueOrThrow(absl::StatusOr<T>&& status) {
225-
MaybeThrow(status.status());
234+
OkOrThrow(status.status());
226235
return std::move(status).value();
227236
}
228237

229238
// `GetValueOrThrow` overload for `Status`.
230239
void GetValueOrThrow(const absl::Status& status);
231240

232-
// Checks that `status` is an ok status.
233-
//
234-
// Otherwise, it will create a new status instance with the given source
235-
// location information, and incorporate its message (alongside the
236-
// status propagation trace) to the crash report.
237-
void OkOrDie(const absl::Status& status, const char* file, const int32_t line,
238-
const char* function, std::string_view message = "");
239-
240241
} // namespace torch_xla
241242

242243
#endif // XLA_TORCH_XLA_CSRC_STATUS_H_

0 commit comments

Comments
 (0)