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

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