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 6.1 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "common/ge_inner_error_codes.h"
  20. #include "common/types.h"
  21. #include "framework/common/debug/ge_log.h"
  22. #include "graph/common/omg_util.h"
  23. #include "graph/passes/pass_utils.h"
  24. #include "graph/utils/graph_utils.h"
  25. namespace ge {
  26. namespace {
  27. const std::vector<int>::size_type kDataInputIndex = 0;
  28. const std::vector<int>::size_type kPredInputIndex = 1;
  29. const int kDefaultInputIndex = -1;
  30. bool ParsePred(const ConstGeTensorPtr &tensor) {
  31. if (tensor == nullptr) {
  32. GELOGE(FAILED, "parameter is null.");
  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. return false;
  66. }
  67. bool pred_value = ParsePred(tensors[0]);
  68. int inactive_output_index = pred_value ? 0 : 1;
  69. if (node == nullptr) {
  70. GELOGE(FAILED, "parameter is null.");
  71. return false;
  72. }
  73. GELOGI("[%s] Inactive output index = %d", node->GetName().c_str(), inactive_output_index);
  74. for (const auto &out_anchor : node->GetAllOutDataAnchors()) {
  75. if (out_anchor->GetIdx() == inactive_output_index) {
  76. inactive_out_data_anchor = out_anchor;
  77. } else {
  78. active_out_data_anchor = out_anchor;
  79. }
  80. }
  81. return true;
  82. }
  83. } // namespace
  84. Status SwitchDeadBranchElimination::DeleteSwitchNode(NodePtr &node, NodePtr &pred_node,
  85. const OutDataAnchorPtr &active_out_data_anchor) {
  86. if (node == nullptr || active_out_data_anchor == nullptr) {
  87. GELOGE(FAILED, "parameter is null.");
  88. return FAILED;
  89. }
  90. // link pred's in control nodes to switch
  91. if (GraphUtils::CopyInCtrlEdges(pred_node, node) != GRAPH_SUCCESS) {
  92. return FAILED;
  93. }
  94. // Remove link between pred and switch
  95. auto in_pred_anchor = node->GetInDataAnchor(kPredInputIndex);
  96. GE_CHECK_NOTNULL(in_pred_anchor);
  97. in_pred_anchor->UnlinkAll();
  98. /// If condition Const is isolate, it will be delete with pruning
  99. /// Isolate Switch and delete it
  100. std::vector<int> switch_io_map = {kDefaultInputIndex, kDefaultInputIndex};
  101. size_t out_index = static_cast<size_t>(active_out_data_anchor->GetIdx());
  102. if (out_index >= switch_io_map.size()) {
  103. GELOGE(FAILED, "[%s] out index check failed, out_index:%zu.", node->GetName().c_str(), out_index);
  104. return FAILED;
  105. }
  106. switch_io_map[out_index] = kDataInputIndex;
  107. return IsolateAndDeleteNode(node, switch_io_map);
  108. }
  109. Status SwitchDeadBranchElimination::Run(NodePtr &node) {
  110. if (node == nullptr) {
  111. GELOGE(PARAM_INVALID, "Param [node] must not be null.");
  112. return PARAM_INVALID;
  113. }
  114. std::string op_type;
  115. GE_CHK_STATUS_RET(GetOriginalType(node, op_type), "Get original type failed");
  116. if ((op_type != SWITCH) && (op_type != REFSWITCH)) {
  117. return SUCCESS;
  118. }
  119. if (node->GetOutAllNodes().empty()) {
  120. return SUCCESS;
  121. }
  122. auto pred_node = PassUtils::GetInDataNode(node, kPredInputIndex);
  123. if (pred_node == nullptr) {
  124. GELOGD("[%s] Pred input is null.", node->GetName().c_str());
  125. return SUCCESS;
  126. }
  127. // Can be optimized when pred is constant
  128. if (!PassUtils::IsConstant(pred_node)) {
  129. GELOGD("[%s] Pred is not constant.", node->GetName().c_str());
  130. return SUCCESS;
  131. }
  132. auto input_node = PassUtils::GetInDataNode(node, kDataInputIndex);
  133. if (input_node == nullptr) {
  134. GELOGD("[%s] Data input is null.", node->GetName().c_str());
  135. return SUCCESS;
  136. }
  137. // Get active & inactive output anchors by the value of pred
  138. OutDataAnchorPtr active_out_data_anchor = nullptr;
  139. OutDataAnchorPtr inactive_out_data_anchor = nullptr;
  140. if (!ParseOutDataAnchors(node, pred_node, active_out_data_anchor, inactive_out_data_anchor)) {
  141. return PARAM_INVALID;
  142. }
  143. if (inactive_out_data_anchor != nullptr) {
  144. GELOGI("[%s] To unlink inactive output %d", node->GetName().c_str(), inactive_out_data_anchor->GetIdx());
  145. std::vector<NodePtr> del_nodes;
  146. std::vector<NodePtr> end_nodes;
  147. Status ret = PassUtils::RemoveInactiveBranchToMerge(inactive_out_data_anchor, del_nodes, end_nodes);
  148. if (ret != SUCCESS) {
  149. return ret;
  150. }
  151. for (auto &end_node : end_nodes) {
  152. AddRePassNode(end_node);
  153. }
  154. for (const auto &delete_node : del_nodes) {
  155. AddNodeDeleted(delete_node);
  156. }
  157. }
  158. return DeleteSwitchNode(node, pred_node, active_out_data_anchor);
  159. }
  160. } // namespace ge

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