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

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

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