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

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

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