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

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