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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 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/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. GELOGE(FAILED, "Link from %s to %s failed.", src_node->GetName().c_str(), dest_node->GetName().c_str());
  60. return FAILED;
  61. }
  62. GELOGD("Link from %s to %s.", src_node->GetName().c_str(), dest_node->GetName().c_str());
  63. }
  64. return SUCCESS;
  65. }
  66. // [pointer can not be null]
  67. bool LinkGenMaskNodesPass::AreAllInputsConst(const NodePtr &node) const {
  68. for (const NodePtr &in_node : node->GetInDataNodes()) {
  69. string op_type = in_node->GetType();
  70. if ((op_type != CONSTANT) && (op_type != CONSTANTOP)) {
  71. return false;
  72. }
  73. }
  74. return true;
  75. }
  76. void LinkGenMaskNodesPass::GetAllGenMaskNodes(ComputeGraphPtr graph, vector<NodePtr> &gen_mask_nodes) const {
  77. set<NodePtr> nodes_set;
  78. for (const NodePtr &node : graph->GetDirectNode()) {
  79. if (node->GetType() != DROPOUTDOMASK) {
  80. continue;
  81. }
  82. if ((node->GetOpDesc() == nullptr) || (node->GetOpDesc()->HasAttr(ATTR_NAME_STREAM_LABEL))) {
  83. continue;
  84. }
  85. auto in_data_nodes = node->GetInDataNodes();
  86. if (in_data_nodes.size() > kGenMaskInputIndex) {
  87. NodePtr &gen_mask = in_data_nodes.at(kGenMaskInputIndex);
  88. if ((gen_mask->GetOpDesc() == nullptr) || (gen_mask->GetOpDesc()->HasAttr(ATTR_NAME_STREAM_LABEL))) {
  89. continue;
  90. }
  91. if (AreAllInputsConst(gen_mask) && nodes_set.count(gen_mask) == 0) {
  92. gen_mask_nodes.emplace_back(gen_mask);
  93. nodes_set.emplace(gen_mask);
  94. }
  95. }
  96. }
  97. }
  98. Status LinkGenMaskNodesPass::GetGenMaskGroupSize(vector<NodePtr> &gen_mask_nodes, size_t &gen_mask_group_size) const {
  99. if (gen_mask_nodes.empty()) {
  100. return SUCCESS;
  101. }
  102. NodePtr gen_mask_node = gen_mask_nodes.front();
  103. GE_CHECK_NOTNULL(gen_mask_node);
  104. OpDescPtr gen_mask_op = gen_mask_node->GetOpDesc();
  105. GE_CHECK_NOTNULL(gen_mask_op);
  106. auto ge_lib = GELib::GetInstance();
  107. if ((ge_lib != nullptr) && ge_lib->InitFlag()) {
  108. (void)ge_lib->DNNEngineManagerObj().GetDNNEngineName(gen_mask_node);
  109. }
  110. size_t gen_mask_group_num = kDefaultMaxParallelNum;
  111. string engine_name = gen_mask_op->GetOpEngineName();
  112. auto iter = stream_max_parallel_num_.find(engine_name);
  113. if (iter != stream_max_parallel_num_.end()) {
  114. gen_mask_group_num = static_cast<size_t>(iter->second);
  115. }
  116. GELOGI("gen_mask_group_num: %zu.", gen_mask_group_num);
  117. if (gen_mask_group_num > 0) {
  118. gen_mask_group_size = (gen_mask_nodes.size() + 1) / gen_mask_group_num;
  119. }
  120. GELOGI("gen_mask_group_size: %zu.", gen_mask_group_size);
  121. return SUCCESS;
  122. }
  123. } // namespace ge

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