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

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