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.

dnnengine_manager.cc 34 kB

5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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 "engine_manager/dnnengine_manager.h"
  17. #include <cstdio>
  18. #include <map>
  19. #include "framework/common/debug/log.h"
  20. #include "common/ge/ge_util.h"
  21. #include "analyzer/analyzer.h"
  22. #include "graph/ge_context.h"
  23. #include "graph/utils/graph_utils.h"
  24. #include "graph/utils/node_utils.h"
  25. #include "init/gelib.h"
  26. namespace {
  27. const char *const kSchedulerUnits = "schedule_units";
  28. const char *const kId = "id";
  29. const char *const kName = "name";
  30. const char *const kExAttrs = "ex_attrs";
  31. const char *const kIndependent = "independent";
  32. const char *const kSkipAssignStream = "skip_assign_stream";
  33. const char *const kCalEngines = "cal_engines";
  34. const char *const kAttach = "attach";
  35. const char *const kVectorCore = "VectorCore";
  36. const char *const kVectorEngine = "VectorEngine";
  37. const char *const kAIcoreEngine = "AIcoreEngine";
  38. const char *const kHostCpuEngineName = "DNN_VM_HOST_CPU";
  39. const char *const kHostCpuOpKernelLibName = "DNN_VM_HOST_CPU_OP_STORE";
  40. } // namespace
  41. namespace ge {
  42. namespace {
  43. const std::set<std::string> kNotCpuOp = {DATA, CONSTANT, CONSTANTOP, VARIABLE, NETOUTPUT};
  44. const char *const kGetDNNEngineObjs = "GetDNNEngineObjs";
  45. const char *const kGetCompoundEngineContains = "GetCompoundEngineContains";
  46. const char *const kInvalidCompoundEngineName = "InvalidCompoundEngineName";
  47. constexpr uint32_t kMaxRecursiveDepth = 10;
  48. bool ExecOnHostCpu(const OpDescPtr &op_desc) {
  49. bool is_host_cpu_op = (kNotCpuOp.find(op_desc->GetType()) == kNotCpuOp.end());
  50. return ge::GetContext().GetHostExecFlag() && is_host_cpu_op;
  51. }
  52. } // namespace
  53. DNNEngineManager::DNNEngineManager() : init_flag_(false) {}
  54. DNNEngineManager::~DNNEngineManager() {
  55. engines_attrs_map_.clear();
  56. schedulers_.clear();
  57. }
  58. Status DNNEngineManager::Initialize(const std::map<std::string, std::string> &options) {
  59. // Multiple initializations are not supported
  60. if (init_flag_) {
  61. GELOGW("DNNEngineManager has been initialized.");
  62. return SUCCESS;
  63. }
  64. // Load engine so
  65. std::string plugin_so_path = "plugin/nnengine/";
  66. std::string path = PluginManager::GetPath();
  67. std::string engine_plugin_path = path + plugin_so_path;
  68. std::vector<std::string> so_func{kGetDNNEngineObjs};
  69. Status status = plugin_mgr_.Load(engine_plugin_path, so_func);
  70. if (status != SUCCESS) {
  71. GELOGE(status, "[Load][EngineSo]Failed, lib path %s", path.c_str());
  72. REPORT_CALL_ERROR("E19999", "Load engine so failed, lib path %s", engine_plugin_path.c_str());
  73. return status;
  74. }
  75. status = plugin_mgr_.InvokeAll<std::map<std::string, DNNEnginePtr> &>(kGetDNNEngineObjs, engines_map_);
  76. if (status != SUCCESS) {
  77. GELOGE(status, "[Get][DNNEngineObjs]Failed, so_api_func %s", kGetDNNEngineObjs);
  78. REPORT_CALL_ERROR("E19999", "Get DNNEngineObjs failed, so_api_func %s", kGetDNNEngineObjs);
  79. return status;
  80. }
  81. GELOGI("The number of DNNEngineObjs is %zu.", engines_map_.size());
  82. // Engines initialize
  83. for (auto iter = engines_map_.begin(); iter != engines_map_.end(); ++iter) {
  84. if (iter->second == nullptr) {
  85. GELOGI("Engine: %s point to nullptr", (iter->first).c_str());
  86. continue;
  87. }
  88. GELOGI("DNNEngine name: %s.", (iter->first).c_str());
  89. status = iter->second->Initialize(options);
  90. if (status != SUCCESS) {
  91. GELOGE(status, "[Init][Engine]Failed, engine %s", (iter->first).c_str());
  92. REPORT_CALL_ERROR("E19999", "Initialize engine %s failed", (iter->first).c_str());
  93. return status;
  94. }
  95. // Check engines' attribute
  96. DNNEngineAttribute attrs;
  97. iter->second->GetAttributes(attrs);
  98. if (attrs.runtime_type == RuntimeType::DEVICE) {
  99. if ((attrs.mem_type.size()) != 1 || (attrs.mem_type[0] != GE_ENGINE_ATTR_MEM_TYPE_HBM)) {
  100. GELOGE(GE_ENG_MEMTYPE_ERROR, "[Check][Param]Engine %s in aicore, but the memory type is "
  101. "not HBM, mem_type_size %lu", (iter->first).c_str(), attrs.mem_type.size());
  102. REPORT_INNER_ERROR("E19999", "Engine %s in aicore, but the memory type is not HBM, mem_type_size %lu",
  103. (iter->first).c_str(), attrs.mem_type.size());
  104. return GE_ENG_MEMTYPE_ERROR;
  105. }
  106. }
  107. }
  108. status = ParserJsonFile();
  109. if (status != SUCCESS) {
  110. GELOGE(status, "[Parse][JsonFile]Failed");
  111. return status;
  112. }
  113. status = CheckJsonFile();
  114. if (status != SUCCESS) {
  115. GELOGE(status, "[Check][JsonFile]Failed");
  116. return status;
  117. }
  118. init_flag_ = true;
  119. return SUCCESS;
  120. }
  121. Status DNNEngineManager::Finalize() {
  122. // Finalize is not allowed, initialize first is necessary
  123. if (!init_flag_) {
  124. GELOGW("DNNEngineManager has been finalized.");
  125. return SUCCESS;
  126. }
  127. for (auto iter = engines_map_.begin(); iter != engines_map_.end(); ++iter) {
  128. if (iter->second != nullptr) {
  129. GELOGI("DNNEngine name: %s.", (iter->first).c_str());
  130. Status status = iter->second->Finalize();
  131. if (status != SUCCESS) {
  132. GELOGE(status, "[Finalize][Engine]Failed, engine %s", (iter->first).c_str());
  133. REPORT_CALL_ERROR("E19999", "Finalize engine %s failed", (iter->first).c_str());
  134. return status;
  135. }
  136. }
  137. }
  138. init_flag_ = false;
  139. engines_map_.clear();
  140. atomic_2_compound_.clear();
  141. return SUCCESS;
  142. }
  143. std::shared_ptr<ge::DNNEngine> DNNEngineManager::GetEngine(const std::string &name) const {
  144. auto iter = engines_map_.find(name);
  145. if (iter != engines_map_.end()) {
  146. return iter->second;
  147. }
  148. GELOGW("Failed to get engine object by engine name. %s.", name.c_str());
  149. return nullptr;
  150. }
  151. bool DNNEngineManager::IsEngineRegistered(const std::string &name) {
  152. auto iter = engines_map_.find(name);
  153. if (iter != engines_map_.end()) {
  154. return true;
  155. }
  156. GELOGW("Engine: %s is not Registered", name.c_str());
  157. return false;
  158. }
  159. void DNNEngineManager::InitPerformanceStatistic() {
  160. std::lock_guard<std::mutex> lock(mutex_);
  161. checksupport_cost_.clear();
  162. }
  163. const map<string, uint64_t> &DNNEngineManager::GetCheckSupportCost() const {
  164. std::lock_guard<std::mutex> lock(mutex_);
  165. return checksupport_cost_;
  166. }
  167. std::string DNNEngineManager::GetDNNEngineName(const ge::NodePtr &node_ptr) {
  168. std::lock_guard<std::mutex> lock(mutex_);
  169. GE_IF_BOOL_EXEC(node_ptr == nullptr, GELOGE(GE_CLI_GE_NOT_INITIALIZED, "DNNEngineManager: node_ptr is nullptr");
  170. return "");
  171. auto op_desc = node_ptr->GetOpDesc();
  172. GE_IF_BOOL_EXEC(op_desc == nullptr, GELOGE(GE_CLI_GE_NOT_INITIALIZED, "DNNEngineManager: op_desc is nullptr");
  173. return "");
  174. // Use the OpsKernelManager in GELib to get the opInfos for this opCode
  175. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  176. if ((instance_ptr == nullptr) || (!instance_ptr->InitFlag())) {
  177. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Get][DNNEngineName]Failed, gelib not init before");
  178. REPORT_INNER_ERROR("E19999", "Get DNNEngineName failed, gelib not init before");
  179. return "";
  180. }
  181. OpsKernelManager &ops_kernel_manager = instance_ptr->OpsKernelManagerObj();
  182. std::vector<OpInfo> op_infos = ops_kernel_manager.GetOpsKernelInfo(op_desc->GetType());
  183. if (op_infos.empty()) {
  184. GELOGI("DNNEngineManager: Can not get op info by op type %s", op_desc->GetType().c_str());
  185. return "";
  186. }
  187. GE_IF_BOOL_EXEC(ExecOnHostCpu(op_desc), return GetHostCpuEngineName(op_infos, op_desc));
  188. std::string ge_core_type;
  189. Status ret = ge::GetContext().GetOption(ge::CORE_TYPE, ge_core_type);
  190. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGD("get the option CORE_TYPE fail, set it to default value VECTOR_ENGINE"));
  191. std::string exclude_core_Type = (ge_core_type == kVectorCore) ? kAIcoreEngine : kVectorEngine;
  192. GELOGD("engine type will exclude: %s", exclude_core_Type.c_str());
  193. std::map<std::string, std::string> unsupported_reasons;
  194. for (const auto &it : op_infos) {
  195. if (it.engine == exclude_core_Type) {
  196. continue;
  197. }
  198. const auto &kernel_name = it.opKernelLib;
  199. auto kernel_info_store = ops_kernel_manager.GetOpsKernelInfoStore(kernel_name);
  200. if (kernel_info_store == nullptr) {
  201. GELOGW("DNNEngineManager:Can not find any supported ops kernel info store by kernel_name %s, op type is %s, "
  202. "op name is %s", kernel_name.c_str(), op_desc->GetType().c_str(), op_desc->GetName().c_str());
  203. }
  204. std::string unsupported_reason;
  205. // It will be replaced by engine's check support
  206. uint64_t start_time = GetCurrentTimestamp();
  207. if (kernel_info_store->CheckSupported(node_ptr, unsupported_reason)) {
  208. checksupport_cost_[kernel_name] += GetCurrentTimestamp() - start_time;
  209. op_desc->SetOpEngineName(it.engine);
  210. op_desc->SetOpKernelLibName(kernel_name);
  211. // set attrs for taking information when load txt to graph object
  212. (void) AttrUtils::SetStr(op_desc, ATTR_NAME_ENGINE_NAME_FOR_LX, it.engine);
  213. (void) AttrUtils::SetStr(op_desc, ATTR_NAME_KKERNEL_LIB_NAME_FOR_LX, kernel_name);
  214. GELOGD("DNNEngineManager:Set kernel_lib %s, atomic engine %s, to node %s", kernel_name.c_str(), it.engine.c_str(),
  215. op_desc->GetName().c_str());
  216. return it.engine;
  217. } else {
  218. checksupport_cost_[kernel_name] += GetCurrentTimestamp() - start_time;
  219. unsupported_reasons.emplace(kernel_name, unsupported_reason);
  220. GELOGI("DNNEngineManager:Check support failed, kernel_name is %s, op type is %s, op name is %s",
  221. kernel_name.c_str(), op_desc->GetType().c_str(), op_desc->GetName().c_str());
  222. if (!op_desc->HasAttr("_is_ge_op")) {
  223. ErrorManager::GetInstance().ATCReportErrMessage("W11001", {"opname"}, {op_desc->GetName()});
  224. }
  225. }
  226. }
  227. // concat unsupported reasons analyzed data selection
  228. string reason;
  229. for (const auto &it : unsupported_reasons) {
  230. reason += it.first + ":" + it.second + ";";
  231. ErrorManager::GetInstance().ATCReportErrMessage(
  232. "E13002", {"optype", "opskernel", "reason"}, {op_desc->GetType(), it.first, it.second});
  233. GELOGE(GE_GRAPH_ASSIGN_ENGINE_FAILED, "[Check][OpSupported]Op type %s of ops kernel %s "
  234. "is unsupported, reason : %s",
  235. op_desc->GetType().c_str(), it.first.c_str(), it.second.c_str());
  236. }
  237. auto root_graph = ge::GraphUtils::FindRootGraph(node_ptr->GetOwnerComputeGraph());
  238. analyzer::DataInfo analyze_info{root_graph->GetSessionID(), root_graph->GetGraphID(),
  239. analyzer::CHECKSUPPORT, node_ptr, reason};
  240. // do not change original process
  241. (void)Analyzer::GetInstance()->DoAnalyze(analyze_info);
  242. ErrorManager::GetInstance().ATCReportErrMessage(
  243. "E13003", {"opname", "optype"}, {op_desc->GetName(), op_desc->GetType()});
  244. GELOGE(GE_GRAPH_ASSIGN_ENGINE_FAILED, "[Get][DNNEngineName]Can't find any supported ops kernel "
  245. "and engine of %s, type is %s",
  246. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  247. return "";
  248. }
  249. std::string DNNEngineManager::GetCompoundEngineName(const ge::NodePtr &node_ptr, uint32_t recursive_depth) {
  250. // op_desc of node should not be null
  251. const auto &op_desc = node_ptr->GetOpDesc();
  252. if (recursive_depth > kMaxRecursiveDepth) {
  253. REPORT_INNER_ERROR("E19999", "Get CompoundEngineName will be terminated because too many nesting levels(%d) of "
  254. "subgraphs, last node is %s", recursive_depth, op_desc->GetName().c_str());
  255. GELOGE(PARAM_INVALID,
  256. "[Check][Param] Get CompoundEngineName will be terminated because too many nesting levels(%d) of subgraphs, "
  257. "last node is %s", recursive_depth, op_desc->GetName().c_str());
  258. return "";
  259. }
  260. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  261. if ((instance_ptr == nullptr) || (!instance_ptr->InitFlag())) {
  262. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Get][CompoundEngineName]Failed, gelib not init before");
  263. REPORT_INNER_ERROR("E19999", "Get CompoundEngineName failed, gelib not init before");
  264. return "";
  265. }
  266. if (instance_ptr->OpsKernelManagerObj().GetCompoundEngineContains().empty() ||
  267. instance_ptr->OpsKernelManagerObj().GetCompoundEngineKernelLibName().empty()) {
  268. return "";
  269. }
  270. // compound engine name exist
  271. std::string compound_engine_name;
  272. (void)AttrUtils::GetStr(op_desc, ATTR_NAME_COMPOUND_ENGINE_NAME, compound_engine_name);
  273. std::string compound_engine_kernel_lib_name;
  274. (void)AttrUtils::GetStr(op_desc, ATTR_NAME_COMPOUND_ENGINE_KERNEL_LIB_NAME, compound_engine_kernel_lib_name);
  275. if (!(compound_engine_name.empty() || compound_engine_kernel_lib_name.empty())) {
  276. return compound_engine_name;
  277. }
  278. // normal node without subgraph
  279. if (op_desc->GetSubgraphInstanceNames().empty()) {
  280. return GetCompoundEngineForNormalNode(node_ptr);
  281. }
  282. return GetCompoundEngineForFuncNode(node_ptr, recursive_depth);
  283. }
  284. std::string DNNEngineManager::GetCompoundEngineForNormalNode(const NodePtr &node) {
  285. // op_desc of node should not be null
  286. const auto &op_desc = node->GetOpDesc();
  287. auto atomic_engine_name = op_desc->GetOpEngineName();
  288. if (atomic_engine_name.empty()) {
  289. atomic_engine_name = GetDNNEngineName(node);
  290. }
  291. bool gelocal_follow_flag = false;
  292. if (IsStreamAssignSkip(atomic_engine_name)) {
  293. bool in_diff_flag = false;
  294. std::string in_compound_engine_name = kInvalidCompoundEngineName;
  295. for (const auto &in_node : node->GetInAllNodes()) {
  296. std::string tmp_compound_engine_name;
  297. (void)AttrUtils::GetStr(in_node->GetOpDesc(), ATTR_NAME_COMPOUND_ENGINE_NAME, tmp_compound_engine_name);
  298. if (in_compound_engine_name == kInvalidCompoundEngineName) {
  299. in_compound_engine_name = tmp_compound_engine_name;
  300. } else if (in_compound_engine_name != tmp_compound_engine_name) {
  301. in_diff_flag = true;
  302. break;
  303. }
  304. }
  305. if (!(in_diff_flag ||
  306. (in_compound_engine_name == kInvalidCompoundEngineName) ||
  307. in_compound_engine_name.empty())) {
  308. gelocal_follow_flag = true;
  309. }
  310. }
  311. std::string compound_engine_name;
  312. if (!gelocal_follow_flag) {
  313. compound_engine_name = GetOwningCompoundEngine(atomic_engine_name);
  314. }
  315. const auto &compound_engine_kernel_lib_name = GetCompoundEngineKernelLibName(compound_engine_name);
  316. if (compound_engine_name.empty() || compound_engine_kernel_lib_name.empty()) {
  317. (void)op_desc->DelAttr(ATTR_NAME_COMPOUND_ENGINE_NAME);
  318. (void)op_desc->DelAttr(ATTR_NAME_COMPOUND_ENGINE_KERNEL_LIB_NAME);
  319. } else {
  320. GELOGI("Assign compound engine %s, kernel lib name %s for node %s.",
  321. compound_engine_name.c_str(), compound_engine_kernel_lib_name.c_str(), op_desc->GetName().c_str());
  322. (void)AttrUtils::SetStr(op_desc, ATTR_NAME_COMPOUND_ENGINE_NAME, compound_engine_name);
  323. (void)AttrUtils::SetStr(op_desc, ATTR_NAME_COMPOUND_ENGINE_KERNEL_LIB_NAME, compound_engine_kernel_lib_name);
  324. }
  325. return compound_engine_name;
  326. }
  327. std::string DNNEngineManager::GetCompoundEngineForFuncNode(const NodePtr &node, uint32_t recursive_depth) {
  328. // op_desc of node should not be null
  329. const auto &op_desc = node->GetOpDesc();
  330. bool graph_diff_compound_engine_flag = false;
  331. std::string graph_compound_engine_name = kInvalidCompoundEngineName;
  332. std::vector<ComputeGraphPtr> subgraphs;
  333. if (NodeUtils::GetSubgraphs(node, subgraphs) != GRAPH_SUCCESS) {
  334. REPORT_CALL_ERROR("E19999", "Get subgraphs of node %s failed", op_desc->GetName().c_str());
  335. GELOGE(FAILED, "[Check][Param] Get subgraphs of node %s failed", op_desc->GetName().c_str());
  336. return "";
  337. }
  338. for (const auto &subgraph : subgraphs) {
  339. std::string cur_graph_compound_engine_name = GetCompoundEngineForSubgraph(subgraph, recursive_depth);
  340. if (graph_compound_engine_name == kInvalidCompoundEngineName) {
  341. graph_compound_engine_name = cur_graph_compound_engine_name;
  342. } else if (graph_compound_engine_name != cur_graph_compound_engine_name) {
  343. graph_diff_compound_engine_flag = true;
  344. break;
  345. }
  346. }
  347. std::string compound_engine_name;
  348. std::string compound_engine_kernel_lib_name = GetCompoundEngineKernelLibName(graph_compound_engine_name);
  349. if (!(graph_diff_compound_engine_flag || (graph_compound_engine_name == kInvalidCompoundEngineName) ||
  350. graph_compound_engine_name.empty() || compound_engine_kernel_lib_name.empty())) {
  351. compound_engine_name = graph_compound_engine_name;
  352. GELOGI("Assign compound engine %s, kernel lib name %s for node %s.",
  353. compound_engine_name.c_str(), compound_engine_kernel_lib_name.c_str(), op_desc->GetName().c_str());
  354. (void)AttrUtils::SetStr(op_desc, ATTR_NAME_COMPOUND_ENGINE_NAME, compound_engine_name);
  355. (void)AttrUtils::SetStr(op_desc, ATTR_NAME_COMPOUND_ENGINE_KERNEL_LIB_NAME, compound_engine_kernel_lib_name);
  356. } else {
  357. (void)op_desc->DelAttr(ATTR_NAME_COMPOUND_ENGINE_NAME);
  358. (void)op_desc->DelAttr(ATTR_NAME_COMPOUND_ENGINE_KERNEL_LIB_NAME);
  359. }
  360. return compound_engine_name;
  361. }
  362. std::string DNNEngineManager::GetCompoundEngineForSubgraph(const ComputeGraphPtr &subgraph, uint32_t recursive_depth) {
  363. std::string graph_compound_engine_name;
  364. // if subgraph has been assigned
  365. if (subgraph->HasAttr(ATTR_NAME_COMPOUND_ENGINE_NAME)) {
  366. (void)AttrUtils::GetStr(subgraph, ATTR_NAME_COMPOUND_ENGINE_NAME, graph_compound_engine_name);
  367. } else {
  368. bool node_diff_compound_engine_flag = false;
  369. std::string node_compound_engine_name = kInvalidCompoundEngineName;
  370. uint32_t assign_node_num = 0;
  371. for (const auto &cur_node : subgraph->GetDirectNode()) {
  372. if (IsStreamAssignSkip(cur_node) && cur_node->GetOpDesc()->GetSubgraphInstanceNames().empty()) {
  373. continue;
  374. }
  375. assign_node_num++;
  376. std::string cur_node_compound_engine_name = GetCompoundEngineName(cur_node, recursive_depth + 1);
  377. if (node_compound_engine_name == kInvalidCompoundEngineName) {
  378. node_compound_engine_name = cur_node_compound_engine_name;
  379. } else if (node_compound_engine_name != cur_node_compound_engine_name) {
  380. node_diff_compound_engine_flag = true;
  381. break;
  382. }
  383. }
  384. if (assign_node_num == 0) {
  385. GELOGD("all nodes in subgraph %s belongs to ge_local engine", subgraph->GetName().c_str());
  386. return "";
  387. }
  388. if (!(node_diff_compound_engine_flag ||
  389. (node_compound_engine_name == kInvalidCompoundEngineName) ||
  390. node_compound_engine_name.empty())) {
  391. GELOGI("Assign compound engine %s for subgraph %s.", node_compound_engine_name.c_str(), subgraph->GetName().c_str());
  392. (void)AttrUtils::SetStr(subgraph, ATTR_NAME_COMPOUND_ENGINE_NAME, node_compound_engine_name);
  393. graph_compound_engine_name = node_compound_engine_name;
  394. }
  395. else {
  396. (void)subgraph->DelAttr(ATTR_NAME_COMPOUND_ENGINE_NAME);
  397. }
  398. }
  399. return graph_compound_engine_name;
  400. }
  401. std::string DNNEngineManager::GetOwningCompoundEngine(const string &atomic_engine_name) {
  402. if (atomic_2_compound_.empty()) {
  403. InitAtomicCompoundMapping();
  404. }
  405. const auto &iter = atomic_2_compound_.find(atomic_engine_name);
  406. if (iter == atomic_2_compound_.end()) {
  407. GELOGW("Compound engine which contains atomic engine %s is not registered", atomic_engine_name.c_str());
  408. return "";
  409. }
  410. return iter->second;
  411. }
  412. std::string DNNEngineManager::GetCompoundEngineKernelLibName(const string &compound_engine_name) const {
  413. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  414. if ((instance_ptr == nullptr) || (!instance_ptr->InitFlag())) {
  415. GELOGW("[Get][CompoundEngineKernelLibName]Failed, gelib not init before");
  416. return "";
  417. }
  418. const auto &compound_engine_2_kernel_lib_name = instance_ptr->OpsKernelManagerObj().GetCompoundEngineKernelLibName();
  419. const auto &iter = compound_engine_2_kernel_lib_name.find(compound_engine_name);
  420. if (iter == compound_engine_2_kernel_lib_name.end()) {
  421. GELOGW("Kernel lib name of compound engine %s is not registered", compound_engine_name.c_str());
  422. return "";
  423. }
  424. return iter->second;
  425. }
  426. std::string DNNEngineManager::GetHostCpuEngineName(const std::vector<OpInfo> &op_infos,
  427. const OpDescPtr &op_desc) const {
  428. for (const auto &it : op_infos) {
  429. if ((it.engine == kHostCpuEngineName) && (it.opKernelLib == kHostCpuOpKernelLibName)) {
  430. op_desc->SetOpEngineName(kHostCpuEngineName);
  431. op_desc->SetOpKernelLibName(kHostCpuOpKernelLibName);
  432. GELOGI("DNNEngineManager: Set OpKernelLibName %s and OpEngineName %s to %s",
  433. kHostCpuOpKernelLibName, kHostCpuEngineName, op_desc->GetName().c_str());
  434. return kHostCpuEngineName;
  435. }
  436. }
  437. GELOGE(FAILED, "[Get][HostCpuEngineName]Failed, HostCpuEngine not support [%s, %s]",
  438. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  439. REPORT_INNER_ERROR("E19999", "Get HostCpuEngineName failed, HostCpuEngine not support [%s, %s]",
  440. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  441. return "";
  442. }
  443. const std::map<std::string, SchedulerConf> &DNNEngineManager::GetSchedulers() const { return schedulers_; }
  444. Status DNNEngineManager::ParserJsonFile() {
  445. GELOGI("Begin to parser json file");
  446. std::string json_file_path = "plugin/nnengine/ge_config/engine_conf.json";
  447. std::string path = PluginManager::GetPath();
  448. path.append(json_file_path);
  449. nlohmann::json scheduler_json_file;
  450. Status status = ReadJsonFile(path, &scheduler_json_file);
  451. if (status != SUCCESS) {
  452. GELOGE(FAILED, "[Read][JsonFile]Failed, file %s", path.c_str());
  453. REPORT_CALL_ERROR("E19999", "Read json file %s failed", path.c_str());
  454. return FAILED;
  455. }
  456. if (scheduler_json_file.is_null()) {
  457. // when engine_conf.json is not exist, just return success
  458. GELOGW("Json file is null");
  459. return SUCCESS;
  460. }
  461. try {
  462. nlohmann::json scheduler_utils_json = scheduler_json_file[kSchedulerUnits];
  463. if (scheduler_utils_json.is_null()) {
  464. GELOGE(FAILED, "[Check[Param]Find scheduler units failed, the message is null, file %s", path.c_str());
  465. REPORT_INNER_ERROR("E19999", "Find scheduler units failed, the message is null, file %s", path.c_str());
  466. return FAILED;
  467. }
  468. if (!scheduler_utils_json.is_array()) {
  469. GELOGE(FAILED, "[Check][Param]The message of kSchedulerUnits is not array and "
  470. "the file path is %s", path.c_str());
  471. REPORT_INNER_ERROR("E19999", "The message of kSchedulerUnits is not array and "
  472. "the file path is %s", path.c_str());
  473. return FAILED;
  474. }
  475. auto size = scheduler_json_file[kSchedulerUnits].size();
  476. for (size_t i = 0; i < size; i++) {
  477. SchedulerConf scheduler_conf;
  478. std::map<std::string, EngineConfPtr> engine_conf_map;
  479. nlohmann::json engines_json_map = scheduler_utils_json[i][kCalEngines];
  480. if (engines_json_map.is_null()) {
  481. GELOGE(FAILED, "[Check][Param]The message of cal_engines is null, file %s", path.c_str());
  482. REPORT_INNER_ERROR("E19999", "The message of cal_engines is null, file %s", path.c_str());
  483. return FAILED;
  484. }
  485. std::string scheduler_id_temp = scheduler_utils_json[i][kId];
  486. if (!scheduler_id_temp.empty()) {
  487. scheduler_conf.id = scheduler_id_temp;
  488. } else {
  489. GELOGE(FAILED, "[Check][Param]Scheduler ID is null, file %s", path.c_str());
  490. REPORT_INNER_ERROR("E19999", "Scheduler ID is null, file %s", path.c_str());
  491. return FAILED;
  492. }
  493. status = ParserEngineMessage(engines_json_map, scheduler_id_temp, engine_conf_map);
  494. if (status != SUCCESS) {
  495. GELOGE(FAILED, "[Parse][EngineMessage]Failed, scheduler_id_temp %s", scheduler_id_temp.c_str());
  496. REPORT_CALL_ERROR("E19999", "Parse engine message failed, scheduler_id_temp %s",
  497. scheduler_id_temp.c_str());
  498. return FAILED;
  499. }
  500. scheduler_conf.name = scheduler_utils_json[i][kName];
  501. scheduler_conf.ex_attrs = scheduler_utils_json[i][kExAttrs];
  502. scheduler_conf.cal_engines = engine_conf_map;
  503. auto it = schedulers_.find(scheduler_id_temp);
  504. if (it != schedulers_.end()) {
  505. GELOGE(FAILED, "[Check][Param]There are the same scheduler ts %s in the json file",
  506. scheduler_id_temp.c_str());
  507. REPORT_INNER_ERROR("E19999", "[Check][Param]There are the same scheduler ts %s "
  508. "in the json file", scheduler_id_temp.c_str());
  509. return FAILED;
  510. }
  511. schedulers_.emplace(scheduler_id_temp, scheduler_conf);
  512. }
  513. } catch (const nlohmann::detail::type_error &e) {
  514. GELOGE(FAILED, "[Parse][JsonFile]Failed, file %s, reason %s", path.c_str(), e.what());
  515. REPORT_CALL_ERROR("E19999", "Parse json file %s failed, reason %s", path.c_str(), e.what());
  516. return FAILED;
  517. }
  518. GELOGI("Parser json file SUCCESS");
  519. return SUCCESS;
  520. }
  521. Status DNNEngineManager::ParserEngineMessage(const json engines_json, const std::string &scheduler_mark,
  522. std::map<std::string, EngineConfPtr> &engines) {
  523. GELOGI("Begin to parser engine massage");
  524. if (engines_json.is_null()) {
  525. GELOGE(FAILED, "[Check][Param]The message of cal_engines is null");
  526. REPORT_INNER_ERROR("E19999", "The message of cal_engines is null");
  527. return FAILED;
  528. }
  529. try {
  530. if (engines_json.is_array()) {
  531. for (size_t i = 0; i < engines_json.size(); i++) {
  532. nlohmann::json engines_elems = engines_json[i];
  533. EngineConfPtr engine_conf_ptr = MakeShared<EngineConf>();
  534. if (engine_conf_ptr == nullptr) {
  535. return FAILED;
  536. }
  537. std::string engine_id = engines_elems[kId];
  538. if (!engine_id.empty()) {
  539. engine_conf_ptr->id = engine_id;
  540. } else {
  541. GELOGE(FAILED, "[Check][Param]Engine ID is null");
  542. REPORT_INNER_ERROR("E19999", "Engine ID is null");
  543. return FAILED;
  544. }
  545. if (engines_elems.find(kName) != engines_elems.end()) {
  546. engine_conf_ptr->name = engines_elems[kName];
  547. } else {
  548. GELOGW("The engine %s name is null", engine_id.c_str());
  549. }
  550. if (engines_elems.find(kIndependent) != engines_elems.end()) {
  551. engine_conf_ptr->independent = engines_elems[kIndependent];
  552. }
  553. if (engines_elems.find(kAttach) != engines_elems.end()) {
  554. engine_conf_ptr->attach = engines_elems[kAttach];
  555. }
  556. if (engines_elems.find(kSkipAssignStream) != engines_elems.end()) {
  557. engine_conf_ptr->skip_assign_stream = engines_elems[kSkipAssignStream];
  558. }
  559. engine_conf_ptr->scheduler_id = scheduler_mark;
  560. auto it = engines.find(engine_id);
  561. if (it != engines.end()) {
  562. GELOGE(FAILED, "[Check][Param]There are the same engine %s message in the json file",
  563. engine_id.c_str());
  564. REPORT_INNER_ERROR("E19999", "There are the same engine %s message in the json file",
  565. engine_id.c_str());
  566. return FAILED;
  567. }
  568. engines.emplace(engine_id, engine_conf_ptr);
  569. }
  570. } else {
  571. GELOGE(FAILED, "[Check][Param]The message of cal_engines is not array in the json file");
  572. REPORT_INNER_ERROR("E19999", "The message of cal_engines is not array in the json file");
  573. return FAILED;
  574. }
  575. } catch (const json::exception &e) {
  576. GELOGE(FAILED, "[Construct][JsonContent]Failed, reason %s", e.what());
  577. REPORT_INNER_ERROR("E19999", "Construct json content failed, reason %s", e.what());
  578. return FAILED;
  579. }
  580. GELOGI("Parser engine massage success");
  581. return SUCCESS;
  582. }
  583. Status DNNEngineManager::ReadJsonFile(const std::string &file_path, JsonHandle handle) {
  584. GELOGD("Begin to read json file");
  585. if (file_path.empty()) {
  586. GELOGE(FAILED, "[Check][Param]Json path is empty");
  587. REPORT_INNER_ERROR("E19999", "Json path is empty");
  588. return FAILED;
  589. }
  590. nlohmann::json *json_file = reinterpret_cast<nlohmann::json *>(handle);
  591. if (json_file == nullptr) {
  592. GELOGE(FAILED, "[Check][Param]Json file is nullptr");
  593. REPORT_CALL_ERROR("E19999", "Json file is nullptr");
  594. return FAILED;
  595. }
  596. const char *file = file_path.data();
  597. if ((mmAccess2(file, M_F_OK)) != EN_OK) {
  598. if (engines_map_.size() != 0) {
  599. GELOGE(FAILED, "[Check][Param]The json file %s not exists, err %s",
  600. file_path.c_str(), strerror(errno));
  601. REPORT_CALL_ERROR("E19999", "Json file %s not exists, err %s",
  602. file_path.c_str(), strerror(errno));
  603. return FAILED;
  604. } else {
  605. GELOGW("The json file %s is not needed.", file_path.c_str());
  606. return SUCCESS;
  607. }
  608. }
  609. std::ifstream ifs(file_path);
  610. if (!ifs.is_open()) {
  611. GELOGE(FAILED, "[Open][JsonFile]Failed, file %s", file_path.c_str());
  612. REPORT_CALL_ERROR("E19999", "Open json file %s failed", file_path.c_str());
  613. return FAILED;
  614. }
  615. try {
  616. ifs >> *json_file;
  617. } catch (const json::exception &e) {
  618. GELOGE(FAILED, "[Read][JsonFile]Failed, reason %s", e.what());
  619. REPORT_CALL_ERROR("E19999", "Read json file failed, reason %s", e.what());
  620. ifs.close();
  621. return FAILED;
  622. }
  623. ifs.close();
  624. GELOGD("Read json file success");
  625. return SUCCESS;
  626. }
  627. Status DNNEngineManager::CheckJsonFile() {
  628. GELOGD("Begin to check json file");
  629. for (auto &it : engines_map_) {
  630. if (!it.second->IsAtomic()) {
  631. continue;
  632. }
  633. std::string engine_name = it.first;
  634. int count = 0;
  635. for (auto &iter : schedulers_) {
  636. auto engine_map = iter.second.cal_engines;
  637. auto iter_engine_name = engine_map.find(engine_name);
  638. if (iter_engine_name != engine_map.end()) {
  639. count++;
  640. }
  641. }
  642. if (count == 0) {
  643. GELOGE(FAILED, "[Check][JsonFile]The engine message %s is not found in the json file",
  644. engine_name.c_str());
  645. REPORT_INNER_ERROR("E19999", "The engine message %s is not found in the json file",
  646. engine_name.c_str());
  647. return FAILED;
  648. }
  649. if (count > 1) {
  650. GELOGE(FAILED, "[Check][JsonFile]The same engine message %s exists in the json file",
  651. engine_name.c_str());
  652. REPORT_INNER_ERROR("E19999", "The same engine message %s exists in the json file",
  653. engine_name.c_str());
  654. return FAILED;
  655. }
  656. }
  657. GELOGD("Check json file success");
  658. return SUCCESS;
  659. }
  660. void DNNEngineManager::InitAtomicCompoundMapping() {
  661. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  662. if ((instance_ptr == nullptr) || (!instance_ptr->InitFlag())) {
  663. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Get][CompoundEngineName]Failed, gelib not init before");
  664. REPORT_INNER_ERROR("E19999", "Get CompoundEngineName failed, gelib not init before");
  665. return;
  666. }
  667. const auto &compound_engine_2_kernel_lib_name = instance_ptr->OpsKernelManagerObj().GetCompoundEngineKernelLibName();
  668. for (const auto &item : instance_ptr->OpsKernelManagerObj().GetCompoundEngineContains()) {
  669. const auto &compound_engine = GetEngine(item.first);
  670. if ((compound_engine == nullptr) || compound_engine->IsAtomic()) {
  671. GELOGW("Compound engine %s is not registered", item.first.c_str());
  672. }
  673. const auto &iter = compound_engine_2_kernel_lib_name.find(item.first);
  674. if ((iter == compound_engine_2_kernel_lib_name.end()) || iter->second.empty()) {
  675. GELOGW("Kernel lib name of compound engine %s is empty", item.first.c_str());
  676. }
  677. for (const auto &atomic_engine_name : item.second) {
  678. const auto &atomic_engine = GetEngine(atomic_engine_name);
  679. if ((atomic_engine == nullptr) || !atomic_engine->IsAtomic()) {
  680. GELOGW("Atomic engine %s is not registered", atomic_engine_name.c_str());
  681. continue;
  682. }
  683. auto iter = atomic_2_compound_.find(atomic_engine_name);
  684. if (iter != atomic_2_compound_.end()) {
  685. GELOGW("Atomic engine %s has been contained in compound engine %s, and will be overwritten by engine %s",
  686. atomic_engine_name.c_str(), iter->second.c_str(), item.first.c_str());
  687. }
  688. atomic_2_compound_[atomic_engine_name] = item.first;
  689. }
  690. }
  691. }
  692. bool DNNEngineManager::IsStreamAssignSkip(const NodePtr &node) {
  693. const auto &op_desc = node->GetOpDesc();
  694. if (op_desc == nullptr) {
  695. return false;
  696. }
  697. std::string engine_name = op_desc->GetOpEngineName();
  698. if (engine_name.empty()) {
  699. engine_name = GetDNNEngineName(node);
  700. }
  701. return IsStreamAssignSkip(engine_name);
  702. }
  703. bool DNNEngineManager::IsStreamAssignSkip(const string &engine_name) {
  704. // Only one scheduler has been supported by now
  705. for (const auto &scheduler : schedulers_) {
  706. const map<string, EngineConfPtr> cal_engines = scheduler.second.cal_engines;
  707. auto cal_engines_iter = cal_engines.find(engine_name);
  708. if (cal_engines_iter == cal_engines.end()) {
  709. GELOGW("No cal_engines found within engine %s", engine_name.c_str());
  710. continue;
  711. }
  712. EngineConfPtr engine_conf_ptr = cal_engines_iter->second;
  713. if (engine_conf_ptr == nullptr) {
  714. GELOGW("engine_conf_ptr within engine %s is null", engine_name.c_str());
  715. continue;
  716. }
  717. return engine_conf_ptr->skip_assign_stream;
  718. }
  719. return false;
  720. }
  721. } // namespace ge

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