You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

python_utils.cc 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "cxx_api/python_utils.h"
  17. #include <dlfcn.h>
  18. #include <mutex>
  19. #include <vector>
  20. #include <memory>
  21. #include <string>
  22. #include <fstream>
  23. #include "mindspore/core/utils/ms_context.h"
  24. #include "pybind11/pybind11.h"
  25. #include "backend/kernel_compiler/oplib/oplib.h"
  26. namespace py = pybind11;
  27. static std::mutex init_mutex;
  28. static bool Initialized = false;
  29. namespace mindspore {
  30. static void RegAllOpFromPython() {
  31. MsContext::GetInstance()->set_param<int>(MS_CTX_EXECUTION_MODE, kGraphMode);
  32. Py_Initialize();
  33. auto c_expression = PyImport_ImportModule("mindspore._c_expression");
  34. MS_EXCEPTION_IF_NULL(c_expression);
  35. PyObject *c_expression_dict = PyModule_GetDict(c_expression);
  36. MS_EXCEPTION_IF_NULL(c_expression_dict);
  37. PyObject *op_info_loader_class = PyDict_GetItemString(c_expression_dict, "OpInfoLoaderPy");
  38. MS_EXCEPTION_IF_NULL(op_info_loader_class);
  39. PyObject *op_info_loader = PyInstanceMethod_New(op_info_loader_class);
  40. MS_EXCEPTION_IF_NULL(op_info_loader);
  41. PyObject *op_info_loader_ins = PyObject_CallObject(op_info_loader, nullptr);
  42. MS_EXCEPTION_IF_NULL(op_info_loader_ins);
  43. auto all_ops_info_vector_addr_ul = PyObject_CallMethod(op_info_loader_ins, "get_all_ops_info", nullptr);
  44. MS_EXCEPTION_IF_NULL(all_ops_info_vector_addr_ul);
  45. auto all_ops_info_vector_addr = PyLong_AsVoidPtr(all_ops_info_vector_addr_ul);
  46. auto all_ops_info = static_cast<std::vector<kernel::OpInfo *> *>(all_ops_info_vector_addr);
  47. for (auto op_info : *all_ops_info) {
  48. kernel::OpLib::RegOpInfo(std::shared_ptr<kernel::OpInfo>(op_info));
  49. }
  50. all_ops_info->clear();
  51. delete all_ops_info;
  52. Py_DECREF(op_info_loader);
  53. Py_DECREF(op_info_loader_class);
  54. Py_DECREF(c_expression_dict);
  55. Py_DECREF(c_expression);
  56. }
  57. static bool RegAllOpFromFile() {
  58. Dl_info info;
  59. int dl_ret = dladdr(reinterpret_cast<void *>(RegAllOpFromFile), &info);
  60. if (dl_ret == 0) {
  61. MS_LOG(INFO) << "Get dladdr failed, skip.";
  62. return false;
  63. }
  64. std::string dir(info.dli_fname);
  65. MS_LOG(INFO) << "Get library path is " << dir;
  66. auto split_pos = dir.find_last_of('/');
  67. if (dir.empty() || split_pos == std::string::npos) {
  68. MS_LOG(INFO) << "Missing op config file, skip.";
  69. return false;
  70. }
  71. dir = dir.substr(0, split_pos) + "/../config/op_info.config";
  72. if (dir.size() >= PATH_MAX) {
  73. MS_LOG(ERROR) << "Op info path is invalid: " << dir;
  74. return false;
  75. }
  76. char real_path_mem[PATH_MAX] = {0};
  77. if (realpath(common::SafeCStr(dir), real_path_mem) == nullptr) {
  78. MS_LOG(ERROR) << "Op info path is invalid: " << dir;
  79. return false;
  80. }
  81. std::string real_path(real_path_mem);
  82. MS_LOG(INFO) << "Start to read op info from local file " << real_path;
  83. std::ifstream file(real_path);
  84. if (!file.is_open()) {
  85. MS_LOG(ERROR) << "Find op info file failed.";
  86. return false;
  87. }
  88. std::string line;
  89. while (getline(file, line)) {
  90. if (!line.empty()) {
  91. (void)kernel::OpLib::RegOp(line, "");
  92. }
  93. }
  94. MS_LOG(INFO) << "End";
  95. return true;
  96. }
  97. void RegAllOp() {
  98. std::lock_guard<std::mutex> lock(init_mutex);
  99. if (Initialized) {
  100. return;
  101. }
  102. bool ret = RegAllOpFromFile();
  103. if (!ret) {
  104. MS_LOG(INFO) << "Reg all op from file failed, start to reg from python.";
  105. RegAllOpFromPython();
  106. }
  107. Initialized = true;
  108. }
  109. bool PythonIsInited() { return Py_IsInitialized() != 0; }
  110. void InitPython() {
  111. if (!PythonIsInited()) {
  112. Py_Initialize();
  113. }
  114. }
  115. void FinalizePython() {
  116. if (PythonIsInited()) {
  117. Py_Finalize();
  118. }
  119. }
  120. PythonEnvGuard::PythonEnvGuard() {
  121. origin_init_status_ = PythonIsInited();
  122. InitPython();
  123. }
  124. PythonEnvGuard::~PythonEnvGuard() {
  125. // finalize when init by this
  126. if (!origin_init_status_) {
  127. FinalizePython();
  128. }
  129. }
  130. } // namespace mindspore