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.cc 9.9 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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/node_executor/node_executor.h"
  17. #include "framework/common/debug/log.h"
  18. #include "graph/utils/node_utils.h"
  19. #include "init/gelib.h"
  20. #include "hybrid/model/hybrid_model.h"
  21. #include "graph/debug/ge_attr_define.h"
  22. namespace ge {
  23. namespace hybrid {
  24. namespace {
  25. const char *const kEngineNameAiCore = "AIcoreEngine";
  26. const char *const kEngineNameGeLocal = "DNN_VM_GE_LOCAL_OP_STORE";
  27. const char *const kEngineNameAiCpu = "aicpu_ascend_kernel";
  28. const char *const kEngineNameAiCpuTf = "aicpu_tf_kernel";
  29. const char *const kEngineNameHccl = "ops_kernel_info_hccl";
  30. const char *const kEngineNameRts = "DNN_VM_RTS_OP_STORE";
  31. const char *const kEngineNameHostCpu = "DNN_VM_HOST_CPU_OP_STORE";
  32. }
  33. Status NodeExecutor::PrepareTask(NodeTask &task, TaskContext &context) const {
  34. GE_CHK_STATUS_RET_NOLOG(context.AllocateOutputs());
  35. GE_CHK_STATUS_RET_NOLOG(task.UpdateTilingData(context)); // update op_desc before alloc ws
  36. GE_CHK_STATUS_RET_NOLOG(context.AllocateWorkspaces());
  37. GE_CHK_STATUS_RET_NOLOG(task.UpdateArgs(context));
  38. return SUCCESS;
  39. }
  40. Status NodeExecutor::ExecuteTask(NodeTask &task, TaskContext &context, const std::function<void()> &callback) const {
  41. GE_CHK_STATUS_RET(task.ExecuteAsync(context, callback),
  42. "Failed to execute task. node = %s",
  43. context.GetNodeItem().NodeName().c_str());
  44. return SUCCESS;
  45. }
  46. Status NodeExecutor::LoadTask(const HybridModel &model, const NodePtr &node, shared_ptr<NodeTask> &task) const {
  47. return UNSUPPORTED;
  48. }
  49. Status NodeExecutor::CompileTask(const HybridModel &model, const NodePtr &node, shared_ptr<NodeTask> &task) const {
  50. return UNSUPPORTED;
  51. }
  52. Status NodeExecutorManager::EnsureInitialized() {
  53. GE_CHK_STATUS_RET(InitializeExecutors());
  54. std::lock_guard<std::mutex> lk(mu_);
  55. if (initialized_) {
  56. return SUCCESS;
  57. }
  58. engine_mapping_.emplace(kEngineNameAiCore, NodeExecutorManager::ExecutorType::AICORE);
  59. engine_mapping_.emplace(kEngineNameGeLocal, NodeExecutorManager::ExecutorType::GE_LOCAL);
  60. engine_mapping_.emplace(kEngineNameAiCpuTf, NodeExecutorManager::ExecutorType::AICPU_TF);
  61. engine_mapping_.emplace(kEngineNameAiCpu, NodeExecutorManager::ExecutorType::AICPU_TF);
  62. engine_mapping_.emplace(kEngineNameHccl, NodeExecutorManager::ExecutorType::HCCL);
  63. engine_mapping_.emplace(kEngineNameRts, NodeExecutorManager::ExecutorType::RTS);
  64. engine_mapping_.emplace(kEngineNameHostCpu, NodeExecutorManager::ExecutorType::HOST_CPU);
  65. std::shared_ptr<GELib> instance_ptr = GELib::GetInstance();
  66. if ((instance_ptr == nullptr) || (!instance_ptr->InitFlag())) {
  67. GELOGW("GELib not initialized");
  68. return FAILED;
  69. }
  70. OpsKernelManager &ops_kernel_manager = instance_ptr->OpsKernelManagerObj();
  71. for (auto &it : ops_kernel_manager.GetAllOpsKernelInfoStores()) {
  72. GELOGD("add kernel store: %s", it.first.c_str());
  73. kernel_stores_.emplace(it.first, it.second.get());
  74. }
  75. initialized_ = true;
  76. GELOGI("Initializing NodeExecutors successfully");
  77. return SUCCESS;
  78. }
  79. NodeExecutorManager::ExecutorType NodeExecutorManager::ResolveExecutorType(Node &node) const {
  80. auto op_type = node.GetType();
  81. if (op_type == PARTITIONEDCALL) {
  82. const auto &subgraph = NodeUtils::GetSubgraph(node, 0);
  83. if (subgraph != nullptr && subgraph->GetGraphUnknownFlag()) {
  84. GELOGD("node %s was marked as unknown shape in node_executor.", node.GetName().c_str());
  85. return ExecutorType::DYNAMIC_SUBGRAPH;
  86. }
  87. bool is_dynamic = false;
  88. (void) NodeUtils::GetNodeUnknownShapeStatus(node, is_dynamic);
  89. if (is_dynamic) {
  90. return ExecutorType::DYNAMIC_SUBGRAPH;
  91. }
  92. return ExecutorType::COMPILED_SUBGRAPH;
  93. }
  94. // rts kernel store is assigned to NetOutput
  95. if (op_type == NETOUTPUT || op_type == VARIABLE) {
  96. return ExecutorType::GE_LOCAL;
  97. }
  98. if (op_type == IF || op_type == CASE || op_type == WHILE) {
  99. return ExecutorType::CONTROL_OP;
  100. }
  101. auto op_desc = node.GetOpDesc(); // checked before
  102. const auto &lib_name = op_desc->GetOpKernelLibName();
  103. auto it = engine_mapping_.find(lib_name);
  104. if (it == engine_mapping_.end()) {
  105. GELOGE(UNSUPPORTED, "KernelLib not supported. node = %s, lib_name = %s", node.GetName().c_str(), lib_name.c_str());
  106. return ExecutorType::RESERVED;
  107. }
  108. return it->second;
  109. }
  110. Status NodeExecutorManager::GetExecutor(Node &node, const NodeExecutor **executor) const {
  111. auto executor_type = ResolveExecutorType(node);
  112. const auto it = executors_.find(executor_type);
  113. if (it == executors_.end()) {
  114. GELOGE(INTERNAL_ERROR, "Failed to get executor by type: %d.", executor_type);
  115. return INTERNAL_ERROR;
  116. }
  117. GELOGD("[%s] Set node executor by type: %d.", node.GetName().c_str(), executor_type);
  118. *executor = it->second.get();
  119. return SUCCESS;
  120. }
  121. void NodeExecutorManager::RegisterExecutorBuilder(NodeExecutorManager::ExecutorType executor_type,
  122. const std::function<NodeExecutor *()> &builder) {
  123. builders_.emplace(executor_type, builder);
  124. }
  125. Status NodeExecutorManager::CalcOpRunningParam(Node &node) const {
  126. auto op_desc = node.GetOpDesc();
  127. GE_CHECK_NOTNULL(op_desc);
  128. if (op_desc->GetType() == PARTITIONEDCALL) {
  129. GELOGD("[%s] Skipping CalcOpRunningParam for PartitionedCall.", node.GetName().c_str());
  130. return SUCCESS;
  131. }
  132. for (size_t i = 0; i < node.GetOpDesc()->GetOutputsSize(); ++i) {
  133. GeTensorDescPtr output_tensor = op_desc->MutableOutputDesc(static_cast<uint32_t>(i));
  134. TensorUtils::SetSize(*(output_tensor.get()), 0);
  135. }
  136. auto it = kernel_stores_.find(op_desc->GetOpKernelLibName());
  137. if (it == kernel_stores_.end()) {
  138. GELOGE(INTERNAL_ERROR,
  139. "Failed to get OpKernelStore. libName = %s, node = %s",
  140. op_desc->GetOpKernelLibName().c_str(),
  141. op_desc->GetName().c_str());
  142. return INTERNAL_ERROR;
  143. }
  144. // calc hccl output size independent, hccl ops kernel manager should GetSize for
  145. // input which is the output size of input-op, but sometimes return error
  146. // when multi-thread
  147. if (op_desc->GetOpKernelLibName() == kEngineNameHccl) {
  148. for (size_t i = 0; i < op_desc->GetOutputsSize(); ++i) {
  149. GeTensorDesc output_tensor = op_desc->GetOutputDesc(static_cast<uint32_t>(i));
  150. Format format = output_tensor.GetFormat();
  151. DataType data_type = output_tensor.GetDataType();
  152. GeShape output_shape = output_tensor.GetShape();
  153. int64_t output_mem_size = 0;
  154. GE_CHK_STATUS_RET(TensorUtils::CalcTensorMemSize(output_shape, format, data_type, output_mem_size),
  155. "hccl calc tensor mem size failed.");
  156. output_mem_size = ((output_mem_size +
  157. MEMORY_ALIGN_RATIO * MEMORY_ALIGN_SIZE - 1) / MEMORY_ALIGN_SIZE) * MEMORY_ALIGN_SIZE;
  158. TensorUtils::SetSize(output_tensor, output_mem_size);
  159. GE_CHK_STATUS_RET(op_desc->UpdateOutputDesc(static_cast<uint32_t>(i), output_tensor),
  160. "hccl update output size failed.");
  161. GELOGD("%s output desc[%u], dim_size: %zu, mem_size: %ld.", node.GetName().c_str(), i,
  162. output_tensor.GetShape().GetDimNum(), output_mem_size);
  163. }
  164. return SUCCESS;
  165. }
  166. return it->second->CalcOpRunningParam(node);
  167. }
  168. Status NodeExecutorManager::InitializeExecutors() {
  169. std::lock_guard<std::mutex> lk(mu_);
  170. if (executor_initialized_) {
  171. ++ref_count_;
  172. GELOGI("Executor is already initialized. add ref count to [%d]", ref_count_);
  173. return SUCCESS;
  174. }
  175. GELOGI("Start to Initialize NodeExecutors");
  176. for (auto &it : builders_) {
  177. auto engine_type = it.first;
  178. auto build_fn = it.second;
  179. GE_CHECK_NOTNULL(build_fn);
  180. auto executor = std::unique_ptr<NodeExecutor>(build_fn());
  181. if (executor == nullptr) {
  182. GELOGE(INTERNAL_ERROR, "Failed to create executor for engine type = %d", engine_type);
  183. return INTERNAL_ERROR;
  184. }
  185. GELOGD("Executor of engine type = %d was created successfully", engine_type);
  186. auto ret = executor->Initialize();
  187. if (ret != SUCCESS) {
  188. GELOGE(ret, "Failed to initialize NodeExecutor of type = %d, clear executors", engine_type);
  189. for (auto &executor_it : executors_) {
  190. executor_it.second->Finalize();
  191. }
  192. executors_.clear();
  193. return ret;
  194. }
  195. executors_.emplace(engine_type, std::move(executor));
  196. }
  197. ++ref_count_;
  198. executor_initialized_ = true;
  199. GELOGI("Initializing NodeExecutors successfully.");
  200. return SUCCESS;
  201. }
  202. void NodeExecutorManager::FinalizeExecutors() {
  203. std::lock_guard<std::mutex> lk(mu_);
  204. if (!executor_initialized_) {
  205. GELOGD("No need for finalizing for not initialized.");
  206. return;
  207. }
  208. if (--ref_count_ > 0) {
  209. GELOGD("Ref count = %d, do not finalize executors.", ref_count_);
  210. return;
  211. }
  212. GELOGD("Start to invoke Finalize on executors.");
  213. for (auto &it : executors_) {
  214. it.second->Finalize();
  215. }
  216. executors_.clear();
  217. executor_initialized_ = false;
  218. GELOGD("Done invoking Finalize successfully.");
  219. }
  220. NodeExecutorRegistrar::NodeExecutorRegistrar(NodeExecutorManager::ExecutorType executor_type,
  221. NodeExecutor *(*builder)()) {
  222. NodeExecutorManager::GetInstance().RegisterExecutorBuilder(executor_type, builder);
  223. }
  224. } // namespace hybrid
  225. } // namespace ge

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