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

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

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