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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. } // namespace
  34. OpsKernelBuilderManager::~OpsKernelBuilderManager() {
  35. // it's OK to call Finalize multiply times
  36. (void) Finalize();
  37. }
  38. OpsKernelBuilderManager &OpsKernelBuilderManager::Instance() {
  39. static OpsKernelBuilderManager instance;
  40. return instance;
  41. }
  42. Status OpsKernelBuilderManager::Initialize(const map<std::string, std::string> &options, bool is_train) {
  43. if (is_train) {
  44. std::string lib_paths;
  45. GE_CHK_STATUS_RET_NOLOG(GetLibPaths(options, lib_paths));
  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. }
  50. auto &kernel_builders = OpsKernelBuilderRegistry::GetInstance().GetAll();
  51. GELOGI("Number of OpBuild = %zu", kernel_builders.size());
  52. for (const auto &it : kernel_builders) {
  53. const std::string &kernel_lib_name = it.first;
  54. GELOGI("Initialize ops kernel util for %s", kernel_lib_name.c_str());
  55. GE_CHECK_NOTNULL(it.second);
  56. GE_CHK_STATUS_RET(it.second->Initialize(options),
  57. "Failed to invoke Initialize, kernel lib name = %s",
  58. kernel_lib_name.c_str());
  59. ops_kernel_builders_.emplace(kernel_lib_name, it.second);
  60. }
  61. return SUCCESS;
  62. }
  63. Status OpsKernelBuilderManager::Finalize() {
  64. for (const auto &it : ops_kernel_builders_) {
  65. const std::string &kernel_lib_name = it.first;
  66. GELOGI("Finalize ops kernel util for %s", kernel_lib_name.c_str());
  67. auto ret = it.second->Finalize();
  68. if (ret != SUCCESS) {
  69. GELOGW("Failed to invoke Finalize, kernel lib name = %s",
  70. kernel_lib_name.c_str());
  71. }
  72. }
  73. ops_kernel_builders_.clear();
  74. plugin_manager_.reset();
  75. return SUCCESS;
  76. }
  77. const map<string, OpsKernelBuilderPtr> &OpsKernelBuilderManager::GetAllOpsKernelBuilders() const {
  78. return ops_kernel_builders_;
  79. }
  80. OpsKernelBuilderPtr OpsKernelBuilderManager::GetOpsKernelBuilder(const string &name) const {
  81. auto it = ops_kernel_builders_.find(name);
  82. if (it != ops_kernel_builders_.end()) {
  83. return it->second;
  84. }
  85. GELOGW("Failed to get opsKernelInfoStore object by name. OpKernelLibName is %s", name.c_str());
  86. return nullptr;
  87. }
  88. Status OpsKernelBuilderManager::GetLibPaths(const std::map<std::string, std::string> &options, std::string &lib_paths) {
  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. auto iter = options.find(OPTION_EXEC_HCCL_FLAG);
  98. if (iter == options.end() || iter->second != "0") {
  99. for (const auto &lib_name : kHcclBuilderLibs) {
  100. all_lib_paths += (path + lib_name + ":");
  101. }
  102. }
  103. lib_paths = std::move(all_lib_paths);
  104. GELOGI("Get lib paths by default. paths = %s", lib_paths.c_str());
  105. return SUCCESS;
  106. }
  107. Status OpsKernelBuilderManager::CalcOpRunningParam(Node &node) const {
  108. auto op_desc = node.GetOpDesc();
  109. GE_CHECK_NOTNULL(op_desc);
  110. const std::string &lib_name = op_desc->GetOpKernelLibName();
  111. auto it = ops_kernel_builders_.find(lib_name);
  112. if (it == ops_kernel_builders_.end()) {
  113. GELOGE(INTERNAL_ERROR,
  114. "Failed to get OpKernelStore. libName = %s, node = %s",
  115. lib_name.c_str(),
  116. op_desc->GetName().c_str());
  117. return INTERNAL_ERROR;
  118. }
  119. GELOGD("To invoke CalcOpRunningParam, node = %s, lib name = %s", op_desc->GetName().c_str(), lib_name.c_str());
  120. GE_CHK_STATUS_RET(it->second->CalcOpRunningParam(node),
  121. "Failed to invoke CalcOpRunningParam, libName = %s, node = %s",
  122. lib_name.c_str(),
  123. op_desc->GetName().c_str());
  124. GELOGD("Done invoking CalcOpRunningParam successfully");
  125. return SUCCESS;
  126. }
  127. Status OpsKernelBuilderManager::GenerateTask(const Node &node,
  128. RunContext &context,
  129. std::vector<domi::TaskDef> &tasks) const {
  130. auto op_desc = node.GetOpDesc();
  131. GE_CHECK_NOTNULL(op_desc);
  132. const std::string &lib_name = op_desc->GetOpKernelLibName();
  133. auto it = ops_kernel_builders_.find(lib_name);
  134. if (it == ops_kernel_builders_.end()) {
  135. GELOGE(INTERNAL_ERROR,
  136. "Failed to get OpKernelStore. libName = %s, node = %s",
  137. lib_name.c_str(),
  138. op_desc->GetName().c_str());
  139. return INTERNAL_ERROR;
  140. }
  141. GELOGD("To invoke GenerateTask, node = %s, lib name = %s", op_desc->GetName().c_str(), lib_name.c_str());
  142. GE_CHK_STATUS_RET(it->second->GenerateTask(node, context, tasks),
  143. "Failed to invoke GenerateTask, libName = %s, node = %s",
  144. lib_name.c_str(),
  145. op_desc->GetName().c_str());
  146. GELOGD("Done invoking GenerateTask successfully");
  147. return SUCCESS;
  148. }
  149. } // namespace ge

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