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 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "common/ge/ge_util.h"
  18. #include "ge/ge_api_types.h"
  19. #include "graph/common/omg_util.h"
  20. namespace ge {
  21. Status MergeInputMemcpyPass::Run(ComputeGraphPtr graph) {
  22. GELOGD("MergeInputMemcpyPass Enter");
  23. for (const auto &node : graph->GetDirectNode()) {
  24. std::string type;
  25. GE_CHK_STATUS_RET(GetOriginalType(node, type), "Get node type failed.");
  26. if ((type != MERGE) && (type != REFMERGE)) {
  27. continue;
  28. }
  29. GE_CHECK_NOTNULL(node->GetOpDesc());
  30. GE_CHK_STATUS_RET(AddMemcpyAsyncNodes(graph, node, node->GetOpDesc()->HasAttr(ATTR_INSERT_BY_MBATCH)),
  31. "Merge add memcpy node failed.");
  32. }
  33. GELOGD("MergeInputMemcpyPass Leave");
  34. return SUCCESS;
  35. }
  36. ///
  37. /// @brief Add MemcpyAsync Op as Merge in_node
  38. /// @param [in] graph
  39. /// @param [in] node
  40. /// @param [in] multi_batch_flag
  41. /// @return Status
  42. ///
  43. Status MergeInputMemcpyPass::AddMemcpyAsyncNodes(const ComputeGraphPtr &graph, const NodePtr &node,
  44. bool multi_batch_flag) {
  45. for (const InDataAnchorPtr &in_data_anchor : node->GetAllInDataAnchors()) {
  46. OutDataAnchorPtr peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  47. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  48. NodePtr in_node = peer_out_anchor->GetOwnerNode();
  49. const std::string &type = in_node->GetType();
  50. // For WhileLoop no need memcpy for merge.
  51. GE_IF_BOOL_EXEC((type == ENTER) || (type == REFENTER) || (type == NEXTITERATION) || (type == REFNEXTITERATION),
  52. continue);
  53. const std::string &memcpy_name = node->GetName() + "_input_" + std::to_string(in_data_anchor->GetIdx());
  54. NodePtr memcpy_node = CreateMemcpyAsyncNode(graph, memcpy_name, peer_out_anchor, multi_batch_flag);
  55. GE_CHK_BOOL_EXEC(memcpy_node != nullptr, return FAILED, "Create MemcpyAsync node failed.");
  56. GE_CHK_STATUS(GraphUtils::RemoveEdge(peer_out_anchor, in_data_anchor), "MemcpyAsync node remove edge failed.");
  57. GE_CHK_STATUS(GraphUtils::AddEdge(peer_out_anchor, memcpy_node->GetInDataAnchor(0)),
  58. "MemcpyAsync node add edge failed.");
  59. GE_CHK_STATUS(GraphUtils::AddEdge(memcpy_node->GetOutDataAnchor(0), in_data_anchor),
  60. "MemcpyAsync node add edge failed.");
  61. }
  62. return SUCCESS;
  63. }
  64. ///
  65. /// @brief Add MemcpyAsync Node
  66. /// @param [in] graph
  67. /// @param [in] name
  68. /// @param [in] out_data_anchor
  69. /// @param [in] multi_batch_flag
  70. /// @return ge::NodePtr
  71. ///
  72. NodePtr MergeInputMemcpyPass::CreateMemcpyAsyncNode(const ComputeGraphPtr &graph, const std::string &name,
  73. const OutDataAnchorPtr &out_data_anchor, bool multi_batch_flag) {
  74. OpDescPtr pre_op_desc = out_data_anchor->GetOwnerNode()->GetOpDesc();
  75. GE_CHK_BOOL_EXEC(pre_op_desc != nullptr, return nullptr, "OpDesc of pre node is invalid.");
  76. const std::string &memcpy_type = multi_batch_flag ? MEMCPYADDRASYNC : MEMCPYASYNC;
  77. const std::string &node_name = name + "_" + memcpy_type;
  78. GELOGI("Create MemcpyAsync op:%s.", node_name.c_str());
  79. OpDescPtr op_desc = MakeShared<OpDesc>(node_name, memcpy_type);
  80. if (op_desc == nullptr) {
  81. GELOGE(FAILED, "Create op_desc failed, MemcpyAsync:%s.", node_name.c_str());
  82. return nullptr;
  83. }
  84. GE_CHK_BOOL_EXEC(op_desc->AddInputDesc(pre_op_desc->GetOutputDesc(out_data_anchor->GetIdx())) == GRAPH_SUCCESS,
  85. return nullptr, "Create MemcpyAsync op: add input desc failed.");
  86. GE_CHK_BOOL_EXEC(op_desc->AddOutputDesc(pre_op_desc->GetOutputDesc(out_data_anchor->GetIdx())) == GRAPH_SUCCESS,
  87. return nullptr, "Create MemcpyAsync op: add output desc failed.");
  88. return graph->AddNode(op_desc);
  89. }
  90. } // namespace ge

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