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.

mark_agnostic_pass.cc 6.0 kB

5 years ago
5 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/mark_agnostic_pass.h"
  17. #include "graph/utils/node_utils.h"
  18. #include "graph/utils/tensor_utils.h"
  19. #include "graph/debug/ge_attr_define.h"
  20. namespace ge {
  21. const size_t kTwoInputNodesSize = 2;
  22. Status MarkAgnosticPass::Run(ComputeGraphPtr graph) {
  23. for (const auto &node : graph->GetDirectNode()) {
  24. auto node_type = NodeUtils::GetNodeType(*node);
  25. if (node_type == SWITCH || node_type == SWITCHN) {
  26. GELOGD("Mark format agnostic and continuous for switch node %s", node->GetName().c_str());
  27. const OpDescPtr op_desc = node->GetOpDesc();
  28. const GeTensorDescPtr op_tensor = op_desc->MutableInputDesc(0);
  29. if (op_tensor == nullptr) {
  30. GELOGD("Op: %s, Index:0,has no input", node->GetName().c_str());
  31. continue;
  32. }
  33. AttrUtils::SetInt(op_tensor, ATTR_NAME_FORMAT_CONTINUOUS, 1);
  34. AttrUtils::SetInt(node->GetOpDesc(), ATTR_NAME_FORMAT_AGNOSTIC, 1);
  35. AttrUtils::SetListInt(node->GetOpDesc(), ATTR_NAME_FORMAT_AGNOSTIC_EXCEPT_INPUT, std::vector<int64_t>({1}));
  36. continue;
  37. }
  38. if (node_type == IDENTITY) {
  39. GELOGD("Mark format agnostic for identity node %s", node->GetName().c_str());
  40. AttrUtils::SetInt(node->GetOpDesc(), ATTR_NAME_FORMAT_AGNOSTIC, 1);
  41. continue;
  42. }
  43. if (node_type == REFMERGE || node_type == REFSWITCH) {
  44. GELOGD("Mark format agnostic for regmerge and refswitch node %s", node->GetName().c_str());
  45. AttrUtils::SetInt(node->GetOpDesc(), ATTR_NAME_FORMAT_AGNOSTIC, 1);
  46. AttrUtils::SetListInt(node->GetOpDesc(), ATTR_NAME_FORMAT_AGNOSTIC_EXCEPT_INPUT, std::vector<int64_t>({1}));
  47. continue;
  48. }
  49. if (node_type == MERGE) {
  50. GELOGD("Mark format agnostic and continuous for merge node %s", node->GetName().c_str());
  51. // Always set continuous attr for merge output 0
  52. GE_CHK_STATUS_RET(SetContinuousAttr(node, {0}));
  53. // Merge-->NetOutput only set merge output 0's continuous attr
  54. const auto &output_nodes = node->GetOutDataNodes();
  55. if (!output_nodes.empty()) {
  56. if (output_nodes.at(0)->GetType() == NETOUTPUT) {
  57. continue;
  58. }
  59. }
  60. // Set format agnostic attr for merge in and out tensordesc
  61. AttrUtils::SetInt(node->GetOpDesc(), ATTR_NAME_FORMAT_AGNOSTIC, 1);
  62. AttrUtils::SetListInt(node->GetOpDesc(), ATTR_NAME_FORMAT_AGNOSTIC_EXCEPT_OUTPUT, std::vector<int64_t>({1}));
  63. // Set attr for enter and nextiteration
  64. if (HandWhileLoop(node) != SUCCESS) {
  65. GELOGE(FAILED, "[Hand][WhileLoop] for node:%s failed.", node->GetName().c_str());
  66. return FAILED;
  67. }
  68. continue;
  69. }
  70. }
  71. return SUCCESS;
  72. }
  73. bool MarkAgnosticPass::IsWhileLoop(const NodePtr &merge_node, NodePtr &enter, NodePtr &next) {
  74. auto node_type = NodeUtils::GetNodeType(*merge_node);
  75. if (node_type != MERGE) {
  76. GELOGW("Node %s type %s is not merge op.", merge_node->GetName().c_str(), node_type.c_str());
  77. return false;
  78. }
  79. /// Enter-----------+
  80. /// +-> Merge
  81. /// NextIteration---+
  82. auto input_nodes = merge_node->GetInDataNodes();
  83. if (input_nodes.size() != kTwoInputNodesSize) {
  84. GELOGD("Node %s type %s with [data input size[%zu]] is not enter-merge-nextiteration target.",
  85. merge_node->GetName().c_str(), node_type.c_str(), input_nodes.size());
  86. return false;
  87. }
  88. auto in_node0 = input_nodes.at(0);
  89. auto in_node1 = input_nodes.at(1);
  90. auto in_type0 = NodeUtils::GetNodeType(in_node0);
  91. auto in_type1 = NodeUtils::GetNodeType(in_node1);
  92. if ((in_type0 != ENTER || in_type1 != NEXTITERATION) && (in_type0 != NEXTITERATION || in_type1 != ENTER)) {
  93. GELOGD("Node %s type %s with [data input0's type %s input1's type %s] is not enter-merge-nextiteration target.",
  94. merge_node->GetName().c_str(), node_type.c_str(), in_type0.c_str(), in_type1.c_str());
  95. return false;
  96. }
  97. enter = in_node0;
  98. next = in_node1;
  99. return true;
  100. }
  101. Status MarkAgnosticPass::HandWhileLoop(const NodePtr &node) {
  102. NodePtr enter = nullptr;
  103. NodePtr next = nullptr;
  104. if (!IsWhileLoop(node, enter, next)) {
  105. return SUCCESS;
  106. }
  107. GE_CHECK_NOTNULL(enter);
  108. GE_CHECK_NOTNULL(next);
  109. // Set continuous attr
  110. GE_CHK_STATUS_RET(SetContinuousAttr(enter, {0}));
  111. GE_CHK_STATUS_RET(SetContinuousAttr(next, {0}));
  112. // Set format agnostic attr
  113. (void)AttrUtils::SetInt(enter->GetOpDesc(), ATTR_NAME_FORMAT_AGNOSTIC, 1);
  114. (void)AttrUtils::SetInt(next->GetOpDesc(), ATTR_NAME_FORMAT_AGNOSTIC, 1);
  115. return SUCCESS;
  116. }
  117. Status MarkAgnosticPass::SetContinuousAttr(const NodePtr &node, const std::vector<uint32_t> &indexes) {
  118. auto op_desc = node->GetOpDesc();
  119. GE_CHECK_NOTNULL(op_desc);
  120. // This flag is for fe performance optimization
  121. (void)AttrUtils::SetBool(op_desc, ATTR_NAME_REFRESH_CONTINUOUS_FLAG, true);
  122. for (auto index : indexes) {
  123. auto out = op_desc->MutableOutputDesc(index);
  124. if (out == nullptr) {
  125. REPORT_INNER_ERROR("E19999", "Op:%s(%s) output:%u desc is nullptr, check invalid",
  126. op_desc->GetName().c_str(), op_desc->GetType().c_str(), index);
  127. GELOGE(FAILED, "[Check][Param]Op:%s(%s) output:%u desc is nullptr",
  128. op_desc->GetName().c_str(), op_desc->GetType().c_str(), index);
  129. return FAILED;
  130. }
  131. // This attr is for out's dtype and format continuous with it's peer input
  132. (void)AttrUtils::SetInt(out, ATTR_NAME_FORMAT_CONTINUOUS, 1);
  133. }
  134. return SUCCESS;
  135. }
  136. } // namespace ge

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