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.

constant_fuse_same_pass.cc 7.2 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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/constant_fuse_same_pass.h"
  17. #include <map>
  18. #include <memory>
  19. #include <string>
  20. #include <vector>
  21. #include "graph/utils/op_desc_utils.h"
  22. #include "graph/utils/type_utils.h"
  23. namespace ge {
  24. namespace {
  25. const size_t kCorrectNum = 1;
  26. const char *const kOriginElementNumAttrName = "origin_element_num";
  27. bool CheckConstInAndOut(const NodePtr &node) {
  28. // has none in control
  29. // has one out data anchor
  30. if ((node->GetInControlNodes().empty()) && (node->GetAllOutDataAnchorsSize() == kCorrectNum)) {
  31. return true;
  32. }
  33. return false;
  34. }
  35. void GetOutDataNodeToIndexMap(NodePtr &node, std::map<string, InDataAnchorPtr> &out_node_to_indexs) {
  36. auto out_data_anchor = node->GetOutDataAnchor(0);
  37. GE_CHECK_NOTNULL_JUST_RETURN(out_data_anchor);
  38. auto peer_in_anchors = out_data_anchor->GetPeerInDataAnchors();
  39. if (!peer_in_anchors.empty()) {
  40. for (auto &anchor : peer_in_anchors) {
  41. int index = anchor->GetIdx();
  42. NodePtr out_node = anchor->GetOwnerNode();
  43. if (out_node == nullptr) {
  44. continue;
  45. }
  46. string key_name = out_node->GetName() + "-" + std::to_string(index);
  47. out_node_to_indexs[key_name] = anchor;
  48. }
  49. }
  50. }
  51. } // namespace
  52. Status ConstantFuseSamePass::Run(ge::ComputeGraphPtr graph) {
  53. if (graph == nullptr) {
  54. GELOGE(GE_GRAPH_PARAM_NULLPTR, "Compute graph is null.");
  55. return GE_GRAPH_PARAM_NULLPTR;
  56. }
  57. GELOGI("ConstantFuseSamePass in.");
  58. std::map<SameConstKey, std::vector<NodePtr>> fuse_nodes;
  59. GetFuseConstNodes(graph, fuse_nodes);
  60. return FuseConstNodes(graph, fuse_nodes);
  61. }
  62. void ConstantFuseSamePass::GetFuseConstNodes(ComputeGraphPtr &graph,
  63. std::map<SameConstKey, std::vector<NodePtr>> &fuse_nodes) {
  64. int total_const_nums = 0;
  65. int insert_const_nums = 0;
  66. for (auto &node : graph->GetDirectNode()) {
  67. if (node->GetType() != CONSTANT && node->GetType() != CONSTANTOP) {
  68. continue;
  69. }
  70. OpDescPtr op_desc = node->GetOpDesc();
  71. if (op_desc == nullptr) {
  72. continue;
  73. }
  74. ++total_const_nums;
  75. if (!CheckConstInAndOut(node)) {
  76. GELOGD("The const %s does not support to fusion, skip it", node->GetName().c_str());
  77. continue;
  78. }
  79. GeTensorPtr weight;
  80. if (!AttrUtils::MutableTensor(op_desc, ATTR_NAME_WEIGHTS, weight)) {
  81. GELOGW("The const node %s does not have weight attr, skip it", node->GetName().c_str());
  82. continue;
  83. }
  84. int64_t origin_element_num = -1;
  85. if (!AttrUtils::GetInt(weight->MutableTensorDesc(), kOriginElementNumAttrName, origin_element_num)) {
  86. GELOGI("The const %s does not have origin element num attribute, skip it", node->GetName().c_str());
  87. continue;
  88. }
  89. if (origin_element_num != 1) {
  90. GELOGI("The const %s origin element num %ld, does not support to fusion now", node->GetName().c_str(),
  91. origin_element_num);
  92. continue;
  93. }
  94. auto output_tensor = op_desc->MutableOutputDesc(0);
  95. if (output_tensor == nullptr) {
  96. GELOGW("The const %s does not have output 0, skip to fusion", node->GetName().c_str());
  97. continue;
  98. }
  99. auto data_type = output_tensor->GetDataType();
  100. auto type_size = GetSizeByDataType(data_type);
  101. if (type_size < 0) {
  102. GELOGI("The data type of const %s does not support fusion, data type %s", node->GetName().c_str(),
  103. TypeUtils::DataTypeToSerialString(data_type).c_str());
  104. continue;
  105. }
  106. if ((type_size != 0) && (weight->MutableData().GetAlignedPtr() == nullptr)) {
  107. GELOGW("aligned_ptr is null while size is not 0");
  108. continue;
  109. }
  110. ++insert_const_nums;
  111. SameConstKey map_key;
  112. map_key.data_size = type_size;
  113. map_key.aligned_ptr = weight->MutableData().GetAlignedPtr();
  114. map_key.data_type = data_type;
  115. map_key.format = output_tensor->GetFormat();
  116. map_key.shape = output_tensor->GetShape().GetDims();
  117. fuse_nodes[map_key].emplace_back(node);
  118. GELOGD("ConstantFuseSamePass, format %s, datatype %s, data_size %d, shape_size %zu. node name %s",
  119. TypeUtils::FormatToSerialString(map_key.format).c_str(),
  120. TypeUtils::DataTypeToSerialString(map_key.data_type).c_str(),
  121. map_key.data_size, map_key.shape.size(), node->GetName().c_str());
  122. }
  123. GELOGI("ConstantFuseSamePass, total_const_nums %d, insert_const_nums %d, fuse_nodes size is %zu.",
  124. total_const_nums, insert_const_nums, fuse_nodes.size());
  125. }
  126. Status ConstantFuseSamePass::MoveOutDataEdges(NodePtr &src_node, NodePtr &dst_node) {
  127. // key is node_name-in_index
  128. std::map<string, InDataAnchorPtr> src_out_node_to_indexs;
  129. GetOutDataNodeToIndexMap(src_node, src_out_node_to_indexs);
  130. if (src_out_node_to_indexs.empty()) {
  131. return SUCCESS;
  132. }
  133. std::map<string, InDataAnchorPtr> dst_out_node_to_indexs;
  134. GetOutDataNodeToIndexMap(dst_node, dst_out_node_to_indexs);
  135. auto dst_out_data_anchor = dst_node->GetOutDataAnchor(0);
  136. GE_CHECK_NOTNULL(dst_out_data_anchor);
  137. auto src_out_data_anchor = src_node->GetOutDataAnchor(0);
  138. GE_CHECK_NOTNULL(src_out_data_anchor);
  139. src_out_data_anchor->UnlinkAll();
  140. for (auto it = src_out_node_to_indexs.begin(); it != src_out_node_to_indexs.end(); ++it) {
  141. if (dst_out_node_to_indexs.count(it->first) > 0) {
  142. continue; // exclusion of duplication
  143. }
  144. auto ret = dst_out_data_anchor->LinkTo(it->second);
  145. if (ret != SUCCESS) {
  146. GELOGE(FAILED, "Failed to move out data edge from %s to %s", src_node->GetName().c_str(),
  147. dst_node->GetName().c_str());
  148. return FAILED;
  149. }
  150. }
  151. return SUCCESS;
  152. }
  153. Status ConstantFuseSamePass::FuseConstNodes(ComputeGraphPtr &graph,
  154. std::map<SameConstKey, std::vector<NodePtr>> &fuse_nodes) {
  155. for (auto iter = fuse_nodes.begin(); iter != fuse_nodes.end(); ++iter) {
  156. auto nodes = iter->second;
  157. size_t len = nodes.size();
  158. auto first_node = nodes.at(0);
  159. for (size_t i = 1; i < len; ++i) {
  160. auto node = nodes.at(i);
  161. GELOGI("Replace redundant const ndoe %s by %s", node->GetName().c_str(), first_node->GetName().c_str());
  162. // the const node which can be fused has none input(both data and control in)
  163. if (GraphUtils::MoveOutCtrlEdges(node, first_node) != SUCCESS) {
  164. return FAILED;
  165. }
  166. if (MoveOutDataEdges(node, first_node) != SUCCESS) {
  167. return FAILED;
  168. }
  169. if (GraphUtils::RemoveNodeWithoutRelink(graph, node) != SUCCESS) {
  170. GELOGE(FAILED, "[%s] RemoveNodeWithoutRelink failed.", node->GetName().c_str());
  171. return FAILED;
  172. }
  173. }
  174. }
  175. return SUCCESS;
  176. }
  177. } // namespace ge

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