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.

node_item.cc 9.7 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**
  2. * Copyright 2019-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 "node_item.h"
  17. #include <sstream>
  18. #include "common/debug/log.h"
  19. #include "graph/common/omg_util.h"
  20. #include "graph/compute_graph.h"
  21. #include "graph/debug/ge_attr_define.h"
  22. #include "graph/utils/node_utils.h"
  23. #include "hybrid/node_executor/node_executor.h"
  24. namespace ge {
  25. namespace hybrid {
  26. namespace {
  27. const char *const kAttrNameOriginalFusionGraph = "_original_fusion_graph";
  28. const char *const kNodeTypeRetVal = "_RetVal";
  29. std::set<std::string> kControlOpTypes{
  30. IF, STATELESSIF, CASE, WHILE, STATELESSWHILE
  31. };
  32. Status ParseInputMapping(Node &node, OpDesc &op_desc, FusedSubgraph &fused_subgraph) {
  33. uint32_t parent_index = 0;
  34. if (!AttrUtils::GetInt(op_desc, ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  35. GELOGE(FAILED,
  36. "[%s] Failed to get attr [%s]",
  37. op_desc.GetName().c_str(),
  38. ATTR_NAME_PARENT_NODE_INDEX.c_str());
  39. return FAILED;
  40. }
  41. for (auto &node_and_anchor : node.GetOutDataNodesAndAnchors()) {
  42. auto dst_op_desc = node_and_anchor.first->GetOpDesc();
  43. GE_CHECK_NOTNULL(dst_op_desc);
  44. auto in_idx = node_and_anchor.second->GetIdx();
  45. auto tensor_desc = dst_op_desc->MutableInputDesc(in_idx);
  46. fused_subgraph.input_mapping[parent_index].emplace_back(tensor_desc);
  47. GELOGD("Input[%u] mapped to [%s:%u]", parent_index, dst_op_desc->GetName().c_str(), in_idx);
  48. }
  49. return SUCCESS;
  50. }
  51. Status ParseOutputMapping(const OpDescPtr &op_desc, FusedSubgraph &fused_subgraph) {
  52. uint32_t parent_index = 0;
  53. if (!AttrUtils::GetInt(op_desc, ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  54. GELOGE(FAILED,
  55. "[%s] Failed to get attr [%s]",
  56. op_desc->GetName().c_str(),
  57. ATTR_NAME_PARENT_NODE_INDEX.c_str());
  58. return FAILED;
  59. }
  60. fused_subgraph.output_mapping.emplace(parent_index, op_desc);
  61. return SUCCESS;
  62. }
  63. Status ParseFusedSubgraph(NodeItem &node_item) {
  64. if (!node_item.op_desc->HasAttr(kAttrNameOriginalFusionGraph)) {
  65. return SUCCESS;
  66. }
  67. GELOGI("[%s] Start to parse fused subgraph.", node_item.node_name.c_str());
  68. auto fused_subgraph = std::unique_ptr<FusedSubgraph>(new(std::nothrow)FusedSubgraph());
  69. GE_CHECK_NOTNULL(fused_subgraph);
  70. ComputeGraphPtr fused_graph;
  71. (void) AttrUtils::GetGraph(*node_item.op_desc, kAttrNameOriginalFusionGraph, fused_graph);
  72. GE_CHECK_NOTNULL(fused_graph);
  73. fused_graph->SetGraphUnknownFlag(true);
  74. fused_subgraph->graph = fused_graph;
  75. GE_CHK_GRAPH_STATUS_RET(fused_graph->TopologicalSorting());
  76. for (auto &node : fused_graph->GetAllNodes()) {
  77. GE_CHECK_NOTNULL(node);
  78. auto op_desc = node->GetOpDesc();
  79. GE_CHECK_NOTNULL(op_desc);
  80. std::string node_type;
  81. GE_CHK_STATUS_RET(GetOriginalType(node, node_type));
  82. if (node_type == DATA) {
  83. GE_CHK_GRAPH_STATUS_RET(ParseInputMapping(*node, *op_desc, *fused_subgraph));
  84. } else if (node_type == kNodeTypeRetVal) {
  85. GE_CHK_GRAPH_STATUS_RET(ParseOutputMapping(op_desc, *fused_subgraph));
  86. } else {
  87. fused_subgraph->nodes.emplace_back(node);
  88. }
  89. }
  90. node_item.fused_subgraph = std::move(fused_subgraph);
  91. GELOGI("[%s] Done parsing fused subgraph successfully.", node_item.NodeName().c_str());
  92. return SUCCESS;
  93. }
  94. } // namespace
  95. bool IsControlOp(const std::string &op_type) {
  96. return kControlOpTypes.count(op_type) > 0;
  97. }
  98. NodeItem::NodeItem(NodePtr node) : node(std::move(node)) {
  99. this->op_desc = this->node->GetOpDesc().get();
  100. this->node_name = this->node->GetName();
  101. this->node_type = this->node->GetType();
  102. }
  103. Status NodeItem::Create(const NodePtr &node, std::unique_ptr<NodeItem> &node_item) {
  104. GE_CHECK_NOTNULL(node);
  105. GE_CHECK_NOTNULL(node->GetOpDesc());
  106. std::unique_ptr<NodeItem> instance(new(std::nothrow)NodeItem(node));
  107. GE_CHECK_NOTNULL(instance);
  108. GE_CHK_STATUS_RET(instance->Init(), "Failed to init NodeItem [%s] .", node->GetName().c_str());
  109. node_item = std::move(instance);
  110. return SUCCESS;
  111. }
  112. Status NodeItem::Init() {
  113. GE_CHECK_LE(op_desc->GetInputsSize(), INT32_MAX);
  114. GE_CHECK_LE(op_desc->GetOutputsSize(), INT32_MAX);
  115. num_inputs = static_cast<int>(op_desc->GetInputsSize());
  116. num_outputs = static_cast<int>(op_desc->GetOutputsSize());
  117. if (op_desc->GetAllInputsSize() != op_desc->GetInputsSize()) {
  118. has_optional_inputs = true;
  119. for (size_t i = 0; i < op_desc->GetAllInputsSize(); ++i) {
  120. const auto &input_desc = op_desc->MutableInputDesc(i);
  121. if (input_desc == nullptr) {
  122. GELOGD("[%s] Input[%zu] is optional and invalid", NodeName().c_str(), i);
  123. } else {
  124. input_desc_indices_.emplace_back(static_cast<uint32_t>(i));
  125. }
  126. }
  127. }
  128. (void) AttrUtils::GetBool(op_desc, ATTR_NAME_FORCE_UNKNOWN_SHAPE, is_dynamic);
  129. GELOGD("node name = %s, is_dynamic = %d.", this->node_name.c_str(), is_dynamic);
  130. if (!is_dynamic) {
  131. GE_CHK_STATUS_RET(NodeUtils::GetNodeUnknownShapeStatus(*node, is_dynamic),
  132. "[%s] Failed to get shape status.",
  133. node->GetName().c_str());
  134. }
  135. if (is_dynamic) {
  136. for (int i = 0; i < num_inputs; ++i) {
  137. const auto &input_desc = MutableInputDesc(i);
  138. GE_CHECK_NOTNULL(input_desc);
  139. if (input_desc->MutableShape().IsUnknownShape()) {
  140. is_input_shape_static_.push_back(false);
  141. } else {
  142. num_static_input_shapes++;
  143. is_input_shape_static_.push_back(true);
  144. GELOGD("[%s] The shape of input[%d] is static. shape = [%s]",
  145. NodeName().c_str(), i, input_desc->MutableShape().ToString().c_str());
  146. }
  147. }
  148. for (int i = 0; i < num_outputs; ++i) {
  149. const auto &output_desc = op_desc->MutableOutputDesc(i);
  150. GE_CHECK_NOTNULL(output_desc);
  151. if (output_desc->MutableShape().IsUnknownShape()) {
  152. is_output_shape_static = false;
  153. break;
  154. }
  155. }
  156. if (IsControlOp() || node_type == PARTITIONEDCALL) {
  157. shape_inference_type = DEPEND_COMPUTE;
  158. } else {
  159. int32_t unknown_shape_type_val = 0;
  160. (void) AttrUtils::GetInt(op_desc, ::ge::ATTR_NAME_UNKNOWN_SHAPE_TYPE, unknown_shape_type_val);
  161. shape_inference_type = static_cast<UnknowShapeOpType>(unknown_shape_type_val);
  162. }
  163. GE_CHK_STATUS_RET(ParseFusedSubgraph(*this), "[%s] Failed to parse fused subgraph", node_name.c_str());
  164. }
  165. return SUCCESS;
  166. }
  167. bool NodeItem::IsControlOp() const {
  168. return ge::hybrid::IsControlOp(op_desc->GetType());
  169. }
  170. std::string NodeItem::DebugString() const {
  171. std::stringstream ss;
  172. ss << "Node: ";
  173. ss << "id = " << node_id;
  174. ss << ", name = [" << node->GetName();
  175. ss << "], type = " << node->GetType();
  176. ss << ", is_dynamic = " << (is_dynamic ? "True" : "False");
  177. ss << ", is_output_static = " << (is_output_shape_static ? "True" : "False");
  178. ss << ", unknown_shape_op_type = " << shape_inference_type;
  179. ss << ", input_start = " << input_start;
  180. ss << ", num_inputs = " << num_inputs;
  181. ss << ", output_start = " << output_start;
  182. ss << ", num_outputs = " << num_outputs;
  183. ss << ", dependent_nodes = [";
  184. for (const auto &dep_node : dependents_for_shape_inference) {
  185. ss << dep_node->GetName() << ", ";
  186. }
  187. ss << "]";
  188. int index = 0;
  189. for (auto &items : outputs) {
  190. ss << ", output[" << index++ << "]: ";
  191. for (auto &item : items) {
  192. ss << "(" << item.second->NodeName() << ":" << item.first << "), ";
  193. }
  194. }
  195. return ss.str();
  196. }
  197. void NodeItem::SetToDynamic() {
  198. num_static_input_shapes = 0;
  199. is_dynamic = true;
  200. for (size_t i = 0; i < is_input_shape_static_.size(); ++i) {
  201. is_input_shape_static_[i] = false;
  202. }
  203. if (kernel_task != nullptr && !kernel_task->IsSupportDynamicShape()) {
  204. GELOGD("[%s] Dynamic shape is not supported, clear node task.", node_name.c_str());
  205. kernel_task = nullptr;
  206. }
  207. }
  208. GeTensorDescPtr NodeItem::MutableInputDesc(int index) const {
  209. if (!has_optional_inputs) {
  210. return op_desc->MutableInputDesc(static_cast<uint32_t>(index));
  211. }
  212. if (index < 0 || index >= num_inputs) {
  213. GELOGE(PARAM_INVALID,
  214. "[%s] Invalid input index, num inputs = %d, index = %d",
  215. node_name.c_str(),
  216. num_inputs,
  217. index);
  218. return nullptr;
  219. }
  220. return op_desc->MutableInputDesc(input_desc_indices_[index]);
  221. }
  222. Status NodeItem::GetCanonicalInputIndex(uint32_t index, int &canonical_index) const {
  223. if (!has_optional_inputs) {
  224. canonical_index = index;
  225. return SUCCESS;
  226. }
  227. auto iter = std::find(input_desc_indices_.begin(), input_desc_indices_.end(), index);
  228. if (iter == input_desc_indices_.end()) {
  229. GELOGE(INTERNAL_ERROR, "[%s] Invalid input index: %u", node_name.c_str(), index);
  230. return INTERNAL_ERROR;
  231. }
  232. canonical_index = static_cast<int>(iter - input_desc_indices_.begin());
  233. GELOGD("[%s] Canonicalize input index from [%u] to [%d]", node_name.c_str(), index, canonical_index);
  234. return SUCCESS;
  235. }
  236. bool NodeItem::IsInputShapeStatic(int index) const {
  237. if (!is_dynamic) {
  238. return true;
  239. }
  240. if (static_cast<size_t>(index) >= is_input_shape_static_.size()) {
  241. GELOGE(PARAM_INVALID, "Input index(%d) out of range: [0, %zu)", index, is_input_shape_static_.size());
  242. return false;
  243. }
  244. return is_input_shape_static_[index];
  245. }
  246. } // namespace hybrid
  247. } // namespace ge

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