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.

tbe_plugin_manager.cc 8.9 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 "common/ge/tbe_plugin_manager.h"
  17. #include <algorithm>
  18. #include <cstring>
  19. #include <fstream>
  20. #include <iostream>
  21. #include <map>
  22. #include <memory>
  23. #include <string>
  24. #include "common/ge/ge_util.h"
  25. #include "framework/common/debug/log.h"
  26. #include "framework/common/debug/ge_log.h"
  27. #include "framework/common/util.h"
  28. #include "framework/common/ge_inner_error_codes.h"
  29. #include "framework/engine/dnnengine.h"
  30. #include "framework/omg/omg_inner_types.h"
  31. #include "external/ge/ge_api_types.h"
  32. #include "register/op_registry.h"
  33. #include "graph/opsproto_manager.h"
  34. #include "graph/utils/type_utils.h"
  35. namespace ge {
  36. const int kBaseInt = 10;
  37. std::map<string, string> TBEPluginManager::options_ = {};
  38. // Get Singleton Instance
  39. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY TBEPluginManager &TBEPluginManager::Instance() {
  40. static TBEPluginManager instance_ptr_;
  41. return instance_ptr_;
  42. }
  43. Status TBEPluginManager::ClearHandles_() {
  44. Status ret = SUCCESS;
  45. for (const auto &handle : handles_vec_) {
  46. if (mmDlclose(handle) != 0) {
  47. ret = FAILED;
  48. const char *error = mmDlerror();
  49. GE_IF_BOOL_EXEC(error == nullptr, error = "");
  50. GELOGW("Failed to close handle: %s", error);
  51. }
  52. }
  53. handles_vec_.clear();
  54. return ret;
  55. }
  56. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status TBEPluginManager::Finalize() {
  57. Status ret = ClearHandles_();
  58. return ret;
  59. }
  60. string TBEPluginManager::GetPath() {
  61. mmDlInfo dl_info;
  62. if (mmDladdr(reinterpret_cast<void *>(&TBEPluginManager::GetPath), &dl_info) != EN_OK) {
  63. GELOGW("Failed to read so path!");
  64. return string();
  65. } else {
  66. string so_path = dl_info.dli_fname;
  67. char path[MMPA_MAX_PATH] = {0};
  68. if (so_path.length() >= MMPA_MAX_PATH) {
  69. GELOGW("File path is too long!");
  70. return string();
  71. }
  72. if (mmRealPath(so_path.c_str(), path, MMPA_MAX_PATH) != EN_OK) {
  73. GELOGW("Failed to get realpath of %s", so_path.c_str());
  74. return string();
  75. }
  76. so_path = path;
  77. so_path = so_path.substr(0, so_path.rfind('/') + 1);
  78. return so_path;
  79. }
  80. }
  81. void TBEPluginManager::ProcessSoFullName(vector<string> &file_list, string &caffe_parser_path, string &full_name,
  82. const string &caffe_parser_so_suff, const string &aicpu_so_suff,
  83. const string &aicpu_host_so_suff) {
  84. if (full_name.size() >= caffe_parser_so_suff.size() &&
  85. full_name.compare(full_name.size() - caffe_parser_so_suff.size(), caffe_parser_so_suff.size(),
  86. caffe_parser_so_suff) == 0) {
  87. caffe_parser_path = full_name;
  88. } else {
  89. // Save parser so path into file_list vector
  90. file_list.push_back(full_name);
  91. }
  92. }
  93. void TBEPluginManager::FindParserSo(const string &path, vector<string> &file_list, string &caffe_parser_path) {
  94. // Path, change to absolute path
  95. string real_path = RealPath(path.c_str());
  96. // Plugin path does not exist
  97. if (real_path.empty()) {
  98. GELOGW("RealPath is empty.");
  99. return;
  100. }
  101. INT32 is_dir = mmIsDir(real_path.c_str());
  102. // Lib plugin path not exist
  103. if (is_dir != EN_OK) {
  104. GELOGW("%s is not a dir.", real_path.c_str());
  105. return;
  106. }
  107. mmDirent **entries = nullptr;
  108. auto ret = mmScandir(real_path.c_str(), &entries, nullptr, nullptr);
  109. if (ret < EN_OK) {
  110. GELOGW("scan dir failed. path = %s, ret = %d", real_path.c_str(), ret);
  111. return;
  112. }
  113. for (int i = 0; i < ret; ++i) {
  114. mmDirent *dent = entries[i];
  115. if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) continue;
  116. string name = dent->d_name;
  117. string full_name = real_path + "/" + name;
  118. const string so_suff = ".so";
  119. const string caffe_parser_so_suff = "lib_caffe_parser.so";
  120. const string aicpu_so_suff = "_aicpu.so";
  121. const string aicpu_host_so_suff = "_online.so";
  122. if (name.size() >= so_suff.size() && name.compare(name.size() - so_suff.size(), so_suff.size(), so_suff) == 0) {
  123. ProcessSoFullName(file_list, caffe_parser_path, full_name, caffe_parser_so_suff, aicpu_so_suff,
  124. aicpu_host_so_suff);
  125. } else {
  126. FindParserSo(full_name, file_list, caffe_parser_path);
  127. }
  128. }
  129. mmScandirFree(entries, ret);
  130. }
  131. void TBEPluginManager::GetPluginSoFileList(const string &path, vector<string> &file_list, string &caffe_parser_path) {
  132. // Support to split multiple so directories by ":"
  133. vector<string> v_path = StringUtils::Split(path, ':');
  134. for (size_t i = 0; i < v_path.size(); ++i) {
  135. FindParserSo(v_path[i], file_list, caffe_parser_path);
  136. GELOGI("CustomOpLib full name = %s", v_path[i].c_str());
  137. }
  138. }
  139. void TBEPluginManager::GetCustomOpPath(std::string &customop_path) {
  140. GELOGI("Enter get custom op path schedule");
  141. std::string fmk_type;
  142. domi::FrameworkType type = domi::TENSORFLOW;
  143. auto it = options_.find(FRAMEWORK_TYPE);
  144. if (it != options_.end()) {
  145. type = static_cast<domi::FrameworkType>(std::strtol(it->second.c_str(), nullptr, kBaseInt));
  146. }
  147. fmk_type = ge::TypeUtils::FmkTypeToSerialString(type);
  148. GELOGI("Framework type is %s.", fmk_type.c_str());
  149. char path_env[MMPA_MAX_PATH] = { 0x00 };
  150. INT32 res = mmGetEnv("ASCEND_OPP_PATH", path_env, MMPA_MAX_PATH);
  151. if (res == EN_OK) {
  152. std::string path = path_env;
  153. customop_path = (path + "/framework/custom" + "/:") + (path + "/framework/built-in/" + fmk_type);
  154. GELOGI("Get custom so path from env : %s", path_env);
  155. return;
  156. }
  157. std::string path_base = GetPath();
  158. GELOGI("path_base is %s", path_base.c_str());
  159. path_base = path_base.substr(0, path_base.rfind('/'));
  160. path_base = path_base.substr(0, path_base.rfind('/') + 1);
  161. customop_path = (path_base + "ops/framework/custom" + "/:") + (path_base + "ops/framework/built-in/" + fmk_type);
  162. return;
  163. }
  164. void TBEPluginManager::LoadCustomOpLib() {
  165. LoadPluginSo(options_);
  166. std::string fmk_type = std::to_string(domi::TENSORFLOW);
  167. auto it = options_.find(ge::FRAMEWORK_TYPE);
  168. if (it != options_.end()) {
  169. fmk_type = it->second;
  170. }
  171. std::vector<OpRegistrationData> registration_datas = domi::OpRegistry::Instance()->registrationDatas;
  172. GELOGI("The size of registration_datas is: %zu", registration_datas.size());
  173. for (OpRegistrationData reg_data : registration_datas) {
  174. if (std::to_string(reg_data.GetFrameworkType()) == fmk_type) {
  175. GELOGD("Begin to register optype: %s, imply_type: %s", reg_data.GetOmOptype().c_str(),
  176. TypeUtils::ImplyTypeToSerialString(reg_data.GetImplyType()).c_str());
  177. (void)domi::OpRegistry::Instance()->Register(reg_data);
  178. }
  179. }
  180. }
  181. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY
  182. void TBEPluginManager::LoadPluginSo(const std::map<string, string> &options) {
  183. vector<string> file_list;
  184. string caffe_parser_path;
  185. std::string plugin_path;
  186. options_ = options;
  187. GetCustomOpPath(plugin_path);
  188. // Whether there are files in the plugin so path
  189. GetPluginSoFileList(plugin_path, file_list, caffe_parser_path);
  190. // No file
  191. if (file_list.empty()) {
  192. // Print log
  193. GELOGW("Can not find any plugin file in plugin_path: %s", plugin_path.c_str());
  194. }
  195. GELOGW("The shared library will not be checked. Please ensure that the source of the shared library is trusted.");
  196. // Load other so files except lib_caffe_parser.so in the plugin so path
  197. for (auto elem : file_list) {
  198. StringUtils::Trim(elem);
  199. void *handle = mmDlopen(elem.c_str(), MMPA_RTLD_NOW | MMPA_RTLD_GLOBAL | MMPA_RTLD_NODELETE);
  200. if (handle == nullptr) {
  201. const char *error = mmDlerror();
  202. GE_IF_BOOL_EXEC(error == nullptr, error = "");
  203. GELOGW("dlopen failed, plugin name:%s. Message(%s).", elem.c_str(), error);
  204. } else if (find(handles_vec_.begin(), handles_vec_.end(), handle) == handles_vec_.end()) {
  205. // Close dl when the program exist, not close here
  206. GELOGI("Plugin load %s success.", elem.c_str());
  207. handles_vec_.push_back(handle);
  208. } else {
  209. GELOGI("Plugin so has already been loaded, no need to load again.");
  210. }
  211. }
  212. }
  213. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY
  214. void TBEPluginManager::InitPreparation(const std::map<string, string> &options) {
  215. options_.insert(options.begin(), options.end());
  216. // Load TBE plugin
  217. TBEPluginManager::Instance().LoadCustomOpLib();
  218. }
  219. } // namespace ge

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