Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 0 additions & 101 deletions test/cpp/test_status_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ class StatusTest : public testing::TestWithParam<CppStacktracesMode> {
namespace cpp_test {

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

Expand All @@ -102,50 +100,6 @@ inline std::string GetStatusPropagationTrace(const absl::Status& status) {
: "";
}

TEST_P(StatusTest, OkOrThrowWithOkStatus) {
absl::Status ok_status = absl::OkStatus();
EXPECT_NO_THROW(OkOrThrow(ok_status));
}

TEST_P(StatusTest, OkOrThrowWithErrorStatus) {
try {
absl::Status error_status = absl::InvalidArgumentError(kMessage);
OkOrThrow(error_status);
} catch (const c10::Error& error) {
if (IsShowCppStacktracesMode()) {
EXPECT_THAT(std::string_view(error.what()),
::testing::StartsWith(absl::StrCat(
kMessage, "\n\n", kTorchCppStacktracePrefixDeprecated)));
} else {
EXPECT_EQ(std::string_view(error.what_without_backtrace()),
std::string_view(kMessage));
}
}
}

TEST_P(StatusTest, GetValueOrThrowWithOkStatusOr) {
int value = 42;
absl::StatusOr<int> status_or = value;
int result = GetValueOrThrow(std::move(status_or));
EXPECT_EQ(result, value);
}

TEST_P(StatusTest, GetValueOrThrowWithErrorStatusOr) {
try {
absl::StatusOr<int> error_status = absl::InvalidArgumentError(kMessage);
int value = GetValueOrThrow(error_status);
} catch (const c10::Error& error) {
if (IsShowCppStacktracesMode()) {
EXPECT_THAT(std::string_view(error.what()),
::testing::StartsWith(absl::StrCat(
kMessage, "\n\n", kTorchCppStacktracePrefixDeprecated)));
} else {
EXPECT_EQ(std::string_view(error.what_without_backtrace()),
std::string_view(kMessage));
}
}
}

TEST_P(StatusTest, MaybeWithLocationPropagatesErrorStatus) {
absl::Status error_status = absl::InvalidArgumentError(kMessage);
absl::Status result =
Expand Down Expand Up @@ -345,61 +299,6 @@ TEST_P(StatusTest, MacroErrorWithLocation) {
}
}

TEST_P(StatusTest, OkOrThrowWithErrorPropagationWithNewMessage) {
int32_t errline0 = __LINE__ + 2;
auto innerfn = [&]() -> absl::Status {
return XLA_ERROR_WITH_LOCATION(absl::InvalidArgumentError(kMessage));
};

int32_t errline1 = __LINE__ + 2;
auto midfn = [&]() -> absl::Status {
XLA_RETURN_IF_ERROR(innerfn(), kNewMessage);
return absl::OkStatus();
};

int32_t errline2 = __LINE__ + 2;
auto outerfn = [&]() -> absl::Status {
XLA_RETURN_IF_ERROR(midfn());
return absl::OkStatus();
};

try {
OkOrThrow(outerfn());
} catch (const c10::Error& error) {
if (IsShowCppStacktracesMode()) {
// Expected Error Message Prefix
// =============================
//
// New test error kMessage
//
// Status Propagation Stacktrace:
// From: ./test/cpp/test_status_common.h:329 (error: Test error
// kMessage) From: ./test/cpp/test_status_common.h:335 (error: New
// test error kMessage) From: ./test/cpp/test_status_common.h:342
//
// C++ Stacktrace:
//
std::ostringstream oss;
oss << kNewMessage;
oss << "\n\n";
oss << "Status Propagation Trace:";
oss << kEntryPrefix << "From: operator() at " << __FILE__ << ":"
<< errline0 << " (error: " << kMessage << ")";
oss << kEntryPrefix << "From: operator() at " << __FILE__ << ":"
<< errline1 << " (error: " << kNewMessage << ")";
oss << kEntryPrefix << "From: operator() at " << __FILE__ << ":"
<< errline2;
oss << "\n\n";
oss << kTorchCppStacktracePrefixDeprecated;
EXPECT_THAT(std::string_view(error.what()),
::testing::StartsWith(oss.str()));
} else {
EXPECT_EQ(std::string_view(error.what_without_backtrace()),
std::string_view(kNewMessage));
}
}
}

TEST_P(StatusTest, MacroThrowIfErrorWithErrorPropagationWithNewMessage) {
int32_t errline0 = __LINE__ + 2;
auto innerfn = [&]() -> absl::Status {
Expand Down
7 changes: 0 additions & 7 deletions torch_xla/csrc/status.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,6 @@ void status_internal::ThrowStatusError(const absl::Status& status,
LineBreakIfCppStacktracesEnabled()));
}

void OkOrThrow(const absl::Status& status) {
TORCH_CHECK(status.ok(), absl::StrCat(BuildStatusErrorMessage(status),
LineBreakIfCppStacktracesEnabled()));
}

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

void status_internal::OkOrDie(const absl::Status& status, const char* file,
const int32_t line, const char* function,
std::string_view message) {
Expand Down
29 changes: 0 additions & 29 deletions torch_xla/csrc/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,35 +297,6 @@ void OkOrDie(const absl::Status& status, const char* file, const int32_t line,
// It doesn't add a trailing line break.
std::string BuildStatusErrorMessage(const absl::Status& status);

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

// Either returns the value `status` holds, if it's an ok-status, or throw an
// exception from its error status.
template <class T>
T& GetValueOrThrow(absl::StatusOr<T>& status) {
OkOrThrow(status.status());
return status.value();
}

template <class T>
const T& GetValueOrThrow(const absl::StatusOr<T>& status) {
OkOrThrow(status.status());
return status.value();
}

template <class T>
T GetValueOrThrow(absl::StatusOr<T>&& status) {
OkOrThrow(status.status());
return std::move(status).value();
}

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

} // namespace torch_xla

#endif // XLA_TORCH_XLA_CSRC_STATUS_H_