From 8e4c193698edfa0485ae8c6b28b112e398fb9f93 Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Wed, 21 Apr 2021 14:00:46 +0800 Subject: [PATCH] [NPU] register npu finalize on exit (#32390) * [NPU] register finalize on exit * fix --- paddle/fluid/operators/CMakeLists.txt | 2 +- paddle/fluid/platform/npu_info.cc | 2 ++ paddle/fluid/pybind/pybind.cc | 4 +--- python/paddle/fluid/__init__.py | 6 ++++++ 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/paddle/fluid/operators/CMakeLists.txt b/paddle/fluid/operators/CMakeLists.txt index 3aa47c9e092..7b17899d3da 100644 --- a/paddle/fluid/operators/CMakeLists.txt +++ b/paddle/fluid/operators/CMakeLists.txt @@ -167,7 +167,7 @@ endif() if (WITH_ASCEND_CL) cc_test(range_op_npu_test SRCS range_op_npu_test.cc DEPS op_registry range_op scope device_context enforce executor) - cc_test(expand_op_npu_test SRCS expand_op_npu_test.cc DEPS op_registry expand_op scope device_context enforce executor compare_op) + cc_test(expand_op_npu_test SRCS expand_op_npu_test.cc DEPS op_registry expand_op eigen_cc_function scope device_context enforce executor compare_op) endif() set(GLOB_OP_LIB ${OP_LIBRARY} CACHE INTERNAL "Global OP library") diff --git a/paddle/fluid/platform/npu_info.cc b/paddle/fluid/platform/npu_info.cc index 3814faa7662..bb36eedb832 100644 --- a/paddle/fluid/platform/npu_info.cc +++ b/paddle/fluid/platform/npu_info.cc @@ -190,6 +190,8 @@ void NPUMemcpySync(void *dst, const void *src, size_t count, enum aclrtMemcpyKind kind, size_t dst_max_count) { // NOTE(zhiqiu): The default max_count is count dst_max_count = dst_max_count ? dst_max_count : count; + VLOG(4) << dst << " " << dst_max_count << " " << src << " " << count << " " + << kind; PADDLE_ENFORCE_NPU_SUCCESS(aclrtMemcpy(dst, dst_max_count, src, count, kind)); } diff --git a/paddle/fluid/pybind/pybind.cc b/paddle/fluid/pybind/pybind.cc index 038bcc7f850..f315323ed59 100644 --- a/paddle/fluid/pybind/pybind.cc +++ b/paddle/fluid/pybind/pybind.cc @@ -2178,9 +2178,7 @@ All parameter, weight, gradient are variables in Paddle. #ifdef PADDLE_WITH_ASCEND_CL m.def("get_npu_device_count", platform::GetNPUDeviceCount); - m.def("_npu_finalize", []() { - platform::AclInstance::Instance().Finalize(); - }); // private interface + m.def("npu_finalize", []() { platform::AclInstance::Instance().Finalize(); }); py::class_(m, "NPUProfConfigWrapper"); diff --git a/python/paddle/fluid/__init__.py b/python/paddle/fluid/__init__.py index 6dd1478dc1f..439b5c64615 100644 --- a/python/paddle/fluid/__init__.py +++ b/python/paddle/fluid/__init__.py @@ -15,6 +15,7 @@ from __future__ import print_function import os import sys +import atexit # The legacy core need to be removed before "import core", # in case of users installing paddlepadde without -U option @@ -255,3 +256,8 @@ def __bootstrap__(): monkey_patch_variable() __bootstrap__() monkey_patch_varbase() + +# NOTE(zhiqiu): register npu_finalize on the exit of Python, +# do some clean up manually. +if core.is_compiled_with_npu(): + atexit.register(core.npu_finalize) -- GitLab