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_state.cc 8.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 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
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/node_state.h"
  17. #include <chrono>
  18. #include "framework/common/debug/log.h"
  19. #include "graph/compute_graph.h"
  20. #include "graph/utils/tensor_utils.h"
  21. #include "hybrid_execution_context.h"
  22. #include "subgraph_context.h"
  23. namespace ge {
  24. namespace hybrid {
  25. namespace {
  26. // 5s * 120, wait for 10m
  27. constexpr auto kWaitInternal = 5;
  28. constexpr auto kMaxWaitTimes = 120;
  29. }
  30. ShapeInferenceState::ShapeInferenceState(const NodeItem &node_item) : node_item(node_item) {
  31. this->num_pending_shapes_ = node_item.num_inputs - node_item.num_static_input_shapes;
  32. GELOGD("[%s] ShapeInferenceState created, pending shape count = %d",
  33. node_item.NodeName().c_str(),
  34. this->num_pending_shapes_);
  35. }
  36. Status ShapeInferenceState::UpdateInputShape(int idx, const GeTensorDesc &target) {
  37. if (node_item.IsInputShapeStatic(idx)) {
  38. GELOGD("[%s] Trying to update static shape, idx = %d. old shape = [%s], new shape = [%s]",
  39. node_item.NodeName().c_str(),
  40. idx,
  41. node_item.MutableInputDesc(idx)->GetShape().ToString().c_str(),
  42. target.GetShape().ToString().c_str());
  43. return SUCCESS;
  44. }
  45. int64_t tensor_size = -1;
  46. (void) TensorUtils::GetSize(target, tensor_size);
  47. GELOGD("[%s] Update input shape [%d] with Shape: [%s] and OriginalShape: [%s], size = %ld",
  48. node_item.NodeName().c_str(),
  49. idx,
  50. target.GetShape().ToString().c_str(),
  51. target.GetOriginShape().ToString().c_str(),
  52. tensor_size);
  53. std::lock_guard<std::mutex> lk(mu_);
  54. auto tensor_desc = node_item.MutableInputDesc(idx);
  55. GE_CHECK_NOTNULL(tensor_desc);
  56. tensor_desc->SetShape(target.GetShape());
  57. tensor_desc->SetOriginShape(target.GetOriginShape());
  58. (void) TensorUtils::SetSize(*tensor_desc, tensor_size);
  59. if (--num_pending_shapes_ <= 0) {
  60. ready_cv_.notify_all();
  61. }
  62. return SUCCESS;
  63. }
  64. void ShapeInferenceState::UpdateInputShapeFuture(int idx, ShapeFuture &&future) {
  65. if (node_item.IsInputShapeStatic(idx)) {
  66. GELOGD("[%s] Trying to update constant shape, idx = %d", node_item.NodeName().c_str(), idx);
  67. return;
  68. }
  69. GELOGD("[%s] Update input shape [%d] with ShapeFuture.", node_item.NodeName().c_str(), idx);
  70. std::lock_guard<std::mutex> lk(mu_);
  71. shape_futures.emplace_back(idx, std::move(future));
  72. if (--num_pending_shapes_ == 0) {
  73. ready_cv_.notify_all();
  74. }
  75. }
  76. Status ShapeInferenceState::AwaitShapesReady(const GraphExecutionContext &context) {
  77. if (!node_item.is_dynamic) {
  78. return SUCCESS;
  79. }
  80. std::unique_lock<std::mutex> lk(mu_);
  81. if (num_pending_shapes_ > 0) {
  82. GELOGD("[%s] Await pending shape or shape future start.", node_item.NodeName().c_str());
  83. int try_count = 0;
  84. bool wait_success = false;
  85. while (try_count++ < kMaxWaitTimes) {
  86. if (ready_cv_.wait_for(lk, std::chrono::seconds(kWaitInternal), [&]() { return num_pending_shapes_ == 0; })) {
  87. GELOGD("[%s] Await pending shape or shape future end.", node_item.NodeName().c_str());
  88. wait_success = true;
  89. break;
  90. }
  91. if (context.is_eos_) {
  92. GELOGD("[%s] Await pending shape cancelled due to end of sequence", node_item.NodeName().c_str());
  93. return END_OF_SEQUENCE;
  94. }
  95. if (context.GetStatus() != SUCCESS) {
  96. GELOGE(FAILED, "[%s] Await pending shape cancelled", node_item.NodeName().c_str());
  97. break;
  98. }
  99. }
  100. if (!wait_success) {
  101. GELOGE(FAILED, "[%s] Wait for shape timeout.", node_item.NodeName().c_str());
  102. return FAILED;
  103. }
  104. }
  105. for (auto &p : shape_futures) {
  106. auto idx = p.first;
  107. auto &future = p.second;
  108. RECORD_SHAPE_INFERENCE_EVENT(&context, node_item.NodeName().c_str(), "[AwaitShape] [idx = %u] Start", idx);
  109. GeTensorDescPtr src_tensor_desc;
  110. GE_CHK_STATUS_RET_NOLOG(future.GetTensorDesc(src_tensor_desc));
  111. GE_CHECK_NOTNULL(src_tensor_desc);
  112. RECORD_SHAPE_INFERENCE_EVENT(&context, node_item.NodeName().c_str(), "[AwaitShape] [idx = %u] End", idx);
  113. auto input_desc = node_item.MutableInputDesc(idx);
  114. GE_CHECK_NOTNULL(input_desc);
  115. int64_t tensor_size = -1;
  116. (void) TensorUtils::GetSize(*src_tensor_desc, tensor_size);
  117. GELOGD("[%s] Update input shape [%u] with shape: [%s] and ori_shape: [%s], index = %zu",
  118. node_item.NodeName().c_str(),
  119. idx,
  120. src_tensor_desc->GetShape().ToString().c_str(),
  121. src_tensor_desc->GetOriginShape().ToString().c_str(),
  122. tensor_size);
  123. input_desc->SetShape(src_tensor_desc->GetShape());
  124. input_desc->SetOriginShape(src_tensor_desc->GetOriginShape());
  125. (void) TensorUtils::SetSize(*input_desc, tensor_size);
  126. }
  127. return SUCCESS;
  128. }
  129. ShapeFuture::ShapeFuture(NodePtr src_node,
  130. uint32_t src_index,
  131. SubgraphContext *subgraph_context)
  132. : src_node_(std::move(src_node)), src_index_(src_index), subgraph_context_(subgraph_context) {
  133. }
  134. NodeState::NodeState(const NodeItem &node_item, SubgraphContext *subgraph_context)
  135. : node_item_(&node_item), shape_inference_state_(node_item), subgraph_context_(subgraph_context) {
  136. this->op_desc_ = node_item.node->GetOpDesc();
  137. }
  138. Status NodeState::AwaitInputTensors(GraphExecutionContext &context) const {
  139. for (auto &src_node : node_item_->dependents_for_execution) {
  140. GELOGD("[%s] Start to wait for data dependent node: [%s]",
  141. node_item_->NodeName().c_str(),
  142. src_node->GetName().c_str());
  143. RECORD_EXECUTION_EVENT(&context,
  144. node_item_->NodeName().c_str(),
  145. "[AwaitNodeDone] [%s] Start",
  146. src_node->GetName().c_str());
  147. HYBRID_CHK_STATUS_RET(subgraph_context_->Await(src_node),
  148. "[%s] Await node [%s] failed.",
  149. GetName().c_str(),
  150. src_node->GetName().c_str());
  151. RECORD_EXECUTION_EVENT(&context,
  152. node_item_->NodeName().c_str(),
  153. "[AwaitNodeDone] [%s] End",
  154. src_node->GetName().c_str());
  155. GELOGD("[%s] Done waiting node.", src_node->GetName().c_str());
  156. }
  157. return SUCCESS;
  158. }
  159. Status NodeState::WaitForPrepareDone() {
  160. if (prepare_future_.valid()) {
  161. GELOGD("[%s] Start to wait for prepare future.", GetName().c_str());
  162. GE_CHK_STATUS_RET(prepare_future_.get(),
  163. "[%s] PreRun failed.", GetName().c_str());
  164. }
  165. return SUCCESS;
  166. }
  167. Status ShapeFuture::Get(GeShape &ori_shape, GeShape &shape) {
  168. GELOGD("Start to wait node: %s for getting shape", src_node_->GetName().c_str());
  169. HYBRID_CHK_STATUS_RET(subgraph_context_->Await(src_node_), "cancelled");
  170. shape = src_node_->GetOpDesc()->MutableOutputDesc(src_index_)->MutableShape();
  171. ori_shape = src_node_->GetOpDesc()->MutableOutputDesc(src_index_)->GetOriginShape();
  172. GELOGD("Get shape from %s:%u. shape = [%s]", src_node_->GetName().c_str(), src_index_, shape.ToString().c_str());
  173. return SUCCESS;
  174. }
  175. Status ShapeFuture::GetTensorDesc(GeTensorDescPtr &tensor_desc) {
  176. GELOGD("Start to wait node: %s for getting shape", src_node_->GetName().c_str());
  177. HYBRID_CHK_STATUS_RET(subgraph_context_->Await(src_node_), "cancelled");
  178. tensor_desc = src_node_->GetOpDesc()->MutableOutputDesc(src_index_);
  179. return SUCCESS;
  180. }
  181. } // namespace hybrid
  182. } // namespace ge

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