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

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

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