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_input_memcpy_pass.cc 8.4 kB

4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/passes/merge_input_memcpy_pass.h"
  17. #include <queue>
  18. #include "common/ge/ge_util.h"
  19. #include "ge/ge_api_types.h"
  20. #include "graph/common/omg_util.h"
  21. namespace ge {
  22. namespace {
  23. const std::set<std::string> kLoopMergeInputs{
  24. ENTER, REFENTER, NEXTITERATION, REFNEXTITERATION
  25. };
  26. }
  27. Status MergeInputMemcpyPass::Run(ComputeGraphPtr graph) {
  28. GELOGD("MergeInputMemcpyPass Enter");
  29. std::unordered_map<NodePtr, std::vector<NodePtr>> switch_groups;
  30. for (const auto &node : graph->GetDirectNode()) {
  31. std::string type;
  32. GE_CHK_STATUS_RET(GetOriginalType(node, type), "Get node type failed.");
  33. if ((type != MERGE) && (type != REFMERGE)) {
  34. continue;
  35. }
  36. GE_CHECK_NOTNULL(node->GetOpDesc());
  37. GE_CHK_STATUS_RET(AddMemcpyAsyncNodes(graph, node, node->GetOpDesc()->HasAttr(ATTR_INSERT_BY_MBATCH)),
  38. "Merge add memcpy node failed.");
  39. CollectSwitchGroup(node, switch_groups);
  40. }
  41. MarkUnknownForSwitch(switch_groups);
  42. GELOGD("MergeInputMemcpyPass Leave");
  43. return SUCCESS;
  44. }
  45. ///
  46. /// @brief Add MemcpyAsync Op as Merge in_node
  47. /// @param [in] graph
  48. /// @param [in] node
  49. /// @param [in] multi_batch_flag
  50. /// @return Status
  51. ///
  52. Status MergeInputMemcpyPass::AddMemcpyAsyncNodes(const ComputeGraphPtr &graph, const NodePtr &node,
  53. bool multi_batch_flag) {
  54. for (const InDataAnchorPtr &in_data_anchor : node->GetAllInDataAnchors()) {
  55. OutDataAnchorPtr peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  56. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  57. NodePtr in_node = peer_out_anchor->GetOwnerNode();
  58. const std::string &type = in_node->GetType();
  59. // For WhileLoop no need memcpy for merge.
  60. GE_IF_BOOL_EXEC((type == ENTER) || (type == REFENTER) || (type == NEXTITERATION) || (type == REFNEXTITERATION),
  61. continue);
  62. const std::string &memcpy_name = node->GetName() + "_input_" + std::to_string(in_data_anchor->GetIdx());
  63. NodePtr memcpy_node = CreateMemcpyAsyncNode(graph, memcpy_name, peer_out_anchor, multi_batch_flag);
  64. GE_CHK_BOOL_EXEC(memcpy_node != nullptr, return FAILED, "Create MemcpyAsync node failed.");
  65. GE_CHK_STATUS(GraphUtils::RemoveEdge(peer_out_anchor, in_data_anchor),
  66. "MemcpyAsync node remove edge failed.");
  67. GE_CHK_STATUS(GraphUtils::AddEdge(peer_out_anchor, memcpy_node->GetInDataAnchor(0)),
  68. "MemcpyAsync node add edge failed.");
  69. GE_CHK_STATUS(GraphUtils::AddEdge(memcpy_node->GetOutDataAnchor(0), in_data_anchor),
  70. "MemcpyAsync node add edge failed.");
  71. }
  72. return SUCCESS;
  73. }
  74. ///
  75. /// @brief Add MemcpyAsync Node
  76. /// @param [in] graph
  77. /// @param [in] name
  78. /// @param [in] out_data_anchor
  79. /// @param [in] multi_batch_flag
  80. /// @return ge::NodePtr
  81. ///
  82. NodePtr MergeInputMemcpyPass::CreateMemcpyAsyncNode(const ComputeGraphPtr &graph, const std::string &name,
  83. const OutDataAnchorPtr &out_data_anchor, bool multi_batch_flag) {
  84. OpDescPtr pre_op_desc = out_data_anchor->GetOwnerNode()->GetOpDesc();
  85. GE_CHK_BOOL_EXEC(pre_op_desc != nullptr, return nullptr, "OpDesc of pre node is invalid.");
  86. const std::string &memcpy_type = multi_batch_flag ? MEMCPYADDRASYNC : MEMCPYASYNC;
  87. const std::string &node_name = name + "_" + memcpy_type;
  88. GELOGI("Create MemcpyAsync op:%s.", node_name.c_str());
  89. OpDescPtr op_desc = MakeShared<OpDesc>(node_name, memcpy_type);
  90. if (op_desc == nullptr) {
  91. GELOGE(FAILED, "Create op_desc failed, MemcpyAsync:%s.", node_name.c_str());
  92. return nullptr;
  93. }
  94. GE_CHK_BOOL_EXEC(op_desc->AddInputDesc(pre_op_desc->GetOutputDesc(out_data_anchor->GetIdx())) == GRAPH_SUCCESS,
  95. REPORT_CALL_ERROR("E19999", "Add input to op:%s(%s) failed",
  96. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  97. return nullptr, "Create MemcpyAsync op: add input desc failed.");
  98. GE_CHK_BOOL_EXEC(op_desc->AddOutputDesc(pre_op_desc->GetOutputDesc(out_data_anchor->GetIdx())) == GRAPH_SUCCESS,
  99. REPORT_CALL_ERROR("E19999", "Add output to op:%s(%s) failed",
  100. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  101. return nullptr, "Create MemcpyAsync op: add output desc failed.");
  102. return graph->AddNode(op_desc);
  103. }
  104. ///
  105. /// @brief Mark force unknown shape for Switch node
  106. /// @param [in] merge node
  107. /// @param [out] switch_groups
  108. /// @return
  109. ///
  110. void MergeInputMemcpyPass::CollectSwitchGroup(const NodePtr &node,
  111. std::unordered_map<NodePtr, std::vector<NodePtr>> &switch_groups) {
  112. const auto &op_desc = node->GetOpDesc();
  113. for (const auto &in_anchor : node->GetAllInDataAnchors()) {
  114. const auto &src_out_anchor = in_anchor->GetPeerOutAnchor();
  115. if (src_out_anchor == nullptr) {
  116. continue;
  117. }
  118. std::string node_type;
  119. GetOriginalType(src_out_anchor->GetOwnerNode(), node_type);
  120. if (kLoopMergeInputs.count(node_type) > 0) {
  121. return;
  122. }
  123. }
  124. // Switch --> {Switch --> Merge} --> Merge
  125. std::queue<std::pair<NodePtr, uint32_t>> search_queue;
  126. search_queue.push({node, 0});
  127. std::vector<NodePtr> &switch_group = switch_groups[node];
  128. while (!search_queue.empty()) {
  129. const auto dst_node = search_queue.front().first;
  130. const auto dst_span = search_queue.front().second;
  131. search_queue.pop();
  132. // Switch --> Identity --> Constant
  133. for (const auto &in_ctrl_node : dst_node->GetInControlNodes()) {
  134. if (in_ctrl_node->GetType() == IDENTITY) {
  135. GELOGD("Travel node: %s, In control: %s, span is: %u",
  136. dst_node->GetName().c_str(), in_ctrl_node->GetName().c_str(), dst_span);
  137. search_queue.push({in_ctrl_node, dst_span});
  138. }
  139. }
  140. for (const auto &in_data_node : dst_node->GetInDataNodes()) {
  141. std::string node_type;
  142. GetOriginalType(in_data_node, node_type);
  143. GELOGD("Travel node: %s, %s node: %s, span is: %u",
  144. dst_node->GetName().c_str(), node_type.c_str(), in_data_node->GetName().c_str(), dst_span);
  145. if (node_type == SWITCH || node_type == REFSWITCH) {
  146. if (dst_span > 0) {
  147. search_queue.push({in_data_node, dst_span - 1});
  148. } else {
  149. switch_group.emplace_back(in_data_node);
  150. }
  151. } else if (node_type == MERGE || node_type == REFMERGE) {
  152. search_queue.push({in_data_node, dst_span + 1});
  153. } else {
  154. search_queue.push({in_data_node, dst_span});
  155. }
  156. }
  157. }
  158. if (IsUnknownShapeTensor(op_desc->GetOutputDesc(0)) || op_desc->HasAttr(ATTR_NAME_FORCE_UNKNOWN_SHAPE)) {
  159. GELOGI("Mark [%s] as for unknown shape, switch groups: %zu", node->GetName().c_str(), switch_groups.size());
  160. MarkForceUnknownShape(node, true);
  161. for (const auto &n : switch_group) {
  162. MarkForceUnknownShape(n, true);
  163. }
  164. }
  165. }
  166. void MergeInputMemcpyPass::MarkUnknownForSwitch(const std::unordered_map<NodePtr, std::vector<NodePtr>> &switch_groups) {
  167. std::function<bool(const NodePtr &)> callback = [](const NodePtr &n) {
  168. return n->GetOpDesc()->HasAttr(ATTR_NAME_FORCE_UNKNOWN_SHAPE);
  169. };
  170. for (const auto &item : switch_groups) {
  171. const auto &node = item.first;
  172. if (node->GetOpDesc()->HasAttr(ATTR_NAME_FORCE_UNKNOWN_SHAPE)) {
  173. continue;
  174. }
  175. const std::vector<NodePtr> &switch_group = item.second;
  176. if (std::any_of(switch_group.begin(), switch_group.end(), callback)) {
  177. GELOGI("Mark [%s] as force unknown shape, switch nodes: %zu", node->GetName().c_str(), switch_group.size());
  178. MarkForceUnknownShape(node, true);
  179. for (const auto &n : switch_group) {
  180. MarkForceUnknownShape(n, true);
  181. }
  182. }
  183. }
  184. }
  185. } // namespace ge

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