Skip to content

Commit 57cd41c

Browse files
authored
Refactor the status error message builder. (#9546)
1 parent 41bfd62 commit 57cd41c

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

torch_xla/csrc/init_python_bindings.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3378,13 +3378,16 @@ void InitXlaModuleBindings(py::module m) {
33783378
absl::StatusOr<std::vector<absl_nonnull XLATensorPtr>>
33793379
xtensors_status = bridge::GetXlaTensors(tensors);
33803380
ABSL_CHECK(xtensors_status.ok())
3381-
<< "_get_graph_hash(): error retrieving the XLA tensors from "
3382-
<< "the given tensor arguments. "
3381+
<< "\n\n"
3382+
<< "Internal Error:\n"
3383+
<< " _get_graph_hash(): error retrieving the XLA tensors "
3384+
"from the given tensor arguments. "
33833385
<< "This is a bug! Please, open an issue in the PyTorch/XLA "
33843386
<< "GitHub repository: https://github.com/pytorch/xla"
3385-
<< std::endl
3386-
<< "Status Error: "
3387-
<< BuildStatusErrorMessage(xtensors_status.status());
3387+
<< "\n\n"
3388+
<< "Status Error:\n"
3389+
<< " " << BuildStatusErrorMessage(xtensors_status.status())
3390+
<< "\n";
33883391
std::vector<absl_nonnull XLATensorPtr> xtensors =
33893392
xtensors_status.value();
33903393
torch::lazy::hash_t hash =

torch_xla/csrc/status.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,29 +103,23 @@ static std::string GetFormattedStatusPropagationTrace(
103103
auto status_propagation_trace = GetStatusPropagationTraceOrEmpty(status);
104104
return status_propagation_trace.empty()
105105
? ""
106-
: absl::StrCat("\nStatus Propagation Trace:",
107-
status_propagation_trace.Flatten(), "\n");
108-
}
109-
110-
// Get the status message followed by a line break, if we are printing the
111-
// C++ stacktraces.
112-
//
113-
// This is needed so we have a blank line in between the status message and
114-
// the dumped C++ traces (either the status propagation one, or the C++
115-
// stacktrace).
116-
static std::string MaybeGetMessageWithLineBreak(const absl::Status& status) {
117-
return torch::get_cpp_stacktraces_enabled()
118-
? absl::StrCat(status.message(), "\n")
119-
: std::string(status.message());
106+
: absl::StrCat("\n\nStatus Propagation Trace:",
107+
status_propagation_trace.Flatten());
120108
}
121109

122110
std::string BuildStatusErrorMessage(const absl::Status& status) {
123-
return absl::StrCat(MaybeGetMessageWithLineBreak(status),
111+
return absl::StrCat(status.message(),
124112
GetFormattedStatusPropagationTrace(status));
125113
}
126114

115+
// Return a line break if torch::get_cpp_stacktraces_enabled() is true.
116+
static std::string LineBreakIfCppStacktracesEnabled() {
117+
return torch::get_cpp_stacktraces_enabled() ? "\n" : "";
118+
}
119+
127120
void MaybeThrow(const absl::Status& status) {
128-
TORCH_CHECK(status.ok(), BuildStatusErrorMessage(status));
121+
TORCH_CHECK(status.ok(), absl::StrCat(BuildStatusErrorMessage(status),
122+
LineBreakIfCppStacktracesEnabled()));
129123
}
130124

131125
void GetValueOrThrow(const absl::Status& status) { MaybeThrow(status); }

torch_xla/csrc/status.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,7 @@ absl::Status MaybeWithNewMessage(const absl::Status& status, const char* file,
179179
// If `TORCH_SHOW_CPP_STACKTRACES` is enabled, returns the concatenation of
180180
// `status.message()` with its inner status propagation trace.
181181
//
182-
// TODO(ysiraichi): this call does not append the C++ stacktrace, which,
183-
// ideally, should. It can be done by not using `TORCH_CHECK()` macro directly
184-
// in `MaybeThrow()`, but using PyTorch `c10::get_lazy_backtrace()`
185-
// (at c10/util/Backtrace.h).
182+
// It doesn't add a trailing line break.
186183
std::string BuildStatusErrorMessage(const absl::Status& status);
187184

188185
// Maybe throws an exception if `status` has a non-ok code.

0 commit comments

Comments
 (0)