From a46d0c55fb82bb27c6b60e77d75761ca0bddb8db Mon Sep 17 00:00:00 2001 From: wilfChen Date: Thu, 18 Mar 2021 11:26:12 +0800 Subject: [PATCH] register akg kernel --- mindspore/ccsrc/cxx_api/CMakeLists.txt | 7 +- ...python_utils.cc => akg_kernel_register.cc} | 66 +------------------ .../{python_utils.h => akg_kernel_register.h} | 18 +---- .../cxx_api/graph/ascend/ascend_graph_impl.cc | 29 +++++++- .../cxx_api/graph/ascend/ascend_graph_impl.h | 12 ++++ .../ccsrc/cxx_api/graph/gpu/gpu_graph_impl.cc | 3 + 6 files changed, 53 insertions(+), 82 deletions(-) rename mindspore/ccsrc/cxx_api/{python_utils.cc => akg_kernel_register.cc} (52%) rename mindspore/ccsrc/cxx_api/{python_utils.h => akg_kernel_register.h} (67%) diff --git a/mindspore/ccsrc/cxx_api/CMakeLists.txt b/mindspore/ccsrc/cxx_api/CMakeLists.txt index 59c967ba16..35e3799160 100644 --- a/mindspore/ccsrc/cxx_api/CMakeLists.txt +++ b/mindspore/ccsrc/cxx_api/CMakeLists.txt @@ -10,7 +10,7 @@ if(ENABLE_ACL) include_directories(${CMAKE_SOURCE_DIR}/graphengine/ge) include_directories(${CMAKE_BINARY_DIR}/proto/ge) file(GLOB_RECURSE API_ACL_SRC ${CMAKE_CURRENT_SOURCE_DIR} - "python_utils.cc" + "akg_kernel_register.cc" "model/acl/*.cc" "model/model_converter_utils/*.cc" "graph/acl/*.cc" @@ -19,11 +19,12 @@ endif() if(ENABLE_D) file(GLOB_RECURSE API_MS_INFER_SRC ${CMAKE_CURRENT_SOURCE_DIR} - "python_utils.cc" "model/ms/*.cc" "graph/ascend/*.cc") + "akg_kernel_register.cc" "model/ms/*.cc" "graph/ascend/*.cc") endif() if(ENABLE_GPU) - file(GLOB_RECURSE API_MS_INFER_SRC ${CMAKE_CURRENT_SOURCE_DIR} "model/ms/*.cc" "graph/gpu/*.cc") + file(GLOB_RECURSE API_MS_INFER_SRC ${CMAKE_CURRENT_SOURCE_DIR} + "akg_kernel_register.cc" "model/ms/*.cc" "graph/gpu/*.cc") endif() set(MSLIB_SRC ${CMAKE_CURRENT_SOURCE_DIR}/types.cc diff --git a/mindspore/ccsrc/cxx_api/python_utils.cc b/mindspore/ccsrc/cxx_api/akg_kernel_register.cc similarity index 52% rename from mindspore/ccsrc/cxx_api/python_utils.cc rename to mindspore/ccsrc/cxx_api/akg_kernel_register.cc index 89a0d2793b..6ab67f918d 100644 --- a/mindspore/ccsrc/cxx_api/python_utils.cc +++ b/mindspore/ccsrc/cxx_api/akg_kernel_register.cc @@ -13,52 +13,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "cxx_api/python_utils.h" +#include "cxx_api/akg_kernel_register.h" #include #include -#include #include #include #include -#include "mindspore/core/utils/ms_context.h" -#include "pybind11/pybind11.h" #include "backend/kernel_compiler/oplib/oplib.h" -namespace py = pybind11; - static std::mutex init_mutex; static bool Initialized = false; namespace mindspore { -static void RegAllOpFromPython() { - MsContext::GetInstance()->set_param(MS_CTX_EXECUTION_MODE, kGraphMode); - Py_Initialize(); - auto c_expression = PyImport_ImportModule("mindspore._c_expression"); - MS_EXCEPTION_IF_NULL(c_expression); - PyObject *c_expression_dict = PyModule_GetDict(c_expression); - MS_EXCEPTION_IF_NULL(c_expression_dict); - - PyObject *op_info_loader_class = PyDict_GetItemString(c_expression_dict, "OpInfoLoaderPy"); - MS_EXCEPTION_IF_NULL(op_info_loader_class); - PyObject *op_info_loader = PyInstanceMethod_New(op_info_loader_class); - MS_EXCEPTION_IF_NULL(op_info_loader); - PyObject *op_info_loader_ins = PyObject_CallObject(op_info_loader, nullptr); - MS_EXCEPTION_IF_NULL(op_info_loader_ins); - auto all_ops_info_vector_addr_ul = PyObject_CallMethod(op_info_loader_ins, "get_all_ops_info", nullptr); - MS_EXCEPTION_IF_NULL(all_ops_info_vector_addr_ul); - auto all_ops_info_vector_addr = PyLong_AsVoidPtr(all_ops_info_vector_addr_ul); - auto all_ops_info = static_cast *>(all_ops_info_vector_addr); - for (auto op_info : *all_ops_info) { - kernel::OpLib::RegOpInfo(std::shared_ptr(op_info)); - } - all_ops_info->clear(); - delete all_ops_info; - Py_DECREF(op_info_loader); - Py_DECREF(op_info_loader_class); - Py_DECREF(c_expression_dict); - Py_DECREF(c_expression); -} - static bool RegAllOpFromFile() { Dl_info info; int dl_ret = dladdr(reinterpret_cast(RegAllOpFromFile), &info); @@ -111,36 +77,10 @@ void RegAllOp() { } bool ret = RegAllOpFromFile(); if (!ret) { - MS_LOG(INFO) << "Reg all op from file failed, start to reg from python."; - RegAllOpFromPython(); + MS_LOG(ERROR) << "Register operators failed. The package may damaged or file is missing."; + return; } Initialized = true; } - -bool PythonIsInited() { return Py_IsInitialized() != 0; } - -void InitPython() { - if (!PythonIsInited()) { - Py_Initialize(); - } -} - -void FinalizePython() { - if (PythonIsInited()) { - Py_Finalize(); - } -} - -PythonEnvGuard::PythonEnvGuard() { - origin_init_status_ = PythonIsInited(); - InitPython(); -} - -PythonEnvGuard::~PythonEnvGuard() { - // finalize when init by this - if (!origin_init_status_) { - FinalizePython(); - } -} } // namespace mindspore diff --git a/mindspore/ccsrc/cxx_api/python_utils.h b/mindspore/ccsrc/cxx_api/akg_kernel_register.h similarity index 67% rename from mindspore/ccsrc/cxx_api/python_utils.h rename to mindspore/ccsrc/cxx_api/akg_kernel_register.h index dbc6dacb9f..2562cc5469 100644 --- a/mindspore/ccsrc/cxx_api/python_utils.h +++ b/mindspore/ccsrc/cxx_api/akg_kernel_register.h @@ -13,22 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef MINDSPORE_CCSRC_CXXAPI_PYTHON_UTILS_H -#define MINDSPORE_CCSRC_CXXAPI_PYTHON_UTILS_H +#ifndef MINDSPORE_CCSRC_CXXAPI_AKG_KERNEL_REGISTER_H_ +#define MINDSPORE_CCSRC_CXXAPI_AKG_KERNEL_REGISTER_H_ namespace mindspore { void RegAllOp(); -bool PythonIsInited(); -void InitPython(); -void FinalizePython(); - -class PythonEnvGuard { - public: - PythonEnvGuard(); - ~PythonEnvGuard(); - - private: - bool origin_init_status_; -}; } // namespace mindspore -#endif // MINDSPORE_CCSRC_CXXAPI_PYTHON_UTILS_H +#endif // MINDSPORE_CCSRC_CXXAPI_AKG_KERNEL_REGISTER_H_ diff --git a/mindspore/ccsrc/cxx_api/graph/ascend/ascend_graph_impl.cc b/mindspore/ccsrc/cxx_api/graph/ascend/ascend_graph_impl.cc index a5ba26997d..a9cead17af 100644 --- a/mindspore/ccsrc/cxx_api/graph/ascend/ascend_graph_impl.cc +++ b/mindspore/ccsrc/cxx_api/graph/ascend/ascend_graph_impl.cc @@ -17,7 +17,7 @@ #include #include "include/api/context.h" #include "cxx_api/factory.h" -#include "cxx_api/python_utils.h" +#include "cxx_api/akg_kernel_register.h" #include "utils/log_adapter.h" #include "utils/context/context_extends.h" #include "mindspore/core/base/base_ref_utils.h" @@ -27,6 +27,7 @@ #include "runtime/dev.h" #include "pipeline/jit/pipeline.h" #include "frontend/parallel/step_parallel.h" +#include "pybind11/pybind11.h" namespace mindspore { API_FACTORY_REG(GraphCell::GraphImpl, Ascend910, AscendGraphImpl); @@ -380,4 +381,30 @@ std::shared_ptr AscendGraphImpl::MsEnvGuard::GetEnv std::map> AscendGraphImpl::MsEnvGuard::global_ms_env_; std::mutex AscendGraphImpl::MsEnvGuard::global_ms_env_mutex_; + +PythonEnvGuard::PythonEnvGuard() { + origin_init_status_ = PythonIsInited(); + InitPython(); +} + +PythonEnvGuard::~PythonEnvGuard() { + // finalize when init by this + if (!origin_init_status_) { + FinalizePython(); + } +} + +bool PythonEnvGuard::PythonIsInited() { return Py_IsInitialized() != 0; } + +void PythonEnvGuard::InitPython() { + if (!PythonIsInited()) { + Py_Initialize(); + } +} + +void PythonEnvGuard::FinalizePython() { + if (PythonIsInited()) { + Py_Finalize(); + } +} } // namespace mindspore diff --git a/mindspore/ccsrc/cxx_api/graph/ascend/ascend_graph_impl.h b/mindspore/ccsrc/cxx_api/graph/ascend/ascend_graph_impl.h index b918e4ec4f..c8fcb160fe 100644 --- a/mindspore/ccsrc/cxx_api/graph/ascend/ascend_graph_impl.h +++ b/mindspore/ccsrc/cxx_api/graph/ascend/ascend_graph_impl.h @@ -79,5 +79,17 @@ class AscendGraphImpl::MsEnvGuard { Status errno_; uint32_t device_id_; }; + +class PythonEnvGuard { + public: + PythonEnvGuard(); + ~PythonEnvGuard(); + + private: + bool PythonIsInited(); + void InitPython(); + void FinalizePython(); + bool origin_init_status_; +}; } // namespace mindspore #endif // MINDSPORE_CCSRC_CXX_API_GRAPH_MS_ASCEND_GRAPH_IMPL_H diff --git a/mindspore/ccsrc/cxx_api/graph/gpu/gpu_graph_impl.cc b/mindspore/ccsrc/cxx_api/graph/gpu/gpu_graph_impl.cc index b755a0600e..f40a62a4bf 100644 --- a/mindspore/ccsrc/cxx_api/graph/gpu/gpu_graph_impl.cc +++ b/mindspore/ccsrc/cxx_api/graph/gpu/gpu_graph_impl.cc @@ -17,6 +17,7 @@ #include #include "include/api/context.h" #include "cxx_api/factory.h" +#include "cxx_api/akg_kernel_register.h" #include "utils/log_adapter.h" #include "mindspore/core/base/base_ref_utils.h" #include "backend/session/session_factory.h" @@ -43,6 +44,8 @@ Status GPUGraphImpl::InitEnv() { return kSuccess; } + // Register op implemented with AKG. + RegAllOp(); auto ms_context = MsContext::GetInstance(); if (ms_context == nullptr) { MS_LOG(ERROR) << "Get Context failed!";