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.

shape_inference_engine.cc 11 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
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 "hybrid/executor/worker/shape_inference_engine.h"
  17. #include "graph/shape_refiner.h"
  18. #include "graph/utils/node_utils.h"
  19. #include "hybrid/node_executor/node_executor.h"
  20. namespace ge {
  21. namespace hybrid {
  22. ShapeInferenceEngine::ShapeInferenceEngine(GraphExecutionContext *execution_context, SubgraphContext *subgraph_context)
  23. : execution_context_(execution_context),
  24. subgraph_context_(subgraph_context) {
  25. }
  26. Status ShapeInferenceEngine::InferShape(NodeState &node_state) {
  27. // Wait for all input shape become valid
  28. GE_CHK_STATUS_RET_NOLOG(node_state.GetShapeInferenceState().AwaitShapesReady(*execution_context_));
  29. auto &node_item = *node_state.GetNodeItem();
  30. // Wait for "const input nodes" if node's shape inference function requires any.
  31. // Even if output shape is static, there are cases that the const-input will be used in OpTiling and Execution
  32. GE_CHK_STATUS_RET_NOLOG(AwaitDependentNodes(node_state));
  33. if (node_item.is_output_shape_static) {
  34. return SUCCESS;
  35. }
  36. if (node_item.fused_subgraph != nullptr) {
  37. return InferShapeForSubgraph(node_item, *node_item.fused_subgraph);
  38. }
  39. // Skip shape inference for node of type DEPEND_COMPUTE
  40. if (node_item.shape_inference_type == DEPEND_COMPUTE) {
  41. GELOGD("[%s] Skipping node with unknown shape type DEPEND_COMPUTE", node_item.NodeName().c_str());
  42. return SUCCESS;
  43. }
  44. // Clear shape range in case shape inference func forgot to do it
  45. if (node_item.shape_inference_type == DEPEND_SHAPE_RANGE) {
  46. // in case InferFunc forgot to reset output shape
  47. for (auto &output_desc : node_item.op_desc->GetAllOutputsDescPtr()) {
  48. output_desc->SetShape(GeShape({UNKNOWN_DIM_NUM}));
  49. }
  50. }
  51. // Do shape inference
  52. GELOGD("[%s] Start to invoke InferShapeAndType", node_item.NodeName().c_str());
  53. {
  54. std::lock_guard<std::mutex> lk(mu_);
  55. RECORD_SHAPE_INFERENCE_EVENT(execution_context_, node_item.NodeName().c_str(), "[InferShapeAndType] Start");
  56. <<<<<<< Updated upstream
  57. =======
  58. <<<<<<< Updated upstream
  59. >>>>>>> Stashed changes
  60. <<<<<<< HEAD
  61. GE_CHK_STATUS_RET(ShapeRefiner::InferShapeAndTypeForRunning(node_item.node, true), "Invoke InferShapeAndType failed.");
  62. =======
  63. GE_CHK_STATUS_RET(ShapeRefiner::InferShapeAndTypeForRunning(node_item.node, true), "Invoke InferShapeAndType failed.");
  64. >>>>>>> efe1779b76b2f4508a8eaa330cd169d19c00813d
  65. <<<<<<< Updated upstream
  66. =======
  67. =======
  68. GE_CHK_STATUS_RET(ShapeRefiner::InferShapeAndTypeForRunning(node_item.node, true), "Invoke InferShapeAndType failed.");
  69. >>>>>>> Stashed changes
  70. >>>>>>> Stashed changes
  71. RECORD_SHAPE_INFERENCE_EVENT(execution_context_, node_item.NodeName().c_str(), "[InferShapeAndType] End");
  72. }
  73. // Check again to make sure shape is valid after shape inference
  74. if (node_item.shape_inference_type != DEPEND_SHAPE_RANGE) {
  75. bool is_unknown_shape = false;
  76. GE_CHK_STATUS_RET(NodeUtils::GetNodeUnknownShapeStatus(*node_item.node, is_unknown_shape),
  77. "Failed to get shape status. node = %s",
  78. node_item.NodeName().c_str());
  79. GE_CHK_BOOL_RET_STATUS(!is_unknown_shape,
  80. INTERNAL_ERROR,
  81. "[%s] Shape is still unknown after shape inference.",
  82. node_item.NodeName().c_str());
  83. }
  84. GELOGD("[%s] [HybridTrace] After shape inference. Node = %s",
  85. node_item.NodeName().c_str(),
  86. node_item.DebugString().c_str());
  87. GELOGD("[%s] InferShapeAndType finished successfully.", node_item.NodeName().c_str());
  88. return SUCCESS;
  89. }
  90. Status ShapeInferenceEngine::AwaitDependentNodes(NodeState &node_state) {
  91. auto &node_item = *node_state.GetNodeItem();
  92. for (auto &src_node : node_item.dependents_for_shape_inference) {
  93. GELOGI("[%s] Start to wait for data dependent node: %s",
  94. node_item.NodeName().c_str(),
  95. src_node->GetName().c_str());
  96. RECORD_SHAPE_INFERENCE_EVENT(execution_context_,
  97. node_item.NodeName().c_str(),
  98. "[AwaitNodeDone] [%s] Start",
  99. src_node->GetName().c_str());
  100. if (!subgraph_context_->Await(src_node)) {
  101. GELOGE(INTERNAL_ERROR, "[%s] Await node failed.", src_node->GetName().c_str());
  102. return INTERNAL_ERROR;
  103. }
  104. RECORD_SHAPE_INFERENCE_EVENT(execution_context_,
  105. node_item.NodeName().c_str(),
  106. "[AwaitNodeDone] [%s] End",
  107. src_node->GetName().c_str());
  108. GELOGI("[%s] Done waiting node.", src_node->GetName().c_str());
  109. }
  110. return SUCCESS;
  111. }
  112. Status ShapeInferenceEngine::PropagateOutputShapes(const NodeItem &node_item) {
  113. if (node_item.is_output_shape_static) {
  114. return SUCCESS;
  115. }
  116. // output shape will not be valid until compute is done.
  117. bool shape_is_future =
  118. node_item.shape_inference_type == DEPEND_SHAPE_RANGE || node_item.shape_inference_type == DEPEND_COMPUTE;
  119. GELOGD("[%s] Start to propagate output shapes. shape_type = %d",
  120. node_item.NodeName().c_str(),
  121. node_item.shape_inference_type);
  122. RECORD_SHAPE_INFERENCE_EVENT(execution_context_, node_item.NodeName().c_str(), "[PropagateOutputShapes] Start");
  123. // propagate each output
  124. for (int i = 0; i < node_item.num_outputs; ++i) {
  125. auto output_desc = node_item.op_desc->MutableOutputDesc(i);
  126. const auto &shape = output_desc->MutableShape();
  127. const auto &ori_shape = output_desc->GetOriginShape();
  128. auto &output_nodes = node_item.outputs[i];
  129. // propagate output to all sub-inputs
  130. for (auto &dst_input_index_and_node : output_nodes) {
  131. auto &dst_node_item = dst_input_index_and_node.second;
  132. auto dst_node_state = subgraph_context_->GetOrCreateNodeState(dst_node_item);
  133. GE_CHECK_NOTNULL(dst_node_state);
  134. GELOGI("[%s] Update dst node [%s], input index = %d",
  135. node_item.NodeName().c_str(),
  136. dst_node_item->NodeName().c_str(),
  137. dst_input_index_and_node.first);
  138. // in case type 3 and 4, shape will be valid after computing is done
  139. if (shape_is_future) {
  140. ShapeFuture future(node_item.node, i, subgraph_context_);
  141. dst_node_state->GetShapeInferenceState().UpdateInputShapeFuture(dst_input_index_and_node.first,
  142. std::move(future));
  143. } else {
  144. dst_node_state->GetShapeInferenceState().UpdateInputShape(dst_input_index_and_node.first, ori_shape, shape);
  145. }
  146. }
  147. }
  148. RECORD_SHAPE_INFERENCE_EVENT(execution_context_, node_item.NodeName().c_str(), "[PropagateOutputShapes] End");
  149. GELOGD("[%s] Propagating output shapes finished successfully.", node_item.NodeName().c_str());
  150. return SUCCESS;
  151. }
  152. Status ShapeInferenceEngine::InferShapeForSubgraph(const NodeItem &node_item, const FusedSubgraph &fused_subgraph) {
  153. GELOGD("[%s] Start to infer shape by fused subgraph", node_item.NodeName().c_str());
  154. for (auto &it : fused_subgraph.input_mapping) {
  155. auto parent_tensor_desc = node_item.op_desc->MutableInputDesc(it.first);
  156. GE_CHECK_NOTNULL(parent_tensor_desc);
  157. GELOGD("Start to update shape by input[%u]", it.first);
  158. GELOGD("Update shape to [%s]", parent_tensor_desc->GetShape().ToString().c_str());
  159. GELOGD("Update original shape to [%s]", parent_tensor_desc->GetOriginShape().ToString().c_str());
  160. for (auto &tensor_desc : it.second) {
  161. tensor_desc->SetShape(parent_tensor_desc->GetShape());
  162. tensor_desc->SetOriginShape(parent_tensor_desc->GetOriginShape());
  163. }
  164. }
  165. for (auto &node : fused_subgraph.nodes) {
  166. GELOGD("[%s] Start to invoke InferShapeAndType", node->GetName().c_str());
  167. GE_CHK_STATUS_RET(ShapeRefiner::InferShapeAndType(node));
  168. GELOGD("[%s] Done invoking InferShapeAndType", node->GetName().c_str());
  169. GE_CHK_STATUS_RET(UpdatePeerNodeShape(*node),
  170. "[%s] Failed to update shapes of peer node.",
  171. node->GetName().c_str());
  172. }
  173. for (auto &it : fused_subgraph.output_mapping) {
  174. uint32_t parent_output_idx = it.first;
  175. const auto &op_desc = it.second;
  176. GELOGD("Update parent output[%d] by [%s]", parent_output_idx, op_desc->GetName().c_str());
  177. auto input_desc = op_desc->MutableInputDesc(0);
  178. GE_CHECK_NOTNULL(input_desc);
  179. auto parent_output_tensor_desc = node_item.op_desc->MutableOutputDesc(parent_output_idx);
  180. GE_CHECK_NOTNULL(parent_output_tensor_desc);
  181. GELOGD("Update shape to [%s]", input_desc->GetShape().ToString().c_str());
  182. GELOGD("Update original shape to [%s]", input_desc->GetOriginShape().ToString().c_str());
  183. parent_output_tensor_desc->SetOriginShape(input_desc->GetOriginShape());
  184. parent_output_tensor_desc->SetShape(input_desc->GetShape());
  185. }
  186. GELOGD("[%s] Done shape inference by subgraph successfully.", node_item.NodeName().c_str());
  187. return SUCCESS;
  188. }
  189. Status ShapeInferenceEngine::UpdatePeerNodeShape(const Node &node) {
  190. auto op_desc = node.GetOpDesc();
  191. for (const auto &out_anchor : node.GetAllOutDataAnchors()) {
  192. auto output_tensor = op_desc->MutableOutputDesc(out_anchor->GetIdx());
  193. for (const auto &peer_anchor : out_anchor->GetPeerInDataAnchors()) {
  194. auto peer_node = peer_anchor->GetOwnerNode();
  195. GE_CHECK_NOTNULL(peer_node);
  196. auto peer_op_desc = peer_node->GetOpDesc();
  197. GE_CHECK_NOTNULL(peer_op_desc);
  198. auto peer_input_desc = peer_op_desc->MutableInputDesc(peer_anchor->GetIdx());
  199. if (peer_input_desc == nullptr) {
  200. GELOGE(GRAPH_FAILED, "peer_input_desc is nullptr");
  201. continue;
  202. }
  203. GELOGI("Peer input op desc name is %s, need to flush: shape size is %zu, datatype is %d, original datatype is %d",
  204. peer_anchor->GetOwnerNode()->GetOpDesc()->GetName().c_str(),
  205. output_tensor->GetShape().GetDimNum(), output_tensor->GetDataType(),
  206. output_tensor->GetOriginDataType());
  207. peer_input_desc->SetOriginShape(output_tensor->GetOriginShape());
  208. peer_input_desc->SetShape(output_tensor->GetShape());
  209. GELOGI("Peer input op desc name is %s, shape size is %zu, datatype is %d, original datatype is %d",
  210. peer_anchor->GetOwnerNode()->GetOpDesc()->GetName().c_str(),
  211. peer_input_desc->GetShape().GetDimNum(), peer_input_desc->GetDataType(),
  212. peer_input_desc->GetOriginDataType());
  213. }
  214. }
  215. return SUCCESS;
  216. }
  217. } // namespace hybrid
  218. } // namespace ge

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