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

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