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.

merge_pass.cc 9.3 kB

5 years ago
5 years ago
modified: ge/graph/passes/base_pass.cc modified: ge/graph/passes/base_pass.h modified: ge/graph/passes/folding_pass.cc modified: ge/graph/passes/infershape_pass.cc modified: ge/graph/passes/infershape_pass.h modified: ge/graph/passes/switch_dead_branch_elimination.cc modified: ge/graph/preprocess/graph_preprocess.cc modified: ge/graph/passes/base_pass.cc modified: ge/graph/passes/base_pass.h modified: ge/graph/passes/folding_pass.cc modified: ge/graph/passes/infer_base_pass.h modified: ge/graph/passes/infer_value_range_pass.cc modified: ge/graph/passes/infer_value_range_pass.h modified: ge/graph/passes/infershape_pass.cc modified: ge/graph/passes/infershape_pass.h modified: ge/graph/passes/switch_dead_branch_elimination.cc modified: ge/graph/preprocess/graph_preprocess.cc modified: ge/graph/passes/base_pass.cc modified: ge/graph/passes/base_pass.h modified: ge/graph/passes/folding_pass.cc modified: ge/graph/passes/infer_base_pass.h modified: ge/graph/passes/infer_value_range_pass.cc modified: ge/graph/passes/infer_value_range_pass.h modified: ge/graph/passes/infershape_pass.cc modified: ge/graph/passes/infershape_pass.h modified: ge/graph/passes/merge_pass.cc modified: ge/graph/passes/switch_dead_branch_elimination.cc modified: ge/graph/preprocess/graph_preprocess.cc modified: ge/graph/passes/base_pass.cc modified: ge/graph/passes/base_pass.h modified: ge/graph/passes/folding_pass.cc modified: ge/graph/passes/infer_base_pass.h modified: ge/graph/passes/infer_value_range_pass.cc modified: ge/graph/passes/infer_value_range_pass.h modified: ge/graph/passes/infershape_pass.cc modified: ge/graph/passes/infershape_pass.h modified: ge/graph/passes/merge_pass.cc modified: ge/graph/passes/switch_dead_branch_elimination.cc modified: ge/graph/preprocess/graph_preprocess.cc modified: ge/graph/passes/base_pass.cc modified: ge/graph/passes/base_pass.h modified: ge/graph/passes/folding_pass.cc modified: ge/graph/passes/infer_base_pass.h modified: ge/graph/passes/infer_value_range_pass.cc modified: ge/graph/passes/infer_value_range_pass.h modified: ge/graph/passes/infershape_pass.cc modified: ge/graph/passes/infershape_pass.h modified: ge/graph/passes/merge_pass.cc modified: ge/graph/passes/switch_dead_branch_elimination.cc modified: ge/graph/preprocess/graph_preprocess.cc
4 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
5 years ago
modified: ge/graph/passes/base_pass.cc modified: ge/graph/passes/base_pass.h modified: ge/graph/passes/folding_pass.cc modified: ge/graph/passes/infershape_pass.cc modified: ge/graph/passes/infershape_pass.h modified: ge/graph/passes/switch_dead_branch_elimination.cc modified: ge/graph/preprocess/graph_preprocess.cc modified: ge/graph/passes/base_pass.cc modified: ge/graph/passes/base_pass.h modified: ge/graph/passes/folding_pass.cc modified: ge/graph/passes/infer_base_pass.h modified: ge/graph/passes/infer_value_range_pass.cc modified: ge/graph/passes/infer_value_range_pass.h modified: ge/graph/passes/infershape_pass.cc modified: ge/graph/passes/infershape_pass.h modified: ge/graph/passes/switch_dead_branch_elimination.cc modified: ge/graph/preprocess/graph_preprocess.cc modified: ge/graph/passes/base_pass.cc modified: ge/graph/passes/base_pass.h modified: ge/graph/passes/folding_pass.cc modified: ge/graph/passes/infer_base_pass.h modified: ge/graph/passes/infer_value_range_pass.cc modified: ge/graph/passes/infer_value_range_pass.h modified: ge/graph/passes/infershape_pass.cc modified: ge/graph/passes/infershape_pass.h modified: ge/graph/passes/merge_pass.cc modified: ge/graph/passes/switch_dead_branch_elimination.cc modified: ge/graph/preprocess/graph_preprocess.cc modified: ge/graph/passes/base_pass.cc modified: ge/graph/passes/base_pass.h modified: ge/graph/passes/folding_pass.cc modified: ge/graph/passes/infer_base_pass.h modified: ge/graph/passes/infer_value_range_pass.cc modified: ge/graph/passes/infer_value_range_pass.h modified: ge/graph/passes/infershape_pass.cc modified: ge/graph/passes/infershape_pass.h modified: ge/graph/passes/merge_pass.cc modified: ge/graph/passes/switch_dead_branch_elimination.cc modified: ge/graph/preprocess/graph_preprocess.cc modified: ge/graph/passes/base_pass.cc modified: ge/graph/passes/base_pass.h modified: ge/graph/passes/folding_pass.cc modified: ge/graph/passes/infer_base_pass.h modified: ge/graph/passes/infer_value_range_pass.cc modified: ge/graph/passes/infer_value_range_pass.h modified: ge/graph/passes/infershape_pass.cc modified: ge/graph/passes/infershape_pass.h modified: ge/graph/passes/merge_pass.cc modified: ge/graph/passes/switch_dead_branch_elimination.cc modified: ge/graph/preprocess/graph_preprocess.cc
4 years ago
4 years ago
modified: ge/graph/passes/base_pass.cc modified: ge/graph/passes/base_pass.h modified: ge/graph/passes/folding_pass.cc modified: ge/graph/passes/infershape_pass.cc modified: ge/graph/passes/infershape_pass.h modified: ge/graph/passes/switch_dead_branch_elimination.cc modified: ge/graph/preprocess/graph_preprocess.cc modified: ge/graph/passes/base_pass.cc modified: ge/graph/passes/base_pass.h modified: ge/graph/passes/folding_pass.cc modified: ge/graph/passes/infer_base_pass.h modified: ge/graph/passes/infer_value_range_pass.cc modified: ge/graph/passes/infer_value_range_pass.h modified: ge/graph/passes/infershape_pass.cc modified: ge/graph/passes/infershape_pass.h modified: ge/graph/passes/switch_dead_branch_elimination.cc modified: ge/graph/preprocess/graph_preprocess.cc modified: ge/graph/passes/base_pass.cc modified: ge/graph/passes/base_pass.h modified: ge/graph/passes/folding_pass.cc modified: ge/graph/passes/infer_base_pass.h modified: ge/graph/passes/infer_value_range_pass.cc modified: ge/graph/passes/infer_value_range_pass.h modified: ge/graph/passes/infershape_pass.cc modified: ge/graph/passes/infershape_pass.h modified: ge/graph/passes/merge_pass.cc modified: ge/graph/passes/switch_dead_branch_elimination.cc modified: ge/graph/preprocess/graph_preprocess.cc modified: ge/graph/passes/base_pass.cc modified: ge/graph/passes/base_pass.h modified: ge/graph/passes/folding_pass.cc modified: ge/graph/passes/infer_base_pass.h modified: ge/graph/passes/infer_value_range_pass.cc modified: ge/graph/passes/infer_value_range_pass.h modified: ge/graph/passes/infershape_pass.cc modified: ge/graph/passes/infershape_pass.h modified: ge/graph/passes/merge_pass.cc modified: ge/graph/passes/switch_dead_branch_elimination.cc modified: ge/graph/preprocess/graph_preprocess.cc modified: ge/graph/passes/base_pass.cc modified: ge/graph/passes/base_pass.h modified: ge/graph/passes/folding_pass.cc modified: ge/graph/passes/infer_base_pass.h modified: ge/graph/passes/infer_value_range_pass.cc modified: ge/graph/passes/infer_value_range_pass.h modified: ge/graph/passes/infershape_pass.cc modified: ge/graph/passes/infershape_pass.h modified: ge/graph/passes/merge_pass.cc modified: ge/graph/passes/switch_dead_branch_elimination.cc modified: ge/graph/preprocess/graph_preprocess.cc
4 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
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**
  2. * Copyright 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/passes/merge_pass.h"
  17. #include <memory>
  18. #include <string>
  19. #include <vector>
  20. #include "framework/common/debug/ge_log.h"
  21. #include "common/ge/ge_util.h"
  22. #include "graph/common/omg_util.h"
  23. #include "graph/debug/ge_attr_define.h"
  24. #include "graph/utils/graph_utils.h"
  25. #include "graph/passes/pass_utils.h"
  26. namespace ge {
  27. const int kValueIndexOutputIndex = 1;
  28. const size_t kCaseNoInput = 0;
  29. const size_t kCaseOneInput = 1;
  30. const bool kWillRepassImmediately = true;
  31. Status MergePass::Run(NodePtr &node) {
  32. GELOGD("MergePass running");
  33. if (node == nullptr) {
  34. REPORT_INNER_ERROR("E19999", "Param node is nullptr, check invalid");
  35. GELOGE(PARAM_INVALID, "[Check][Param] param [node] must not be null.");
  36. return PARAM_INVALID;
  37. }
  38. std::string op_type;
  39. GE_CHK_STATUS_RET(GetOriginalType(node, op_type), "[Get][OriginalType] of node:%s failed", node->GetName().c_str());
  40. if (op_type != MERGE) {
  41. return SUCCESS;
  42. }
  43. if (node->GetAllOutDataAnchors().empty()) {
  44. REPORT_INNER_ERROR("E19999", "Param node:%s(%s) all data anchor size is 0, check invalid",
  45. node->GetName().c_str(), node->GetType().c_str());
  46. GELOGE(PARAM_INVALID, "[Check][Param] Param node:%s(%s) all data anchor size is 0",
  47. node->GetName().c_str(), node->GetType().c_str());
  48. return PARAM_INVALID;
  49. }
  50. const auto &in_data_nodes = node->GetInDataNodes();
  51. switch (in_data_nodes.size()) {
  52. case kCaseNoInput: {
  53. /// Case A: input_count = 0, the output of merge node is inactive as well
  54. /// In which case the output branch can be removed
  55. /// until another merge node is met
  56. std::vector<NodePtr> del_nodes;
  57. std::vector<NodePtr> end_nodes;
  58. Status ret = PassUtils::RemoveBranch(node, del_nodes, end_nodes);
  59. for (auto &end_node : end_nodes) {
  60. AddRePassNode(end_node);
  61. }
  62. for (const auto &delete_node : del_nodes) {
  63. AddNodeDeleted(delete_node);
  64. }
  65. return ret;
  66. }
  67. case kCaseOneInput: { // Case B: input_count = 1, the merge node can be optimized out
  68. std::vector<int> merge_io_map = {PassUtils::GetUniqueInDataAnchorIndex(node), -1};
  69. if (merge_io_map[0] != -1 && IsNeedChangeIndexToConstant(node)) {
  70. int index = merge_io_map[0];
  71. if (ChangeIndexToConstant(node, index) != SUCCESS) {
  72. GELOGE(FAILED, "[Change][Index] to be Constant failed, node:%s.", node->GetName().c_str());
  73. return FAILED;
  74. }
  75. }
  76. auto in_node = in_data_nodes.at(0);
  77. if (IsMergeInputNeedOptimized(in_node)) {
  78. if (IsolateAndDeleteNode(in_node, {0}, kWillRepassImmediately) != SUCCESS) {
  79. REPORT_CALL_ERROR("E19999", "Isolate and delete node:%s(%s) failed",
  80. in_node->GetName().c_str(), in_node->GetType().c_str());
  81. GELOGE(FAILED, "[Remove][Node] %s failed.", in_node->GetName().c_str());
  82. return FAILED;
  83. }
  84. }
  85. return IsolateAndDeleteNode(node, merge_io_map, kWillRepassImmediately);
  86. }
  87. default: {
  88. // Case C: input_count > 1, the merge node can not be optimized
  89. return SUCCESS;
  90. }
  91. }
  92. }
  93. bool MergePass::IsNeedChangeIndexToConstant(NodePtr &node) const {
  94. /// value_index is the index 1 output of the Merge
  95. /// value_index link to other node, change it to be Constant
  96. GE_IF_BOOL_EXEC(node == nullptr, GELOGW("Node is nullptr"); return false);
  97. auto out_anchor = node->GetOutDataAnchor(kValueIndexOutputIndex);
  98. GE_IF_BOOL_EXEC(out_anchor == nullptr, GELOGW("Out_anchor is nullptr"); return false);
  99. for (const auto &peer_in_anchor : out_anchor->GetPeerInDataAnchors()) {
  100. if (peer_in_anchor != nullptr && peer_in_anchor->GetOwnerNode() != nullptr) {
  101. GELOGI(
  102. "[%s] MergePass, value_index link to other node, "
  103. "change it to be Constant.",
  104. node->GetName().c_str());
  105. return true;
  106. }
  107. }
  108. return false;
  109. }
  110. Status MergePass::ChangeIndexToConstant(NodePtr &node, int &value_index) {
  111. GE_CHECK_NOTNULL(node);
  112. ComputeGraphPtr graph = node->GetOwnerComputeGraph();
  113. if (graph == nullptr) {
  114. REPORT_INNER_ERROR("E19999", "Owner graph of node:%s(%s) is nullptr, check invalid",
  115. node->GetName().c_str(), node->GetType().c_str());
  116. GELOGE(FAILED, "[Get][ComputeGraph] failed, Owner graph of node:%s(%s) is nullptr.",
  117. node->GetName().c_str(), node->GetType().c_str());
  118. return FAILED;
  119. }
  120. OpDescPtr constant_op_desc = nullptr;
  121. if (CreateConstByValue(node, value_index, constant_op_desc) != SUCCESS) {
  122. return FAILED;
  123. }
  124. NodePtr const_node = graph->AddNode(constant_op_desc);
  125. if (const_node == nullptr) {
  126. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  127. constant_op_desc->GetName().c_str(), constant_op_desc->GetType().c_str(),
  128. graph->GetName().c_str());
  129. return FAILED;
  130. }
  131. // Change peer in anchors from value_index to new Constant node
  132. if (GraphUtils::ReplaceNodeAnchors(const_node, node, {}, {1}) != GRAPH_SUCCESS) {
  133. REPORT_CALL_ERROR("E19999", "Replace node:%s(%s) by node:%s(%s) failed",
  134. node->GetName().c_str(), node->GetType().c_str(),
  135. const_node->GetName().c_str(), const_node->GetType().c_str());
  136. GELOGE(FAILED, "[Replace][Node] %s(%s) by node:%s(%s) failed",
  137. node->GetName().c_str(), node->GetType().c_str(),
  138. const_node->GetName().c_str(), const_node->GetType().c_str());
  139. return FAILED;
  140. }
  141. auto out_control_anchor = node->GetOutControlAnchor();
  142. GE_CHECK_NOTNULL(out_control_anchor);
  143. // Add control anchor between Merge and Constant
  144. if (out_control_anchor->LinkTo(const_node->GetInControlAnchor()) != GRAPH_SUCCESS) {
  145. REPORT_CALL_ERROR("E19999", "Op:%s(%s) link control to op:%s(%s) failed",
  146. node->GetName().c_str(), node->GetType().c_str(),
  147. const_node->GetName().c_str(), const_node->GetType().c_str());
  148. return FAILED;
  149. }
  150. return SUCCESS;
  151. }
  152. Status MergePass::CreateConstByValue(NodePtr &node, int value_index, OpDescPtr &op_desc) {
  153. std::string constant_name = node->GetName() + "_value_index";
  154. // 1. create Constant OpDesc
  155. op_desc = MakeShared<OpDesc>(constant_name, CONSTANT);
  156. if (op_desc == nullptr) {
  157. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  158. GELOGE(FAILED, "[New][OpDesc] failed, name:%s.", constant_name.c_str());
  159. return FAILED;
  160. }
  161. // 2. get OpDesc of output number one of Merge(value_index)
  162. OpDescPtr original_op_desc = node->GetOpDesc();
  163. if (original_op_desc == nullptr) {
  164. REPORT_INNER_ERROR("E19999", "OpDesc in node is nullptr, check invalid");
  165. GELOGE(FAILED, "[Get][OpDesc] failed, Op desc must not be null.");
  166. return FAILED;
  167. }
  168. GeTensorDesc original_out_tensor_desc = original_op_desc->GetOutputDesc(1);
  169. original_out_tensor_desc.SetDataType(DT_INT32);
  170. // 3. create attr value of Constant, is a tensor
  171. GeTensorPtr const_tensor_ptr =
  172. MakeShared<GeTensor>(original_out_tensor_desc, reinterpret_cast<uint8_t *>(&value_index), sizeof(int));
  173. if (const_tensor_ptr == nullptr) {
  174. REPORT_CALL_ERROR("E19999", "New GeTensor failed");
  175. GELOGE(FAILED, "[New][GeTensor] failed.");
  176. return FAILED;
  177. }
  178. GE_IF_BOOL_EXEC(!AttrUtils::SetTensor(op_desc, ATTR_NAME_WEIGHTS, const_tensor_ptr),
  179. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_NAME_WEIGHTS.c_str(),
  180. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  181. GELOGE(FAILED, "[Set][Attr] %s to op:%s(%s) failed", ATTR_NAME_WEIGHTS.c_str(),
  182. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  183. return FAILED);
  184. // 4. set Constant output desc
  185. GE_CHK_GRAPH_STATUS_RET(op_desc->AddOutputDesc(original_out_tensor_desc),
  186. "[Add][OutputDesc] to op:%s(%s) failed",
  187. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  188. return SUCCESS;
  189. }
  190. bool MergePass::IsMergeInputNeedOptimized(NodePtr &node) const {
  191. if (node == nullptr) {
  192. return false;
  193. }
  194. // node is not inserted by MergeInputMemcpyPass
  195. if ((node->GetType() != MEMCPYASYNC) && (node->GetType() != MEMCPYADDRASYNC)) {
  196. return false;
  197. }
  198. if (node->GetInDataNodes().size() != 1) {
  199. return false;
  200. }
  201. auto in_node = node->GetInDataNodes().at(0);
  202. if (in_node == nullptr) {
  203. return false;
  204. }
  205. // in_node may be global_step var
  206. if ((in_node->GetType() == VARIABLE) || (in_node->GetType() == VARIABLEV2)) {
  207. return false;
  208. }
  209. return true;
  210. }
  211. } // namespace ge

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