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.1 kB

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

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