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

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