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 32 kB

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
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
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
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. if ((node_ptr == nullptr) || (node_ptr->GetOpDesc() == nullptr)) {
  251. return "";
  252. }
  253. const auto &op_desc = node_ptr->GetOpDesc();
  254. if (recursive_depth > kMaxRecursiveDepth) {
  255. REPORT_INNER_ERROR("E19999", "Get CompoundEngineName will be terminated because too many nesting levels(%d) of "
  256. "subgraphs, last node is %s", recursive_depth, op_desc->GetName().c_str());
  257. GELOGE(PARAM_INVALID,
  258. "[Check][Param] Get CompoundEngineName will be terminated because too many nesting levels(%d) of subgraphs, "
  259. "last node is %s", recursive_depth, op_desc->GetName().c_str());
  260. return "";
  261. }
  262. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  263. if ((instance_ptr == nullptr) || (!instance_ptr->InitFlag())) {
  264. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Get][CompoundEngineName]Failed, gelib not init before");
  265. REPORT_INNER_ERROR("E19999", "Get CompoundEngineName failed, gelib not init before");
  266. return "";
  267. }
  268. if (instance_ptr->OpsKernelManagerObj().GetCompoundEngineContains().empty() ||
  269. instance_ptr->OpsKernelManagerObj().GetCompoundEngineKernelLibName().empty()) {
  270. return "";
  271. }
  272. // compound engine name exist
  273. std::string compound_engine_name;
  274. (void)AttrUtils::GetStr(op_desc, ATTR_NAME_COMPOUND_ENGINE_NAME, compound_engine_name);
  275. std::string compound_engine_kernel_lib_name;
  276. (void)AttrUtils::GetStr(op_desc, ATTR_NAME_COMPOUND_ENGINE_KERNEL_LIB_NAME, compound_engine_kernel_lib_name);
  277. if (!(compound_engine_name.empty() || compound_engine_kernel_lib_name.empty())) {
  278. return compound_engine_name;
  279. }
  280. // normal node without subgraph
  281. if (op_desc->GetSubgraphInstanceNames().empty()) {
  282. auto atomic_engine_name = op_desc->GetOpEngineName();
  283. if (atomic_engine_name.empty()) {
  284. atomic_engine_name = GetDNNEngineName(node_ptr);
  285. }
  286. compound_engine_name = GetOwningCompoundEngine(atomic_engine_name);
  287. compound_engine_kernel_lib_name = GetCompoundEngineKernelLibName(compound_engine_name);
  288. if (compound_engine_name.empty() || compound_engine_kernel_lib_name.empty()) {
  289. (void)op_desc->DelAttr(ATTR_NAME_COMPOUND_ENGINE_NAME);
  290. (void)op_desc->DelAttr(ATTR_NAME_COMPOUND_ENGINE_KERNEL_LIB_NAME);
  291. } else {
  292. GELOGI("Assign compound engine %s, kernel lib name %s for node %s.",
  293. compound_engine_name.c_str(), compound_engine_kernel_lib_name.c_str(), op_desc->GetName().c_str());
  294. (void)AttrUtils::SetStr(op_desc, ATTR_NAME_COMPOUND_ENGINE_NAME, compound_engine_name);
  295. (void)AttrUtils::SetStr(op_desc, ATTR_NAME_COMPOUND_ENGINE_KERNEL_LIB_NAME, compound_engine_kernel_lib_name);
  296. }
  297. return compound_engine_name;
  298. }
  299. bool graph_diff_compound_engine_flag = false;
  300. std::string graph_compound_engine_name = kInvalidCompoundEngineName;
  301. std::vector<ComputeGraphPtr> subgraphs;
  302. if (NodeUtils::GetSubgraphs(node_ptr, subgraphs) != GRAPH_SUCCESS) {
  303. REPORT_CALL_ERROR("E19999", "Get subgraphs of node %s failed", op_desc->GetName().c_str());
  304. GELOGE(FAILED, "[Check][Param] Get subgraphs of node %s failed", op_desc->GetName().c_str());
  305. return "";
  306. }
  307. for (const auto &subgraph : subgraphs) {
  308. std::string cur_graph_compound_engine_name;
  309. // if subgraph has been assigned
  310. if (subgraph->HasAttr(ATTR_NAME_COMPOUND_ENGINE_NAME)) {
  311. (void)AttrUtils::GetStr(subgraph, ATTR_NAME_COMPOUND_ENGINE_NAME, cur_graph_compound_engine_name);
  312. } else {
  313. bool node_diff_compound_engine_flag = false;
  314. std::string node_compound_engine_name = kInvalidCompoundEngineName;
  315. uint32_t assign_node_num = 0;
  316. for (const auto &cur_node : subgraph->GetDirectNode()) {
  317. if (IsStreamAssignSkip(cur_node) && cur_node->GetOpDesc()->GetSubgraphInstanceNames().empty()) {
  318. continue;
  319. }
  320. assign_node_num++;
  321. std::string cur_node_compound_engine_name = GetCompoundEngineName(cur_node, recursive_depth + 1);
  322. if (node_compound_engine_name == kInvalidCompoundEngineName) {
  323. node_compound_engine_name = cur_node_compound_engine_name;
  324. } else if (node_compound_engine_name != cur_node_compound_engine_name) {
  325. node_diff_compound_engine_flag = true;
  326. break;
  327. }
  328. }
  329. if (assign_node_num == 0) {
  330. GELOGD("all nodes in subgraph %s belongs to ge_local engine", subgraph->GetName().c_str());
  331. continue;
  332. }
  333. if (!(node_diff_compound_engine_flag ||
  334. (node_compound_engine_name == kInvalidCompoundEngineName) ||
  335. node_compound_engine_name.empty())) {
  336. GELOGI("Assign compound engine %s for subgraph %s.", node_compound_engine_name.c_str(), subgraph->GetName().c_str());
  337. (void)AttrUtils::SetStr(subgraph, ATTR_NAME_COMPOUND_ENGINE_NAME, node_compound_engine_name);
  338. cur_graph_compound_engine_name = node_compound_engine_name;
  339. } else {
  340. (void)subgraph->DelAttr(ATTR_NAME_COMPOUND_ENGINE_NAME);
  341. cur_graph_compound_engine_name.clear();
  342. }
  343. }
  344. if (graph_compound_engine_name == kInvalidCompoundEngineName) {
  345. graph_compound_engine_name = cur_graph_compound_engine_name;
  346. } else if (graph_compound_engine_name != cur_graph_compound_engine_name) {
  347. graph_diff_compound_engine_flag = true;
  348. break;
  349. }
  350. }
  351. compound_engine_kernel_lib_name = GetCompoundEngineKernelLibName(graph_compound_engine_name);
  352. if (!(graph_diff_compound_engine_flag || (graph_compound_engine_name == kInvalidCompoundEngineName) ||
  353. graph_compound_engine_name.empty() || compound_engine_kernel_lib_name.empty())) {
  354. compound_engine_name = graph_compound_engine_name;
  355. GELOGI("Assign compound engine %s, kernel lib name %s for node %s.",
  356. compound_engine_name.c_str(), compound_engine_kernel_lib_name.c_str(), op_desc->GetName().c_str());
  357. (void)AttrUtils::SetStr(op_desc, ATTR_NAME_COMPOUND_ENGINE_NAME, compound_engine_name);
  358. (void)AttrUtils::SetStr(op_desc, ATTR_NAME_COMPOUND_ENGINE_KERNEL_LIB_NAME, compound_engine_kernel_lib_name);
  359. } else {
  360. (void)op_desc->DelAttr(ATTR_NAME_COMPOUND_ENGINE_NAME);
  361. (void)op_desc->DelAttr(ATTR_NAME_COMPOUND_ENGINE_KERNEL_LIB_NAME);
  362. }
  363. return compound_engine_name;
  364. }
  365. std::string DNNEngineManager::GetOwningCompoundEngine(const string &atomic_engine_name) {
  366. if (atomic_2_compound_.empty()) {
  367. InitAtomicCompoundMapping();
  368. }
  369. const auto &iter = atomic_2_compound_.find(atomic_engine_name);
  370. if (iter == atomic_2_compound_.end()) {
  371. GELOGW("Compound engine which contains atomic engine %s is not registered", atomic_engine_name.c_str());
  372. return "";
  373. }
  374. return iter->second;
  375. }
  376. std::string DNNEngineManager::GetCompoundEngineKernelLibName(const string &compound_engine_name) const {
  377. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  378. if ((instance_ptr == nullptr) || (!instance_ptr->InitFlag())) {
  379. GELOGW("[Get][CompoundEngineKernelLibName]Failed, gelib not init before");
  380. return "";
  381. }
  382. const auto &compound_engine_2_kernel_lib_name = instance_ptr->OpsKernelManagerObj().GetCompoundEngineKernelLibName();
  383. const auto &iter = compound_engine_2_kernel_lib_name.find(compound_engine_name);
  384. if (iter == compound_engine_2_kernel_lib_name.end()) {
  385. GELOGW("Kernel lib name of compound engine %s is not registered", compound_engine_name.c_str());
  386. return "";
  387. }
  388. return iter->second;
  389. }
  390. std::string DNNEngineManager::GetHostCpuEngineName(const std::vector<OpInfo> &op_infos,
  391. const OpDescPtr &op_desc) const {
  392. for (const auto &it : op_infos) {
  393. if ((it.engine == kHostCpuEngineName) && (it.opKernelLib == kHostCpuOpKernelLibName)) {
  394. op_desc->SetOpEngineName(kHostCpuEngineName);
  395. op_desc->SetOpKernelLibName(kHostCpuOpKernelLibName);
  396. GELOGI("DNNEngineManager: Set OpKernelLibName %s and OpEngineName %s to %s",
  397. kHostCpuOpKernelLibName, kHostCpuEngineName, op_desc->GetName().c_str());
  398. return kHostCpuEngineName;
  399. }
  400. }
  401. GELOGE(FAILED, "[Get][HostCpuEngineName]Failed, HostCpuEngine not support [%s, %s]",
  402. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  403. REPORT_INNER_ERROR("E19999", "Get HostCpuEngineName failed, HostCpuEngine not support [%s, %s]",
  404. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  405. return "";
  406. }
  407. const std::map<std::string, SchedulerConf> &DNNEngineManager::GetSchedulers() const { return schedulers_; }
  408. Status DNNEngineManager::ParserJsonFile() {
  409. GELOGI("Begin to parser json file");
  410. std::string json_file_path = "plugin/nnengine/ge_config/engine_conf.json";
  411. std::string path = PluginManager::GetPath();
  412. path.append(json_file_path);
  413. nlohmann::json scheduler_json_file;
  414. Status status = ReadJsonFile(path, &scheduler_json_file);
  415. if (status != SUCCESS) {
  416. GELOGE(FAILED, "[Read][JsonFile]Failed, file %s", path.c_str());
  417. REPORT_CALL_ERROR("E19999", "Read json file %s failed", path.c_str());
  418. return FAILED;
  419. }
  420. if (scheduler_json_file.is_null()) {
  421. // when engine_conf.json is not exist, just return success
  422. GELOGW("Json file is null");
  423. return SUCCESS;
  424. }
  425. try {
  426. nlohmann::json scheduler_utils_json = scheduler_json_file[kSchedulerUnits];
  427. if (scheduler_utils_json.is_null()) {
  428. GELOGE(FAILED, "[Check[Param]Find scheduler units failed, the message is null, file %s", path.c_str());
  429. REPORT_INNER_ERROR("E19999", "Find scheduler units failed, the message is null, file %s", path.c_str());
  430. return FAILED;
  431. }
  432. if (!scheduler_utils_json.is_array()) {
  433. GELOGE(FAILED, "[Check][Param]The message of kSchedulerUnits is not array and "
  434. "the file path is %s", path.c_str());
  435. REPORT_INNER_ERROR("E19999", "The message of kSchedulerUnits is not array and "
  436. "the file path is %s", path.c_str());
  437. return FAILED;
  438. }
  439. auto size = scheduler_json_file[kSchedulerUnits].size();
  440. for (size_t i = 0; i < size; i++) {
  441. SchedulerConf scheduler_conf;
  442. std::map<std::string, EngineConfPtr> engine_conf_map;
  443. nlohmann::json engines_json_map = scheduler_utils_json[i][kCalEngines];
  444. if (engines_json_map.is_null()) {
  445. GELOGE(FAILED, "[Check][Param]The message of cal_engines is null, file %s", path.c_str());
  446. REPORT_INNER_ERROR("E19999", "The message of cal_engines is null, file %s", path.c_str());
  447. return FAILED;
  448. }
  449. std::string scheduler_id_temp = scheduler_utils_json[i][kId];
  450. if (!scheduler_id_temp.empty()) {
  451. scheduler_conf.id = scheduler_id_temp;
  452. } else {
  453. GELOGE(FAILED, "[Check][Param]Scheduler ID is null, file %s", path.c_str());
  454. REPORT_INNER_ERROR("E19999", "Scheduler ID is null, file %s", path.c_str());
  455. return FAILED;
  456. }
  457. status = ParserEngineMessage(engines_json_map, scheduler_id_temp, engine_conf_map);
  458. if (status != SUCCESS) {
  459. GELOGE(FAILED, "[Parse][EngineMessage]Failed, scheduler_id_temp %s", scheduler_id_temp.c_str());
  460. REPORT_CALL_ERROR("E19999", "Parse engine message failed, scheduler_id_temp %s",
  461. scheduler_id_temp.c_str());
  462. return FAILED;
  463. }
  464. scheduler_conf.name = scheduler_utils_json[i][kName];
  465. scheduler_conf.ex_attrs = scheduler_utils_json[i][kExAttrs];
  466. scheduler_conf.cal_engines = engine_conf_map;
  467. auto it = schedulers_.find(scheduler_id_temp);
  468. if (it != schedulers_.end()) {
  469. GELOGE(FAILED, "[Check][Param]There are the same scheduler ts %s in the json file",
  470. scheduler_id_temp.c_str());
  471. REPORT_INNER_ERROR("E19999", "[Check][Param]There are the same scheduler ts %s "
  472. "in the json file", scheduler_id_temp.c_str());
  473. return FAILED;
  474. }
  475. schedulers_.emplace(scheduler_id_temp, scheduler_conf);
  476. }
  477. } catch (const nlohmann::detail::type_error &e) {
  478. GELOGE(FAILED, "[Parse][JsonFile]Failed, file %s, reason %s", path.c_str(), e.what());
  479. REPORT_CALL_ERROR("E19999", "Parse json file %s failed, reason %s", path.c_str(), e.what());
  480. return FAILED;
  481. }
  482. GELOGI("Parser json file SUCCESS");
  483. return SUCCESS;
  484. }
  485. Status DNNEngineManager::ParserEngineMessage(const json engines_json, const std::string &scheduler_mark,
  486. std::map<std::string, EngineConfPtr> &engines) {
  487. GELOGI("Begin to parser engine massage");
  488. if (engines_json.is_null()) {
  489. GELOGE(FAILED, "[Check][Param]The message of cal_engines is null");
  490. REPORT_INNER_ERROR("E19999", "The message of cal_engines is null");
  491. return FAILED;
  492. }
  493. try {
  494. if (engines_json.is_array()) {
  495. for (size_t i = 0; i < engines_json.size(); i++) {
  496. nlohmann::json engines_elems = engines_json[i];
  497. EngineConfPtr engine_conf_ptr = MakeShared<EngineConf>();
  498. if (engine_conf_ptr == nullptr) {
  499. return FAILED;
  500. }
  501. std::string engine_id = engines_elems[kId];
  502. if (!engine_id.empty()) {
  503. engine_conf_ptr->id = engine_id;
  504. } else {
  505. GELOGE(FAILED, "[Check][Param]Engine ID is null");
  506. REPORT_INNER_ERROR("E19999", "Engine ID is null");
  507. return FAILED;
  508. }
  509. if (engines_elems.find(kName) != engines_elems.end()) {
  510. engine_conf_ptr->name = engines_elems[kName];
  511. } else {
  512. GELOGW("The engine %s name is null", engine_id.c_str());
  513. }
  514. if (engines_elems.find(kIndependent) != engines_elems.end()) {
  515. engine_conf_ptr->independent = engines_elems[kIndependent];
  516. }
  517. if (engines_elems.find(kAttach) != engines_elems.end()) {
  518. engine_conf_ptr->attach = engines_elems[kAttach];
  519. }
  520. if (engines_elems.find(kSkipAssignStream) != engines_elems.end()) {
  521. engine_conf_ptr->skip_assign_stream = engines_elems[kSkipAssignStream];
  522. }
  523. engine_conf_ptr->scheduler_id = scheduler_mark;
  524. auto it = engines.find(engine_id);
  525. if (it != engines.end()) {
  526. GELOGE(FAILED, "[Check][Param]There are the same engine %s message in the json file",
  527. engine_id.c_str());
  528. REPORT_INNER_ERROR("E19999", "There are the same engine %s message in the json file",
  529. engine_id.c_str());
  530. return FAILED;
  531. }
  532. engines.emplace(engine_id, engine_conf_ptr);
  533. }
  534. } else {
  535. GELOGE(FAILED, "[Check][Param]The message of cal_engines is not array in the json file");
  536. REPORT_INNER_ERROR("E19999", "The message of cal_engines is not array in the json file");
  537. return FAILED;
  538. }
  539. } catch (const json::exception &e) {
  540. GELOGE(FAILED, "[Construct][JsonContent]Failed, reason %s", e.what());
  541. REPORT_INNER_ERROR("E19999", "Construct json content failed, reason %s", e.what());
  542. return FAILED;
  543. }
  544. GELOGI("Parser engine massage success");
  545. return SUCCESS;
  546. }
  547. Status DNNEngineManager::ReadJsonFile(const std::string &file_path, JsonHandle handle) {
  548. GELOGD("Begin to read json file");
  549. if (file_path.empty()) {
  550. GELOGE(FAILED, "[Check][Param]Json path is empty");
  551. REPORT_INNER_ERROR("E19999", "Json path is empty");
  552. return FAILED;
  553. }
  554. nlohmann::json *json_file = reinterpret_cast<nlohmann::json *>(handle);
  555. if (json_file == nullptr) {
  556. GELOGE(FAILED, "[Check][Param]Json file is nullptr");
  557. REPORT_CALL_ERROR("E19999", "Json file is nullptr");
  558. return FAILED;
  559. }
  560. const char *file = file_path.data();
  561. if ((mmAccess2(file, M_F_OK)) != EN_OK) {
  562. if (engines_map_.size() != 0) {
  563. GELOGE(FAILED, "[Check][Param]The json file %s not exists, err %s",
  564. file_path.c_str(), strerror(errno));
  565. REPORT_CALL_ERROR("E19999", "Json file %s not exists, err %s",
  566. file_path.c_str(), strerror(errno));
  567. return FAILED;
  568. } else {
  569. GELOGW("The json file %s is not needed.", file_path.c_str());
  570. return SUCCESS;
  571. }
  572. }
  573. std::ifstream ifs(file_path);
  574. if (!ifs.is_open()) {
  575. GELOGE(FAILED, "[Open][JsonFile]Failed, file %s", file_path.c_str());
  576. REPORT_CALL_ERROR("E19999", "Open json file %s failed", file_path.c_str());
  577. return FAILED;
  578. }
  579. try {
  580. ifs >> *json_file;
  581. } catch (const json::exception &e) {
  582. GELOGE(FAILED, "[Read][JsonFile]Failed, reason %s", e.what());
  583. REPORT_CALL_ERROR("E19999", "Read json file failed, reason %s", e.what());
  584. ifs.close();
  585. return FAILED;
  586. }
  587. ifs.close();
  588. GELOGD("Read json file success");
  589. return SUCCESS;
  590. }
  591. Status DNNEngineManager::CheckJsonFile() {
  592. GELOGD("Begin to check json file");
  593. for (auto &it : engines_map_) {
  594. if (!it.second->IsAtomic()) {
  595. continue;
  596. }
  597. std::string engine_name = it.first;
  598. int count = 0;
  599. for (auto &iter : schedulers_) {
  600. auto engine_map = iter.second.cal_engines;
  601. auto iter_engine_name = engine_map.find(engine_name);
  602. if (iter_engine_name != engine_map.end()) {
  603. count++;
  604. }
  605. }
  606. if (count == 0) {
  607. GELOGE(FAILED, "[Check][JsonFile]The engine message %s is not found in the json file",
  608. engine_name.c_str());
  609. REPORT_INNER_ERROR("E19999", "The engine message %s is not found in the json file",
  610. engine_name.c_str());
  611. return FAILED;
  612. }
  613. if (count > 1) {
  614. GELOGE(FAILED, "[Check][JsonFile]The same engine message %s exists in the json file",
  615. engine_name.c_str());
  616. REPORT_INNER_ERROR("E19999", "The same engine message %s exists in the json file",
  617. engine_name.c_str());
  618. return FAILED;
  619. }
  620. }
  621. GELOGD("Check json file success");
  622. return SUCCESS;
  623. }
  624. void DNNEngineManager::InitAtomicCompoundMapping() {
  625. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  626. if ((instance_ptr == nullptr) || (!instance_ptr->InitFlag())) {
  627. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Get][CompoundEngineName]Failed, gelib not init before");
  628. REPORT_INNER_ERROR("E19999", "Get CompoundEngineName failed, gelib not init before");
  629. return;
  630. }
  631. const auto &compound_engine_2_kernel_lib_name = instance_ptr->OpsKernelManagerObj().GetCompoundEngineKernelLibName();
  632. for (const auto &item : instance_ptr->OpsKernelManagerObj().GetCompoundEngineContains()) {
  633. const auto &compound_engine = GetEngine(item.first);
  634. if ((compound_engine == nullptr) || compound_engine->IsAtomic()) {
  635. GELOGW("Compound engine %s is not registered", item.first.c_str());
  636. }
  637. const auto &iter = compound_engine_2_kernel_lib_name.find(item.first);
  638. if ((iter == compound_engine_2_kernel_lib_name.end()) || iter->second.empty()) {
  639. GELOGW("Kernel lib name of compound engine %s is empty", item.first.c_str());
  640. }
  641. for (const auto &atomic_engine_name : item.second) {
  642. const auto &atomic_engine = GetEngine(atomic_engine_name);
  643. if ((atomic_engine == nullptr) || !atomic_engine->IsAtomic()) {
  644. GELOGW("Atomic engine %s is not registered", atomic_engine_name.c_str());
  645. continue;
  646. }
  647. auto iter = atomic_2_compound_.find(atomic_engine_name);
  648. if (iter != atomic_2_compound_.end()) {
  649. GELOGW("Atomic engine %s has been contained in compound engine %s, and will be overwritten by engine %s",
  650. atomic_engine_name.c_str(), iter->second.c_str(), item.first.c_str());
  651. }
  652. atomic_2_compound_[atomic_engine_name] = item.first;
  653. }
  654. }
  655. }
  656. bool DNNEngineManager::IsStreamAssignSkip(const NodePtr &node) {
  657. const auto &op_desc = node->GetOpDesc();
  658. if (op_desc == nullptr) {
  659. return false;
  660. }
  661. std::string engine_name = op_desc->GetOpEngineName();
  662. if (engine_name.empty()) {
  663. engine_name = GetDNNEngineName(node);
  664. }
  665. return IsStreamAssignSkip(engine_name);
  666. }
  667. bool DNNEngineManager::IsStreamAssignSkip(const string &engine_name) {
  668. // Only one scheduler has been supported by now
  669. for (const auto &scheduler : schedulers_) {
  670. const map<string, EngineConfPtr> cal_engines = scheduler.second.cal_engines;
  671. auto cal_engines_iter = cal_engines.find(engine_name);
  672. if (cal_engines_iter == cal_engines.end()) {
  673. GELOGW("No cal_engines found within engine %s", engine_name.c_str());
  674. continue;
  675. }
  676. EngineConfPtr engine_conf_ptr = cal_engines_iter->second;
  677. if (engine_conf_ptr == nullptr) {
  678. GELOGW("engine_conf_ptr within engine %s is null", engine_name.c_str());
  679. continue;
  680. }
  681. return engine_conf_ptr->skip_assign_stream;
  682. }
  683. return false;
  684. }
  685. } // namespace ge

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