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 11 kB

5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 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
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 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
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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,
  57. mem_type_aicore,
  58. COST_0,
  59. DEVICE,
  60. FORMAT_RESERVED,
  61. FORMAT_RESERVED,
  62. true };
  63. DNNEnginePtr aicore_engine_ptr = MakeShared<AICoreDNNEngine>(attr_aicore);
  64. if (aicore_engine_ptr == nullptr) {
  65. GELOGE(ge::FAILED, "[Register][AiCoreEngine] failed, as malloc shared_ptr failed.");
  66. REPORT_INNER_ERROR("E19999", "RegisterAiCoreEngine failed for new DNNEnginePtr failed.");
  67. return;
  68. }
  69. if (EngineManager::RegisterEngine(ai_core, aicore_engine_ptr) != SUCCESS) {
  70. GELOGW("register ai_core failed");
  71. }
  72. }
  73. void RegisterVectorEngine() {
  74. const std::string vector_core = "VectorEngine";
  75. std::vector<std::string> mem_type_aivcore;
  76. mem_type_aivcore.emplace_back(GE_ENGINE_ATTR_MEM_TYPE_HBM);
  77. DNNEngineAttribute attr_vector_core = { vector_core,
  78. mem_type_aivcore,
  79. COST_1,
  80. DEVICE,
  81. FORMAT_RESERVED,
  82. FORMAT_RESERVED,
  83. true };
  84. DNNEnginePtr vectorcore_engine_ptr = MakeShared<VectorCoreDNNEngine>(attr_vector_core);
  85. if (vectorcore_engine_ptr == nullptr) {
  86. GELOGE(ge::FAILED, "[Register][VectorEngine] failed, as malloc shared_ptr failed.");
  87. REPORT_INNER_ERROR("E19999", "RegisterVectorEngine failed for new DNNEnginePtr failed.");
  88. return;
  89. }
  90. if (EngineManager::RegisterEngine(vector_core, vectorcore_engine_ptr) != SUCCESS) {
  91. GELOGW("register vector_core failed");
  92. }
  93. }
  94. void RegisterAiCpuEngine() {
  95. const std::string vm_aicpu = "DNN_VM_AICPU_ASCEND";
  96. std::vector<std::string> mem_type_aicpu;
  97. mem_type_aicpu.emplace_back(GE_ENGINE_ATTR_MEM_TYPE_HBM);
  98. DNNEngineAttribute attr_aicpu = { vm_aicpu,
  99. mem_type_aicpu,
  100. COST_2,
  101. DEVICE,
  102. FORMAT_RESERVED,
  103. FORMAT_RESERVED,
  104. true };
  105. DNNEnginePtr vm_engine_ptr = MakeShared<AICpuDNNEngine>(attr_aicpu);
  106. if (vm_engine_ptr == nullptr) {
  107. GELOGE(ge::FAILED, "[Register][AiCpuEngine] failed, as malloc shared_ptr failed.");
  108. REPORT_INNER_ERROR("E19999", "RegisterAiCpuEngine failed for new DNNEnginePtr failed.");
  109. return;
  110. }
  111. if (EngineManager::RegisterEngine(vm_aicpu, vm_engine_ptr) != SUCCESS) {
  112. GELOGW("register vmAicpuEngine failed");
  113. }
  114. }
  115. void RegisterAiCpuTFEngine() {
  116. const std::string vm_aicpu_tf = "DNN_VM_AICPU";
  117. std::vector<std::string> mem_type_aicpu_tf;
  118. mem_type_aicpu_tf.emplace_back(GE_ENGINE_ATTR_MEM_TYPE_HBM);
  119. DNNEngineAttribute attr_aicpu_tf = { vm_aicpu_tf,
  120. mem_type_aicpu_tf,
  121. COST_3,
  122. DEVICE,
  123. FORMAT_RESERVED,
  124. FORMAT_RESERVED,
  125. true };
  126. DNNEnginePtr vm_engine_ptr = MakeShared<AICpuTFDNNEngine>(attr_aicpu_tf);
  127. if (vm_engine_ptr == nullptr) {
  128. GELOGE(ge::FAILED, "[Register][AiCpuTFEngine]make vm_engine_ptr failed");
  129. REPORT_INNER_ERROR("E19999", "RegisterAiCpuTFEngine failed for new DNNEnginePtr failed.");
  130. return;
  131. }
  132. if (EngineManager::RegisterEngine(vm_aicpu_tf, vm_engine_ptr) != SUCCESS) {
  133. GELOGW("register vmAicpuTFEngine failed");
  134. }
  135. }
  136. void RegisterGeLocalEngine() {
  137. const std::string vm_ge_local = "DNN_VM_GE_LOCAL";
  138. std::vector<std::string> mem_type_ge_local;
  139. mem_type_ge_local.emplace_back(GE_ENGINE_ATTR_MEM_TYPE_HBM);
  140. // GeLocal use minimum priority, set it as 9
  141. DNNEngineAttribute attr_ge_local = { vm_ge_local,
  142. mem_type_ge_local,
  143. COST_9,
  144. DEVICE,
  145. FORMAT_RESERVED,
  146. FORMAT_RESERVED,
  147. true };
  148. DNNEnginePtr ge_local_engine = MakeShared<GeLocalDNNEngine>(attr_ge_local);
  149. if (ge_local_engine == nullptr) {
  150. GELOGE(ge::FAILED, "[Register][GeLocalEngine] failed, as malloc shared_ptr failed.");
  151. REPORT_INNER_ERROR("E19999", "RegisterGeLocalEngine failed for new DNNEnginePtr failed.");
  152. return;
  153. }
  154. if (EngineManager::RegisterEngine(vm_ge_local, ge_local_engine) != SUCCESS) {
  155. GELOGW("register ge_local_engine failed");
  156. }
  157. }
  158. void RegisterHostCpuEngine() {
  159. const std::string vm_host_cpu = "DNN_VM_HOST_CPU";
  160. std::vector<std::string> mem_type_host_cpu;
  161. mem_type_host_cpu.emplace_back(GE_ENGINE_ATTR_MEM_TYPE_HBM);
  162. // HostCpu use minimum priority, set it as 10
  163. DNNEngineAttribute attr_host_cpu = { vm_host_cpu,
  164. mem_type_host_cpu,
  165. COST_10,
  166. HOST,
  167. FORMAT_RESERVED,
  168. FORMAT_RESERVED,
  169. true };
  170. DNNEnginePtr host_cpu_engine = MakeShared<HostCpuDNNEngine>(attr_host_cpu);
  171. if (host_cpu_engine == nullptr) {
  172. GELOGE(ge::FAILED, "[Register][HostCpuEngine] failed, as malloc shared_ptr failed.");
  173. REPORT_INNER_ERROR("E19999", "RegisterHostCpuEngine failed for new DNNEnginePtr failed.");
  174. return;
  175. }
  176. if (EngineManager::RegisterEngine(vm_host_cpu, host_cpu_engine) != SUCCESS) {
  177. GELOGW("register host_cpu_engine failed");
  178. }
  179. }
  180. void RegisterRtsEngine() {
  181. const std::string vm_rts = "DNN_VM_RTS";
  182. std::vector<std::string> mem_type_rts;
  183. mem_type_rts.emplace_back(GE_ENGINE_ATTR_MEM_TYPE_HBM);
  184. DNNEngineAttribute attr_rts = { vm_rts,
  185. mem_type_rts,
  186. COST_1,
  187. DEVICE,
  188. FORMAT_RESERVED,
  189. FORMAT_RESERVED,
  190. true };
  191. DNNEnginePtr rts_engine = MakeShared<RtsDNNEngine>(attr_rts);
  192. if (rts_engine == nullptr) {
  193. GELOGE(ge::FAILED, "[Register][RtsEngine] failed, as malloc shared_ptr failed.");
  194. REPORT_INNER_ERROR("E19999", "RegisterRtsEngine failed for new DNNEnginePtr failed.");
  195. return;
  196. }
  197. if (EngineManager::RegisterEngine(vm_rts, rts_engine) != SUCCESS) {
  198. GELOGW("register rts_engine failed");
  199. }
  200. }
  201. void RegisterHcclEngine() {
  202. const std::string dnn_hccl = "DNN_HCCL";
  203. std::vector<std::string> mem_type_hccl;
  204. mem_type_hccl.emplace_back(GE_ENGINE_ATTR_MEM_TYPE_HBM);
  205. DNNEngineAttribute attr_hccl = { dnn_hccl,
  206. mem_type_hccl,
  207. COST_1,
  208. DEVICE,
  209. FORMAT_RESERVED,
  210. FORMAT_RESERVED,
  211. true };
  212. DNNEnginePtr hccl_engine = MakeShared<HcclDNNEngine>(attr_hccl);
  213. if (hccl_engine == nullptr) {
  214. GELOGE(ge::FAILED, "[Register][HcclEngine] failed, as malloc shared_ptr failed.");
  215. REPORT_INNER_ERROR("E19999", "RegisterHcclEngine failed for new DNNEnginePtr failed.");
  216. return;
  217. }
  218. if (EngineManager::RegisterEngine(dnn_hccl, hccl_engine) != SUCCESS) {
  219. GELOGW("register hccl_engine failed");
  220. }
  221. }
  222. void RegisterFftsPlusEngine() {
  223. const std::string dnn_ffts_plus = "DNN_FFTS_PLUS";
  224. std::vector<std::string> mem_type_ffts_plus;
  225. mem_type_ffts_plus.emplace_back(GE_ENGINE_ATTR_MEM_TYPE_HBM);
  226. DNNEngineAttribute attr_ffts_plus = { dnn_ffts_plus,
  227. mem_type_ffts_plus,
  228. COST_0,
  229. DEVICE,
  230. FORMAT_RESERVED,
  231. FORMAT_RESERVED,
  232. false };
  233. DNNEnginePtr ffts_plus_engine = MakeShared<FftsPlusDNNEngine>(attr_ffts_plus);
  234. if (ffts_plus_engine == nullptr) {
  235. GELOGE(ge::FAILED, "[Register][FftsPlusDNNEngine] failed, as malloc shared_ptr failed.");
  236. REPORT_INNER_ERROR("E19999", "RegisterFftsPlusEngine failed for new DNNEnginePtr failed.");
  237. return;
  238. }
  239. if (EngineManager::RegisterEngine(dnn_ffts_plus, ffts_plus_engine) != SUCCESS) {
  240. GELOGW("register ffts_plus_engine failed");
  241. }
  242. }
  243. void GetDNNEngineObjs(std::map<std::string, DNNEnginePtr> &engines) {
  244. RegisterAiCoreEngine();
  245. RegisterVectorEngine();
  246. RegisterAiCpuTFEngine();
  247. RegisterAiCpuEngine();
  248. RegisterGeLocalEngine();
  249. RegisterHostCpuEngine();
  250. RegisterRtsEngine();
  251. RegisterHcclEngine();
  252. RegisterFftsPlusEngine();
  253. for (auto it = EngineManager::engine_map_->begin(); it != EngineManager::engine_map_->end(); ++it) {
  254. GELOGI("get engine %s from engine plugin.", it->first.c_str());
  255. engines.emplace(std::pair<std::string, DNNEnginePtr>(it->first, it->second));
  256. }
  257. GELOGI("after get engine, engine size: %zu", engines.size());
  258. return;
  259. }
  260. } // namespace ge

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