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

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