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

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

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