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.

ops_kernel_manager.cc 20 kB

5 years ago
5 years ago
3 years ago
5 years ago
5 years ago
3 years ago
5 years ago
5 years ago
5 years ago
5 years ago
3 years ago
5 years ago
3 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 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
5 years ago
3 years ago
5 years ago
5 years ago
3 years ago
3 years ago
5 years ago
3 years ago
5 years ago
3 years ago
5 years ago
3 years ago
5 years ago
5 years ago
3 years ago
5 years ago
3 years ago
5 years ago
5 years ago
5 years ago
5 years ago
3 years ago
5 years ago
5 years ago
3 years ago
5 years ago
3 years ago
5 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
5 years ago
3 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 "opskernel_manager/ops_kernel_manager.h"
  17. #include "init/gelib.h"
  18. #include "proto/optimizer_priority.pb.h"
  19. namespace {
  20. const char *const kInitialize = "Initialize";
  21. const char *const kGetOpsKernelInfoStores = "GetOpsKernelInfoStores";
  22. const char *const kGetGraphOptimizerObjs = "GetGraphOptimizerObjs";
  23. const char *const kFinalize = "Finalize";
  24. const char *const kGetCompoundEngineContains = "GetCompoundEngineContains";
  25. std::mutex ops_kernel_info_mutex;
  26. } // namespace
  27. namespace ge {
  28. OpsKernelManager::OpsKernelManager()
  29. : plugin_manager_(), op_tiling_manager_(), init_flag_(false), enable_fe_flag_(false), enable_aicpu_flag_(false) {}
  30. OpsKernelManager::~OpsKernelManager() {
  31. graph_optimizers_.clear();
  32. ops_kernel_store_.clear();
  33. atomic_graph_optimizers_.clear();
  34. compound_graph_optimizers_.clear();
  35. atomic_graph_optimizers_by_priority_.clear();
  36. atomic_first_optimizers_by_priority_.clear();
  37. compound_first_optimizers_by_priority_.clear();
  38. compound_engine_contains_.clear();
  39. ops_kernel_info_.clear();
  40. }
  41. Status OpsKernelManager::Initialize(const map<string, string> &options_const) {
  42. if (init_flag_) {
  43. GELOGW("OpsKernelManager has been initialized.");
  44. return SUCCESS;
  45. }
  46. std::map<string, string> options(options_const);
  47. Status ret = InitPluginOptions(options);
  48. if (ret != SUCCESS) {
  49. GELOGE(ret, "[Init][PluginOptions] parse pluginFlag from ge options failed.");
  50. REPORT_CALL_ERROR("E19999", "InitPluginOptions failed.");
  51. return ret;
  52. }
  53. vector<string> func_check_list = {kInitialize, kGetOpsKernelInfoStores, kGetGraphOptimizerObjs, kFinalize};
  54. string extern_engine_path;
  55. auto iter = options.find(OPTION_EXEC_IS_USEHCOM);
  56. if (iter == options.end()) {
  57. GELOGI("OPTION_EXEC_IS_USEHCOM is not set, default is single P");
  58. options.emplace("ge.exec.isUseHcom", to_string(0));
  59. }
  60. iter = options.find(OPTION_EXEC_IS_USEHVD);
  61. if (iter == options.end()) {
  62. GELOGI("OPTION_EXEC_IS_USEHVD is not set, default is single P");
  63. options.emplace("ge.exec.isUseHvd", to_string(0));
  64. }
  65. GetExternalEnginePath(extern_engine_path, options);
  66. GELOGI("OPTION_EXEC_EXTERN_PLUGIN_PATH=%s.", extern_engine_path.c_str());
  67. op_tiling_manager_.LoadSo();
  68. ret = plugin_manager_.LoadSo(extern_engine_path, func_check_list);
  69. if (ret != SUCCESS) {
  70. GELOGE(ret, "[Check][SoFile] not find any valid so file.");
  71. REPORT_INNER_ERROR("E19999", "OpsKernelManager::Initialize failed for not find any valid so file.");
  72. return ret;
  73. }
  74. initialize_ = options;
  75. if (plugin_manager_.InvokeAll<map<string, string> &, Status>(kInitialize, initialize_) == FAILED) {
  76. GELOGE(GE_OPS_GET_NO_VALID_SO, "[Invoke][OpsKernelInfo]PluginManager InvokeAll failed.");
  77. REPORT_INNER_ERROR("E19999", "PluginManager InvokeAll failed.");
  78. return GE_OPS_GET_NO_VALID_SO;
  79. }
  80. if (plugin_manager_.InvokeAll<map<string, OpsKernelInfoStorePtr> &>(kGetOpsKernelInfoStores,
  81. ops_kernel_store_) != SUCCESS) {
  82. GELOGW("Initialize OpsKernelInfo failed.");
  83. }
  84. if (plugin_manager_.InvokeAll<map<string, GraphOptimizerPtr> &>(kGetGraphOptimizerObjs,
  85. graph_optimizers_) != SUCCESS) {
  86. GELOGW("Initialize GraphOptimizerObjs failed.");
  87. }
  88. std::map<std::string, std::string> tmp;
  89. plugin_manager_.
  90. OptionalInvokeAll<std::map<std::string, std::set<std::string>> &, std::map<std::string, std::string> &>(
  91. kGetCompoundEngineContains, compound_engine_contains_, tmp);
  92. ret = CheckPluginPtr();
  93. if (ret != SUCCESS) {
  94. return ret;
  95. }
  96. ret = InitOpKernelInfoStores(options);
  97. if (ret != SUCCESS) {
  98. return ret;
  99. }
  100. InitOpsKernelInfo();
  101. ret = InitGraphOptimizers(options);
  102. if (ret != SUCCESS) {
  103. return ret;
  104. }
  105. ClassifyGraphOptimizers();
  106. InitGraphOptimizerPriority();
  107. init_flag_ = true;
  108. return SUCCESS;
  109. }
  110. void OpsKernelManager::GetExternalEnginePath(std::string &extern_engine_path,
  111. const std::map<string, string>& options) {
  112. GELOGI("Enter get external engine so path schedule");
  113. const char *path_env = std::getenv("ASCEND_ENGINE_PATH");
  114. if (path_env != nullptr) {
  115. extern_engine_path = path_env;
  116. GELOGI("OpsKernelManager get external engine so path from env.");
  117. return;
  118. }
  119. std::string path_base = PluginManager::GetPath();
  120. std::string so_path = "plugin/opskernel/";
  121. std::string path = path_base + so_path;
  122. extern_engine_path = (path + "libfe.so" + ":") + (path + "libge_local_engine.so" + ":") +
  123. (path + "librts_engine.so" + ":") + (path + "libaicpu_ascend_engine.so" + ":") +
  124. (path + "libhost_cpu_engine.so" + ":") + (path + "libaicpu_tf_engine.so" + ":");
  125. auto iter = options.find(OPTION_EXEC_HCCL_FLAG);
  126. if (iter == options.end() || iter->second != "0") {
  127. extern_engine_path += (path_base + "libhcom_graph_adaptor.so");
  128. }
  129. }
  130. Status OpsKernelManager::InitPluginOptions(const map<string, string> &options) {
  131. Status ret;
  132. // parse fe
  133. ret = ParsePluginOptions(options, GE_FE_FLAG, enable_fe_flag_);
  134. if (ret != SUCCESS) {
  135. return ret;
  136. }
  137. // parse aiCpu
  138. ret = ParsePluginOptions(options, GE_AICPU_FLAG, enable_aicpu_flag_);
  139. if (ret != SUCCESS) {
  140. return ret;
  141. }
  142. return SUCCESS;
  143. }
  144. Status OpsKernelManager::ParsePluginOptions(const map<string, string> &options, const string &plugin_name,
  145. bool &enable_flag) {
  146. GELOGI("Parse the Plugin Options, plugin_name:%s.", plugin_name.c_str());
  147. auto iter = options.find(plugin_name);
  148. if (iter != options.end()) {
  149. try {
  150. int32_t flag = std::stoi(iter->second.c_str());
  151. if (flag == 0) {
  152. enable_flag = false;
  153. } else if (flag == 1) {
  154. enable_flag = true;
  155. } else {
  156. GELOGE(GE_GRAPH_OPTIONS_INVALID,
  157. "[Parse][PluginOptions]option_key:%s, its value %s is invalid, it must be 0 or 1.",
  158. plugin_name.c_str(), iter->second.c_str());
  159. REPORT_INNER_ERROR("E19999", "ParsePluginOptions failed, option_key:%s, "
  160. "its value %s is invalid, it must be 0 or 1.", plugin_name.c_str(), iter->second.c_str());
  161. return GE_GRAPH_OPTIONS_INVALID;
  162. }
  163. } catch (std::invalid_argument &) {
  164. GELOGE(GE_GRAPH_OPTIONS_INVALID, "[Parse][PluginOptions] failed, option_key:ge.feFlag,"
  165. "its value %s is invalid_argument, it must be 0 or 1.",
  166. iter->second.c_str());
  167. REPORT_INNER_ERROR("E19999", "ParsePluginOptions failed, option_key:ge.feFlag,"
  168. "its value %s is invalid_argument, it must be 0 or 1.",
  169. iter->second.c_str());
  170. return GE_GRAPH_OPTIONS_INVALID;
  171. } catch (std::out_of_range &) {
  172. GELOGE(GE_GRAPH_OPTIONS_INVALID,
  173. "[Parse][PluginOptions]failed, option_key:ge.feFlag, its value %s is out of range, it must be 0 or 1.",
  174. iter->second.c_str());
  175. REPORT_INNER_ERROR("E19999", "ParsePluginOptions failed, option_key:ge.feFlag,"
  176. "its value %s is out of range, it must be 0 or 1.",
  177. iter->second.c_str());
  178. return GE_GRAPH_OPTIONS_INVALID;
  179. } catch (...) {
  180. GELOGE(GE_GRAPH_OPTIONS_INVALID,
  181. "[Parse][PluginOptions]option_key:%s, its value %s is invalid, it must be 0 or 1.",
  182. plugin_name.c_str(), iter->second.c_str());
  183. REPORT_INNER_ERROR("E19999", "ParsePluginOptions failed, option_key:%s, "
  184. "its value %s is invalid, it must be 0 or 1.", plugin_name.c_str(), iter->second.c_str());
  185. return GE_GRAPH_OPTIONS_INVALID;
  186. }
  187. } else {
  188. GELOGI("Not find option_key %s, set to default value false.", plugin_name.c_str());
  189. enable_flag = false;
  190. }
  191. return SUCCESS;
  192. }
  193. Status OpsKernelManager::CheckPluginPtr() const {
  194. for (auto iter = ops_kernel_store_.begin(); iter != ops_kernel_store_.end(); ++iter) {
  195. if (iter->second == nullptr) {
  196. GELOGE(INTERNAL_ERROR, "[Check][PluginPtr] OpsKernelInfoStorePtr key=%s is null", iter->first.c_str());
  197. REPORT_INNER_ERROR("E19999", "CheckPluginPtr OpsKernelInfoStorePtr key=%s is null", iter->first.c_str());
  198. return FAILED;
  199. }
  200. }
  201. for (auto iter1 = graph_optimizers_.begin(); iter1 != graph_optimizers_.end(); ++iter1) {
  202. if (iter1->second == nullptr) {
  203. GELOGE(INTERNAL_ERROR, "[Check][PluginPtr] GraphOptimizerPtr key=%s is null", iter1->first.c_str());
  204. REPORT_INNER_ERROR("E19999", "GraphOptimizerPtr key=%s is null", iter1->first.c_str());
  205. return FAILED;
  206. }
  207. }
  208. return SUCCESS;
  209. }
  210. Status OpsKernelManager::InitOpKernelInfoStores(const map<string, string> &options) {
  211. GELOGI("The number of OpKernelInfoStoreObjs are %lu.", ops_kernel_store_.size());
  212. for (const auto &it : ops_kernel_store_) {
  213. GELOGI("OpKernelInfoStore name: %s.", (it.first).c_str());
  214. Status ret = it.second->Initialize(options);
  215. if (ret != SUCCESS) {
  216. GELOGE(GE_OPS_KERNEL_STORE_INIT_FAILED,
  217. "[Init][OpKernelLib]OpKernelInfoStore: %s initialize failed.", (it.first).c_str());
  218. REPORT_CALL_ERROR("E19999", "OpKernelInfoStore: %s initialize failed.", (it.first).c_str());
  219. return GE_OPS_KERNEL_STORE_INIT_FAILED;
  220. }
  221. }
  222. return SUCCESS;
  223. }
  224. void OpsKernelManager::InitOpsKernelInfo() {
  225. ops_kernel_info_.clear();
  226. for (const auto &it : ops_kernel_store_) {
  227. map<string, OpInfo> op_infos{};
  228. it.second->GetAllOpsKernelInfo(op_infos);
  229. for (const auto &op_info_it : op_infos) {
  230. auto op_info_copy = op_info_it.second;
  231. // flush ops kernel
  232. op_info_copy.opKernelLib = it.first;
  233. ops_kernel_info_[op_info_it.first].emplace_back(op_info_copy);
  234. GELOGD("OpKernelInfoStore name: %s, found op type is %s, engine name is %s, opkernel name is %s",
  235. (it.first).c_str(), op_info_it.first.c_str(), op_info_it.second.engine.c_str(),
  236. op_info_it.second.opKernelLib.c_str());
  237. }
  238. }
  239. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  240. if (instance_ptr == nullptr) {
  241. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Get][GELib]malloc instance_ptr failed.");
  242. REPORT_INNER_ERROR("E19999", "InitOpsKernelInfo failed for new GELib.");
  243. return;
  244. }
  245. // sort op_info of ops_kernel_info_
  246. for (auto &it : ops_kernel_info_) {
  247. if (it.second.empty()) {
  248. continue;
  249. }
  250. auto comp_func = [&instance_ptr](const OpInfo &op_a, const OpInfo &op_b) -> bool {
  251. const string &a = op_a.engine;
  252. const string &b = op_b.engine;
  253. // check if a or b is registered
  254. if (!(instance_ptr->DNNEngineManagerObj().IsEngineRegistered(a))) {
  255. return false;
  256. }
  257. if (!(instance_ptr->DNNEngineManagerObj().IsEngineRegistered(b))) {
  258. return true;
  259. }
  260. // compare compute cost of a and b, IsEngineRegistered make sure engine is not nullptr
  261. auto engine_a = instance_ptr->DNNEngineManagerObj().GetEngine(a);
  262. auto engine_b = instance_ptr->DNNEngineManagerObj().GetEngine(b);
  263. DNNEngineAttribute attr_a, attr_b;
  264. engine_a->GetAttributes(attr_a);
  265. engine_b->GetAttributes(attr_b);
  266. return attr_a.compute_cost < attr_b.compute_cost;
  267. };
  268. // Sort the OpInfos based on the compute cost of the engine
  269. std::sort(it.second.begin(), it.second.end(), comp_func);
  270. }
  271. GELOGI("Init opsKernelInfo finished, size is %zu", ops_kernel_info_.size());
  272. }
  273. Status OpsKernelManager::InitGraphOptimizers(const map<string, string> &options) {
  274. GELOGI("Init graph optimizers options count %zu", options.size());
  275. for (const auto &option : options) {
  276. GELOGI("Init graph optimizers option %s: %s", option.first.c_str(), option.second.c_str());
  277. }
  278. GELOGI("The number of GraphOptimizerObjs are %zu.", graph_optimizers_.size());
  279. for (const auto &it : graph_optimizers_) {
  280. GELOGI("GraphOptimizer name: %s.", (it.first).c_str());
  281. GraphOptimizerAttribute attrs;
  282. GE_CHK_STATUS_RET(it.second->GetAttributes(attrs))
  283. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  284. if (instance_ptr == nullptr) {
  285. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Get][GELib]malloc instance_ptr failed.");
  286. REPORT_INNER_ERROR("E19999", "InitGraphOptimizers failed for new GELib.");
  287. return GE_CLI_GE_NOT_INITIALIZED;
  288. }
  289. if (!instance_ptr->DNNEngineManagerObj().IsEngineRegistered(attrs.engineName)) {
  290. GELOGW("Engine: %s is not registered.", attrs.engineName.c_str());
  291. continue;
  292. }
  293. if (it.second->Initialize(options) != SUCCESS) {
  294. GELOGE(GE_OPS_GRAPH_OPTIMIZER_INIT_FAILED,
  295. "[Init][GraphOptimizer] GraphOptimizer: %s initialize failed.", (it.first).c_str());
  296. REPORT_CALL_ERROR("E19999", "InitGraphOptimizers failed. %s initialize failed.", (it.first).c_str());
  297. return GE_OPS_GRAPH_OPTIMIZER_INIT_FAILED;
  298. }
  299. }
  300. return SUCCESS;
  301. }
  302. Status OpsKernelManager::Finalize() {
  303. if (!init_flag_) {
  304. GELOGW("Finalize is not allowed, initialize first is necessary.");
  305. return SUCCESS;
  306. }
  307. GELOGI("free ops kernel resource.");
  308. for (auto iter = ops_kernel_store_.begin(); iter != ops_kernel_store_.end(); ++iter) {
  309. GELOGI("OpsKernelStore finalize, name: %s.", (iter->first).c_str());
  310. Status status = iter->second->Finalize();
  311. if (SUCCESS != status) {
  312. GELOGE(status, "[Check][Status]OpsKernelStore finalize failed, name: %s.", (iter->first).c_str());
  313. REPORT_CALL_ERROR("E19999", "OpsKernelStore finalize failed, name: %s.", (iter->first).c_str());
  314. return status;
  315. }
  316. }
  317. for (auto iter = graph_optimizers_.begin(); iter != graph_optimizers_.end(); ++iter) {
  318. GELOGI("GraphOptimizer finalize, name: %s.", (iter->first).c_str());
  319. Status status = iter->second->Finalize();
  320. if (status != SUCCESS) {
  321. GELOGE(status, "[Check][Status] GraphOptimizer finalize failed, name: %s.", (iter->first).c_str());
  322. REPORT_CALL_ERROR("E19999", "GraphOptimizer finalize failed, name: %s.", (iter->first).c_str());
  323. return status;
  324. }
  325. }
  326. Status ret = FinalizeOpsKernel();
  327. if (ret != SUCCESS) {
  328. GELOGE(ret, "[Free][Ops Kernel Resource] failed.");
  329. REPORT_CALL_ERROR("E19999", "FinalizeOpsKernel failed, Free Ops kernel resource failed.");
  330. return ret;
  331. }
  332. init_flag_ = false;
  333. return SUCCESS;
  334. }
  335. const vector<OpInfo> &OpsKernelManager::GetOpsKernelInfo(const string &op_type) {
  336. std::lock_guard<std::mutex> lock(ops_kernel_info_mutex);
  337. auto find = ops_kernel_info_.find(op_type);
  338. if (find != ops_kernel_info_.end()) {
  339. return find->second;
  340. } else {
  341. InitOpsKernelInfo();
  342. find = ops_kernel_info_.find(op_type);
  343. if (find != ops_kernel_info_.end()) {
  344. return find->second;
  345. }
  346. GELOGW("Failed to get opsKernelInfo object by type: %s.", op_type.c_str());
  347. return empty_op_info_;
  348. }
  349. }
  350. const map<string, vector<OpInfo>> &OpsKernelManager::GetAllOpsKernelInfo() const {
  351. std::lock_guard<std::mutex> lock(ops_kernel_info_mutex);
  352. return ops_kernel_info_;
  353. }
  354. OpsKernelInfoStorePtr OpsKernelManager::GetOpsKernelInfoStore(const std::string &name) const {
  355. auto find = ops_kernel_store_.find(name);
  356. if (find != ops_kernel_store_.end()) {
  357. return find->second;
  358. }
  359. GELOGW("Failed to get opsKernelInfoStore object by name. OpKernelLibName is %s", name.c_str());
  360. return nullptr;
  361. }
  362. const map<string, OpsKernelInfoStorePtr> &OpsKernelManager::GetAllOpsKernelInfoStores() const {
  363. return ops_kernel_store_;
  364. }
  365. const map<string, GraphOptimizerPtr> &OpsKernelManager::GetAllGraphOptimizerObjs() const { return graph_optimizers_; }
  366. const vector<pair<string, GraphOptimizerPtr>> &OpsKernelManager::GetAllGraphOptimizerObjsByPriority(bool atomic_first_flag) const {
  367. if (atomic_first_flag) {
  368. return atomic_first_optimizers_by_priority_;
  369. } else {
  370. return compound_first_optimizers_by_priority_;
  371. }
  372. }
  373. void OpsKernelManager::GetGraphOptimizerByEngine(const std::string &engine_name,
  374. vector<GraphOptimizerPtr> &graph_optimizer) {
  375. for (const auto &it : graph_optimizers_) {
  376. GraphOptimizerAttribute attrs;
  377. if (it.second->GetAttributes(attrs) != SUCCESS) {
  378. GELOGW("Get GraphOptimizer name: %s attributes failed.", (it.first).c_str());
  379. continue;
  380. }
  381. if (attrs.engineName == engine_name) {
  382. GELOGD("GetGraphOptimizerByEngine GraphOptimizer name: %s, engineName: %s", (it.first).c_str(),
  383. attrs.engineName.c_str());
  384. graph_optimizer.push_back(it.second);
  385. }
  386. }
  387. if (graph_optimizer.empty()) {
  388. GELOGI("GetGraphOptimizerByEngine EngineName %s has no graph_optimizer.", engine_name.c_str());
  389. }
  390. }
  391. bool OpsKernelManager::GetEnableFeFlag() const { return enable_fe_flag_; }
  392. bool OpsKernelManager::GetEnableAICPUFlag() const { return enable_aicpu_flag_; }
  393. bool OpsKernelManager::GetEnablePluginFlag() const { return (enable_fe_flag_ || enable_aicpu_flag_); }
  394. void OpsKernelManager::ClassifyGraphOptimizers() {
  395. if (compound_engine_contains_.empty()) {
  396. atomic_graph_optimizers_ = graph_optimizers_;
  397. compound_graph_optimizers_.clear();
  398. return;
  399. }
  400. for (const auto &item : graph_optimizers_) {
  401. if (compound_engine_contains_.find(item.first) != compound_engine_contains_.end()) {
  402. GELOGI("Engine %s is a compound engine.", item.first.c_str());
  403. compound_graph_optimizers_.emplace(item);
  404. } else {
  405. GELOGI("Engine %s is an atomic engine.", item.first.c_str());
  406. atomic_graph_optimizers_.emplace(item);
  407. }
  408. }
  409. }
  410. void OpsKernelManager::InitGraphOptimizerPriority() {
  411. string priority_conf_path = "plugin/opskernel/optimizer_priority.pbtxt";
  412. string path = PluginManager::GetPath();
  413. path.append(priority_conf_path);
  414. optimizers::Priority optimizerPriority;
  415. if (!ReadProtoFromText(path.c_str(), &optimizerPriority)) {
  416. GELOGW("Read priority file failed. Follow loading sequence.");
  417. return;
  418. }
  419. auto priorities = optimizerPriority.optimizer();
  420. if (priorities.empty()) {
  421. GELOGI("No priority file config. Follow loading sequence.");
  422. return;
  423. }
  424. // sort optimizer map by priority
  425. std::stringstream priority_seq;
  426. for (const auto optimizer_name : priorities) {
  427. auto name_to_optimizer_pair = atomic_graph_optimizers_.find(optimizer_name);
  428. if (name_to_optimizer_pair != atomic_graph_optimizers_.end()) {
  429. atomic_graph_optimizers_by_priority_.emplace_back(*name_to_optimizer_pair);
  430. priority_seq << optimizer_name.c_str() << ' ';
  431. } else {
  432. GELOGW("Unknown optimizer %s show up in priority config file. Please check.", optimizer_name.c_str());
  433. }
  434. }
  435. GELOGI("Atomic graph Optimizers priority initialized. The sequence will follow : %s.", priority_seq.str().c_str());
  436. atomic_first_optimizers_by_priority_ = atomic_graph_optimizers_by_priority_;
  437. for (const auto &item : compound_graph_optimizers_) {
  438. atomic_first_optimizers_by_priority_.emplace_back(std::make_pair(item.first, item.second));
  439. compound_first_optimizers_by_priority_.emplace_back(std::make_pair(item.first, item.second));
  440. }
  441. for (const auto &item : atomic_graph_optimizers_by_priority_) {
  442. compound_first_optimizers_by_priority_.emplace_back(std::make_pair(item.first, item.second));
  443. }
  444. }
  445. Status OpsKernelManager::FinalizeOpsKernel() {
  446. GELOGI("ge invoke ops kernel finalize.");
  447. Status ret = plugin_manager_.InvokeAll<Status>(kFinalize);
  448. if (ret != SUCCESS) {
  449. GELOGE(ret, "[Finalize][Check][Status] invoke Fe finalize failed.");
  450. REPORT_INNER_ERROR("E19999", "PluginManager InvokeAll failed.");
  451. return ret;
  452. }
  453. return SUCCESS;
  454. }
  455. } // namespace ge

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