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

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