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

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

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