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.

switch_dead_branch_elimination.cc 7.3 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
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 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
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/switch_dead_branch_elimination.h"
  17. #include <string>
  18. #include <vector>
  19. #include "framework/common/debug/ge_log.h"
  20. #include "graph/common/omg_util.h"
  21. #include "graph/passes/pass_utils.h"
  22. #include "graph/utils/graph_utils.h"
  23. namespace ge {
  24. namespace {
  25. const std::vector<int>::size_type kDataInputIndex = 0;
  26. const std::vector<int>::size_type kPredInputIndex = 1;
  27. const int kDefaultInputIndex = -1;
  28. bool ParsePred(const ConstGeTensorPtr &tensor) {
  29. if (tensor == nullptr) {
  30. REPORT_INNER_ERROR("E19999", "Param tensor is nullptr, check invalid");
  31. GELOGE(FAILED, "parameter is null.");
  32. return false;
  33. }
  34. const uint8_t *data_ptr = tensor->GetData().data();
  35. auto type = tensor->GetTensorDesc().GetDataType();
  36. switch (type) {
  37. case DT_BOOL:
  38. return *reinterpret_cast<const bool *>(data_ptr);
  39. case DT_FLOAT:
  40. return static_cast<bool>(*reinterpret_cast<const float *>(data_ptr));
  41. case DT_DOUBLE:
  42. return static_cast<bool>(*reinterpret_cast<const double *>(data_ptr));
  43. case DT_INT8:
  44. case DT_UINT8:
  45. return static_cast<bool>(*data_ptr);
  46. case DT_FLOAT16:
  47. case DT_INT16:
  48. case DT_UINT16:
  49. return static_cast<bool>(*reinterpret_cast<const int16_t *>(data_ptr));
  50. case DT_INT32:
  51. case DT_UINT32:
  52. return static_cast<bool>(*reinterpret_cast<const int32_t *>(data_ptr));
  53. case DT_INT64:
  54. case DT_UINT64:
  55. return static_cast<bool>(*reinterpret_cast<const int64_t *>(data_ptr));
  56. default:
  57. return static_cast<bool>(*data_ptr);
  58. }
  59. }
  60. bool ParseOutDataAnchors(const NodePtr &node, const NodePtr &pred_node, OutDataAnchorPtr &active_out_data_anchor,
  61. OutDataAnchorPtr &inactive_out_data_anchor) {
  62. auto tensors = OpDescUtils::MutableWeights(pred_node);
  63. if (tensors.empty()) {
  64. REPORT_INNER_ERROR("E19999", "Node:%s(%s) has no weight, check invalid",
  65. pred_node->GetName().c_str(), pred_node->GetType().c_str());
  66. return false;
  67. }
  68. bool pred_value = ParsePred(tensors[0]);
  69. int inactive_output_index = pred_value ? 0 : 1;
  70. if (node == nullptr) {
  71. REPORT_INNER_ERROR("E19999", "Param node is nullptr, check invalid");
  72. GELOGE(FAILED, "parameter is null.");
  73. return false;
  74. }
  75. GELOGI("[%s] Inactive output index = %d", node->GetName().c_str(), inactive_output_index);
  76. for (const auto &out_anchor : node->GetAllOutDataAnchors()) {
  77. if (out_anchor->GetIdx() == inactive_output_index) {
  78. inactive_out_data_anchor = out_anchor;
  79. } else {
  80. active_out_data_anchor = out_anchor;
  81. }
  82. }
  83. return true;
  84. }
  85. } // namespace
  86. Status SwitchDeadBranchElimination::DeleteSwitchNode(NodePtr &node, NodePtr &pred_node,
  87. const OutDataAnchorPtr &active_out_data_anchor) {
  88. if (node == nullptr || active_out_data_anchor == nullptr) {
  89. REPORT_INNER_ERROR("E19999", "Param node or active_out_data_anchor is nullptr, check invalid");
  90. GELOGE(FAILED, "parameter is null.");
  91. return FAILED;
  92. }
  93. // If two nodes aren't in same graph, get node's direct in_node instead of pred_node.
  94. if (node->GetOwnerComputeGraph() != pred_node->GetOwnerComputeGraph()) {
  95. pred_node = PassUtils::GetInDataNode(node, kPredInputIndex);
  96. }
  97. // link pred's in control nodes to switch
  98. if (GraphUtils::CopyInCtrlEdges(pred_node, node) != GRAPH_SUCCESS) {
  99. REPORT_CALL_ERROR("E19999", "Copy in control edge from node:%s(%s) to node:%s(%s) failed",
  100. pred_node->GetName().c_str(), pred_node->GetType().c_str(),
  101. node->GetName().c_str(), node->GetType().c_str());
  102. return FAILED;
  103. }
  104. // Remove link between pred and switch
  105. auto in_pred_anchor = node->GetInDataAnchor(kPredInputIndex);
  106. GE_CHECK_NOTNULL(in_pred_anchor);
  107. in_pred_anchor->UnlinkAll();
  108. /// If condition Const is isolate, it will be delete with pruning
  109. /// Isolate Switch and delete it
  110. std::vector<int> switch_io_map = {kDefaultInputIndex, kDefaultInputIndex};
  111. size_t out_index = static_cast<size_t>(active_out_data_anchor->GetIdx());
  112. if (out_index >= switch_io_map.size()) {
  113. REPORT_INNER_ERROR("E19999", "Out index:%zu of node:%s(%s) >= %zu, check invalid", out_index,
  114. node->GetName().c_str(), node->GetType().c_str(), switch_io_map.size());
  115. GELOGE(FAILED, "[%s] out index check failed, out_index:%zu.", node->GetName().c_str(), out_index);
  116. return FAILED;
  117. }
  118. switch_io_map[out_index] = kDataInputIndex;
  119. return IsolateAndDeleteNode(node, switch_io_map);
  120. }
  121. Status SwitchDeadBranchElimination::Run(NodePtr &node) {
  122. if (node == nullptr) {
  123. REPORT_INNER_ERROR("E19999", "Param node is nullptr, check invalid");
  124. GELOGE(PARAM_INVALID, "Param [node] must not be null.");
  125. return PARAM_INVALID;
  126. }
  127. std::string op_type;
  128. GE_CHK_STATUS_RET(GetOriginalType(node, op_type), "Get original type failed");
  129. if ((op_type != SWITCH) && (op_type != REFSWITCH)) {
  130. return SUCCESS;
  131. }
  132. if (node->GetOutAllNodes().empty()) {
  133. return SUCCESS;
  134. }
  135. auto pred_node = PassUtils::GetInNodeCrossSubgraphByIndex(node, kPredInputIndex);
  136. if (pred_node == nullptr) {
  137. GELOGD("[%s] Pred input is null.", node->GetName().c_str());
  138. return SUCCESS;
  139. }
  140. // Can be optimized when pred is constant
  141. if (!PassUtils::IsConstant(pred_node)) {
  142. GELOGD("[%s] Pred is not constant.", node->GetName().c_str());
  143. return SUCCESS;
  144. }
  145. auto input_node = PassUtils::GetInNodeCrossSubgraphByIndex(node, kDataInputIndex);
  146. if (input_node == nullptr) {
  147. GELOGD("[%s] Data input is null.", node->GetName().c_str());
  148. return SUCCESS;
  149. }
  150. // Get active & inactive output anchors by the value of pred
  151. OutDataAnchorPtr active_out_data_anchor = nullptr;
  152. OutDataAnchorPtr inactive_out_data_anchor = nullptr;
  153. if (!ParseOutDataAnchors(node, pred_node, active_out_data_anchor, inactive_out_data_anchor)) {
  154. return PARAM_INVALID;
  155. }
  156. if (inactive_out_data_anchor != nullptr) {
  157. GELOGI("[%s] To unlink inactive output %d", node->GetName().c_str(), inactive_out_data_anchor->GetIdx());
  158. std::vector<NodePtr> del_nodes;
  159. std::vector<NodePtr> end_nodes;
  160. Status ret = PassUtils::RemoveInactiveBranchToMerge(inactive_out_data_anchor, del_nodes, end_nodes);
  161. if (ret != SUCCESS) {
  162. REPORT_CALL_ERROR("E19999", "Remove inactive branch from node:%s(%s) to merge failed",
  163. node->GetName().c_str(), node->GetType().c_str());
  164. return ret;
  165. }
  166. for (auto &end_node : end_nodes) {
  167. AddRePassNode(end_node);
  168. }
  169. for (const auto &delete_node : del_nodes) {
  170. AddNodeDeleted(delete_node);
  171. }
  172. }
  173. return DeleteSwitchNode(node, pred_node, active_out_data_anchor);
  174. }
  175. } // namespace ge

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