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_executor.h 7.2 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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_NODE_EXECUTOR_NODE_EXECUTOR_H_
  17. #define GE_HYBRID_NODE_EXECUTOR_NODE_EXECUTOR_H_
  18. #include "external/ge/ge_api_error_codes.h"
  19. #include "common/opskernel/ops_kernel_builder.h"
  20. #include "graph/node.h"
  21. #include "hybrid/node_executor/task_context.h"
  22. namespace ge {
  23. const uint32_t MEMORY_ALIGN_RATIO = 2;
  24. const uint32_t MEMORY_ALIGN_SIZE = 32;
  25. namespace hybrid {
  26. class HybridModel;
  27. using NodeTaskPtr = std::shared_ptr<NodeTask>;
  28. // Base class of Node Task
  29. class NodeTask {
  30. public:
  31. NodeTask() = default;
  32. virtual ~NodeTask() = default;
  33. /**
  34. * Update tiling data
  35. * @param context instance of TaskContext
  36. * @return SUCCESS on success, error code otherwise
  37. */
  38. virtual Status UpdateTilingData(TaskContext &context) {
  39. return SUCCESS;
  40. }
  41. /**
  42. * Init
  43. * @param context instance of TaskContext
  44. * @return SUCCESS on success, error code otherwise
  45. */
  46. virtual Status Init(TaskContext &context) {
  47. return SUCCESS;
  48. }
  49. /**
  50. * Whether this task supports dynamic shape
  51. * @return true if this task supports dynamic shape, false otherwise
  52. */
  53. virtual bool IsSupportDynamicShape() {
  54. return true;
  55. }
  56. /**
  57. * Update args for execution
  58. * @param context instance of TaskContext
  59. * @return SUCCESS on success, error code otherwise
  60. */
  61. virtual Status UpdateArgs(TaskContext &context) = 0;
  62. /**
  63. * Execute task async
  64. * @param context instance of TaskContext
  65. * @param done_callback callback function, will be invoked after task is done
  66. * @return SUCCESS on success, error code otherwise
  67. */
  68. virtual Status ExecuteAsync(TaskContext &context, std::function<void()> done_callback) = 0;
  69. };
  70. class NoOpTask : public NodeTask {
  71. public:
  72. Status UpdateArgs(TaskContext &context) override;
  73. Status ExecuteAsync(TaskContext &context, std::function<void()> done_callback) override;
  74. };
  75. // Node executor
  76. class NodeExecutor {
  77. public:
  78. NodeExecutor() = default;
  79. virtual ~NodeExecutor() = default;
  80. /**
  81. * Initialize node executor
  82. * @return SUCCESS on success, error code otherwise
  83. */
  84. virtual Status Initialize() {
  85. return SUCCESS;
  86. }
  87. /**
  88. * Finalize node executor
  89. * @return SUCCESS on success, error code otherwise
  90. */
  91. virtual Status Finalize() {
  92. return SUCCESS;
  93. }
  94. /**
  95. * Load task in load stage
  96. * @param model instance of HybridModel
  97. * @param node node
  98. * @param task generated node task
  99. * @return SUCCESS on success, error code otherwise
  100. */
  101. virtual Status LoadTask(const HybridModel &model,
  102. const NodePtr &node,
  103. std::shared_ptr<NodeTask> &task) const;
  104. /**
  105. * Compile task in run stage
  106. * @param model instance of HybridModel
  107. * @param node node
  108. * @param task generated node task
  109. * @return SUCCESS on success, error code otherwise
  110. */
  111. virtual Status CompileTask(const HybridModel &model,
  112. const NodePtr &node,
  113. std::shared_ptr<NodeTask> &task) const;
  114. /**
  115. * Preparation actions before execution
  116. * @param task instance of NodeTask
  117. * @param context instance of TaskContext
  118. * @return SUCCESS on success, error code otherwise
  119. */
  120. virtual Status PrepareTask(NodeTask &task, TaskContext &context) const;
  121. /**
  122. * Execute task
  123. * @param task instance of NodeTask
  124. * @param context instance of TaskContext
  125. * @param callback callback function which will be invoked after computation is done
  126. * @return SUCCESS on success, error code otherwise
  127. */
  128. virtual Status ExecuteTask(NodeTask &task, TaskContext &context, const std::function<void()> &callback) const;
  129. };
  130. class NodeExecutorManager {
  131. public:
  132. enum class ExecutorType {
  133. AICORE,
  134. AICPU_TF,
  135. AICPU_CUSTOM,
  136. COMPILED_SUBGRAPH,
  137. DYNAMIC_SUBGRAPH,
  138. GE_LOCAL,
  139. CONTROL_OP,
  140. HCCL,
  141. RTS,
  142. HOST_CPU,
  143. RESERVED
  144. };
  145. static NodeExecutorManager &GetInstance() {
  146. static NodeExecutorManager instance;
  147. return instance;
  148. }
  149. /**
  150. * Register build of executor
  151. * @param executor_type type of executor
  152. * @param builder build function
  153. */
  154. void RegisterExecutorBuilder(ExecutorType executor_type, const std::function<NodeExecutor *()> &builder);
  155. /**
  156. * Initialize executor if needed
  157. * @return SUCCESS on success, error code otherwise
  158. */
  159. Status EnsureInitialized();
  160. void FinalizeExecutors();
  161. /**
  162. * CalcOpRunningParam
  163. * @param node node
  164. * @return SUCCESS on success, error code otherwise
  165. */
  166. Status CalcOpRunningParam(Node &node) const;
  167. /**
  168. * Get executor by node
  169. * @param node node
  170. * @param executor executor
  171. * @return SUCCESS on success, error code otherwise
  172. */
  173. Status GetExecutor(Node &node, const NodeExecutor **executor);
  174. /**
  175. * Resolve executor type by node
  176. * @param node node
  177. * @return executor type
  178. */
  179. ExecutorType ResolveExecutorType(Node &node) const;
  180. Status GetOrCreateExecutor(ExecutorType executor_type, const NodeExecutor **executor);
  181. bool IsExecutorInitialized(ExecutorType executor_type);
  182. private:
  183. std::map<ExecutorType, std::unique_ptr<NodeExecutor>> executors_;
  184. std::map<ExecutorType, std::function<NodeExecutor *()>> builders_;
  185. std::map<std::string, NodeExecutorManager::ExecutorType> engine_mapping_;
  186. std::mutex mu_;
  187. bool initialized_ = false;
  188. int ref_count_ = 0;
  189. };
  190. class NodeExecutorRegistrar {
  191. public:
  192. NodeExecutorRegistrar(NodeExecutorManager::ExecutorType executor_type,
  193. NodeExecutor *(*builder)());
  194. ~NodeExecutorRegistrar() = default;
  195. };
  196. } // namespace hybrid
  197. } // namespace ge
  198. #define REGISTER_NODE_EXECUTOR_BUILDER(engine_type, executor) \
  199. REGISTER_NODE_EXECUTOR_BUILDER_UNIQ_HELPER(__COUNTER__, engine_type, executor)
  200. #define REGISTER_NODE_EXECUTOR_BUILDER_UNIQ_HELPER(ctr, engine_type, executor) \
  201. REGISTER_NODE_EXECUTOR_BUILDER_UNIQ(ctr, engine_type, executor)
  202. #define REGISTER_NODE_EXECUTOR_BUILDER_UNIQ(ctr, engine_type, executor) \
  203. static ::ge::hybrid::NodeExecutorRegistrar register_##ctr \
  204. __attribute__((unused)) = \
  205. ::ge::hybrid::NodeExecutorRegistrar(engine_type, []()->::ge::hybrid::NodeExecutor* { \
  206. return new (std::nothrow) executor(); \
  207. })
  208. #endif // GE_HYBRID_NODE_EXECUTOR_NODE_EXECUTOR_H_

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