-
Notifications
You must be signed in to change notification settings - Fork 745
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
Export env to python #7792
Export env to python #7792
Changes from 9 commits
29ccd30
8509484
434b7af
86296cb
454f5e7
d1d9ad7
a58348d
8bb83a1
fe64379
9ca83c6
9b5cecc
72426fc
da8b44d
cf14a1e
e3297f4
6f5d7c6
5d0c648
97cf982
a119bf0
d2b36a4
d8862b9
6e22eb4
e58bee9
df91894
93191d7
da35647
a3d45e3
1ec6dc3
3d6c7cd
f7e2241
c1afee6
7c4cb89
7daab5a
07bb2a2
47738f7
87c748f
9d5ab85
80a4542
57fff70
ce55514
c679884
53d088a
c6ecc12
204aa54
3124ecf
8c6c03e
72c30fd
5d6212f
19c18d4
002bacf
5cfcadd
aef2edc
b2d87f2
f00f991
ae9d601
89f3ce2
ad18575
b67a60e
fb8b9fa
ec2c402
45fd613
b730ef0
fbd921c
a365516
244ee42
e99bac0
595d13f
340f6f9
9a078cd
61cc655
e07072c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,19 +16,41 @@ limitations under the License. | |
#include <pybind11/pybind11.h> | ||
#include "oneflow/api/python/env/env.h" | ||
#include "oneflow/api/python/of_api_registry.h" | ||
#include "oneflow/core/common/global.h" | ||
#include "oneflow/core/vm/vm_util.h" | ||
#include "oneflow/core/vm/virtual_machine.h" | ||
#include "oneflow/core/framework/shut_down_util.h" | ||
|
||
namespace py = pybind11; | ||
|
||
namespace oneflow { | ||
|
||
Maybe<void> SwitchToShuttingDownPhase(EnvGlobalObjectsScope* env, bool is_normal_exit) { | ||
if (is_normal_exit) { | ||
JUST(vm::ClusterSync()); | ||
auto* vm = JUST(GlobalMaybe<VirtualMachine>()); | ||
JUST(vm->CloseVMThreads()); | ||
} | ||
JUST(env->init_is_normal_exit(is_normal_exit)); | ||
SetShuttingDown(true); | ||
return Maybe<void>::Ok(); | ||
} | ||
|
||
ONEFLOW_API_PYBIND11_MODULE("", m) { | ||
m.def("CurrentResource", &CurrentResource); | ||
m.def("EnvResource", &EnvResource); | ||
m.def("EnableEagerEnvironment", &EnableEagerEnvironment); | ||
|
||
m.def("IsEnvInited", &IsEnvInited); | ||
m.def("InitEnv", &InitEnv); | ||
m.def("DestroyEnv", &DestroyEnv, py::call_guard<py::gil_scoped_release>()); | ||
py::class_<EnvGlobalObjectsScope, std::shared_ptr<EnvGlobalObjectsScope>>(m, "Env") | ||
.def(py::init([](const std::string& env_proto_str) { | ||
return CreateEnv(env_proto_str).GetPtrOrThrow(); | ||
})) | ||
.def( | ||
"SwitchToShuttingDownPhase", | ||
[](EnvGlobalObjectsScope* env, bool is_normal_exit) { | ||
SwitchToShuttingDownPhase(env, is_normal_exit).GetOrThrow(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里可以 return SwitchToShuttingDownPhase(env, is_normal_exit); 了(46 行 GetPtrOrThrow() 还不能去掉因为 py::init 有特殊要求) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 好的 |
||
}, | ||
py::call_guard<py::gil_scoped_release>()); | ||
|
||
m.def("CurrentMachineId", &CurrentMachineId); | ||
|
||
|
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -229,6 +229,7 @@ Maybe<void> EnvGlobalObjectsScope::Init(const EnvProto& env_proto) { | |
} | ||
|
||
EnvGlobalObjectsScope::~EnvGlobalObjectsScope() { | ||
if (is_normal_exit_.has_value() && !CHECK_JUST(is_normal_exit_)) { return; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 也许应该命名为is_abnormal_exit |
||
auto session_ctx = Global<MultiClientSessionContext>::Get(); | ||
if (session_ctx != nullptr) { | ||
VLOG(1) << "Multi client session has not closed , env close it at env scope destruction."; | ||
|
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.
旧版的逻辑写在python层。如果遇到系统异常退出,则完全不执行DeleteEnv。为了对齐此逻辑,我们让EnvGlobalObjectsScope的析构在!is_normal_exit的时候不执行那一系列的Global::Delete();