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.

graph_optimize.cc 15 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 "graph/optimize/graph_optimize.h"
  17. #include "graph/ge_context.h"
  18. #include "graph/common/local_context.h"
  19. #include "graph/passes/dimension_adjust_pass.h"
  20. #include "inc/pass_manager.h"
  21. #include "init/gelib.h"
  22. namespace {
  23. const char *const kVectorCore = "VectorCore";
  24. const char *const kVectorEngine = "VectorEngine";
  25. const char *const kAicoreEngine = "AIcoreEngine";
  26. } // namespace
  27. namespace ge {
  28. GraphOptimize::GraphOptimize()
  29. : optimize_type_(domi::FrameworkType::TENSORFLOW),
  30. cal_config_(""),
  31. insert_op_config_(""),
  32. parse_out_node_(""),
  33. core_type_(""),
  34. graph_context_(nullptr) {}
  35. void AddNodeInputProperty(ComputeGraphPtr &compute_graph) {
  36. if (compute_graph == nullptr) {
  37. GELOGE(GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL, "[AddNodeInputProperty]: compute_graph is nullptr.");
  38. return;
  39. }
  40. for (ge::NodePtr &node : compute_graph->GetDirectNode()) {
  41. auto node_op_desc = node->GetOpDesc();
  42. GE_IF_BOOL_EXEC(node_op_desc == nullptr, GELOGW("node_op_desc is nullptr!"); return );
  43. auto in_control_anchor = node->GetInControlAnchor();
  44. vector<string> src_name_list;
  45. vector<string> input_name_list;
  46. vector<int64_t> src_index_list;
  47. GE_IF_BOOL_EXEC(
  48. in_control_anchor != nullptr, string src_name_temp; for (auto &out_control_anchor
  49. : in_control_anchor->GetPeerOutControlAnchors()) {
  50. ge::NodePtr src_node = out_control_anchor->GetOwnerNode();
  51. GE_IF_BOOL_EXEC(src_node == nullptr, GELOGW("src_node is nullptr!"); continue);
  52. src_name_temp = src_name_temp == "" ? src_node->GetName() : src_name_temp + ":" + src_node->GetName();
  53. } GE_IF_BOOL_EXEC(src_name_temp != "", src_name_list.emplace_back(src_name_temp);
  54. node_op_desc->SetSrcName(src_name_list);))
  55. for (auto &in_data_anchor : node->GetAllInDataAnchors()) {
  56. auto peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  57. GE_IF_BOOL_EXEC(
  58. peer_out_anchor == nullptr, GELOGW("peer_out_anchor is nullptr! node: %s", node->GetName().c_str()); continue);
  59. ge::NodePtr src_node = peer_out_anchor->GetOwnerNode();
  60. src_index_list = node_op_desc->GetSrcIndex();
  61. src_name_list.emplace_back(src_node->GetName());
  62. src_index_list.emplace_back(peer_out_anchor->GetIdx());
  63. node_op_desc->SetSrcName(src_name_list);
  64. node_op_desc->SetSrcIndex(src_index_list);
  65. GE_IF_BOOL_EXEC(!(node_op_desc->GetType() == NETOUTPUT && GetLocalOmgContext().type == domi::TENSORFLOW),
  66. ge::NodePtr peer_owner_node = peer_out_anchor->GetOwnerNode();
  67. input_name_list.emplace_back(
  68. peer_owner_node->GetName() +
  69. (peer_out_anchor->GetIdx() == 0 ? "" : ": " + to_string(peer_out_anchor->GetIdx())));
  70. node_op_desc->SetInputName(input_name_list);)
  71. }
  72. }
  73. }
  74. Status GraphOptimize::OptimizeSubGraph(ComputeGraphPtr &compute_graph, const std::string &engine_name) {
  75. if (compute_graph == nullptr) {
  76. GELOGE(GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL, "[OptimizeSubGraph]: compute_graph is nullptr.");
  77. return GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL;
  78. }
  79. Status ret = SUCCESS;
  80. vector<GraphOptimizerPtr> graph_optimizer;
  81. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  82. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  83. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "GraphOptimzer: GE is not initialized");
  84. return GE_CLI_GE_NOT_INITIALIZED;
  85. }
  86. if (instance_ptr->DNNEngineManagerObj().IsEngineRegistered(engine_name)) {
  87. instance_ptr->OpsKernelManagerObj().GetGraphOptimizerByEngine(engine_name, graph_optimizer);
  88. AddNodeInputProperty(compute_graph);
  89. if (compute_graph->GetDirectNode().size() == 0) {
  90. GELOGW("[OptimizeSubGraph] compute_graph do not has any node.");
  91. return SUCCESS;
  92. }
  93. if (build_mode_ == BUILD_MODE_TUNING && build_step_ == BUILD_STEP_AFTER_UB_MATCH) {
  94. for (auto iter = graph_optimizer.begin(); iter != graph_optimizer.end(); ++iter) {
  95. Status ret = (*iter)->OptimizeFusedGraphAfterGraphSlice(*(compute_graph));
  96. if (ret != SUCCESS) {
  97. GELOGE(ret, "[OptimizeSubGraph][OptimizeFusedGraphStage2]: graph optimize failed, ret:%d", ret);
  98. return ret;
  99. }
  100. }
  101. return SUCCESS;
  102. }
  103. for (auto iter = graph_optimizer.begin(); iter != graph_optimizer.end(); ++iter) {
  104. ret = (*iter)->OptimizeFusedGraph(*(compute_graph));
  105. if (ret != SUCCESS) {
  106. GELOGE(ret, "[OptimizeSubGraph][OptimizeFusedGraph]: graph optimize failed, ret:%d", ret);
  107. return ret;
  108. }
  109. }
  110. } else {
  111. GELOGI("Engine: %s is not registered. do nothing in subGraph Optimize by ATC.", engine_name.c_str());
  112. }
  113. return ret;
  114. }
  115. Status GraphOptimize::OptimizeOriginalGraph(ComputeGraphPtr &compute_graph) {
  116. if (compute_graph == nullptr) {
  117. GELOGE(GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL, "[OptimizeOriginalGraph]: compute_graph is nullptr.");
  118. return GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL;
  119. }
  120. Status ret = SUCCESS;
  121. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  122. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  123. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "OptimizeOriginalGraph failed.");
  124. return GE_CLI_GE_NOT_INITIALIZED;
  125. }
  126. auto graph_optimizer = instance_ptr->OpsKernelManagerObj().GetAllGraphOptimizerObjsByPriority();
  127. GELOGI("optimize by opskernel in original graph optimize phase. num of graph_optimizer is %lu.",
  128. graph_optimizer.size());
  129. string exclude_core_Type = (core_type_ == kVectorCore) ? kAicoreEngine : kVectorEngine;
  130. GELOGD("[OptimizeOriginalGraph]: engine type will exclude: %s", exclude_core_Type.c_str());
  131. if (graph_optimizer.size() != 0) {
  132. for (auto iter = graph_optimizer.begin(); iter != graph_optimizer.end(); ++iter) {
  133. if (iter->first == exclude_core_Type) {
  134. continue;
  135. }
  136. ret = (iter->second)->OptimizeOriginalGraph(*compute_graph);
  137. if (ret != SUCCESS) {
  138. GELOGE(ret, "[OptimizeOriginalGraph]: graph optimize failed, ret:%d", ret);
  139. return ret;
  140. }
  141. }
  142. }
  143. return ret;
  144. }
  145. Status GraphOptimize::OptimizeOriginalGraphJudgeInsert(ComputeGraphPtr &compute_graph) {
  146. GELOGD("OptimizeOriginalGraphJudgeInsert in");
  147. if (GetContext().GetHostExecFlag()) {
  148. // graph exec on host, no need OptimizeOriginalGraph
  149. return SUCCESS;
  150. }
  151. GE_CHECK_NOTNULL(compute_graph);
  152. Status ret = SUCCESS;
  153. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  154. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  155. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "OptimizeOriginalGraph failed.");
  156. return GE_CLI_GE_NOT_INITIALIZED;
  157. }
  158. auto graph_optimizer = instance_ptr->OpsKernelManagerObj().GetAllGraphOptimizerObjsByPriority();
  159. GELOGI("optimize by opskernel in original graph optimize phase. num of graph_optimizer is %lu.",
  160. graph_optimizer.size());
  161. string exclude_core_Type = (core_type_ == kVectorCore) ? kAicoreEngine : kVectorEngine;
  162. if (graph_optimizer.size() != 0) {
  163. for (auto iter = graph_optimizer.begin(); iter != graph_optimizer.end(); ++iter) {
  164. if (iter->first == exclude_core_Type) {
  165. GELOGI("[OptimizeOriginalGraphJudgeInsert]: engine type will exclude: %s", exclude_core_Type.c_str());
  166. continue;
  167. }
  168. GELOGI("Begin to refine running format by engine %s", iter->first.c_str());
  169. ret = (iter->second)->OptimizeOriginalGraphJudgeInsert(*compute_graph);
  170. if (ret != SUCCESS) {
  171. GELOGE(ret, "[OptimizeOriginalGraphJudgeInsert]: graph optimize failed, ret:%d", ret);
  172. return ret;
  173. }
  174. }
  175. }
  176. return ret;
  177. }
  178. Status GraphOptimize::OptimizeOriginalGraphForQuantize(ComputeGraphPtr &compute_graph) {
  179. if (compute_graph == nullptr) {
  180. GELOGE(GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL, "[OptimizeOriginalGraph]: compute_graph is nullptr.");
  181. return GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL;
  182. }
  183. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  184. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  185. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "OptimizeOriginalGraph failed.");
  186. return GE_CLI_GE_NOT_INITIALIZED;
  187. }
  188. auto graph_optimizer = instance_ptr->OpsKernelManagerObj().GetAllGraphOptimizerObjsByPriority();
  189. GELOGI("optimize by opskernel in original graph optimize quantize phase. num of graph_optimizer is %zu.",
  190. graph_optimizer.size());
  191. Status ret = SUCCESS;
  192. string exclude_core_Type = (core_type_ == kVectorCore) ? kAicoreEngine : kVectorEngine;
  193. GELOGD("[OptimizeOriginalGraphForQuantize]: engine type will exclude: %s", exclude_core_Type.c_str());
  194. if (graph_optimizer.size() != 0) {
  195. for (auto iter = graph_optimizer.begin(); iter != graph_optimizer.end(); ++iter) {
  196. if (iter->first == exclude_core_Type || iter->second == nullptr) {
  197. continue;
  198. }
  199. ret = iter->second->OptimizeGraphPrepare(*compute_graph);
  200. if (ret != SUCCESS) {
  201. GELOGE(ret, "[OptimizeOriginalGraphForQuantize]: graph optimize failed, ret:%u", ret);
  202. return ret;
  203. }
  204. }
  205. }
  206. return ret;
  207. }
  208. Status GraphOptimize::OptimizeGraphBeforeBuildForRts(ComputeGraphPtr &compute_graph) {
  209. if (compute_graph == nullptr) {
  210. GELOGE(GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL, "[OptimizeGraphBeforeBuildForRts]: compute_graph is nullptr.");
  211. return GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL;
  212. }
  213. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  214. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  215. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "OptimizeGraphBeforeBuildForRts failed.");
  216. return GE_CLI_GE_NOT_INITIALIZED;
  217. }
  218. auto graph_optimizer = instance_ptr->OpsKernelManagerObj().GetAllGraphOptimizerObjsByPriority();
  219. GELOGI("optimize by opskernel in graph optimize before build phase. num of graph_optimizer is %zu.",
  220. graph_optimizer.size());
  221. Status ret = SUCCESS;
  222. string exclude_core_Type = (core_type_ == kVectorCore) ? kAicoreEngine : kVectorEngine;
  223. GELOGI("[OptimizeGraphBeforeBuildForRts]: engine type will exclude: %s, core_type_: %s", exclude_core_Type.c_str(),
  224. core_type_.c_str());
  225. if (graph_optimizer.size() != 0) {
  226. for (auto iter = graph_optimizer.begin(); iter != graph_optimizer.end(); ++iter) {
  227. if (iter->first == exclude_core_Type || iter->second == nullptr) {
  228. continue;
  229. }
  230. ret = iter->second->OptimizeGraphBeforeBuild(*compute_graph);
  231. if (ret != SUCCESS) {
  232. GELOGE(ret, "[OptimizeGraphBeforeBuildForRts]: graph optimize failed, ret:%u", ret);
  233. return ret;
  234. }
  235. }
  236. }
  237. return ret;
  238. }
  239. Status GraphOptimize::SetOptions(const ge::GraphManagerOptions &options) {
  240. if (options.framework_type >= static_cast<int32_t>(domi::FrameworkType::FRAMEWORK_RESERVED)) {
  241. GELOGE(GE_GRAPH_OPTIONS_INVALID, "Optimize Type %d invalid.", options.framework_type);
  242. return GE_GRAPH_OPTIONS_INVALID;
  243. }
  244. optimize_type_ = static_cast<domi::FrameworkType>(options.framework_type);
  245. cal_config_ = options.calibration_conf_file;
  246. insert_op_config_ = options.insert_op_file;
  247. train_graph_flag_ = options.train_graph_flag;
  248. local_fmk_op_flag_ = options.local_fmk_op_flag;
  249. func_bin_path_ = options.func_bin_path;
  250. core_type_ = options.core_type;
  251. build_mode_ = options.build_mode;
  252. build_step_ = options.build_step;
  253. return SUCCESS;
  254. }
  255. void GraphOptimize::TranFrameOp(ComputeGraphPtr &compute_graph) {
  256. GE_CHECK_NOTNULL_JUST_RETURN(compute_graph);
  257. vector<string> local_framework_op_vec = {
  258. "TensorDataset", "QueueDataset", "DeviceQueueDataset", "ParallelMapDataset", "BatchDatasetV2",
  259. "IteratorV2", "MakeIterator", "IteratorGetNext", "FilterDataset", "MapAndBatchDatasetV2"};
  260. for (auto &nodePtr : compute_graph->GetAllNodes()) {
  261. OpDescPtr op = nodePtr->GetOpDesc();
  262. GE_IF_BOOL_EXEC(op == nullptr, GELOGW("op is nullptr!"); continue);
  263. // fwkop black-white sheet
  264. vector<string>::iterator iter =
  265. std::find(local_framework_op_vec.begin(), local_framework_op_vec.end(), op->GetType());
  266. if (iter != local_framework_op_vec.end()) {
  267. // set - original_type
  268. if (!AttrUtils::SetStr(op, ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE, op->GetType())) {
  269. GELOGW("TranFrameOp SetStr ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE failed");
  270. }
  271. // set - framework_type
  272. // [No need to verify return value]
  273. op->SetType("FrameworkOp");
  274. if (!AttrUtils::SetInt(op, ATTR_NAME_FRAMEWORK_FWK_TYPE, domi::FrameworkType::TENSORFLOW)) {
  275. GELOGW("TranFrameOp SetInt ATTR_NAME_FRAMEWORK_FWK_TYPE failed");
  276. }
  277. }
  278. }
  279. }
  280. Status GraphOptimize::IdentifyReference(ComputeGraphPtr &compute_graph) {
  281. for (auto &node : compute_graph->GetAllNodes()) {
  282. GE_CHECK_NOTNULL(node);
  283. auto op_desc = node->GetOpDesc();
  284. GE_CHECK_NOTNULL(op_desc);
  285. auto input_name_index = op_desc->GetAllInputName();
  286. bool is_ref = false;
  287. for (const auto &name_index : input_name_index) {
  288. const int out_index = op_desc->GetOutputIndexByName(name_index.first);
  289. if (out_index != -1) {
  290. auto input_desc = op_desc->GetInputDesc(name_index.second);
  291. input_desc.SetRefPortByIndex({name_index.second});
  292. op_desc->UpdateInputDesc(name_index.second, input_desc);
  293. GELOGI("SetRefPort: set op[%s] input desc[%u-%s] ref.", op_desc->GetName().c_str(), name_index.second,
  294. name_index.first.c_str());
  295. auto output_desc = op_desc->GetOutputDesc(static_cast<uint32_t>(out_index));
  296. output_desc.SetRefPortByIndex({name_index.second});
  297. op_desc->UpdateOutputDesc(static_cast<uint32_t>(out_index), output_desc);
  298. GELOGI("SetRefPort: set op[%s] output desc[%u-%s] ref.", op_desc->GetName().c_str(), out_index,
  299. name_index.first.c_str());
  300. is_ref = true;
  301. }
  302. }
  303. if (is_ref) {
  304. AttrUtils::SetBool(op_desc, ATTR_NAME_REFERENCE, is_ref);
  305. GELOGI("param [node] %s is reference node, set attribute %s to be true.", node->GetName().c_str(),
  306. ATTR_NAME_REFERENCE.c_str());
  307. }
  308. }
  309. return SUCCESS;
  310. }
  311. } // namespace ge

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