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

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