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.

ops_kernel_builder_manager.cc 6.7 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "init/gelib.h"
  17. #include "opskernel_manager/ops_kernel_builder_manager.h"
  18. #include "register/ops_kernel_builder_registry.h"
  19. namespace ge {
  20. namespace {
  21. #ifdef ONLY_COMPILE_OPEN_SRC
  22. const std::vector<std::string> kBasicBuilderLibs = {
  23. "libge_local_opskernel_builder.so",
  24. "libhost_cpu_opskernel_builder.so",
  25. "librts_kernel_builder.so",
  26. "libaicpu_ascend_builder.so",
  27. "libaicpu_tf_builder.so"
  28. };
  29. #else
  30. const std::vector<std::string> kBasicBuilderLibs = {
  31. "libge_local_opskernel_builder.so",
  32. "libhost_cpu_opskernel_builder.so",
  33. "librts_engine.so",
  34. "libaicpu_ascend_engine.so",
  35. "libaicpu_tf_engine.so"
  36. };
  37. #endif
  38. const std::vector<std::string> kHcclBuilderLibs = {
  39. "libhcom_opskernel_builder.so",
  40. "libhvd_opskernel_builder.so",
  41. "libhcom_gradtune_opskernel_builder.so"
  42. };
  43. } // namespace
  44. OpsKernelBuilderManager::~OpsKernelBuilderManager() {
  45. // it's OK to call Finalize multiply times
  46. (void) Finalize();
  47. }
  48. OpsKernelBuilderManager &OpsKernelBuilderManager::Instance() {
  49. static OpsKernelBuilderManager instance;
  50. return instance;
  51. }
  52. Status OpsKernelBuilderManager::Initialize(const map<std::string, std::string> &options, bool is_train) {
  53. if (is_train) {
  54. std::string lib_paths;
  55. GE_CHK_STATUS_RET_NOLOG(GetLibPaths(options, lib_paths));
  56. plugin_manager_.reset(new (std::nothrow)PluginManager());
  57. GE_CHECK_NOTNULL(plugin_manager_);
  58. GE_CHK_STATUS_RET(plugin_manager_->LoadSo(lib_paths),
  59. "[Load][Libs]Failed, lib_paths=%s.", lib_paths.c_str());
  60. }
  61. auto &kernel_builders = OpsKernelBuilderRegistry::GetInstance().GetAll();
  62. GELOGI("[Show][OpsKernelBuilderNum]Number of OpBuild = %zu", kernel_builders.size());
  63. for (const auto &it : kernel_builders) {
  64. const std::string &kernel_lib_name = it.first;
  65. GELOGI("Initialize ops kernel util for %s", kernel_lib_name.c_str());
  66. GE_CHECK_NOTNULL(it.second);
  67. GE_CHK_STATUS_RET(it.second->Initialize(options),
  68. "[Invoke][Initialize]failed, kernel lib name = %s", kernel_lib_name.c_str());
  69. ops_kernel_builders_.emplace(kernel_lib_name, it.second);
  70. }
  71. return SUCCESS;
  72. }
  73. Status OpsKernelBuilderManager::Finalize() {
  74. for (const auto &it : ops_kernel_builders_) {
  75. const std::string &kernel_lib_name = it.first;
  76. GELOGI("Finalize ops kernel util for %s", kernel_lib_name.c_str());
  77. auto ret = it.second->Finalize();
  78. if (ret != SUCCESS) {
  79. GELOGW("Failed to invoke Finalize, kernel lib name = %s",
  80. kernel_lib_name.c_str());
  81. }
  82. }
  83. ops_kernel_builders_.clear();
  84. plugin_manager_.reset();
  85. return SUCCESS;
  86. }
  87. const map<string, OpsKernelBuilderPtr> &OpsKernelBuilderManager::GetAllOpsKernelBuilders() const {
  88. return ops_kernel_builders_;
  89. }
  90. OpsKernelBuilderPtr OpsKernelBuilderManager::GetOpsKernelBuilder(const string &name) const {
  91. auto it = ops_kernel_builders_.find(name);
  92. if (it != ops_kernel_builders_.end()) {
  93. return it->second;
  94. }
  95. GELOGW("Failed to get opsKernelInfoStore object by name. OpKernelLibName is %s", name.c_str());
  96. return nullptr;
  97. }
  98. Status OpsKernelBuilderManager::GetLibPaths(const std::map<std::string,
  99. std::string> &options, std::string &lib_paths) {
  100. GELOGD("Start to execute GetLibPaths");
  101. std::string path_base = PluginManager::GetPath();
  102. std::string so_path = "plugin/opskernel/";
  103. std::string path = path_base + so_path;
  104. std::string all_lib_paths;
  105. for (const auto &lib_name : kBasicBuilderLibs) {
  106. all_lib_paths += (path + lib_name + ":");
  107. }
  108. auto iter = options.find(OPTION_EXEC_HCCL_FLAG);
  109. if (iter == options.end() || iter->second != "0") {
  110. for (const auto &lib_name : kHcclBuilderLibs) {
  111. all_lib_paths += (path + lib_name + ":");
  112. }
  113. }
  114. lib_paths = std::move(all_lib_paths);
  115. GELOGI("Get lib paths by default. paths = %s", lib_paths.c_str());
  116. return SUCCESS;
  117. }
  118. Status OpsKernelBuilderManager::CalcOpRunningParam(Node &node) const {
  119. auto op_desc = node.GetOpDesc();
  120. GE_CHECK_NOTNULL(op_desc);
  121. const std::string &lib_name = op_desc->GetOpKernelLibName();
  122. auto it = ops_kernel_builders_.find(lib_name);
  123. if (it == ops_kernel_builders_.end()) {
  124. GELOGE(INTERNAL_ERROR,"[Find][LibName] fail for libName = %s, node = %s.",
  125. lib_name.c_str(), op_desc->GetName().c_str());
  126. REPORT_INNER_ERROR("E19999",
  127. "find LibName for CalcOpRunningParam failed, libName = %s, node = %s not exist.",
  128. lib_name.c_str(), op_desc->GetName().c_str());
  129. return INTERNAL_ERROR;
  130. }
  131. GELOGD("To invoke CalcOpRunningParam, node = %s, lib name = %s", op_desc->GetName().c_str(), lib_name.c_str());
  132. GE_CHK_STATUS_RET(it->second->CalcOpRunningParam(node),
  133. "[Invoke][CalcOpRunningParam]failed, libName = %s, node = %s", lib_name.c_str(), op_desc->GetName().c_str());
  134. GELOGD("Done invoking CalcOpRunningParam successfully");
  135. return SUCCESS;
  136. }
  137. Status OpsKernelBuilderManager::GenerateTask(const Node &node,
  138. RunContext &context,
  139. std::vector<domi::TaskDef> &tasks) const {
  140. auto op_desc = node.GetOpDesc();
  141. GE_CHECK_NOTNULL(op_desc);
  142. const std::string &lib_name = op_desc->GetOpKernelLibName();
  143. auto it = ops_kernel_builders_.find(lib_name);
  144. if (it == ops_kernel_builders_.end()) {
  145. GELOGE(INTERNAL_ERROR, "[Find][LibName]fail for libName = %s, node:%s", lib_name.c_str(),
  146. op_desc->GetName().c_str());
  147. REPORT_INNER_ERROR("E19999", "find LibName for GenerateTask failed, libName = %s, node = %s not exist",
  148. lib_name.c_str(), op_desc->GetName().c_str());
  149. return INTERNAL_ERROR;
  150. }
  151. GELOGD("To invoke GenerateTask, node = %s, lib name = %s", op_desc->GetName().c_str(), lib_name.c_str());
  152. GE_CHK_STATUS_RET(it->second->GenerateTask(node, context, tasks),
  153. "[Invoke][GenerateTask]failed, libName = %s, node = %s", lib_name.c_str(), op_desc->GetName().c_str());
  154. GELOGD("Done invoking GenerateTask successfully");
  155. return SUCCESS;
  156. }
  157. } // namespace ge

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