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.h 6.0 kB

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
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #ifndef GE_HYBRID_EXECUTOR_NODE_STATE_H_
  17. #define GE_HYBRID_EXECUTOR_NODE_STATE_H_
  18. #include <condition_variable>
  19. #include <future>
  20. #include <mutex>
  21. #include "common/blocking_queue.h"
  22. #include "external/ge/ge_api_error_codes.h"
  23. #include "hybrid/model/node_item.h"
  24. #include "node_done_manager.h"
  25. namespace ge {
  26. namespace hybrid {
  27. class NodeTask;
  28. struct GraphExecutionContext;
  29. class SubgraphContext;
  30. class TaskContext;
  31. struct NodeState;
  32. struct FrameState;
  33. using NodeStatePtr = std::shared_ptr<NodeState>;
  34. using FrameStatePtr = std::shared_ptr<FrameState>;
  35. class ShapeFuture {
  36. public:
  37. ShapeFuture(NodeState *src_node, uint32_t src_index, SubgraphContext *subgraph_context);
  38. ~ShapeFuture() = default;
  39. Status Get(GeShape &ori_shape, GeShape &shape);
  40. Status GetTensorDesc(const GeTensorDesc **tensor_desc);
  41. private:
  42. NodeState *src_node_;
  43. uint32_t src_index_;
  44. SubgraphContext *subgraph_context_;
  45. };
  46. struct ShapeInferenceState {
  47. explicit ShapeInferenceState(const NodeItem &node_item);
  48. void InitShapeState();
  49. Status UpdateInputShape(int idx, const GeTensorDesc &tensor_desc);
  50. void UpdateInputShapeFuture(int idx, ShapeFuture &&future);
  51. Status AwaitShapesReady(const GraphExecutionContext &context);
  52. Status UpdateOutputDesc();
  53. const vector<GeTensorDesc> &GetOutputTensorDesc() const;
  54. const NodeItem &node_item;
  55. private:
  56. Status UpdateInputForMerge(const GraphExecutionContext &context);
  57. friend struct NodeState;
  58. std::vector<std::pair<int, ShapeFuture>> shape_futures;
  59. // do not directly update op_desc, in case race condition across pipelines
  60. std::vector<GeTensorDesc> input_tensor_desc;
  61. std::vector<GeTensorDesc> output_tensor_desc;
  62. int num_pending_shapes_ = 0;
  63. std::condition_variable ready_cv_;
  64. std::mutex mu_;
  65. };
  66. struct FrameState {
  67. public:
  68. FrameState(int64_t id) : frame_id_(id) {}
  69. ~FrameState() = default;
  70. int64_t frame_id_{0};
  71. uint64_t active_count_{0};
  72. uint64_t iteration_count_{0};
  73. std::shared_ptr<FrameState> parent_frame_;
  74. };
  75. // saving sth. dynamic during execution
  76. struct NodeState {
  77. public:
  78. NodeState(const NodeItem &node_item, SubgraphContext *subgraph_context);
  79. ~NodeState() = default;
  80. Status Init(int group, const shared_ptr<FrameState> &frame_state);
  81. OpDesc *GetOpDesc() const {
  82. return op_desc_.get();
  83. }
  84. inline const NodeItem *GetNodeItem() const {
  85. return node_item_;
  86. }
  87. inline const string &GetName() const {
  88. return node_item_->NodeName();
  89. }
  90. inline const string &GetType() const {
  91. return node_item_->NodeType();
  92. }
  93. ShapeInferenceState &GetShapeInferenceState() {
  94. return shape_inference_state_;
  95. }
  96. Status UpdateOutputShapes(int index, const GeShape &shape, const GeShape &ori_shape);
  97. inline bool IsShapeDependence() const {
  98. return node_item_->IsControlFlowOp() || node_item_->shape_inference_type >= DEPEND_SHAPE_RANGE;
  99. }
  100. void RunStreamActive();
  101. void RunNextIteration();
  102. void SavePersistTensor(int input_idx, const TensorValue &tensor);
  103. void UpdatePersistTensor();
  104. Status NodeScheduled(const std::function<void(const NodeItem *)> &ready) const;
  105. void SetScheduleFuture(std::future<Status> &&future);
  106. Status WaitForScheduleDone();
  107. void SetSwitchIndex(int index) {
  108. switch_index_ = index;
  109. }
  110. int GetSwitchIndex() const {
  111. return switch_index_;
  112. }
  113. void SetMergeIndex(int index) {
  114. merge_index_ = index;
  115. }
  116. int GetMergeIndex() const {
  117. return merge_index_;
  118. }
  119. int GetGroup() const {
  120. return group_;
  121. }
  122. const shared_ptr<NodeTask> &GetKernelTask() const {
  123. return kernel_task_;
  124. }
  125. void SetKernelTask(const shared_ptr<NodeTask> &kernel_task) {
  126. kernel_task_ = kernel_task;
  127. }
  128. Status WaitForPrepareDone();
  129. void SetPrepareFuture(std::future<Status> &&prepare_future) {
  130. this->prepare_future_ = std::move(prepare_future);
  131. }
  132. Status AwaitInputTensors(GraphExecutionContext &context) const;
  133. void SetTaskContext(std::shared_ptr<TaskContext> &task_context);
  134. std::shared_ptr<TaskContext> GetTaskContext();
  135. void SetSkipInferShape(bool skip_infershape) { skip_infershape_ = skip_infershape; }
  136. bool MaySkipShapeInference() const { return skip_infershape_; }
  137. private:
  138. bool IsScheduleReady() const;
  139. void SetDataSchedule(const NodeState &node_state, const std::function<void(const NodeItem *)> &ready);
  140. void SetCtrlSchedule(const NodeState &node_state, const std::function<void(const NodeItem *)> &ready);
  141. void ResetContext(uint64_t iteration);
  142. void ScheduleContext(const NodeState &node_state);
  143. void UpdatePersistTensor(int input_idx);
  144. const NodeItem *node_item_ = nullptr;
  145. std::shared_ptr<NodeTask> kernel_task_ = nullptr;
  146. std::future<Status> prepare_future_;
  147. OpDescPtr op_desc_;
  148. ShapeInferenceState shape_inference_state_;
  149. SubgraphContext *subgraph_context_;
  150. std::shared_ptr<TaskContext> task_context_ = nullptr;
  151. std::mutex mu_;
  152. std::future<Status> schedule_future_;
  153. std::shared_ptr<FrameState> frame_state_;
  154. std::map<int, TensorValue> root_tensor_values_;
  155. uint64_t active_count_ = 0;
  156. uint64_t iteration_count_ = 0;
  157. uint32_t ctrl_scheduled_ = 0;
  158. uint32_t data_scheduled_ = 0;
  159. int merge_index_ = -1; // Use for Execute (Reset after Executed).
  160. int switch_index_ = -1; // Use for Schedule (Reset after Prepared).
  161. int group_ = -1;
  162. bool skip_infershape_ = false;
  163. };
  164. } // namespace hybrid
  165. } // namespace ge
  166. #endif // GE_HYBRID_EXECUTOR_NODE_STATE_H_

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