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.

opsproto_manager.cc 5.4 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * Copyright 2019-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 "graph/opsproto_manager.h"
  17. #include <cstdlib>
  18. #include <algorithm>
  19. #include <functional>
  20. #include <iostream>
  21. #include <sstream>
  22. #include "debug/ge_util.h"
  23. #include "framework/common/debug/ge_log.h"
  24. #include "graph/debug/ge_log.h"
  25. namespace ge {
  26. OpsProtoManager *OpsProtoManager::Instance() {
  27. static OpsProtoManager instance;
  28. return &instance;
  29. }
  30. bool OpsProtoManager::Initialize(const std::map<std::string, std::string> &options) {
  31. std::lock_guard<std::mutex> lock(mutex_);
  32. if (is_init_) {
  33. GELOGI("OpsProtoManager is already initialized.");
  34. return true;
  35. }
  36. /*lint -e1561*/
  37. auto proto_iter = options.find("ge.opsProtoLibPath");
  38. /*lint +e1561*/
  39. if (proto_iter == options.end()) {
  40. GELOGW("ge.opsProtoLibPath option not set, return.");
  41. return false;
  42. }
  43. pluginPath_ = proto_iter->second;
  44. LoadOpsProtoPluginSo(pluginPath_);
  45. is_init_ = true;
  46. return true;
  47. }
  48. void OpsProtoManager::Finalize() {
  49. std::lock_guard<std::mutex> lock(mutex_);
  50. if (!is_init_) {
  51. GELOGI("OpsProtoManager is not initialized.");
  52. return;
  53. }
  54. for (auto handle : handles_) {
  55. if (handle != nullptr) {
  56. if (dlclose(handle) != 0) {
  57. GELOGW("failed to close handle, message: %s", dlerror());
  58. continue;
  59. }
  60. GELOGI("close opsprotomanager handler success");
  61. } else {
  62. GELOGW("close opsprotomanager handler failure, handler is nullptr");
  63. }
  64. }
  65. is_init_ = false;
  66. }
  67. static std::vector<std::string> Split(const std::string &str, char delim) {
  68. std::vector<std::string> elems;
  69. if (str.empty()) {
  70. elems.emplace_back("");
  71. return elems;
  72. }
  73. std::stringstream ss(str);
  74. std::string item;
  75. while (getline(ss, item, delim)) {
  76. elems.push_back(item);
  77. }
  78. auto str_size = str.size();
  79. if (str_size > 0 && str[str_size - 1] == delim) {
  80. elems.emplace_back("");
  81. }
  82. return elems;
  83. }
  84. static void FindParserSo(const std::string &path, std::vector<std::string> &file_list) {
  85. // Lib plugin path not exist
  86. if (path.empty()) {
  87. GELOGI("realPath is empty");
  88. return;
  89. }
  90. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(path.size() >= PATH_MAX, return, "path is invalid");
  91. char resolved_path[PATH_MAX] = {0};
  92. // Nullptr is returned when the path does not exist or there is no permission
  93. // Return absolute path when path is accessible
  94. if (realpath(path.c_str(), resolved_path) == nullptr) {
  95. GELOGW("the path [%s] not exsit.", path.c_str());
  96. return;
  97. }
  98. struct dirent *dent = nullptr;
  99. DIR *dir = opendir(resolved_path);
  100. // Lib plugin path not exist
  101. if (dir == nullptr) {
  102. GELOGW("Open directory %s failed,maybe it is not exit or not a dir", resolved_path);
  103. return;
  104. }
  105. while ((dent = readdir(dir)) != nullptr) {
  106. if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) {
  107. continue;
  108. }
  109. std::string name = dent->d_name;
  110. std::string full_name = path + "/" + name;
  111. const std::string so_suff = ".so";
  112. if (dent->d_type != DT_DIR && name.size() >= so_suff.size() &&
  113. name.compare(name.size() - so_suff.size(), so_suff.size(), so_suff) == 0) {
  114. file_list.push_back(full_name);
  115. GELOGI("OpsProtoManager Parse full name = %s \n", full_name.c_str());
  116. }
  117. }
  118. if (closedir(dir) != 0) {
  119. GELOGW("close dir fail.");
  120. }
  121. }
  122. static void GetPluginSoFileList(const std::string &path, std::vector<std::string> &file_list) {
  123. // Support multi lib directory with ":" as delimiter
  124. std::vector<std::string> v_path = Split(path, ':');
  125. for (size_t i = 0; i < v_path.size(); ++i) {
  126. FindParserSo(v_path[i], file_list);
  127. GELOGI("OpsProtoManager full name = %s", v_path[i].c_str());
  128. }
  129. }
  130. void OpsProtoManager::LoadOpsProtoPluginSo(std::string &path) {
  131. if (path.empty()) {
  132. GELOGE(GRAPH_FAILED, "filePath is invalid. please check your text file %s.", path.c_str());
  133. return;
  134. }
  135. std::vector<std::string> file_list;
  136. // If there is .so file in the lib path
  137. GetPluginSoFileList(path, file_list);
  138. // Not found any .so file in the lib path
  139. if (file_list.empty()) {
  140. GELOGE(GRAPH_FAILED, "OpsProtoManager can not find any plugin file in pluginPath: %s \n", path.c_str());
  141. return;
  142. }
  143. // Warning message
  144. GELOGW("The shared library will not be checked. Please ensure that the source of the shared library is trusted.");
  145. // Load .so file
  146. for (auto elem : file_list) {
  147. void *handle = dlopen(elem.c_str(), RTLD_NOW | RTLD_GLOBAL);
  148. if (handle == nullptr) {
  149. GELOGW("OpsProtoManager dlopen failed, plugin name:%s. Message(%s).", elem.c_str(), dlerror());
  150. continue;
  151. } else {
  152. // Close dl when the program exist, not close here
  153. GELOGI("OpsProtoManager plugin load %s success.", elem.c_str());
  154. handles_.push_back(handle);
  155. }
  156. }
  157. }
  158. } // namespace ge

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示