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

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