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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. input_tensor_desc.resize(node_item.num_inputs);
  36. for (int i = 0; i < node_item.num_inputs; ++i) {
  37. node_item.GetInputDesc(i, input_tensor_desc[i]);
  38. }
  39. output_tensor_desc.resize(node_item.num_outputs);
  40. for (int i = 0; i < node_item.num_outputs; ++i) {
  41. node_item.GetOutputDesc(i, output_tensor_desc[i]);
  42. }
  43. }
  44. Status ShapeInferenceState::UpdateInputShape(int idx, const GeTensorDesc &target) {
  45. if (node_item.IsInputShapeStatic(idx)) {
  46. GELOGD("[%s] Trying to update static shape, idx = %d. old shape = [%s], new shape = [%s]",
  47. node_item.NodeName().c_str(),
  48. idx,
  49. node_item.MutableInputDesc(idx)->GetShape().ToString().c_str(),
  50. target.GetShape().ToString().c_str());
  51. return SUCCESS;
  52. }
  53. std::lock_guard<std::mutex> lk(mu_);
  54. auto &input_desc = input_tensor_desc[idx];
  55. GeShape shape = target.GetShape();
  56. input_desc.SetShape(shape);
  57. input_desc.SetOriginShape(target.GetOriginShape());
  58. int64_t tensor_size = -1;
  59. (void) TensorUtils::GetSize(target, tensor_size);
  60. if (tensor_size <= 0) {
  61. Format format = input_desc.GetFormat();
  62. DataType data_type = input_desc.GetDataType();
  63. if (TensorUtils::CalcTensorMemSize(shape, format, data_type, tensor_size) != GRAPH_SUCCESS) {
  64. GELOGE(FAILED, "[Invoke][CalcTensorMemSize] failed for [%s].", node_item.NodeName().c_str());
  65. REPORT_CALL_ERROR("E19999", "CalcTensorMemSize failed for [%s].", node_item.NodeName().c_str());
  66. return FAILED;
  67. }
  68. }
  69. GELOGD("[%s] Update input shape [%d] with Shape: [%s] and OriginalShape: [%s], size = %ld",
  70. node_item.NodeName().c_str(),
  71. idx,
  72. shape.ToString().c_str(),
  73. target.GetOriginShape().ToString().c_str(),
  74. tensor_size);
  75. (void) TensorUtils::SetSize(input_desc, tensor_size);
  76. if (--num_pending_shapes_ <= 0) {
  77. ready_cv_.notify_all();
  78. }
  79. return SUCCESS;
  80. }
  81. void ShapeInferenceState::UpdateInputShapeFuture(int idx, ShapeFuture &&future) {
  82. if (node_item.IsInputShapeStatic(idx)) {
  83. GELOGD("[%s] Trying to update constant shape, idx = %d", node_item.NodeName().c_str(), idx);
  84. return;
  85. }
  86. GELOGD("[%s] Update input shape [%d] with ShapeFuture.", node_item.NodeName().c_str(), idx);
  87. std::lock_guard<std::mutex> lk(mu_);
  88. shape_futures.emplace_back(idx, std::move(future));
  89. if (--num_pending_shapes_ == 0) {
  90. ready_cv_.notify_all();
  91. }
  92. }
  93. Status ShapeInferenceState::AwaitShapesReady(const GraphExecutionContext &context) {
  94. if (!node_item.is_dynamic) {
  95. return SUCCESS;
  96. }
  97. std::unique_lock<std::mutex> lk(mu_);
  98. if (num_pending_shapes_ > 0) {
  99. GELOGD("[%s] Await pending shape or shape future start.", node_item.NodeName().c_str());
  100. int try_count = 0;
  101. bool wait_success = false;
  102. while (try_count++ < kMaxWaitTimes) {
  103. if (ready_cv_.wait_for(lk, std::chrono::seconds(kWaitInternal), [&]() { return num_pending_shapes_ == 0; })) {
  104. GELOGD("[%s] Await pending shape or shape future end.", node_item.NodeName().c_str());
  105. wait_success = true;
  106. break;
  107. }
  108. if (context.is_eos_) {
  109. GELOGD("[%s] Await pending shape cancelled due to end of sequence", node_item.NodeName().c_str());
  110. return END_OF_SEQUENCE;
  111. }
  112. if (context.GetStatus() != SUCCESS) {
  113. GELOGE(FAILED, "[Check][Status][%s] Await pending shape cancelled.", node_item.NodeName().c_str());
  114. REPORT_CALL_ERROR("E19999", "[%s] Await pending shape cancelled.", node_item.NodeName().c_str());
  115. break;
  116. }
  117. }
  118. if (!wait_success) {
  119. GELOGE(FAILED, "[Check][Status][%s] Wait for shape timeout:%d.", node_item.NodeName().c_str(), kWaitInternal);
  120. REPORT_CALL_ERROR("E19999", "[%s] Wait for shape timeout:%d.", node_item.NodeName().c_str(), kWaitInternal);
  121. return FAILED;
  122. }
  123. }
  124. for (size_t i = 0; i < input_tensor_desc.size(); ++i) {
  125. auto dst_tensor_desc = node_item.op_desc->MutableInputDesc(i);
  126. if (dst_tensor_desc == nullptr) {
  127. continue;
  128. }
  129. auto &tensor_desc = input_tensor_desc[i];
  130. int64_t tensor_size = -1;
  131. (void) TensorUtils::GetSize(tensor_desc, tensor_size);
  132. dst_tensor_desc->SetShape(tensor_desc.MutableShape());
  133. dst_tensor_desc->SetOriginShape(tensor_desc.GetOriginShape());
  134. (void) TensorUtils::SetSize(*dst_tensor_desc, tensor_size);
  135. }
  136. for (auto &p : shape_futures) {
  137. auto idx = p.first;
  138. auto &future = p.second;
  139. RECORD_SHAPE_INFERENCE_EVENT(&context, node_item.NodeName().c_str(), "[AwaitShape] [idx = %u] Start", idx);
  140. const GeTensorDesc* src_tensor_desc = nullptr;
  141. GE_CHK_STATUS_RET_NOLOG(future.GetTensorDesc(&src_tensor_desc));
  142. GE_CHECK_NOTNULL(src_tensor_desc);
  143. RECORD_SHAPE_INFERENCE_EVENT(&context, node_item.NodeName().c_str(), "[AwaitShape] [idx = %u] End", idx);
  144. auto input_desc = node_item.MutableInputDesc(idx);
  145. GE_CHECK_NOTNULL(input_desc);
  146. int64_t tensor_size = -1;
  147. (void) TensorUtils::GetSize(*src_tensor_desc, tensor_size);
  148. GELOGD("[%s] Update input shape [%u] with shape: [%s] and ori_shape: [%s], index = %zu",
  149. node_item.NodeName().c_str(),
  150. idx,
  151. src_tensor_desc->GetShape().ToString().c_str(),
  152. src_tensor_desc->GetOriginShape().ToString().c_str(),
  153. tensor_size);
  154. input_desc->SetShape(src_tensor_desc->GetShape());
  155. input_desc->SetOriginShape(src_tensor_desc->GetOriginShape());
  156. (void) TensorUtils::SetSize(*input_desc, tensor_size);
  157. }
  158. return SUCCESS;
  159. }
  160. const vector<GeTensorDesc> &ShapeInferenceState::GetOutputTensorDesc() const {
  161. return output_tensor_desc;
  162. }
  163. Status ShapeInferenceState::UpdateOutputDesc() {
  164. for (size_t i = 0; i < output_tensor_desc.size(); ++i) {
  165. auto src_tensor_desc = node_item.MutableOutputDesc(i);
  166. GE_CHECK_NOTNULL(src_tensor_desc);
  167. auto &dst_tensor_desc = output_tensor_desc[i];
  168. dst_tensor_desc.SetShape(src_tensor_desc->MutableShape());
  169. dst_tensor_desc.SetOriginShape(src_tensor_desc->GetOriginShape());
  170. int64_t tensor_size = -1;
  171. (void) TensorUtils::GetSize(*src_tensor_desc, tensor_size);
  172. (void) TensorUtils::SetSize(dst_tensor_desc, tensor_size);
  173. }
  174. return SUCCESS;
  175. }
  176. ShapeFuture::ShapeFuture(NodeState *src_node,
  177. uint32_t src_index,
  178. SubgraphContext *subgraph_context)
  179. : src_node_(src_node), src_index_(src_index), subgraph_context_(subgraph_context) {
  180. }
  181. NodeState::NodeState(const NodeItem &node_item, SubgraphContext *subgraph_context)
  182. : node_item_(&node_item), shape_inference_state_(node_item), subgraph_context_(subgraph_context) {
  183. this->op_desc_ = node_item.node->GetOpDesc();
  184. }
  185. Status NodeState::AwaitInputTensors(GraphExecutionContext &context) const {
  186. for (auto &src_node : node_item_->dependents_for_execution) {
  187. GELOGD("[%s] Start to wait for data dependent node: [%s]",
  188. node_item_->NodeName().c_str(),
  189. src_node->GetName().c_str());
  190. RECORD_EXECUTION_EVENT(&context,
  191. node_item_->NodeName().c_str(),
  192. "[AwaitNodeDone] [%s] Start",
  193. src_node->GetName().c_str());
  194. HYBRID_CHK_STATUS_RET(subgraph_context_->Await(src_node),
  195. "[%s] Await node [%s] failed.",
  196. GetName().c_str(),
  197. src_node->GetName().c_str());
  198. RECORD_EXECUTION_EVENT(&context,
  199. node_item_->NodeName().c_str(),
  200. "[AwaitNodeDone] [%s] End",
  201. src_node->GetName().c_str());
  202. GELOGD("[%s] Done waiting node.", src_node->GetName().c_str());
  203. }
  204. return SUCCESS;
  205. }
  206. Status NodeState::WaitForPrepareDone() {
  207. if (prepare_future_.valid()) {
  208. GELOGD("[%s] Start to wait for prepare future.", GetName().c_str());
  209. GE_CHK_STATUS_RET(prepare_future_.get(), "[Check][Status][%s] PreRun failed.", GetName().c_str());
  210. }
  211. return SUCCESS;
  212. }
  213. Status NodeState::UpdateOutputShapes(int index, const GeShape &shape, const GeShape &ori_shape) {
  214. auto self_tensor_desc = op_desc_->MutableOutputDesc(index);
  215. GE_CHECK_NOTNULL(self_tensor_desc);
  216. self_tensor_desc->SetShape(shape);
  217. self_tensor_desc->SetOriginShape(ori_shape);
  218. return SUCCESS;
  219. }
  220. void NodeState::SetTaskContext(std::shared_ptr<TaskContext> &task_context) {
  221. task_context_ = task_context;
  222. }
  223. std::shared_ptr<TaskContext> NodeState::GetTaskContext() {
  224. return task_context_;
  225. }
  226. Status ShapeFuture::Get(GeShape &ori_shape, GeShape &shape) {
  227. GELOGD("Start to wait node: %s for getting shape", src_node_->GetName().c_str());
  228. HYBRID_CHK_STATUS_RET(subgraph_context_->Await(src_node_->GetNodeItem()->node), "cancelled");
  229. auto &output_desc = src_node_->GetShapeInferenceState().GetOutputTensorDesc().at(src_index_);
  230. shape = output_desc.GetShape();
  231. ori_shape = output_desc.GetOriginShape();
  232. GELOGD("Get shape from %s:%u. shape = [%s]", src_node_->GetName().c_str(), src_index_, shape.ToString().c_str());
  233. return SUCCESS;
  234. }
  235. Status ShapeFuture::GetTensorDesc(const GeTensorDesc **tensor_desc) {
  236. GE_CHECK_NOTNULL(tensor_desc);
  237. GELOGD("Start to wait node: %s for getting shape", src_node_->GetName().c_str());
  238. HYBRID_CHK_STATUS_RET(subgraph_context_->Await(src_node_->GetNodeItem()->node), "cancelled");
  239. *tensor_desc = &src_node_->GetShapeInferenceState().GetOutputTensorDesc().at(src_index_);
  240. return SUCCESS;
  241. }
  242. } // namespace hybrid
  243. } // namespace ge

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