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.3 kB

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

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