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.

dnnengine_manager.cc 19 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 "engine_manager/dnnengine_manager.h"
  17. #include <unistd.h>
  18. #include <cstdio>
  19. #include <fstream>
  20. #include <map>
  21. #include <utility>
  22. #include "common/debug/log.h"
  23. #include "common/ge/ge_util.h"
  24. #include "common/util/error_manager/error_manager.h"
  25. #include "framework/common/debug/ge_log.h"
  26. #include "analyzer/analyzer.h"
  27. #include "graph/ge_context.h"
  28. #include "graph/utils/graph_utils.h"
  29. #include "graph/utils/node_utils.h"
  30. #include "init/gelib.h"
  31. namespace {
  32. const char *const kSchedulerUnits = "schedule_units";
  33. const char *const kId = "id";
  34. const char *const kName = "name";
  35. const char *const kExAttrs = "ex_attrs";
  36. const char *const kIndependent = "independent";
  37. const char *const kSkipAssignStream = "skip_assign_stream";
  38. const char *const kCalEngines = "cal_engines";
  39. const char *const kAttch = "attach";
  40. const char *const kVectorCore = "VectorCore";
  41. const char *const kVectorEngine = "VectorEngine";
  42. const char *const kAIcoreEngine = "AIcoreEngine";
  43. const char *const kCustomOpFlag = "_custom_op_flag";
  44. const char *const kHostCpuEngineName = "DNN_VM_HOST_CPU";
  45. const char *const kHostCpuOpKernelLibName = "DNN_VM_HOST_CPU_OP_STORE";
  46. } // namespace
  47. namespace ge {
  48. DNNEngineManager::DNNEngineManager() : init_flag_(false) {}
  49. DNNEngineManager::~DNNEngineManager() {
  50. engines_attrs_map_.clear();
  51. schedulers_.clear();
  52. }
  53. Status DNNEngineManager::Initialize(const std::map<std::string, std::string> &options) {
  54. // Multiple initializations are not supported
  55. if (init_flag_) {
  56. GELOGW("DNNEngineManager has been initialized.");
  57. return SUCCESS;
  58. }
  59. // Load engine so
  60. std::string so_path = "plugin/nnengine/";
  61. std::string path = PluginManager::GetPath();
  62. path.append(so_path);
  63. std::string so_api_func = "GetDNNEngineObjs";
  64. std::vector<std::string> so_func{so_api_func};
  65. Status status = plugin_mgr_.Load(path, so_func);
  66. if (status != SUCCESS) {
  67. GELOGE(status, "Load engine's so failed. LibPath is %s", path.c_str());
  68. return status;
  69. }
  70. status = plugin_mgr_.InvokeAll<std::map<std::string, DNNEnginePtr> &>(so_api_func, engines_map_);
  71. if (status != SUCCESS) {
  72. GELOGE(status, "Get DNNEngineObjs failed.");
  73. return status;
  74. }
  75. GELOGI("The number of DNNEngineObjs is %zu.", engines_map_.size());
  76. // Engines initialize
  77. for (auto iter = engines_map_.begin(); iter != engines_map_.end(); ++iter) {
  78. if (iter->second == nullptr) {
  79. GELOGI("Engine: %s point to nullptr", (iter->first).c_str());
  80. continue;
  81. }
  82. GELOGI("DNNEngine name: %s.", (iter->first).c_str());
  83. status = iter->second->Initialize(options);
  84. if (status != SUCCESS) {
  85. GELOGE(status, "Engine: %s initialize failed.", (iter->first).c_str());
  86. return status;
  87. }
  88. // Check engines' attribute
  89. DNNEngineAttribute attrs;
  90. iter->second->GetAttributes(attrs);
  91. if (attrs.runtime_type == RuntimeType::DEVICE) {
  92. if ((attrs.mem_type.size()) != 1 || (attrs.mem_type[0] != GE_ENGINE_ATTR_MEM_TYPE_HBM)) {
  93. GELOGE(GE_ENG_MEMTYPE_ERROR, "Engine: %s in aicore, but the memory type is not HBM", (iter->first).c_str());
  94. return GE_ENG_MEMTYPE_ERROR;
  95. }
  96. }
  97. }
  98. status = ParserJsonFile();
  99. if (status != SUCCESS) {
  100. GELOGE(status, "parse json file failed");
  101. return status;
  102. }
  103. status = CheckJsonFile();
  104. if (status != SUCCESS) {
  105. GELOGE(status, "check json file failed");
  106. return status;
  107. }
  108. init_flag_ = true;
  109. return SUCCESS;
  110. }
  111. Status DNNEngineManager::Finalize() {
  112. // Finalize is not allowed, initialize first is necessary
  113. if (!init_flag_) {
  114. GELOGW("DNNEngineManager has been finalized.");
  115. return SUCCESS;
  116. }
  117. for (auto iter = engines_map_.begin(); iter != engines_map_.end(); ++iter) {
  118. if (iter->second != nullptr) {
  119. GELOGI("DNNEngine name: %s.", (iter->first).c_str());
  120. Status status = iter->second->Finalize();
  121. if (status != SUCCESS) {
  122. GELOGE(status, "Engine finalize failed.");
  123. return status;
  124. }
  125. }
  126. }
  127. init_flag_ = false;
  128. engines_map_.clear();
  129. return SUCCESS;
  130. }
  131. std::shared_ptr<ge::DNNEngine> DNNEngineManager::GetEngine(const std::string &name) const {
  132. auto iter = engines_map_.find(name);
  133. if (iter != engines_map_.end()) {
  134. return iter->second;
  135. }
  136. GELOGW("Failed to get engine object by engine name. %s.", name.c_str());
  137. return nullptr;
  138. }
  139. bool DNNEngineManager::IsEngineRegistered(const std::string &name) {
  140. auto iter = engines_map_.find(name);
  141. if (iter != engines_map_.end()) {
  142. return true;
  143. }
  144. GELOGW("Engine: %s is not Registered", name.c_str());
  145. return false;
  146. }
  147. void DNNEngineManager::InitPerformanceStaistic() {
  148. std::lock_guard<std::mutex> lock(mutex_);
  149. checksupport_cost_.clear();
  150. }
  151. const map<string, uint64_t> &DNNEngineManager::GetCheckSupportCost() const {
  152. std::lock_guard<std::mutex> lock(mutex_);
  153. return checksupport_cost_;
  154. }
  155. std::string DNNEngineManager::GetDNNEngineName(const ge::NodePtr &node_ptr) {
  156. std::lock_guard<std::mutex> lock(mutex_);
  157. GE_IF_BOOL_EXEC(node_ptr == nullptr, GELOGE(GE_CLI_GE_NOT_INITIALIZED, "DNNEngineManager: node_ptr is nullptr");
  158. return "");
  159. auto op_desc = node_ptr->GetOpDesc();
  160. GE_IF_BOOL_EXEC(op_desc == nullptr, GELOGE(GE_CLI_GE_NOT_INITIALIZED, "DNNEngineManager: op_desc is nullptr");
  161. return "");
  162. // Use the OpsKernelManager in GELib to get the opInfos for this opCode
  163. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  164. if ((instance_ptr == nullptr) || (!instance_ptr->InitFlag())) {
  165. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "GetDNNEngineName failed.");
  166. return "";
  167. }
  168. OpsKernelManager &ops_kernel_manager = instance_ptr->OpsKernelManagerObj();
  169. std::vector<OpInfo> op_infos = ops_kernel_manager.GetOpsKernelInfo(op_desc->GetType());
  170. if (op_infos.empty()) {
  171. GELOGI("DNNEngineManager: Can not get op info by op type %s", op_desc->GetType().c_str());
  172. return "";
  173. }
  174. GE_IF_BOOL_EXEC(ge::GetContext().GetHostExecFlag(), return GetHostCpuEngineName(op_infos, op_desc));
  175. std::string ge_core_type;
  176. Status ret = ge::GetContext().GetOption(ge::CORE_TYPE, ge_core_type);
  177. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGD("get the option CORE_TYPE fail, set it to default value VECTOR_ENGINE"));
  178. std::string exclude_core_Type = (ge_core_type == kVectorCore) ? kAIcoreEngine : kVectorEngine;
  179. GELOGD("engine type will exclude: %s", exclude_core_Type.c_str());
  180. auto root_graph = ge::GraphUtils::FindRootGraph(node_ptr->GetOwnerComputeGraph());
  181. std::map<std::string, std::string> unsupported_reasons;
  182. for (const auto &it : op_infos) {
  183. if (it.engine == exclude_core_Type) {
  184. continue;
  185. }
  186. auto &kernel_map = ops_kernel_manager.GetAllOpsKernelInfoStores();
  187. auto &kernel_name = it.opKernelLib;
  188. auto kernel_info_store = kernel_map.find(kernel_name);
  189. if (kernel_info_store != kernel_map.end()) {
  190. std::string unsupported_reason;
  191. // It will be replaced by engine' checksupport
  192. uint64_t start_time = GetCurrentTimestap();
  193. if (kernel_info_store->second->CheckSupported(op_desc, unsupported_reason)) {
  194. checksupport_cost_[kernel_name] += GetCurrentTimestap() - start_time;
  195. op_desc->SetOpEngineName(it.engine);
  196. op_desc->SetOpKernelLibName(kernel_name);
  197. // set attrs for taking information when load txt to graph object
  198. (void)AttrUtils::SetStr(op_desc, ATTR_NAME_ENGINE_NAME_FOR_LX, it.engine);
  199. (void)AttrUtils::SetStr(op_desc, ATTR_NAME_KKERNEL_LIB_NAME_FOR_LX, kernel_name);
  200. GELOGD("DNNEngineManager:Set OpKernelLibName %s and engine name %s to op_desc %s", kernel_name.c_str(),
  201. it.engine.c_str(), op_desc->GetName().c_str());
  202. return it.engine;
  203. } else {
  204. checksupport_cost_[kernel_name] += GetCurrentTimestap() - start_time;
  205. bool is_custom_op = false;
  206. if ((ge::AttrUtils::GetBool(op_desc, kCustomOpFlag, is_custom_op)) && is_custom_op) {
  207. ErrorManager::GetInstance().ATCReportErrMessage("E13001", {"kernelname", "optype", "opname"},
  208. {kernel_name, op_desc->GetType(), op_desc->GetName()});
  209. GELOGE(FAILED,
  210. "The custom operator registered by the user does not support the logic function delivered by this "
  211. "network. Check support failed, kernel_name is %s, op type is %s, op name is %s",
  212. kernel_name.c_str(), op_desc->GetType().c_str(), op_desc->GetName().c_str());
  213. std::string error_info =
  214. "The custom operator registered by the user does not support the logic function"
  215. "delivered by this network";
  216. return "";
  217. }
  218. unsupported_reasons.emplace(kernel_name, unsupported_reason);
  219. GELOGI("DNNEngineManager:Check support failed, kernel_name is %s, op type is %s, op name is %s",
  220. kernel_name.c_str(), op_desc->GetType().c_str(), op_desc->GetName().c_str());
  221. if (!op_desc->HasAttr("_is_ge_op")) {
  222. ErrorManager::GetInstance().ATCReportErrMessage("W11001", {"opname"}, {op_desc->GetName()});
  223. }
  224. }
  225. } else {
  226. GELOGW(
  227. "DNNEngineManager:Can not find any supported ops kernel info store by kernel_name %s,"
  228. "op type is %s, op name is %s",
  229. kernel_name.c_str(), op_desc->GetType().c_str(), op_desc->GetName().c_str());
  230. }
  231. }
  232. // concat unsupported reasons analyzed data selection
  233. string reason;
  234. for (const auto &it : unsupported_reasons) {
  235. reason += it.first + ":" + it.second + ";";
  236. ErrorManager::GetInstance().ATCReportErrMessage("E13002", {"optype", "opskernel", "reason"},
  237. {op_desc->GetType(), it.first, it.second});
  238. GELOGE(GE_GRAPH_ASSIGN_ENGINE_FAILED, "GetDNNEngineName:Op type %s of ops kernel %s is unsupported, reason:%s",
  239. op_desc->GetType().c_str(), it.first.c_str(), it.second.c_str());
  240. }
  241. analyzer::DataInfo analyze_info{root_graph->GetSessionID(), root_graph->GetGraphID(), analyzer::CHECKSUPPORT,
  242. node_ptr, reason};
  243. // do not change original process
  244. (void)Analyzer::GetInstance()->DoAnalyze(analyze_info);
  245. ErrorManager::GetInstance().ATCReportErrMessage("E13003", {"opname", "optype"},
  246. {op_desc->GetName(), op_desc->GetType()});
  247. GELOGE(GE_GRAPH_ASSIGN_ENGINE_FAILED, "Can't find any supported ops kernel and engine of %s, type is %s",
  248. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  249. return "";
  250. }
  251. std::string DNNEngineManager::GetHostCpuEngineName(const std::vector<OpInfo> &op_infos,
  252. const OpDescPtr &op_desc) const {
  253. for (const auto &it : op_infos) {
  254. if ((it.engine == kHostCpuEngineName) && (it.opKernelLib == kHostCpuOpKernelLibName)) {
  255. op_desc->SetOpEngineName(kHostCpuEngineName);
  256. op_desc->SetOpKernelLibName(kHostCpuOpKernelLibName);
  257. GELOGI("DNNEngineManager: Set OpKernelLibName %s and OpEngineName %s to %s", kHostCpuOpKernelLibName,
  258. kHostCpuEngineName, op_desc->GetName().c_str());
  259. return kHostCpuEngineName;
  260. }
  261. }
  262. GELOGE(FAILED, "DNNEngineManager: HostCpuEngine not support [%s, %s].", op_desc->GetName().c_str(),
  263. op_desc->GetType().c_str());
  264. return "";
  265. }
  266. const std::map<std::string, SchedulerConf> &DNNEngineManager::GetSchedulers() const { return schedulers_; }
  267. Status DNNEngineManager::ParserJsonFile() {
  268. GELOGI("Begin to parser json file");
  269. std::string json_file_path = "plugin/nnengine/ge_config/engine_conf.json";
  270. std::string path = PluginManager::GetPath();
  271. path.append(json_file_path);
  272. nlohmann::json scheduler_json_file;
  273. Status status = ReadJsonFile(path, &scheduler_json_file);
  274. if (status != SUCCESS) {
  275. GELOGE(FAILED, "Read scheduler json file failed and the file path is %s", path.c_str());
  276. return FAILED;
  277. }
  278. if (scheduler_json_file.is_null()) {
  279. // when engine_conf.json is not exist, just return success
  280. GELOGW("Json file is null");
  281. return SUCCESS;
  282. }
  283. try {
  284. nlohmann::json scheduler_utils_json = scheduler_json_file[kSchedulerUnits];
  285. if (scheduler_utils_json.is_null()) {
  286. GELOGE(FAILED, "The message of scheduler units is not found");
  287. return FAILED;
  288. }
  289. if (!scheduler_utils_json.is_array()) {
  290. GELOGE(FAILED, "The message of kSchedulerUnits is not array and the file path is %s", json_file_path.c_str());
  291. return FAILED;
  292. }
  293. auto size = scheduler_json_file[kSchedulerUnits].size();
  294. for (size_t i = 0; i < size; i++) {
  295. SchedulerConf scheduler_conf;
  296. std::map<std::string, EngineConfPtr> engine_conf_map;
  297. nlohmann::json engines_json_map = scheduler_utils_json[i][kCalEngines];
  298. if (engines_json_map.is_null()) {
  299. GELOGE(FAILED, "The message of cal_engines is not found");
  300. return FAILED;
  301. }
  302. std::string scheduler_id_temp = scheduler_utils_json[i][kId];
  303. if (!scheduler_id_temp.empty()) {
  304. scheduler_conf.id = scheduler_id_temp;
  305. } else {
  306. GELOGE(FAILED, "Scheduler ID is null");
  307. return FAILED;
  308. }
  309. status = ParserEngineMessage(engines_json_map, scheduler_id_temp, engine_conf_map);
  310. if (status != SUCCESS) {
  311. GELOGE(FAILED, "Parser engines messages failed");
  312. return FAILED;
  313. }
  314. scheduler_conf.name = scheduler_utils_json[i][kName];
  315. scheduler_conf.ex_attrs = scheduler_utils_json[i][kExAttrs];
  316. scheduler_conf.cal_engines = engine_conf_map;
  317. auto it = schedulers_.find(scheduler_id_temp);
  318. if (it != schedulers_.end()) {
  319. GELOGE(FAILED, "There are the same scheduler ts %s in the json file", scheduler_id_temp.c_str());
  320. return FAILED;
  321. }
  322. schedulers_.emplace(scheduler_id_temp, scheduler_conf);
  323. }
  324. } catch (const nlohmann::detail::type_error &e) {
  325. GELOGE(FAILED, "Parser json file failed");
  326. return FAILED;
  327. }
  328. GELOGI("Parser json file SUCCESS");
  329. return SUCCESS;
  330. }
  331. Status DNNEngineManager::ParserEngineMessage(const json engines_json, const std::string &scheduler_mark,
  332. std::map<std::string, EngineConfPtr> &engines) {
  333. GELOGI("Begin to parser engine massage");
  334. if (engines_json.is_null()) {
  335. GELOGE(FAILED, "The message of cal_engines is null");
  336. return FAILED;
  337. }
  338. try {
  339. if (engines_json.is_array()) {
  340. for (size_t i = 0; i < engines_json.size(); i++) {
  341. nlohmann::json engines_elems = engines_json[i];
  342. EngineConfPtr engine_conf_ptr = MakeShared<EngineConf>();
  343. if (engine_conf_ptr == nullptr) {
  344. return FAILED;
  345. }
  346. std::string engine_id = engines_elems[kId];
  347. if (!engine_id.empty()) {
  348. engine_conf_ptr->id = engine_id;
  349. } else {
  350. GELOGE(FAILED, "engineID is null");
  351. return FAILED;
  352. }
  353. if (engines_elems.find(kName) != engines_elems.end()) {
  354. engine_conf_ptr->name = engines_elems[kName];
  355. } else {
  356. GELOGW("The engine %s name is null", engine_id.c_str());
  357. }
  358. if (engines_elems.find(kIndependent) != engines_elems.end()) {
  359. engine_conf_ptr->independent = engines_elems[kIndependent];
  360. }
  361. if (engines_elems.find(kAttch) != engines_elems.end()) {
  362. engine_conf_ptr->attach = engines_elems[kAttch];
  363. }
  364. if (engines_elems.find(kSkipAssignStream) != engines_elems.end()) {
  365. engine_conf_ptr->skip_assign_stream = engines_elems[kSkipAssignStream];
  366. }
  367. engine_conf_ptr->scheduler_id = scheduler_mark;
  368. auto it = engines.find(engine_id);
  369. if (it != engines.end()) {
  370. GELOGE(FAILED, "There are the same engine %s message in the json file", engine_id.c_str());
  371. return FAILED;
  372. }
  373. engines.emplace(engine_id, engine_conf_ptr);
  374. }
  375. } else {
  376. GELOGE(FAILED, "The message of cal_engines is not array in the json file");
  377. return FAILED;
  378. }
  379. } catch (const json::exception &e) {
  380. GELOGE(FAILED, "construct json content failed");
  381. return FAILED;
  382. }
  383. GELOGI("Parser engine massage success");
  384. return SUCCESS;
  385. }
  386. Status DNNEngineManager::ReadJsonFile(const std::string &file_path, JsonHandle handle) {
  387. GELOGD("Begin to read json file");
  388. if (file_path.empty()) {
  389. GELOGE(FAILED, "Json path %s is not valid", file_path.c_str());
  390. return FAILED;
  391. }
  392. nlohmann::json *json_file = reinterpret_cast<nlohmann::json *>(handle);
  393. if (json_file == nullptr) {
  394. GELOGE(FAILED, "JsonFile is nullptr");
  395. return FAILED;
  396. }
  397. const char *file = file_path.data();
  398. if ((access(file, F_OK)) == -1) {
  399. if (engines_map_.size() != 0) {
  400. GELOGE(FAILED, "The json file %s is not exist, %s", file_path.c_str(), strerror(errno));
  401. return FAILED;
  402. } else {
  403. GELOGW("The json file %s is not needed.", file_path.c_str());
  404. return SUCCESS;
  405. }
  406. }
  407. std::ifstream ifs(file_path);
  408. if (!ifs.is_open()) {
  409. GELOGE(FAILED, "Open json file %s failed", file_path.c_str());
  410. return FAILED;
  411. }
  412. try {
  413. ifs >> *json_file;
  414. } catch (const json::exception &e) {
  415. GELOGE(FAILED, "Read json file failed");
  416. ifs.close();
  417. return FAILED;
  418. }
  419. ifs.close();
  420. GELOGD("Read json file success");
  421. return SUCCESS;
  422. }
  423. Status DNNEngineManager::CheckJsonFile() {
  424. GELOGD("Begin to check json file");
  425. for (auto &it : engines_map_) {
  426. std::string engine_name = it.first;
  427. int count = 0;
  428. for (auto &iter : schedulers_) {
  429. auto engine_map = iter.second.cal_engines;
  430. auto iter_engine_name = engine_map.find(engine_name);
  431. if (iter_engine_name != engine_map.end()) {
  432. count++;
  433. }
  434. }
  435. if (count == 0) {
  436. GELOGE(FAILED, "The engine message %s is not found in the json file", engine_name.c_str());
  437. return FAILED;
  438. }
  439. if (count > 1) {
  440. GELOGE(FAILED, "The same engine message %s is existed in the json file", engine_name.c_str());
  441. return FAILED;
  442. }
  443. }
  444. GELOGD("Check json file success");
  445. return SUCCESS;
  446. }
  447. } // namespace ge

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