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_manage.cc 7.4 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "plugin/engine/engine_manage.h"
  17. #include <map>
  18. #include <string>
  19. #include <utility>
  20. #include "common/ge/ge_util.h"
  21. #include "framework/common/debug/ge_log.h"
  22. #include "plugin/engine/dnnengines.h"
  23. namespace ge {
  24. std::unique_ptr<std::map<std::string, DNNEnginePtr>> EngineManager::engine_map_;
  25. Status EngineManager::RegisterEngine(const std::string &engine_name, DNNEnginePtr engine_ptr) {
  26. if (engine_ptr == nullptr) {
  27. GELOGE(FAILED, "enginePtr is nullptr");
  28. return FAILED;
  29. }
  30. if (engine_map_ == nullptr) {
  31. engine_map_.reset(new (std::nothrow) std::map<std::string, DNNEnginePtr>());
  32. }
  33. auto it = engine_map_->find(engine_name);
  34. if (it != engine_map_->end()) {
  35. GELOGW("engine %s already exist.", engine_name.c_str());
  36. return FAILED;
  37. }
  38. engine_map_->emplace(engine_name, engine_ptr);
  39. return SUCCESS;
  40. }
  41. DNNEnginePtr EngineManager::GetEngine(const std::string &engine_name) {
  42. auto it = engine_map_->find(engine_name);
  43. if (it == engine_map_->end()) {
  44. GELOGW("engine %s not exist.", engine_name.c_str());
  45. return nullptr;
  46. }
  47. auto engine = it->second;
  48. return engine;
  49. }
  50. void RegisterAiCoreEngine() {
  51. const std::string ai_core = "AIcoreEngine";
  52. std::vector<std::string> mem_type_aicore;
  53. mem_type_aicore.emplace_back(GE_ENGINE_ATTR_MEM_TYPE_HBM);
  54. DNNEngineAttribute attr_aicore = {ai_core, mem_type_aicore, COST_0, DEVICE, FORMAT_RESERVED, FORMAT_RESERVED};
  55. DNNEnginePtr aicore_engine_ptr = MakeShared<AICoreDNNEngine>(attr_aicore);
  56. if (aicore_engine_ptr == nullptr) {
  57. GELOGE(ge::FAILED, "make aiCoreEnginePtr failed");
  58. return;
  59. }
  60. if (EngineManager::RegisterEngine(ai_core, aicore_engine_ptr) != SUCCESS) {
  61. GELOGW("register ai_core failed");
  62. }
  63. }
  64. void RegisterVectorEngine() {
  65. const std::string vector_core = "VectorEngine";
  66. std::vector<std::string> mem_type_aivcore;
  67. mem_type_aivcore.emplace_back(GE_ENGINE_ATTR_MEM_TYPE_HBM);
  68. DNNEngineAttribute attr_vector_core = {vector_core, mem_type_aivcore, COST_1,
  69. DEVICE, FORMAT_RESERVED, FORMAT_RESERVED};
  70. DNNEnginePtr vectorcore_engine_ptr = MakeShared<VectorCoreDNNEngine>(attr_vector_core);
  71. if (vectorcore_engine_ptr == nullptr) {
  72. GELOGE(ge::FAILED, "make vectorCoreEnginePtr failed");
  73. return;
  74. }
  75. if (EngineManager::RegisterEngine(vector_core, vectorcore_engine_ptr) != SUCCESS) {
  76. GELOGW("register vector_core failed");
  77. }
  78. }
  79. void RegisterAiCpuEngine() {
  80. const std::string vm_aicpu = "DNN_VM_AICPU_ASCEND";
  81. std::vector<std::string> mem_type_aicpu;
  82. mem_type_aicpu.emplace_back(GE_ENGINE_ATTR_MEM_TYPE_HBM);
  83. DNNEngineAttribute attr_aicpu = {vm_aicpu, mem_type_aicpu, COST_3, DEVICE, FORMAT_RESERVED, FORMAT_RESERVED};
  84. DNNEnginePtr vm_engine_ptr = MakeShared<AICpuDNNEngine>(attr_aicpu);
  85. if (vm_engine_ptr == nullptr) {
  86. GELOGE(ge::FAILED, "make vm_engine_ptr failed");
  87. return;
  88. }
  89. if (EngineManager::RegisterEngine(vm_aicpu, vm_engine_ptr) != SUCCESS) {
  90. GELOGW("register vmAicpuEngine failed");
  91. }
  92. }
  93. void RegisterAiCpuTFEngine() {
  94. const std::string vm_aicpu_tf = "DNN_VM_AICPU";
  95. std::vector<std::string> mem_type_aicpu_tf;
  96. mem_type_aicpu_tf.emplace_back(GE_ENGINE_ATTR_MEM_TYPE_HBM);
  97. DNNEngineAttribute attr_aicpu_tf = {vm_aicpu_tf, mem_type_aicpu_tf, COST_2, DEVICE, FORMAT_RESERVED, FORMAT_RESERVED};
  98. DNNEnginePtr vm_engine_ptr = MakeShared<AICpuTFDNNEngine>(attr_aicpu_tf);
  99. if (vm_engine_ptr == nullptr) {
  100. GELOGE(ge::FAILED, "make vm_engine_ptr failed");
  101. return;
  102. }
  103. if (EngineManager::RegisterEngine(vm_aicpu_tf, vm_engine_ptr) != SUCCESS) {
  104. GELOGW("register vmAicpuTFEngine failed");
  105. }
  106. }
  107. void RegisterGeLocalEngine() {
  108. const std::string vm_ge_local = "DNN_VM_GE_LOCAL";
  109. std::vector<std::string> mem_type_ge_local;
  110. mem_type_ge_local.emplace_back(GE_ENGINE_ATTR_MEM_TYPE_HBM);
  111. // GeLocal use minimum priority, set it as 9
  112. DNNEngineAttribute attr_ge_local = {vm_ge_local, mem_type_ge_local, COST_9, DEVICE, FORMAT_RESERVED, FORMAT_RESERVED};
  113. DNNEnginePtr ge_local_engine = MakeShared<GeLocalDNNEngine>(attr_ge_local);
  114. if (ge_local_engine == nullptr) {
  115. GELOGE(ge::FAILED, "make ge_local_engine failed");
  116. return;
  117. }
  118. if (EngineManager::RegisterEngine(vm_ge_local, ge_local_engine) != SUCCESS) {
  119. GELOGW("register ge_local_engine failed");
  120. }
  121. }
  122. void RegisterHostCpuEngine() {
  123. const std::string vm_host_cpu = "DNN_VM_HOST_CPU";
  124. std::vector<std::string> mem_type_host_cpu;
  125. mem_type_host_cpu.emplace_back(GE_ENGINE_ATTR_MEM_TYPE_HBM);
  126. // HostCpu use minimum priority, set it as 10
  127. DNNEngineAttribute attr_host_cpu = {vm_host_cpu, mem_type_host_cpu, COST_10, HOST, FORMAT_RESERVED, FORMAT_RESERVED};
  128. DNNEnginePtr host_cpu_engine = MakeShared<HostCpuDNNEngine>(attr_host_cpu);
  129. if (host_cpu_engine == nullptr) {
  130. GELOGE(ge::FAILED, "make host_cpu_engine failed");
  131. return;
  132. }
  133. if (EngineManager::RegisterEngine(vm_host_cpu, host_cpu_engine) != SUCCESS) {
  134. GELOGW("register host_cpu_engine failed");
  135. }
  136. }
  137. void RegisterRtsEngine() {
  138. const std::string vm_rts = "DNN_VM_RTS";
  139. std::vector<std::string> mem_type_rts;
  140. mem_type_rts.emplace_back(GE_ENGINE_ATTR_MEM_TYPE_HBM);
  141. DNNEngineAttribute attr_rts = {vm_rts, mem_type_rts, COST_1, DEVICE, FORMAT_RESERVED, FORMAT_RESERVED};
  142. DNNEnginePtr rts_engine = MakeShared<RtsDNNEngine>(attr_rts);
  143. if (rts_engine == nullptr) {
  144. GELOGE(ge::FAILED, "make rts_engine failed");
  145. return;
  146. }
  147. if (EngineManager::RegisterEngine(vm_rts, rts_engine) != SUCCESS) {
  148. GELOGW("register rts_engine failed");
  149. }
  150. }
  151. void RegisterHcclEngine() {
  152. const std::string dnn_hccl = "DNN_HCCL";
  153. std::vector<std::string> mem_type_hccl;
  154. mem_type_hccl.emplace_back(GE_ENGINE_ATTR_MEM_TYPE_HBM);
  155. DNNEngineAttribute attr_hccl = {dnn_hccl, mem_type_hccl, COST_1, DEVICE, FORMAT_RESERVED, FORMAT_RESERVED};
  156. DNNEnginePtr hccl_engine = MakeShared<HcclDNNEngine>(attr_hccl);
  157. if (hccl_engine == nullptr) {
  158. GELOGE(ge::FAILED, "make hccl_engine failed");
  159. return;
  160. }
  161. if (EngineManager::RegisterEngine(dnn_hccl, hccl_engine) != SUCCESS) {
  162. GELOGW("register hccl_engine failed");
  163. }
  164. }
  165. void GetDNNEngineObjs(std::map<std::string, DNNEnginePtr> &engines) {
  166. RegisterAiCoreEngine();
  167. RegisterVectorEngine();
  168. RegisterAiCpuTFEngine();
  169. RegisterAiCpuEngine();
  170. RegisterGeLocalEngine();
  171. RegisterHostCpuEngine();
  172. RegisterRtsEngine();
  173. RegisterHcclEngine();
  174. for (auto it = EngineManager::engine_map_->begin(); it != EngineManager::engine_map_->end(); ++it) {
  175. GELOGI("get engine %s from engine plugin.", it->first.c_str());
  176. engines.emplace(std::pair<std::string, DNNEnginePtr>(it->first, it->second));
  177. }
  178. GELOGI("after get engine, engine size: %zu", engines.size());
  179. return;
  180. }
  181. } // namespace ge

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