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.9 kB

5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. REPORT_INNER_ERROR("E19999", "Param graph is nullptr, check invalid");
  55. GELOGE(GE_GRAPH_PARAM_NULLPTR, "Compute graph is null.");
  56. return GE_GRAPH_PARAM_NULLPTR;
  57. }
  58. GELOGI("ConstantFuseSamePass in.");
  59. std::map<SameConstKey, std::vector<NodePtr>> fuse_nodes;
  60. GetFuseConstNodes(graph, fuse_nodes);
  61. return FuseConstNodes(graph, fuse_nodes);
  62. }
  63. void ConstantFuseSamePass::GetFuseConstNodes(ComputeGraphPtr &graph,
  64. std::map<SameConstKey, std::vector<NodePtr>> &fuse_nodes) {
  65. int total_const_nums = 0;
  66. int insert_const_nums = 0;
  67. for (auto &node : graph->GetDirectNode()) {
  68. if (node->GetType() != CONSTANT && node->GetType() != CONSTANTOP) {
  69. continue;
  70. }
  71. OpDescPtr op_desc = node->GetOpDesc();
  72. if (op_desc == nullptr) {
  73. continue;
  74. }
  75. ++total_const_nums;
  76. if (!CheckConstInAndOut(node)) {
  77. GELOGD("The const %s does not support to fusion, skip it", node->GetName().c_str());
  78. continue;
  79. }
  80. GeTensorPtr weight;
  81. if (!AttrUtils::MutableTensor(op_desc, ATTR_NAME_WEIGHTS, weight)) {
  82. GELOGW("The const node %s does not have weight attr, skip it", node->GetName().c_str());
  83. continue;
  84. }
  85. int64_t origin_element_num = -1;
  86. if (!AttrUtils::GetInt(weight->MutableTensorDesc(), kOriginElementNumAttrName, origin_element_num)) {
  87. GELOGI("The const %s does not have origin element num attribute, skip it", node->GetName().c_str());
  88. continue;
  89. }
  90. if (origin_element_num != 1) {
  91. GELOGI("The const %s origin element num %ld, does not support to fusion now", node->GetName().c_str(),
  92. origin_element_num);
  93. continue;
  94. }
  95. auto output_tensor = op_desc->MutableOutputDesc(0);
  96. if (output_tensor == nullptr) {
  97. GELOGW("The const %s does not have output 0, skip to fusion", node->GetName().c_str());
  98. continue;
  99. }
  100. auto data_type = output_tensor->GetDataType();
  101. auto type_size = GetSizeByDataType(data_type);
  102. if (type_size < 0) {
  103. GELOGI("The data type of const %s does not support fusion, data type %s", node->GetName().c_str(),
  104. TypeUtils::DataTypeToSerialString(data_type).c_str());
  105. continue;
  106. }
  107. if ((type_size != 0) && (weight->MutableData().GetAlignedPtr() == nullptr)) {
  108. GELOGW("aligned_ptr is null while size is not 0");
  109. continue;
  110. }
  111. ++insert_const_nums;
  112. SameConstKey map_key;
  113. map_key.data_size = type_size;
  114. map_key.aligned_ptr = weight->MutableData().GetAlignedPtr();
  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. REPORT_CALL_ERROR("E19999",
  148. "Op:%s(%s) out index:0 link to op:%s(%s) in index:%d failed",
  149. dst_node->GetName().c_str(), dst_node->GetType().c_str(),
  150. it->second->GetOwnerNode()->GetName().c_str(), it->second->GetOwnerNode()->GetType().c_str(),
  151. it->second->GetIdx());
  152. GELOGE(FAILED, "Failed to move out data edge from %s to %s", src_node->GetName().c_str(),
  153. dst_node->GetName().c_str());
  154. return FAILED;
  155. }
  156. }
  157. return SUCCESS;
  158. }
  159. Status ConstantFuseSamePass::FuseConstNodes(ComputeGraphPtr &graph,
  160. std::map<SameConstKey, std::vector<NodePtr>> &fuse_nodes) {
  161. for (auto iter = fuse_nodes.begin(); iter != fuse_nodes.end(); ++iter) {
  162. auto nodes = iter->second;
  163. size_t len = nodes.size();
  164. auto first_node = nodes.at(0);
  165. for (size_t i = 1; i < len; ++i) {
  166. auto node = nodes.at(i);
  167. GELOGI("Replace redundant const ndoe %s by %s", node->GetName().c_str(), first_node->GetName().c_str());
  168. // the const node which can be fused has none input(both data and control in)
  169. if (GraphUtils::MoveOutCtrlEdges(node, first_node) != SUCCESS) {
  170. return FAILED;
  171. }
  172. if (MoveOutDataEdges(node, first_node) != SUCCESS) {
  173. return FAILED;
  174. }
  175. if (GraphUtils::RemoveNodeWithoutRelink(graph, node) != SUCCESS) {
  176. REPORT_CALL_ERROR("E19999", "Remove node:%s(%s) without relink in graph:%s failed",
  177. node->GetName().c_str(), node->GetType().c_str(), graph->GetName().c_str());
  178. GELOGE(FAILED, "[%s] RemoveNodeWithoutRelink failed.", node->GetName().c_str());
  179. return FAILED;
  180. }
  181. }
  182. }
  183. return SUCCESS;
  184. }
  185. } // namespace ge

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