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.

engine_place.cc 5.1 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * Copyright 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 "graph/partition/engine_place.h"
  17. #include <climits>
  18. #include <memory>
  19. #include <string>
  20. #include <utility>
  21. #include <mutex>
  22. #include "framework/common/op/ge_op_utils.h"
  23. #include "common/util/error_manager/error_manager.h"
  24. #include "graph/utils/graph_utils.h"
  25. #include "graph/utils/op_desc_utils.h"
  26. #include "init/gelib.h"
  27. #include "opskernel_manager/ops_kernel_manager.h"
  28. #include "analyzer/analyzer.h"
  29. namespace ge {
  30. namespace {
  31. std::mutex check_support_cost_mutex;
  32. }
  33. Status EnginePlacer::Check() const {
  34. if (compute_graph_ == nullptr) {
  35. REPORT_INNER_ERROR("E19999", "compute_graph_ is nullptr, check invalid.");
  36. GELOGE(GE_GRAPH_NULL_INPUT, "[Check][Param] compute_graph_ is nullptr.");
  37. return FAILED;
  38. }
  39. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  40. if ((instance_ptr == nullptr) || (!instance_ptr->InitFlag())) {
  41. REPORT_INNER_ERROR("E19999", "GELib instance is nullptr or it is not InitFlag, check invalid.");
  42. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Get][GELib] Run enginePlacer failed, because GELib is invalid.");
  43. return FAILED;
  44. }
  45. return SUCCESS;
  46. }
  47. Status EnginePlacer::Run() {
  48. std::lock_guard<std::mutex> lock(check_support_cost_mutex);
  49. GELOGD("Engine placer starts.");
  50. if (Check() != SUCCESS) {
  51. return FAILED;
  52. }
  53. bool is_check_support_success = true;
  54. // Assign engine for each node in the graph
  55. ge::GELib::GetInstance()->DNNEngineManagerObj().InitPerformanceStaistic();
  56. for (const auto &node_ptr : compute_graph_->GetDirectNode()) {
  57. GE_CHECK_NOTNULL(node_ptr);
  58. auto op_desc = node_ptr->GetOpDesc();
  59. GE_CHECK_NOTNULL(op_desc);
  60. std::string engine_name;
  61. std::string kernel_name;
  62. // Check if this node has assigned engine
  63. bool has_engine_attr =
  64. AttrUtils::GetStr(op_desc, ATTR_NAME_ENGINE_NAME_FOR_LX, engine_name) && !engine_name.empty();
  65. bool has_kernel_attr =
  66. AttrUtils::GetStr(op_desc, ATTR_NAME_KKERNEL_LIB_NAME_FOR_LX, kernel_name) && !kernel_name.empty();
  67. bool use_exist_engine_name = !op_desc->GetOpKernelLibName().empty() || (has_kernel_attr && has_engine_attr);
  68. if (use_exist_engine_name) {
  69. if (op_desc->GetOpEngineName().empty()) {
  70. GELOGI("Op %s set engine_name %s engine_name %s from attrs",
  71. op_desc->GetName().c_str(),
  72. engine_name.c_str(),
  73. kernel_name.c_str());
  74. op_desc->SetOpEngineName(engine_name);
  75. op_desc->SetOpKernelLibName(kernel_name);
  76. }
  77. engine_name = op_desc->GetOpEngineName();
  78. } else {
  79. // Call placer cost model to get the "best" engine for this node
  80. engine_name = ge::GELib::GetInstance()->DNNEngineManagerObj().GetDNNEngineName(node_ptr);
  81. // If can't get op's engine name, keep check support finish and return failed
  82. if (engine_name.empty()) {
  83. is_check_support_success = false;
  84. ErrorManager::GetInstance().ATCReportErrMessage(
  85. "E13003", {"opname", "optype"}, {op_desc->GetName(), op_desc->GetType()});
  86. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Check][Param] Can not find engine of op name %s type %s",
  87. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  88. continue;
  89. }
  90. }
  91. if (AssignEngineAndLog(node_ptr, engine_name) != SUCCESS) {
  92. GELOGE(GE_GRAPH_ASSIGN_ENGINE_FAILED, "[Call][AssignEngineAndLog] FAILED, node:%s", op_desc->GetName().c_str());
  93. return FAILED;
  94. }
  95. }
  96. for (auto &it : ge::GELib::GetInstance()->DNNEngineManagerObj().GetCheckSupportCost()) {
  97. GEEVENT("The time cost of %s::CheckSupported is [%lu] micro second.", it.first.c_str(), it.second);
  98. }
  99. GELOGD("Engine placer ends.");
  100. return is_check_support_success ? SUCCESS : FAILED;
  101. }
  102. Status EnginePlacer::AssignEngineAndLog(ge::ConstNodePtr node_ptr, const std::string &engine_name) {
  103. if ((node_ptr == nullptr) || (node_ptr->GetOpDesc() == nullptr)) {
  104. REPORT_INNER_ERROR("E19999", "Param node_ptr is nullptr or it's opdesc is nullptr, check invalid.");
  105. GELOGE(FAILED, "[Check][Param] node_ptr is nullptr.");
  106. return FAILED;
  107. }
  108. // private function, promise node_ptr->GetOpDesc() not null
  109. GELOGD("Assigning DNNEngine %s to node %s, op type %s", engine_name.c_str(), node_ptr->GetName().c_str(),
  110. node_ptr->GetOpDesc()->GetType().c_str());
  111. // Record the node assigned engine name
  112. node_engine_map_.insert(std::make_pair(node_ptr, engine_name));
  113. return SUCCESS;
  114. }
  115. } // namespace ge

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