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.

ge_api.cc 31 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
4 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
5 years ago
4 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
5 years ago
5 years ago
5 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
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
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 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
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
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
5 years ago
4 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
4 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
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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 "external/ge/ge_api.h"
  17. #include <iostream>
  18. #include <malloc.h>
  19. #include "framework/common/debug/log.h"
  20. #include "framework/common/debug/ge_log.h"
  21. #include "framework/executor/ge_executor.h"
  22. #include "common/ge/datatype_util.h"
  23. #include "proto/ge_api.pb.h"
  24. #include "graph/model_serialize.h"
  25. #include "graph/detail/model_serialize_imp.h"
  26. #include "graph/utils/tensor_adapter.h"
  27. #include "init/gelib.h"
  28. #include "session/session_manager.h"
  29. #include "graph/opsproto_manager.h"
  30. #include "graph/utils/type_utils.h"
  31. #include "graph/manager/util/rt_context_util.h"
  32. #include "common/ge_call_wrapper.h"
  33. #include "register/op_registry.h"
  34. #include "common/ge/tbe_plugin_manager.h"
  35. #include "common/util/error_manager/error_manager.h"
  36. #include "toolchain/plog.h"
  37. #include "ir_build/option_utils.h"
  38. using domi::OpRegistry;
  39. using std::map;
  40. using std::string;
  41. using std::vector;
  42. namespace {
  43. const int32_t kMaxStrLen = 128;
  44. } // namespace
  45. static bool g_ge_initialized = false;
  46. static std::mutex g_ge_release_mutex; // GEFinalize and ~Session use
  47. static std::shared_ptr<ge::SessionManager> g_session_manager;
  48. namespace ge {
  49. void GetOpsProtoPath(std::string &opsproto_path) {
  50. GELOGI("Enter get ops proto path schedule");
  51. const char *path_env = std::getenv("ASCEND_OPP_PATH");
  52. if (path_env != nullptr) {
  53. std::string path = path_env;
  54. opsproto_path = (path + "/op_proto/custom/" + ":") + (path + "/op_proto/built-in/");
  55. GELOGI("Get opsproto so path from env: %s", path.c_str());
  56. return;
  57. }
  58. std::string path_base = PluginManager::GetPath();
  59. GELOGI("path_base is %s", path_base.c_str());
  60. path_base = path_base.substr(0, path_base.rfind('/'));
  61. path_base = path_base.substr(0, path_base.rfind('/') + 1);
  62. opsproto_path = (path_base + "ops/op_proto/custom/" + ":") + (path_base + "ops/op_proto/built-in/");
  63. }
  64. Status CheckOptionsValid(const std::map<string, string> &options) {
  65. // check job_id is valid
  66. auto job_id_iter = options.find(OPTION_EXEC_JOB_ID);
  67. if (job_id_iter != options.end()) {
  68. if (job_id_iter->second.length() > kMaxStrLen) {
  69. GELOGE(PARAM_INVALID, "[Check][JobId]Failed, the job_id [%s] string length: %zu > max string length: %d",
  70. job_id_iter->second.c_str(), job_id_iter->second.length(), kMaxStrLen);
  71. REPORT_INPUT_ERROR("E10051", std::vector<std::string>({"id", "length"}),
  72. std::vector<std::string>({job_id_iter->second,
  73. std::to_string(kMaxStrLen)}));
  74. return FAILED;
  75. }
  76. }
  77. // check modify_mixlist is valid
  78. if (ge::CheckModifyMixlistParamValid(options) != ge::SUCCESS) {
  79. return FAILED;
  80. }
  81. return SUCCESS;
  82. }
  83. // Initialize GE, prepare for execution, call GELib::Initialize
  84. Status GEInitializeImpl(const std::map<string, string> &options) {
  85. ErrorManager::GetInstance().GenWorkStreamIdDefault();
  86. GELOGT(TRACE_INIT, "GEInitialize start");
  87. std::string path_base = ge::GELib::GetPath();
  88. auto ret = ErrorManager::GetInstance().Init(path_base);
  89. if (ret != SUCCESS) {
  90. GELOGE(GE_CLI_INIT_FAILED, "[Init][PathBase]Init failed when pass param path_base:%s", path_base.c_str());
  91. REPORT_CALL_ERROR("E19999", "Init failed when pass param path_base:%s", path_base.c_str());
  92. return ret;
  93. }
  94. // 0.check init status
  95. if (g_ge_initialized) {
  96. GELOGW("GEInitialize is called more than once");
  97. return SUCCESS;
  98. }
  99. ErrorManager::GetInstance().SetStage(error_message::kInitialize, error_message::kOpsProtoInit);
  100. // Load OpsProto lib plugin
  101. std::string opsproto_path;
  102. GetOpsProtoPath(opsproto_path);
  103. OpsProtoManager *manager = OpsProtoManager::Instance();
  104. std::map<string, string> option_tmp;
  105. option_tmp.emplace(std::pair<string, string>(string("ge.opsProtoLibPath"), opsproto_path));
  106. GE_TIMESTAMP_START(GEInitialize);
  107. bool is_proto_init = manager->Initialize(option_tmp);
  108. GE_TIMESTAMP_END(GEInitialize, "GEInitialize::ManagerInitialize");
  109. if (!is_proto_init) {
  110. GELOGE(GE_CLI_INIT_FAILED, "[Init][OpsProtoPath]Loading OpsProto lib plugin failed, OpsProtoPath:%s invalid.",
  111. opsproto_path.c_str());
  112. REPORT_CALL_ERROR("E19999", "Loading OpsProto lib plugin failed, OpsProtoPath:%s invalid", opsproto_path.c_str());
  113. return FAILED;
  114. }
  115. ErrorManager::GetInstance().SetStage(error_message::kInitialize, error_message::kOther);
  116. // check options is valid
  117. GE_TIMESTAMP_START(CheckOptionsValid);
  118. if (CheckOptionsValid(options) != SUCCESS) {
  119. return FAILED;
  120. }
  121. GE_TIMESTAMP_END(CheckOptionsValid, "GEInitialize::CheckOptionsValid");
  122. ErrorManager::GetInstance().SetStage(error_message::kInitialize, error_message::kOpsProtoInit);
  123. GE_TIMESTAMP_START(InitPreparation);
  124. TBEPluginManager::Instance().InitPreparation(options);
  125. GE_TIMESTAMP_END(InitPreparation, "GEInitialize::InitPreparation");
  126. // call Initialize
  127. GELOGT(TRACE_RUNNING, "Initializing environment");
  128. ErrorManager::GetInstance().SetStage(error_message::kInitialize, error_message::kOther);
  129. GE_TIMESTAMP_START(GELibInitialize);
  130. ret = ge::GELib::Initialize(options);
  131. GE_TIMESTAMP_END(GELibInitialize, "GEInitialize::GELibInitialize");
  132. if (ret != SUCCESS) {
  133. GELOGE(GE_CLI_INIT_FAILED, "[Init][GELib]Failed, error code = %u", ret);
  134. return FAILED;
  135. }
  136. ErrorManager::GetInstance().SetStage(error_message::kInitialize, error_message::kOther);
  137. GELOGI("sessionManager initial.");
  138. GE_TIMESTAMP_START(SessionManagerInitialize);
  139. g_session_manager = MakeShared<ge::SessionManager>();
  140. if (g_session_manager == nullptr) {
  141. GELOGE(GE_CLI_INIT_FAILED, "[Init][Create]SessionManager failed");
  142. return FAILED;
  143. }
  144. ret = g_session_manager->Initialize(options);
  145. GE_TIMESTAMP_END(SessionManagerInitialize, "InnerInitialize::SessionManagerInitialize");
  146. if (ret != SUCCESS) {
  147. GELOGE(ret, "[Init][SessionManager] GE session manager initial failed.");
  148. REPORT_CALL_ERROR("E19999", "SessionManager initialize failed.");
  149. return ret;
  150. }
  151. ErrorManager::GetInstance().SetStage(error_message::kInitialize, error_message::kOther);
  152. GELOGI("GeExecutor initial.");
  153. GE_TIMESTAMP_START(GeExecutorInitialize);
  154. ret = GeExecutor::Initialize(options);
  155. if (ret != SUCCESS) {
  156. GELOGE(ret, "[Init][GeExecutor] GeExecutor initialize failed.");
  157. REPORT_CALL_ERROR("E19999", "GeExecutor initialize failed.");
  158. return ret;
  159. }
  160. GE_TIMESTAMP_END(GeExecutorInitialize, "GeExecutor::Initialize");
  161. // 7.check return status, return
  162. if (!g_ge_initialized) {
  163. // Initialize success, first time calling initialize
  164. g_ge_initialized = true;
  165. }
  166. GELOGT(TRACE_STOP, "GEInitialize finished");
  167. return ret;
  168. }
  169. // Initialize GE, prepare for execution, call GELib::Initialize
  170. Status GEInitialize(const std::map<string, string> &options) {
  171. ErrorManager::GetInstance().SetStage(error_message::kInitialize, error_message::kOther);
  172. if (DlogReportInitialize() != SUCCESS) {
  173. GELOGW("Dlog report device log initialize failed.");
  174. }
  175. return GEInitializeImpl(options);
  176. }
  177. Status GEInitialize(const std::map<AscendString, AscendString> &options) {
  178. ErrorManager::GetInstance().SetStage(error_message::kInitialize, error_message::kOther);
  179. std::map<std::string, std::string> str_options;
  180. for (auto &option : options) {
  181. if (option.first.GetString() == nullptr || option.second.GetString() == nullptr) {
  182. GELOGE(FAILED, "[Check][Param]Options invalid, first or second option is nullptr.");
  183. REPORT_INNER_ERROR("E19999", "Check parameter's options invalid, the first or second option is nullptr.");
  184. return FAILED;
  185. }
  186. std::string key = option.first.GetString();
  187. std::string val = option.second.GetString();
  188. str_options[key] = val;
  189. }
  190. if (DlogReportInitialize() != SUCCESS) {
  191. GELOGW("Dlog report device log initialize failed.");
  192. }
  193. return GEInitializeImpl(str_options);
  194. }
  195. // GE finalize, releasing all resources
  196. Status GEFinalize() {
  197. std::lock_guard<std::mutex> lock(g_ge_release_mutex);
  198. // check init status
  199. if (!g_ge_initialized) {
  200. GELOGW("[FINAL][FINAL]GEFinalize is called before GEInitialize");
  201. return SUCCESS;
  202. }
  203. ErrorManager::GetInstance().SetStage(error_message::kFinalize, error_message::kFinalize);
  204. ErrorManager::GetInstance().GenWorkStreamIdDefault();
  205. GELOGT(TRACE_INIT, "GEFinalize start");
  206. (void)GeExecutor::FinalizeEx();
  207. GELOGI("SessionManager finalization.");
  208. if (g_session_manager != nullptr) {
  209. (void)g_session_manager->Finalize(); // always success.
  210. }
  211. // call Finalize
  212. Status ret = SUCCESS;
  213. Status middle_ret;
  214. GELOGT(TRACE_RUNNING, "Finalizing environment");
  215. std::shared_ptr<GELib> instancePtr = GELib::GetInstance();
  216. if (instancePtr != nullptr) {
  217. middle_ret = instancePtr->Finalize();
  218. GELOGI("GEFinalize finalize gelib ret=%u", middle_ret);
  219. if (middle_ret != SUCCESS) {
  220. ret = middle_ret;
  221. }
  222. }
  223. middle_ret = TBEPluginManager::Instance().Finalize();
  224. if (middle_ret != SUCCESS) {
  225. ret = middle_ret;
  226. }
  227. if (g_ge_initialized && ret == SUCCESS) {
  228. // Unified destruct rt_context
  229. RtContextUtil::GetInstance().DestroyAllRtContexts();
  230. g_ge_initialized = false;
  231. }
  232. // to avoid memory fragment, use malloc_trim to back free stack to system
  233. malloc_trim(0);
  234. if (DlogReportFinalize() != SUCCESS) {
  235. GELOGW("Dlog report device log finalize failed.");
  236. }
  237. GELOGT(TRACE_STOP, "GEFinalize finished");
  238. return ret;
  239. }
  240. std::string GEGetErrorMsg() {
  241. return ErrorManager::GetInstance().GetErrorMessage();
  242. }
  243. std::string GEGetWarningMsg() {
  244. return ErrorManager::GetInstance().GetWarningMessage();
  245. }
  246. // Initialize session,which calls innerSession
  247. Session::Session(const std::map<string, string> &options) {
  248. ErrorManager::GetInstance().SetStage(error_message::kInitialize, error_message::kOther);
  249. GELOGT(TRACE_INIT, "Start to construct session.");
  250. ErrorManager::GetInstance().GenWorkStreamIdDefault();
  251. // check init status
  252. sessionId_ = 0;
  253. if (!g_ge_initialized) {
  254. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Construct][Session]Failed because lack GEInitialize call before.");
  255. REPORT_INNER_ERROR("E19999", "Creating session failed because lack GEInitialize call before.");
  256. return;
  257. }
  258. GELOGT(TRACE_RUNNING, "Creating session");
  259. uint64_t session_id = 0;
  260. Status ret = g_session_manager->CreateSession(options, session_id);
  261. GELOGT(TRACE_RUNNING, "Session id is %lu", session_id);
  262. // check return status, return, update session id if success
  263. if (ret == SUCCESS) {
  264. sessionId_ = session_id;
  265. } else {
  266. GELOGE(ret, "[Construct][Session]Failed, error code:%u.", ret);
  267. return;
  268. }
  269. GELOGT(TRACE_STOP, "Session Constructor finished");
  270. }
  271. Session::Session(const std::map<AscendString, AscendString> &options) {
  272. ErrorManager::GetInstance().SetStage(error_message::kInitialize, error_message::kOther);
  273. GELOGT(TRACE_INIT, "Session Constructor start");
  274. ErrorManager::GetInstance().GenWorkStreamIdDefault();
  275. // check init status
  276. sessionId_ = 0;
  277. if (!g_ge_initialized) {
  278. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Construct][Session]Failed because lack GEInitialize call before.");
  279. REPORT_INNER_ERROR("E19999", "Creating session failed because lack GEInitialize call before.");
  280. return;
  281. }
  282. // call Initialize
  283. GELOGT(TRACE_RUNNING, "Creating session");
  284. std::map<std::string, std::string> str_options;
  285. for (auto &option : options) {
  286. if (option.first.GetString() == nullptr || option.second.GetString() == nullptr) {
  287. GELOGE(FAILED, "[Construct][Session]Failed, the first or second option is nullptr.");
  288. REPORT_INNER_ERROR("E19999", "Creating session's options invalid, the first or second option is nullptr.");
  289. return;
  290. }
  291. std::string key = option.first.GetString();
  292. std::string val = option.second.GetString();
  293. str_options[key] = val;
  294. }
  295. uint64_t session_id = 0;
  296. Status ret = g_session_manager->CreateSession(str_options, session_id);
  297. GELOGT(TRACE_RUNNING, "Session id is %lu", session_id);
  298. // check return status, return, update session id if success
  299. if (ret == SUCCESS) {
  300. sessionId_ = session_id;
  301. } else {
  302. GELOGE(ret, "[Construct][Session]Failed, error code:%u.", ret);
  303. REPORT_CALL_ERROR("E19999", "Construct session failed, error code:%u.", ret);
  304. return;
  305. }
  306. GELOGT(TRACE_STOP, "Session Constructor finished");
  307. }
  308. // session destructor
  309. Session::~Session() {
  310. ErrorManager::GetInstance().SetStage(error_message::kFinalize, error_message::kFinalize);
  311. GELOGT(TRACE_INIT, "Start to destruct session.");
  312. // 0.check init status
  313. if (!g_ge_initialized) {
  314. GELOGW("GE is not yet initialized or is finalized.");
  315. return;
  316. }
  317. Status ret = FAILED;
  318. std::lock_guard<std::mutex> lock(g_ge_release_mutex);
  319. try {
  320. uint64_t session_id = sessionId_;
  321. // call DestroySession
  322. GELOGT(TRACE_RUNNING, "Session id is %lu", session_id);
  323. GELOGT(TRACE_RUNNING, "Destroying session");
  324. ret = g_session_manager->DestroySession(session_id);
  325. } catch (google::protobuf::FatalException &e) {
  326. GELOGE(GE_CLI_SESS_DESTROY_FAILED, "[Destruct][Session]Failed because get fatalException.");
  327. REPORT_CALL_ERROR("E19999", "Destruct session failed, get fatal exception");
  328. }
  329. // check return status, return, update session id if success
  330. if (ret != SUCCESS) {
  331. GELOGE(ret, "[Destruct][Session]Failed, error code:%u.", ret);
  332. REPORT_CALL_ERROR("E19999", "Destruct session failed, error code:%u.", ret);
  333. }
  334. GELOGT(TRACE_STOP, "Session Destructor finished");
  335. }
  336. // Add Graph
  337. Status Session::AddGraph(uint32_t graph_id, const Graph &graph) {
  338. std::map<std::string, std::string> options;
  339. return AddGraph(graph_id, graph, options);
  340. }
  341. // Add Graph
  342. Status Session::AddGraph(uint32_t graph_id, const Graph &graph, const std::map<std::string, std::string> &options) {
  343. ErrorManager::GetInstance().SetStage(error_message::kModelCompile, error_message::kOther);
  344. GELOGT(TRACE_INIT, "Start to add graph in Session. graph_id: %u, session_id: %lu.", graph_id, sessionId_);
  345. ErrorManager::GetInstance().GenWorkStreamIdBySessionGraph(sessionId_, graph_id);
  346. if (!g_ge_initialized) {
  347. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Construct][Session]Failed because lack GEInitialize call before.");
  348. REPORT_INNER_ERROR("E19999", "Creating session failed because lack GEInitialize call before.");
  349. return FAILED;
  350. }
  351. GELOGD("Adding graph to session");
  352. Status ret = g_session_manager->AddGraph(sessionId_, graph_id, graph, options);
  353. if (ret != SUCCESS) {
  354. GELOGE(ret, "[Add][Graph]Failed, error code:%u, session_id:%lu, graph_id:%u.", ret, sessionId_, graph_id);
  355. return FAILED;
  356. }
  357. GELOGD("AddGraph finished in Session.");
  358. return ret;
  359. }
  360. //Add Graph
  361. Status Session::AddGraph(uint32_t graph_id, const Graph &graph, const std::map<AscendString, AscendString> &options) {
  362. ErrorManager::GetInstance().SetStage(error_message::kModelCompile, error_message::kOther);
  363. GELOGT(TRACE_INIT, "Start to add graph in Session. graph_id: %u, session_id: %lu.", graph_id, sessionId_);
  364. ErrorManager::GetInstance().GenWorkStreamIdBySessionGraph(sessionId_, graph_id);
  365. if (!g_ge_initialized) {
  366. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Construct][Session]Failed because lack GEInitialize call before.");
  367. REPORT_INNER_ERROR("E19999", "Creating session failed because lack GEInitialize call before.");
  368. return FAILED;
  369. }
  370. GELOGD("Adding graph to session");
  371. std::map<std::string, std::string> str_options;
  372. for (auto &option : options) {
  373. if (option.first.GetString() == nullptr || option.second.GetString() == nullptr) {
  374. GELOGE(FAILED, "[Add][Graph]Failed, the first or second option is nullptr.");
  375. REPORT_INNER_ERROR("E19999", "Add Graph Failed, the first or second option is nullptr.");
  376. return FAILED;
  377. }
  378. std::string key = option.first.GetString();
  379. std::string val = option.second.GetString();
  380. str_options[key] = val;
  381. }
  382. Status ret = g_session_manager->AddGraph(sessionId_, graph_id, graph, str_options);
  383. if (ret != SUCCESS) {
  384. GELOGE(ret, "[Add][Graph]Failed, error code:%u, session_id:%lu, graph_id:%u.", ret, sessionId_, graph_id);
  385. return FAILED;
  386. }
  387. GELOGD("AddGraph finished in Session.");
  388. return ret;
  389. }
  390. Status Session::AddGraphWithCopy(uint32_t graph_id, const Graph &graph) {
  391. std::map<AscendString, AscendString> options;
  392. return AddGraphWithCopy(graph_id, graph, options);
  393. }
  394. // Add Graph With Copy
  395. Status Session::AddGraphWithCopy(uint32_t graph_id, const Graph &graph,
  396. const std::map<AscendString, AscendString> &options) {
  397. ErrorManager::GetInstance().SetStage(error_message::kModelCompile, error_message::kOther);
  398. GELOGT(TRACE_INIT, "Start to add graph in Session. graph_id: %u, session_id: %lu.", graph_id, sessionId_);
  399. ErrorManager::GetInstance().GenWorkStreamIdBySessionGraph(sessionId_, graph_id);
  400. if (!g_ge_initialized) {
  401. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Construct][Session]Failed because lack GEInitialize call before.");
  402. REPORT_INNER_ERROR("E19999", "Creating session failed because lack GEInitialize call before.");
  403. return FAILED;
  404. }
  405. std::map<std::string, std::string> str_options;
  406. for (auto it = options.begin(); it != options.end(); ++it) {
  407. str_options.insert({it->first.GetString(), it->second.GetString()});
  408. }
  409. GELOGD("Adding graph to session");
  410. Status ret = g_session_manager->AddGraphWithCopy(sessionId_, graph_id, graph, str_options);
  411. if (ret != SUCCESS) {
  412. GELOGE(ret, "[Add][Graph]Failed, error code:%u, session_id:%lu, graph_id:%u.", ret, sessionId_, graph_id);
  413. return FAILED;
  414. }
  415. GELOGD("AddGraph finished in Session.");
  416. return ret;
  417. }
  418. // Remove Graph
  419. Status Session::RemoveGraph(uint32_t graph_id) {
  420. ErrorManager::GetInstance().SetStage(error_message::kModelCompile, error_message::kOther);
  421. GELOGT(TRACE_INIT, "Session RemoveGraph start");
  422. ErrorManager::GetInstance().GenWorkStreamIdBySessionGraph(sessionId_, graph_id);
  423. // call RemoveGraph
  424. if (!g_ge_initialized) {
  425. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Construct][Session]Failed because lack GEInitialize call before.");
  426. REPORT_INNER_ERROR("E19999", "Creating session failed because lack GEInitialize call before.");
  427. return FAILED;
  428. }
  429. GELOGT(TRACE_RUNNING, "Removing Graph from session");
  430. Status ret = g_session_manager->RemoveGraph(sessionId_, graph_id);
  431. // check return status, return
  432. if (ret != SUCCESS) {
  433. GELOGE(ret, "[Remove][Graph]Failed, error code:%u, session_id:%lu, graph_id:%u.", ret, sessionId_, graph_id);
  434. REPORT_CALL_ERROR("E19999", "Remove graph failed, error code:%u, session_id:%lu, graph_id:%u",
  435. ret, sessionId_, graph_id);
  436. return FAILED;
  437. }
  438. GELOGT(TRACE_STOP, "Session RemoveGraph finished");
  439. return ret;
  440. }
  441. // Print Output Result
  442. void PrintOutputResult(std::vector<Tensor> &outputs) {
  443. if (outputs.empty() || outputs[0].GetData() == nullptr) {
  444. GELOGW("outputs is empty or data is nullptr.");
  445. return;
  446. }
  447. size_t out_buf_size = outputs[0].GetSize();
  448. TensorDesc desc(outputs[0].GetTensorDesc());
  449. DataType data_type = desc.GetDataType();
  450. auto iter = CONST_OPDATA_TYPE_SIZE_MAP.find(data_type);
  451. if (iter == CONST_OPDATA_TYPE_SIZE_MAP.end()) {
  452. GELOGI("DataType %s has not defined size", TypeUtils::DataTypeToSerialString(data_type).c_str());
  453. return;
  454. }
  455. size_t length = CONST_OPDATA_TYPE_SIZE_MAP[data_type];
  456. for (size_t i = 0; i < 10 && i < (out_buf_size / length); ++i) { // take first 10 at most
  457. switch (data_type) {
  458. case DT_BOOL:
  459. case DT_INT8:
  460. case DT_UINT8:
  461. GELOGI("output data[%zu]=%d", i, *(reinterpret_cast<int8_t *>(outputs[0].GetData()) + i));
  462. break;
  463. case DT_INT16:
  464. case DT_UINT16:
  465. GELOGI("output data[%zu]=%d", i, *(reinterpret_cast<int16_t *>(outputs[0].GetData()) + i));
  466. break;
  467. case DT_INT32:
  468. case DT_UINT32:
  469. GELOGI("output data[%zu]=%d", i, *(reinterpret_cast<int32_t *>(outputs[0].GetData()) + i));
  470. break;
  471. case DT_INT64:
  472. case DT_UINT64:
  473. GELOGI("output data[%zu]=%ld", i, *(reinterpret_cast<int64_t *>(outputs[0].GetData()) + i));
  474. break;
  475. case DT_FLOAT:
  476. GELOGI("output data[%zu]=%f", i, *(reinterpret_cast<float *>(outputs[0].GetData()) + i));
  477. break;
  478. case DT_DOUBLE:
  479. GELOGI("output data[%zu]=%lf", i, *(reinterpret_cast<double *>(outputs[0].GetData()) + i));
  480. break;
  481. default:
  482. GELOGI("Output datatype %s is not supported.", TypeUtils::DataTypeToSerialString(data_type).c_str());
  483. return;
  484. }
  485. }
  486. }
  487. // Run Graph
  488. Status Session::RunGraph(uint32_t graph_id, const std::vector<Tensor> &inputs, std::vector<Tensor> &outputs) {
  489. ErrorManager::GetInstance().SetStage(error_message::kModelCompile, error_message::kOther);
  490. GELOGT(TRACE_INIT, "Session RunGraph start");
  491. ErrorManager::GetInstance().GenWorkStreamIdBySessionGraph(sessionId_, graph_id);
  492. if (!g_ge_initialized) {
  493. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Construct][Session]Failed because lack GEInitialize call before.");
  494. REPORT_INNER_ERROR("E19999", "Creating session failed because lack GEInitialize call before.");
  495. return FAILED;
  496. }
  497. // call RunGraph
  498. GELOGT(TRACE_RUNNING, "Running Graph");
  499. Status ret = g_session_manager->RunGraph(sessionId_, graph_id, inputs, outputs);
  500. // check return status
  501. if (ret != SUCCESS) {
  502. GELOGE(ret, "[Run][Graph]Failed, error code:%u, session_id:%lu, graph_id:%u.", ret, sessionId_, graph_id);
  503. REPORT_CALL_ERROR("E19999", "Remove graph failed, error code:%u, session_id:%lu, graph_id:%u",
  504. ret, sessionId_, graph_id);
  505. return FAILED;
  506. }
  507. // print output
  508. if (outputs.size() > 0) {
  509. PrintOutputResult(outputs);
  510. }
  511. // return
  512. GELOGT(TRACE_STOP, "Session RunGraph finished");
  513. return ret;
  514. }
  515. // Run Graph with stream Asynchronously
  516. Status Session::RunGraphWithStreamAsync(uint32_t graph_id, void *stream, const std::vector<Tensor> &inputs,
  517. std::vector<Tensor> &outputs) {
  518. ErrorManager::GetInstance().SetStage(error_message::kModelCompile, error_message::kOther);
  519. GELOGT(TRACE_INIT, "Start to run graph with stream async.");
  520. ErrorManager::GetInstance().GenWorkStreamIdBySessionGraph(sessionId_, graph_id);
  521. if (!g_ge_initialized) {
  522. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Construct][Session]Failed because lack GEInitialize call before.");
  523. REPORT_INNER_ERROR("E19999", "Creating session failed because lack GEInitialize call before.");
  524. return FAILED;
  525. }
  526. GELOGT(TRACE_RUNNING, "Run Graph Run graph with stream asyn.");
  527. Status ret = g_session_manager->RunGraphWithStreamAsync(sessionId_, graph_id, stream, inputs, outputs);
  528. if (ret != SUCCESS) {
  529. GELOGE(ret, "[Run][Graph]Run graph with stream asyn Failed,"
  530. "error code = %u, session id = %lu, graph id = %u, stream = %p.", ret, sessionId_, graph_id, stream);
  531. REPORT_CALL_ERROR("E19999", "[Run][Graph]Run graph with stream asyn failed, error code = %u, session id = %lu,"
  532. "graph id = %u, stream = %p.", ret, sessionId_, graph_id, stream);
  533. return FAILED;
  534. }
  535. GELOGT(TRACE_STOP, "Session run graph with stream async finished");
  536. return SUCCESS;
  537. }
  538. // Register Call Back
  539. Status Session::RegisterCallBackFunc(const std::string &key, const pCallBackFunc &callback) {
  540. ErrorManager::GetInstance().GenWorkStreamIdDefault();
  541. if (!g_ge_initialized) {
  542. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Construct][Session]Failed because lack GEInitialize call before.");
  543. REPORT_INNER_ERROR("E19999", "Creating session failed because lack GEInitialize call before.");
  544. return FAILED;
  545. }
  546. return g_session_manager->RegisterCallBackFunc(sessionId_, key, callback);
  547. }
  548. Status Session::RegisterCallBackFunc(const char *key, const session::pCallBackFunc &callback) {
  549. ErrorManager::GetInstance().GenWorkStreamIdDefault();
  550. if (!g_ge_initialized) {
  551. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Construct][Session]Failed because lack GEInitialize call before.");
  552. REPORT_INNER_ERROR("E19999", "Creating session failed because lack GEInitialize call before.");
  553. return FAILED;
  554. }
  555. std::string str_key;
  556. if (key != nullptr) {
  557. str_key = key;
  558. }
  559. return g_session_manager->RegisterCallBackFunc(sessionId_, str_key, callback);
  560. }
  561. // Build Graph
  562. Status Session::BuildGraph(uint32_t graph_id, const std::vector<InputTensorInfo> &inputs) {
  563. ErrorManager::GetInstance().SetStage(error_message::kModelCompile, error_message::kOther);
  564. ErrorManager::GetInstance().GenWorkStreamIdBySessionGraph(sessionId_, graph_id);
  565. if (!g_ge_initialized) {
  566. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Construct][Session]Failed because lack GEInitialize call before.");
  567. REPORT_INNER_ERROR("E19999", "Creating session failed because lack GEInitialize call before.");
  568. return FAILED;
  569. }
  570. GELOGT(TRACE_RUNNING, "Building Graph");
  571. Status ret = g_session_manager->BuildGraph(sessionId_, graph_id, inputs);
  572. if (ret != SUCCESS) {
  573. GELOGE(ret, "[Build][Graph]Failed, error code:%u, session_id:%lu, graph_id:%u.", ret, sessionId_, graph_id);
  574. REPORT_CALL_ERROR("E19999", "Build graph failed , error code:%u, session_id:%lu, graph_id:%u",
  575. ret, sessionId_, graph_id);
  576. return FAILED;
  577. }
  578. return SUCCESS;
  579. }
  580. // Build Graph
  581. Status Session::BuildGraph(uint32_t graph_id, const std::vector<ge::Tensor> &inputs) {
  582. ErrorManager::GetInstance().SetStage(error_message::kModelCompile, error_message::kOther);
  583. ErrorManager::GetInstance().GenWorkStreamIdBySessionGraph(sessionId_, graph_id);
  584. if (!g_ge_initialized) {
  585. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Construct][Session]Failed because lack GEInitialize call before.");
  586. REPORT_INNER_ERROR("E19999", "Creating session failed because lack GEInitialize call before.");
  587. return FAILED;
  588. }
  589. GELOGT(TRACE_RUNNING, "Building Graph");
  590. Status ret = g_session_manager->BuildGraph(sessionId_, graph_id, inputs);
  591. if (ret != SUCCESS) {
  592. GELOGE(ret, "[Build][Graph]Failed, error code:%u, session_id:%lu, graph_id:%u.", ret, sessionId_, graph_id);
  593. REPORT_CALL_ERROR("E19999", "Build graph failed , error code:%u, session_id:%lu, graph_id:%u",
  594. ret, sessionId_, graph_id);
  595. return FAILED;
  596. }
  597. return SUCCESS;
  598. }
  599. // Run Graph Asynchronously
  600. Status Session::RunGraphAsync(uint32_t graph_id, const std::vector<ge::Tensor> &inputs,
  601. RunAsyncCallback callback) {
  602. ErrorManager::GetInstance().SetStage(error_message::kModelExecute, error_message::kModelExecute);
  603. ErrorManager::GetInstance().GenWorkStreamIdBySessionGraph(sessionId_, graph_id);
  604. if (!g_ge_initialized) {
  605. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Construct][Session]Failed because lack GEInitialize call before.");
  606. REPORT_INNER_ERROR("E19999", "Creating session failed because lack GEInitialize call before.");
  607. return FAILED;
  608. }
  609. GELOGT(TRACE_RUNNING, "Run Graph Asynchronously");
  610. GELOGW(
  611. "The callback function will not be checked. Please ensure that the implementation of the function is trusted.");
  612. Status ret = g_session_manager->RunGraphAsync(sessionId_, graph_id, inputs, callback);
  613. if (ret != SUCCESS) {
  614. GELOGE(ret, "[Run][Graph]RunGraphAsync Failed, error code:%u, session_id:%lu, graph_id:%u.",
  615. ret, sessionId_, graph_id);
  616. REPORT_CALL_ERROR("E19999", "RunGraphAsync Failed, error code:%u, session_id:%lu, graph_id:%u",
  617. ret, sessionId_, graph_id);
  618. return FAILED;
  619. }
  620. return SUCCESS;
  621. }
  622. // Get Variables
  623. Status Session::GetVariables(const std::vector<std::string> &var_names, std::vector<Tensor> &var_values) {
  624. ErrorManager::GetInstance().SetStage(error_message::kModelExecute, error_message::kModelExecute);
  625. ErrorManager::GetInstance().GenWorkStreamIdDefault();
  626. if (!g_ge_initialized) {
  627. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Construct][Session]Failed because lack GEInitialize call before.");
  628. REPORT_INNER_ERROR("E19999", "Creating session failed because lack GEInitialize call before.");
  629. return FAILED;
  630. }
  631. GELOGT(TRACE_RUNNING, "Get Variables");
  632. Status ret = g_session_manager->GetVariables(sessionId_, var_names, var_values);
  633. if (ret != SUCCESS) {
  634. GELOGE(ret, "[Get][Variables]Failed, error code:%u, session_id:%lu.", ret, sessionId_);
  635. return FAILED;
  636. }
  637. return SUCCESS;
  638. }
  639. // Get Variables
  640. Status Session::GetVariables(const std::vector<AscendString> &var_names, std::vector<Tensor> &var_values) {
  641. ErrorManager::GetInstance().SetStage(error_message::kModelExecute, error_message::kModelExecute);
  642. ErrorManager::GetInstance().GenWorkStreamIdDefault();
  643. if (!g_ge_initialized) {
  644. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Construct][Session]Failed because lack GEInitialize call before.");
  645. REPORT_INNER_ERROR("E19999", "Creating session failed because lack GEInitialize call before.");
  646. return FAILED;
  647. }
  648. GELOGT(TRACE_RUNNING, "Get Variables");
  649. std::vector<ge::string> str_var_names;
  650. for (auto &var_name : var_names) {
  651. if (var_name.GetString() == nullptr) {
  652. GELOGE(FAILED, "[Get][Variable]Failed, variables' names are nullptr.");
  653. REPORT_INNER_ERROR("E19999", "GetVariables failed, variables' names are nullptr.");
  654. return FAILED;
  655. }
  656. str_var_names.emplace_back(var_name.GetString());
  657. }
  658. Status ret = g_session_manager->GetVariables(sessionId_, str_var_names, var_values);
  659. if (ret != SUCCESS) {
  660. GELOGE(ret, "[Get][Variables]Failed, error code:%u, session_id:%lu.", ret, sessionId_);
  661. REPORT_CALL_ERROR("E19999", "Get variables failed, error code:%u, session_id:%lu.", ret, sessionId_);
  662. return FAILED;
  663. }
  664. return SUCCESS;
  665. }
  666. bool Session::IsGraphNeedRebuild(uint32_t graph_id) {
  667. if (!g_ge_initialized) {
  668. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Construct][Session]Failed because lack GEInitialize call before.");
  669. REPORT_INNER_ERROR("E19999", "Creating session failed because lack GEInitialize call before.");
  670. return false;
  671. }
  672. return g_session_manager->IsGraphNeedRebuild(sessionId_, graph_id);
  673. }
  674. } // namespace ge

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