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.9 kB

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

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