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.

plugin_manager.h 5.1 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #ifndef GE_COMMON_GE_PLUGIN_MANAGER_H_
  17. #define GE_COMMON_GE_PLUGIN_MANAGER_H_
  18. #include <dlfcn.h>
  19. #include <functional>
  20. #include <iostream>
  21. #include <map>
  22. #include <memory>
  23. #include <string>
  24. #include <type_traits>
  25. #include <typeinfo>
  26. #include <vector>
  27. #include "common/ge_inner_error_codes.h"
  28. #include "engine/dnnengine.h"
  29. #include "framework/common/debug/ge_log.h"
  30. namespace ge {
  31. using SoToHandleMap = std::map<std::string, void *>;
  32. using std::function;
  33. using std::map;
  34. using std::string;
  35. using std::vector;
  36. class PluginManager {
  37. public:
  38. PluginManager() = default;
  39. ~PluginManager();
  40. static string GetPath();
  41. void SplitPath(const string &mutil_path, vector<string> &path_vec);
  42. Status LoadSo(const string &path, const vector<string> &func_check_list = vector<string>());
  43. Status Load(const string &path, const vector<string> &func_check_list = vector<string>());
  44. const vector<string> &GetSoList() const;
  45. template <typename R, typename... Types>
  46. Status GetAllFunctions(const string &func_name, map<string, function<R(Types... args)>> &funcs) {
  47. for (const auto &handle : handles_) {
  48. auto real_fn = (R(*)(Types...))dlsym(handle.second, func_name.c_str());
  49. if (real_fn == nullptr) {
  50. GELOGW("Failed to get function %s in %s!", func_name.c_str(), handle.first.c_str());
  51. return GE_PLGMGR_FUNC_NOT_EXIST;
  52. } else {
  53. funcs[handle.first] = real_fn;
  54. }
  55. }
  56. return SUCCESS;
  57. }
  58. template <typename... Types>
  59. Status InvokeAll(const string &func_name, Types... args) {
  60. for (const auto &handle : handles_) {
  61. // If the funcName is existed, signature of realFn can be casted to any type
  62. auto real_fn = (void (*)(Types...))dlsym(handle.second, func_name.c_str());
  63. if (real_fn == nullptr) {
  64. GELOGW("Failed to invoke function %s in %s!", func_name.c_str(), handle.first.c_str());
  65. return GE_PLGMGR_INVOKE_FAILED;
  66. } else {
  67. real_fn(args...);
  68. }
  69. }
  70. return SUCCESS;
  71. }
  72. template <typename T>
  73. Status InvokeAll(const string &func_name, T arg) {
  74. for (const auto &handle : handles_) {
  75. // If the funcName is existed, signature of realFn can be casted to any type
  76. auto real_fn = (void (*)(T))dlsym(handle.second, func_name.c_str());
  77. if (real_fn == nullptr) {
  78. GELOGW("Failed to invoke function %s in %s!", func_name.c_str(), handle.first.c_str());
  79. return GE_PLGMGR_INVOKE_FAILED;
  80. }
  81. typename std::remove_reference<T>::type arg_temp;
  82. real_fn(arg_temp);
  83. if (std::is_same<typename std::remove_reference<T>::type, map<std::string, std::shared_ptr<DNNEngine>>>::value) {
  84. for (const auto &val : arg_temp) {
  85. if (arg.find(val.first) != arg.end()) {
  86. GELOGW("FuncName %s in so %s find the same key: %s, will replace it", func_name.c_str(),
  87. handle.first.c_str(), val.first.c_str());
  88. arg[val.first] = val.second;
  89. }
  90. }
  91. }
  92. arg.insert(arg_temp.begin(), arg_temp.end());
  93. }
  94. return SUCCESS;
  95. }
  96. template <typename T1, typename T2>
  97. Status InvokeAll(const string &func_name, T1 arg) {
  98. for (const auto &handle : handles_) {
  99. // If the funcName is existed, signature of realFn can be casted to any type
  100. auto real_fn = (T2(*)(T1))dlsym(handle.second, func_name.c_str());
  101. if (real_fn == nullptr) {
  102. GELOGW("Failed to invoke function %s in %s!", func_name.c_str(), handle.first.c_str());
  103. return GE_PLGMGR_INVOKE_FAILED;
  104. } else {
  105. T2 res = real_fn(arg);
  106. if (res != SUCCESS) {
  107. return FAILED;
  108. }
  109. }
  110. }
  111. return SUCCESS;
  112. }
  113. template <typename T>
  114. Status InvokeAll(const string &func_name) {
  115. for (const auto &handle : handles_) {
  116. // If the funcName is existed, signature of realFn can be casted to any type
  117. auto real_fn = (T(*)())dlsym(handle.second, func_name.c_str());
  118. if (real_fn == nullptr) {
  119. GELOGW("Failed to invoke function %s in %s!", func_name.c_str(), handle.first.c_str());
  120. return GE_PLGMGR_INVOKE_FAILED;
  121. } else {
  122. T res = real_fn();
  123. if (res != SUCCESS) {
  124. return FAILED;
  125. }
  126. }
  127. }
  128. return SUCCESS;
  129. }
  130. private:
  131. void ClearHandles_() noexcept;
  132. Status ValidateSo(const string &file_path, int64_t size_of_loaded_so, int64_t &file_size) const;
  133. vector<string> so_list_;
  134. SoToHandleMap handles_;
  135. };
  136. } // namespace ge
  137. #endif // GE_COMMON_GE_PLUGIN_MANAGER_H_

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