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.

link_gen_mask_nodes_pass.cc 5.7 kB

5 years ago
5 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/link_gen_mask_nodes_pass.h"
  17. #include <set>
  18. #include "common/ge_inner_error_codes.h"
  19. #include "framework/common/debug/ge_log.h"
  20. #include "framework/common/types.h"
  21. #include "init/gelib.h"
  22. using std::set;
  23. using std::vector;
  24. namespace ge {
  25. namespace {
  26. const size_t kGenMaskInputIndex = 1;
  27. const size_t kDefaultMaxParallelNum = 1;
  28. } // namespace
  29. LinkGenMaskNodesPass::LinkGenMaskNodesPass(const map<string, int> &stream_max_parallel_num)
  30. : GraphPass(), stream_max_parallel_num_(stream_max_parallel_num) {}
  31. // GenMask is the second input of DoMask and one GenMask's output may be used by multiple DoMask.
  32. // We will control the order of GenMask according to the order of the first DoMask.
  33. Status LinkGenMaskNodesPass::Run(ComputeGraphPtr graph) {
  34. GE_CHECK_NOTNULL(graph);
  35. vector<NodePtr> gen_mask_nodes;
  36. GetAllGenMaskNodes(graph, gen_mask_nodes);
  37. size_t gen_mask_group_size = gen_mask_nodes.size();
  38. Status status = GetGenMaskGroupSize(gen_mask_nodes, gen_mask_group_size);
  39. if (status != SUCCESS) {
  40. GELOGE(FAILED, "Get GenMask group size failed.");
  41. return FAILED;
  42. }
  43. if (gen_mask_group_size < 1) {
  44. gen_mask_group_size = 1;
  45. }
  46. for (size_t index = 1; index < gen_mask_nodes.size(); ++index) {
  47. if (index % gen_mask_group_size == 0) {
  48. GELOGI("skiped index: %zu.", index);
  49. continue;
  50. }
  51. NodePtr &src_node = gen_mask_nodes[index - 1];
  52. auto src_anchor = src_node->GetOutControlAnchor();
  53. GE_CHECK_NOTNULL(src_anchor);
  54. NodePtr &dest_node = gen_mask_nodes[index];
  55. auto dest_anchor = dest_node->GetInControlAnchor();
  56. GE_CHECK_NOTNULL(dest_anchor);
  57. graphStatus status_link_to = src_anchor->LinkTo(dest_anchor);
  58. if (status_link_to != GRAPH_SUCCESS) {
  59. REPORT_CALL_ERROR("E19999", "Op:%s(%s) link control to op:%s(%s) failed",
  60. src_node->GetName().c_str(), src_node->GetType().c_str(),
  61. dest_node->GetName().c_str(), dest_node->GetType().c_str());
  62. GELOGE(FAILED, "Link from %s to %s failed.", src_node->GetName().c_str(), dest_node->GetName().c_str());
  63. return FAILED;
  64. }
  65. GELOGD("Link from %s to %s.", src_node->GetName().c_str(), dest_node->GetName().c_str());
  66. }
  67. return SUCCESS;
  68. }
  69. // [pointer can not be null]
  70. bool LinkGenMaskNodesPass::AreAllInputsConst(const NodePtr &node) const {
  71. for (const NodePtr &in_node : node->GetInDataNodes()) {
  72. string op_type = in_node->GetType();
  73. if ((op_type != CONSTANT) && (op_type != CONSTANTOP)) {
  74. return false;
  75. }
  76. }
  77. return true;
  78. }
  79. void LinkGenMaskNodesPass::GetAllGenMaskNodes(ComputeGraphPtr graph, vector<NodePtr> &gen_mask_nodes) const {
  80. set<NodePtr> nodes_set;
  81. for (const NodePtr &node : graph->GetDirectNode()) {
  82. if (node->GetType() != DROPOUTDOMASK && node->GetType() != DROPOUTDOMASKV3 &&
  83. node->GetType() != DROPOUTDOMASKV3D && node->GetType() != SOFTMAXV2WITHDROPOUTDOMASKV3D) {
  84. continue;
  85. }
  86. if ((node->GetOpDesc() == nullptr) || (node->GetOpDesc()->HasAttr(ATTR_NAME_STREAM_LABEL))) {
  87. continue;
  88. }
  89. auto in_data_nodes = node->GetInDataNodes();
  90. if (in_data_nodes.size() > kGenMaskInputIndex) {
  91. NodePtr &gen_mask = in_data_nodes.at(kGenMaskInputIndex);
  92. for (auto &in_data_node : in_data_nodes) {
  93. // node gen_mask is located at different place in the fused node
  94. if (in_data_node->GetName().find(DROPOUTGENMASK) != in_data_node->GetName().npos) {
  95. gen_mask = in_data_node;
  96. GELOGD("The fused node type [%s], paired with the input node name [%s].",
  97. node->GetType().c_str(), gen_mask->GetName().c_str());
  98. break;
  99. }
  100. }
  101. if ((gen_mask->GetOpDesc() == nullptr) || (gen_mask->GetOpDesc()->HasAttr(ATTR_NAME_STREAM_LABEL))) {
  102. continue;
  103. }
  104. if (AreAllInputsConst(gen_mask) && nodes_set.count(gen_mask) == 0) {
  105. gen_mask_nodes.emplace_back(gen_mask);
  106. nodes_set.emplace(gen_mask);
  107. }
  108. }
  109. }
  110. }
  111. Status LinkGenMaskNodesPass::GetGenMaskGroupSize(vector<NodePtr> &gen_mask_nodes, size_t &gen_mask_group_size) const {
  112. if (gen_mask_nodes.empty()) {
  113. return SUCCESS;
  114. }
  115. NodePtr gen_mask_node = gen_mask_nodes.front();
  116. GE_CHECK_NOTNULL(gen_mask_node);
  117. OpDescPtr gen_mask_op = gen_mask_node->GetOpDesc();
  118. GE_CHECK_NOTNULL(gen_mask_op);
  119. auto ge_lib = GELib::GetInstance();
  120. if ((ge_lib != nullptr) && ge_lib->InitFlag()) {
  121. (void)ge_lib->DNNEngineManagerObj().GetDNNEngineName(gen_mask_node);
  122. }
  123. size_t gen_mask_group_num = kDefaultMaxParallelNum;
  124. string engine_name = gen_mask_op->GetOpEngineName();
  125. auto iter = stream_max_parallel_num_.find(engine_name);
  126. if (iter != stream_max_parallel_num_.end()) {
  127. gen_mask_group_num = static_cast<size_t>(iter->second);
  128. }
  129. GELOGI("gen_mask_group_num: %zu.", gen_mask_group_num);
  130. if (gen_mask_group_num > 0) {
  131. gen_mask_group_size = (gen_mask_nodes.size() + 1) / gen_mask_group_num;
  132. }
  133. GELOGI("gen_mask_group_size: %zu.", gen_mask_group_size);
  134. return SUCCESS;
  135. }
  136. } // namespace ge

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