Skip to content

Commit 4edc827

Browse files
orionrfacebook-github-bot
authored andcommitted
Allow for registration after GlobalInit (pytorch#15876)
Summary: Pull Request resolved: pytorch#15876 Build changes made it so some .so libraries are now registered after GlobalInit is called. Although this shouldn't be common, it also shouldn't be explicitly excluded. These changes allow for late Caffe2 registration, but also warn in that case. Reviewed By: kuttas Differential Revision: D13608186 fbshipit-source-id: 0ca7bcd32516d374077db0c2548cf8c28ccdd5f6
1 parent 9b5ec2a commit 4edc827

2 files changed

Lines changed: 46 additions & 8 deletions

File tree

caffe2/core/init.h

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,32 @@ class CAFFE2_API Caffe2InitializeRegistry {
1515
// multiple shared libraries loaded with RTLD_LOCAL
1616
static Caffe2InitializeRegistry* Registry();
1717

18-
void Register(InitFunction function, bool run_early,
19-
const char* description) {
18+
void
19+
Register(InitFunction function, bool run_early, const char* description) {
2020
if (run_early) {
21-
// Disallow late registration of early init functions
21+
// Disallow registration after GlobalInit of early init functions
2222
CAFFE_ENFORCE(!early_init_functions_run_yet_);
2323
early_init_functions_.emplace_back(function, description);
2424
} else {
25-
// Disallow late registration of init functions
26-
CAFFE_ENFORCE(!init_functions_run_yet_);
27-
init_functions_.emplace_back(function, description);
25+
if (init_functions_run_yet_) {
26+
// Run immediately, since GlobalInit already ran. This should be
27+
// rare but we want to allow it in some cases.
28+
LOG(WARNING) << "Running init function after GlobalInit: "
29+
<< description;
30+
// TODO(orionr): Consider removing argc and argv for non-early
31+
// registration. Unfortunately that would require a new InitFunction
32+
// typedef, so not making the change right now.
33+
//
34+
// Note that init doesn't receive argc and argv, so the function
35+
// might fail and we want to raise an error in that case.
36+
int argc = 0;
37+
char** argv = nullptr;
38+
bool success = (function)(&argc, &argv);
39+
CAFFE_ENFORCE(success);
40+
} else {
41+
// Wait until GlobalInit to run
42+
init_functions_.emplace_back(function, description);
43+
}
2844
}
2945
}
3046

caffe2/core/init_test.cc

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@
88
namespace caffe2 {
99
namespace {
1010
bool gTestInitFunctionHasBeenRun = false;
11+
bool gTestFailInitFunctionHasBeenRun = false;
1112

1213
bool TestInitFunction(int*, char***) {
1314
gTestInitFunctionHasBeenRun = true;
1415
return true;
1516
}
17+
18+
bool TestFailInitFunction(int*, char***) {
19+
gTestFailInitFunctionHasBeenRun = true;
20+
return false;
21+
}
22+
1623
REGISTER_CAFFE2_INIT_FUNCTION(
1724
TestInitFunction,
1825
&TestInitFunction,
@@ -27,6 +34,7 @@ char** dummy_argv = const_cast<char**>(&dummy_name);
2734
TEST(InitTest, TestInitFunctionHasRun) {
2835
caffe2::GlobalInit(&dummy_argc, &dummy_argv);
2936
EXPECT_TRUE(gTestInitFunctionHasBeenRun);
37+
EXPECT_FALSE(gTestFailInitFunctionHasBeenRun);
3038
}
3139

3240
TEST(InitTest, CanRerunGlobalInit) {
@@ -36,12 +44,26 @@ TEST(InitTest, CanRerunGlobalInit) {
3644

3745
void LateRegisterInitFunction() {
3846
::caffe2::InitRegisterer testInitFunc(
39-
TestInitFunction, false, "This should fail");
47+
TestInitFunction, false, "This should succeed but warn");
48+
}
49+
50+
void LateRegisterEarlyInitFunction() {
51+
::caffe2::InitRegisterer testSecondInitFunc(
52+
TestInitFunction, true, "This should fail for early init");
53+
}
54+
55+
void LateRegisterFailInitFunction() {
56+
::caffe2::InitRegisterer testSecondInitFunc(
57+
TestFailInitFunction, false, "This should fail for failed init");
4058
}
4159

4260
TEST(InitTest, FailLateRegisterInitFunction) {
4361
caffe2::GlobalInit(&dummy_argc, &dummy_argv);
44-
EXPECT_THROW(LateRegisterInitFunction(), caffe2::EnforceNotMet);
62+
LateRegisterInitFunction();
63+
EXPECT_THROW(LateRegisterEarlyInitFunction(), ::c10::Error);
64+
EXPECT_THROW(LateRegisterFailInitFunction(), ::c10::Error);
65+
EXPECT_TRUE(gTestInitFunctionHasBeenRun);
66+
EXPECT_TRUE(gTestFailInitFunctionHasBeenRun);
4567
}
4668

4769
} // namespace caffe2

0 commit comments

Comments
 (0)