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

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