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

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

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