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

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

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