-
Notifications
You must be signed in to change notification settings - Fork 550
Fix status source code location logic. #9440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
- Refactor status tests - Remove test_status.cpp as its tests are now covered by specialized context tests - Both specialized tests now cover all status utility functions and macros
0427c0a
to
d6ba41f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
// This file specifically tests behavior when XLA_SHOW_CPP_ERROR_CONTEXT is | ||
// set to "false". | ||
// | ||
// If you add or delete a test in this file, please make the corresponding |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This leads to a lot of code duplication and maintenance overhead.
We should write the test cases once and share them between the two configurations.
torch_xla/csrc/status.cpp
Outdated
context = absl::StrCat(context, "\nFrom Error: ", old_message); | ||
} | ||
} | ||
std::string location = LocationStrWithSpace(file, line); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't create this unless it's actually needed.
torch_xla/csrc/status.cpp
Outdated
} | ||
} | ||
std::string location = LocationStrWithSpace(file, line); | ||
std::string context = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const
torch_xla/csrc/status.cpp
Outdated
std::string location = LocationStrWithSpace(file, line); | ||
std::string context = | ||
(ShouldShowCppErrorContext() && !new_message.empty()) | ||
? std::string(absl::StrCat(location, "\nFrom Error: ", old_message)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: std::string()
call is unnecessary.
// Any new `TEST_P` test cases added to `test_status_common.h` will automatically | ||
// be run in this mode (without C++ error context). | ||
// | ||
INSTANTIATE_TEST_SUITE_WITH_MODE(HIDE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need these files if all the tests are unified under test_status_common.h
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. The problem is that we set the mode
there by setting the XLA_SHOW_CPP_ERROR_CONTEXT
environment variable, which gets cached the first time it's used in a local static variable. This means that even if we reset that environment variable to another value afterwards, it won't affect execution. So, we need to test them in different processes. I think that the easiest and most obvious way to accomplish that is by having separate files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
test/cpp/test_status_common.h
Outdated
|
||
// Enum to control whether C++ error context is shown in status messages | ||
enum class CppErrorContextMode { | ||
SHOW, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per https://google.github.io/styleguide/cppguide.html#Enumerator_Names, this should be kShow
.
Would be great to get familiar with the Google C++ style guide.
test/cpp/test_status_common.h
Outdated
|
||
// Base test class for parameterized status tests with C++ error context control | ||
class StatusTest : public testing::TestWithParam<CppErrorContextMode> { | ||
void SetUp() override { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style: prefer using the StatusTest
default ctor here.
test/cpp/test_status_common.h
Outdated
setenv(runtime::env::kEnvShowCppErrorContext, value, /* replace= */ 1); | ||
} | ||
|
||
public: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per the style guide: public
section should be before protected
or private
section.
Actually, this doesn't need to be public
. It can be protected
.
test/cpp/test_status_common.h
Outdated
|
||
// Macro to instantiate parameterized status tests with specific mode | ||
#define INSTANTIATE_TEST_SUITE_WITH_MODE(mode) \ | ||
using torch_xla::CppErrorContextMode; \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has the side effect of defining some type aliases. Avoid it. Just spell out the types in the macro body.
test/cpp/test_status_common.h
Outdated
}; | ||
|
||
// Macro to instantiate parameterized status tests with specific mode | ||
#define INSTANTIATE_TEST_SUITE_WITH_MODE(mode) \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This name is too general. Also the connection with StatusTest
is unobvious at the call site.
I'd let the caller provide StatusTest
as an argument to make the code more readable.
test/cpp/test_status_common.h
Outdated
|
||
namespace { | ||
|
||
using absl::Status; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style:
avoid using declaration at namespace in headers unless we intend to define a type alias for all users of the header. Since we don't intend code that includes this header to be able to write StatusOr
without absl::
, these using declarations should be removed.
test/cpp/test_status_common.h
Outdated
using absl::StatusOr; | ||
using absl::StrCat; | ||
|
||
constexpr char new_message[] = "New test error message"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Global variables defined in a header should be inline
.
test/cpp/BUILD
Outdated
srcs = ["test_status_dont_show_cpp_error_context.cpp"], | ||
srcs = [ | ||
"test_status_dont_show_cpp_error_context.cpp", | ||
"test_status_common.h", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style: it's better to define a header-only library for test_status_common.h
and add the library to both cc_test
s' deps
. This ensures that the header is self-contained.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
test/cpp/test_status_common.h
Outdated
// is being called. | ||
#define INSTANTIATE_WITH_CPP_ERROR_CONTEXT_MODE(suite, test, mode) \ | ||
INSTANTIATE_TEST_SUITE_P( \ | ||
suite, test, testing::Values(CppErrorContextMode::mode), \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style: in a macro body, always fully qualify the symbols (use absolute namespaces like ::testing::
), so that we don't have to worry about changing the macro meaning if it's used in the wrong namespace.
@@ -1,5 +1,8 @@ | |||
#include "test/cpp/test_status_common.h" | |||
|
|||
using torch_xla::CppErrorContextMode; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style: please make the macro body fully qualify the references of these names so that we don't have to do this here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes. Sorry about that. Since I couldn't do it with StatusTest
(i.e. ::torch_xla::StatusTest
), I incorrectly assumed I couldn't do it for CppErrorContextMode
. It should be fixed, now.
@@ -1,10 +1,14 @@ | |||
#include "test/cpp/test_status_common.h" | |||
|
|||
using torch_xla::CppErrorContextMode; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto.
This PR refines the handling of status source code locations and overhauls related tests. The key changes improve the accuracy of source information and streamline our testing approach:
MacroReturnIfErrorWithNestedError
that exemplifies the undesirable behavior.test_status.cpp
, copying its tests to the other 2 existing tests:test_status_show_cpp_error_context.cpp
andtest_status_dont_show_cpp_error_context.cpp
. Rationale: the tests should pass with both configurations.Undesired Behavior
Below, we can see the results of the newly added test
MacroReturnIfErrorWithNestedError
whenXLA_SHOW_CPP_ERROR_CONTEXT=1
: