未验证 提交 573ca984 编写于 作者: A Aganlengzi 提交者: GitHub

[custom kernel] fix static object de-initialize bug (#40414)

* [custom kernel] fix static object de-initialize bug

* fix text

* fix text

* refine log info
上级 69a01c47
...@@ -729,6 +729,13 @@ PYBIND11_MODULE(core_noavx, m) { ...@@ -729,6 +729,13 @@ PYBIND11_MODULE(core_noavx, m) {
lib[string]: the libarary, could be 'phi', 'fluid' and 'all'. lib[string]: the libarary, could be 'phi', 'fluid' and 'all'.
)DOC"); )DOC");
// NOTE(Aganlengzi): KernelFactory static instance is initialized BEFORE
// plugins are loaded for custom kernels, but de-initialized AFTER they are
// unloaded. We need manually clear symbols(may contain plugins' symbols)
// stored in this static instance to avoid illegal memory access.
m.def("clear_kernel_factory",
[]() { phi::KernelFactory::Instance().kernels().clear(); });
// NOTE(zjl): ctest would load environment variables at the beginning even // NOTE(zjl): ctest would load environment variables at the beginning even
// though we have not `import paddle.fluid as fluid`. So we add this API // though we have not `import paddle.fluid as fluid`. So we add this API
// to enable eager deletion mode in unittest. // to enable eager deletion mode in unittest.
......
...@@ -33,6 +33,10 @@ void CustomKernelMap::RegisterCustomKernel(const std::string& name, ...@@ -33,6 +33,10 @@ void CustomKernelMap::RegisterCustomKernel(const std::string& name,
void CustomKernelMap::RegisterCustomKernels() { void CustomKernelMap::RegisterCustomKernels() {
VLOG(3) << "Size of custom_kernel_map: " << kernels_.size(); VLOG(3) << "Size of custom_kernel_map: " << kernels_.size();
if (kernels_.size() <= 0) {
LOG(INFO) << "No custom kernel info found in loaded lib(s).";
return;
}
auto& kernels = KernelFactory::Instance().kernels(); auto& kernels = KernelFactory::Instance().kernels();
for (auto& pair : kernels_) { for (auto& pair : kernels_) {
PADDLE_ENFORCE_NE( PADDLE_ENFORCE_NE(
...@@ -60,9 +64,10 @@ void CustomKernelMap::RegisterCustomKernels() { ...@@ -60,9 +64,10 @@ void CustomKernelMap::RegisterCustomKernels() {
<< info_pair.first << info_pair.first
<< "] to Paddle. It will be used like native ones."; << "] to Paddle. It will be used like native ones.";
} }
kernels_[pair.first].clear();
} }
LOG(INFO) << "Successed in loading custom kernels."; LOG(INFO) << "Successed in loading " << kernels_.size()
<< " custom kernel(s) from loaded lib(s), will be "
<< "used like native ones.";
kernels_.clear(); kernels_.clear();
} }
......
...@@ -226,3 +226,5 @@ if core.is_compiled_with_npu(): ...@@ -226,3 +226,5 @@ if core.is_compiled_with_npu():
atexit.register(core.npu_finalize) atexit.register(core.npu_finalize)
# NOTE(Aurelius84): clean up ExecutorCacheInfo in advance manually. # NOTE(Aurelius84): clean up ExecutorCacheInfo in advance manually.
atexit.register(core.clear_executor_cache) atexit.register(core.clear_executor_cache)
# NOTE(Aganlengzi): clean up KernelFactory in advance manually.
atexit.register(core.clear_kernel_factory)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册