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

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

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