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 22 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
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
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
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
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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, "CheckOptionsValid job_id failed, string len > %d", kMaxStrLen);
  67. return FAILED;
  68. }
  69. }
  70. return SUCCESS;
  71. }
  72. // Initialize GE, prepare for execution, call GELib::Initialize
  73. Status GEInitializeImpl(const std::map<string, string> &options) {
  74. GELOGT(TRACE_INIT, "GEInitialize start");
  75. std::string path_base = ge::GELib::GetPath();
  76. auto ret = ErrorManager::GetInstance().Init(path_base);
  77. if (ret != SUCCESS) {
  78. GELOGE(GE_CLI_INIT_FAILED, "ErrorManager init fail");
  79. return ret;
  80. }
  81. ErrorManager::GetInstance().GenWorkStreamIdDefault();
  82. // 0.check init status
  83. if (g_ge_initialized) {
  84. GELOGW("GEInitialize is called more than once");
  85. return SUCCESS;
  86. }
  87. // Load OpsProto lib plugin
  88. std::string opsproto_path;
  89. GetOpsProtoPath(opsproto_path);
  90. OpsProtoManager *manager = OpsProtoManager::Instance();
  91. std::map<string, string> option_tmp;
  92. option_tmp.emplace(std::pair<string, string>(string("ge.opsProtoLibPath"), opsproto_path));
  93. GE_TIMESTAMP_START(GEInitialize);
  94. bool is_proto_init = manager->Initialize(option_tmp);
  95. GE_TIMESTAMP_END(GEInitialize, "GEInitialize::ManagerInitialize");
  96. if (!is_proto_init) {
  97. GELOGE(GE_CLI_INIT_FAILED, "geInitialize failed, ops proto path is invalid.");
  98. return FAILED;
  99. }
  100. // check options is valid
  101. GE_TIMESTAMP_START(CheckOptionsValid);
  102. if (CheckOptionsValid(options) != SUCCESS) {
  103. return FAILED;
  104. }
  105. GE_TIMESTAMP_END(CheckOptionsValid, "GEInitialize::CheckOptionsValid");
  106. GE_TIMESTAMP_START(InitPreparation);
  107. TBEPluginManager::Instance().InitPreparation(options);
  108. GE_TIMESTAMP_END(InitPreparation, "GEInitialize::InitPreparation");
  109. // call Initialize
  110. GELOGT(TRACE_RUNNING, "Initializing environment");
  111. GE_TIMESTAMP_START(GELibInitialize);
  112. ret = ge::GELib::Initialize(options);
  113. GE_TIMESTAMP_END(GELibInitialize, "GEInitialize::GELibInitialize");
  114. if (ret != SUCCESS) {
  115. GELOGE(GE_CLI_INIT_FAILED, "geInitialize failed, error code = %u", ret);
  116. return FAILED;
  117. }
  118. // 7.check return status, return
  119. if (!g_ge_initialized) {
  120. // Initialize success, first time calling initialize
  121. g_ge_initialized = true;
  122. }
  123. GELOGT(TRACE_STOP, "GEInitialize finished");
  124. return ret;
  125. }
  126. // Initialize GE, prepare for execution, call GELib::Initialize
  127. Status GEInitialize(const std::map<string, string> &options) {
  128. if (DlogReportInitialize() != SUCCESS) {
  129. GELOGW("Dlog report device log initialize failed.");
  130. }
  131. return GEInitializeImpl(options);
  132. }
  133. Status GEInitialize(const std::map<AscendString, AscendString> &options) {
  134. std::map<std::string, std::string> str_options;
  135. for (auto &option : options) {
  136. if (option.first.GetString() == nullptr || option.second.GetString() == nullptr) {
  137. GELOGE(FAILED, "GEInitialize options is nullptr.");
  138. return FAILED;
  139. }
  140. std::string key = option.first.GetString();
  141. std::string val = option.second.GetString();
  142. str_options[key] = val;
  143. }
  144. if (DlogReportInitialize() != SUCCESS) {
  145. GELOGW("Dlog report device log initialize failed.");
  146. }
  147. return GEInitializeImpl(str_options);
  148. }
  149. // GE finalize, releasing all resources
  150. Status GEFinalize() {
  151. GELOGT(TRACE_INIT, "GEFinalize start");
  152. ErrorManager::GetInstance().GenWorkStreamIdDefault();
  153. // check init status
  154. if (!g_ge_initialized) {
  155. GELOGW("GEFinalize is called before GEInitialize");
  156. return SUCCESS;
  157. }
  158. std::lock_guard<std::mutex> lock(g_ge_release_mutex);
  159. // call Finalize
  160. Status ret = SUCCESS;
  161. Status middle_ret;
  162. GELOGT(TRACE_RUNNING, "Finalizing environment");
  163. std::shared_ptr<GELib> instancePtr = ge::GELib::GetInstance();
  164. if (instancePtr == nullptr || !instancePtr->InitFlag()) {
  165. GELOGW("GEFinalize Failed: GE not initialized.");
  166. ret = GE_CLI_GE_NOT_INITIALIZED;
  167. }
  168. if (ret != GE_CLI_GE_NOT_INITIALIZED) {
  169. middle_ret = instancePtr->Finalize();
  170. GELOGI("GEFinalize finalize gelib ret=%u", middle_ret);
  171. if (middle_ret != SUCCESS) {
  172. ret = middle_ret;
  173. }
  174. }
  175. middle_ret = TBEPluginManager::Instance().Finalize();
  176. if (middle_ret != SUCCESS) {
  177. ret = middle_ret;
  178. }
  179. if (g_ge_initialized && ret == SUCCESS) {
  180. // Unified destruct rt_context
  181. RtContextUtil::GetInstance().DestroyAllRtContexts();
  182. g_ge_initialized = false;
  183. }
  184. // to avoid memory fragment, use malloc_trim to back free stack to system
  185. malloc_trim(0);
  186. if (DlogReportFinalize() != SUCCESS) {
  187. GELOGW("Dlog report device log finalize failed.");
  188. }
  189. GELOGT(TRACE_STOP, "GEFinalize finished");
  190. return ret;
  191. }
  192. std::string GEGetErrorMsg() {
  193. return ErrorManager::GetInstance().GetErrorMessage();
  194. }
  195. std::string GEGetWarningMsg() {
  196. return ErrorManager::GetInstance().GetWarningMessage();
  197. }
  198. // Initialize session,which calls innerSession
  199. Session::Session(const std::map<string, string> &options) {
  200. GELOGT(TRACE_INIT, "Session Constructor start");
  201. ErrorManager::GetInstance().GenWorkStreamIdDefault();
  202. // check init status
  203. sessionId_ = 0;
  204. if (!g_ge_initialized) {
  205. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "GE is not initialized.");
  206. return;
  207. }
  208. // call Initialize
  209. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  210. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  211. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "Session Constructor failed");
  212. return;
  213. }
  214. GELOGT(TRACE_RUNNING, "Creating session");
  215. uint64_t session_id = 0;
  216. Status ret = instance_ptr->SessionManagerObj().CreateSession(options, session_id);
  217. GELOGT(TRACE_RUNNING, "Session id is %lu", session_id);
  218. // check return status, return, update session id if success
  219. if (ret == SUCCESS) {
  220. sessionId_ = session_id;
  221. } else {
  222. GELOGE(ret, "Session constructor failed, session Id not initialized");
  223. return;
  224. }
  225. GELOGT(TRACE_STOP, "Session Constructor finished");
  226. }
  227. Session::Session(const std::map<AscendString, AscendString> &options) {
  228. GELOGT(TRACE_INIT, "Session Constructor start");
  229. ErrorManager::GetInstance().GenWorkStreamIdDefault();
  230. // check init status
  231. sessionId_ = 0;
  232. if (!g_ge_initialized) {
  233. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "GE is not initialized.");
  234. return;
  235. }
  236. // call Initialize
  237. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  238. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  239. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "Session Constructor failed");
  240. return;
  241. }
  242. GELOGT(TRACE_RUNNING, "Creating session");
  243. std::map<std::string, std::string> str_options;
  244. for (auto &option : options) {
  245. if (option.first.GetString() == nullptr || option.second.GetString() == nullptr) {
  246. GELOGE(FAILED, "Session options is nullptr.");
  247. return;
  248. }
  249. std::string key = option.first.GetString();
  250. std::string val = option.second.GetString();
  251. str_options[key] = val;
  252. }
  253. uint64_t session_id = 0;
  254. Status ret = instance_ptr->SessionManagerObj().CreateSession(str_options, session_id);
  255. GELOGT(TRACE_RUNNING, "Session id is %lu", session_id);
  256. // check return status, return, update session id if success
  257. if (ret == SUCCESS) {
  258. sessionId_ = session_id;
  259. } else {
  260. GELOGE(ret, "Session constructor failed, session Id not initialized");
  261. return;
  262. }
  263. GELOGT(TRACE_STOP, "Session Constructor finished");
  264. }
  265. // session destructor
  266. Session::~Session() {
  267. GELOGT(TRACE_INIT, "Session Destructor start");
  268. // 0.check init status
  269. if (!g_ge_initialized) {
  270. GELOGW("GE is not yet initialized or is finalized.");
  271. return;
  272. }
  273. Status ret = FAILED;
  274. std::lock_guard<std::mutex> lock(g_ge_release_mutex);
  275. try {
  276. uint64_t session_id = sessionId_;
  277. // call DestroySession
  278. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  279. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  280. GELOGW("GE is not yet initialized or is finalized.");
  281. return;
  282. }
  283. GELOGT(TRACE_RUNNING, "Session id is %lu", session_id);
  284. GELOGT(TRACE_RUNNING, "Destroying session");
  285. ret = instance_ptr->SessionManagerObj().DestroySession(session_id);
  286. } catch (google::protobuf::FatalException &e) {
  287. GELOGE(GE_CLI_SESS_DESTROY_FAILED, "SessionDestructor throws FatalException");
  288. }
  289. // check return status, return, update session id if success
  290. if (ret != SUCCESS) {
  291. GELOGE(ret, "Session Destructor failed");
  292. }
  293. GELOGT(TRACE_STOP, "Session Destructor finished");
  294. }
  295. Status Session::AddGraph(uint32_t graph_id, const Graph &graph) {
  296. std::map<std::string, std::string> options;
  297. ErrorManager::GetInstance().GenWorkStreamIdBySessionGraph(sessionId_, graph_id);
  298. return AddGraph(graph_id, graph, options);
  299. }
  300. Status Session::AddGraph(uint32_t graph_id, const Graph &graph, const std::map<std::string, std::string> &options) {
  301. GELOGT(TRACE_INIT, "Start to add graph in Session. graph_id: %u, session_id: %lu.", graph_id, sessionId_);
  302. ErrorManager::GetInstance().GenWorkStreamIdBySessionGraph(sessionId_, graph_id);
  303. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  304. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  305. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "AddGraph failed in Session.");
  306. return FAILED;
  307. }
  308. GELOGD("Adding graph to session");
  309. Status ret = instance_ptr->SessionManagerObj().AddGraph(sessionId_, graph_id, graph, options);
  310. if (ret != SUCCESS) {
  311. GELOGE(ret, "AddGraph failed in Session.");
  312. return FAILED;
  313. }
  314. GELOGD("AddGraph finished in Session.");
  315. return ret;
  316. }
  317. Status Session::AddGraph(uint32_t graph_id, const Graph &graph,
  318. const std::map<AscendString, AscendString> &options) {
  319. GELOGT(TRACE_INIT, "Start to add graph in Session. graph_id: %u, session_id: %lu.", graph_id, sessionId_);
  320. ErrorManager::GetInstance().GenWorkStreamIdBySessionGraph(sessionId_, graph_id);
  321. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  322. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  323. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "AddGraph failed in Session.");
  324. return FAILED;
  325. }
  326. GELOGD("Adding graph to session");
  327. std::map<std::string, std::string> str_options;
  328. for (auto &option : options) {
  329. if (option.first.GetString() == nullptr || option.second.GetString() == nullptr) {
  330. GELOGE(FAILED, "AddGraph options is nullptr.");
  331. return FAILED;
  332. }
  333. std::string key = option.first.GetString();
  334. std::string val = option.second.GetString();
  335. str_options[key] = val;
  336. }
  337. Status ret = instance_ptr->SessionManagerObj().AddGraph(sessionId_, graph_id, graph, str_options);
  338. if (ret != SUCCESS) {
  339. GELOGE(ret, "AddGraph failed in Session.");
  340. return FAILED;
  341. }
  342. GELOGD("AddGraph finished in Session.");
  343. return ret;
  344. }
  345. Status Session::AddGraphWithCopy(uint32_t graph_id, const Graph &graph) {
  346. ErrorManager::GetInstance().GenWorkStreamIdBySessionGraph(sessionId_, graph_id);
  347. std::map<AscendString, AscendString> options;
  348. return AddGraphWithCopy(graph_id, graph, options);
  349. }
  350. Status Session::AddGraphWithCopy(uint32_t graph_id, const Graph &graph,
  351. const std::map<AscendString, AscendString> &options) {
  352. GELOGT(TRACE_INIT, "Start to add graph in Session. graph_id: %u, session_id: %lu.", graph_id, sessionId_);
  353. ErrorManager::GetInstance().GenWorkStreamIdBySessionGraph(sessionId_, graph_id);
  354. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  355. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  356. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "AddGraph failed in Session.");
  357. return FAILED;
  358. }
  359. std::map<std::string, std::string> str_options;
  360. for (auto it = options.begin(); it != options.end(); ++it) {
  361. str_options.insert({it->first.GetString(), it->second.GetString()});
  362. }
  363. GELOGD("Adding graph to session");
  364. Status ret = instance_ptr->SessionManagerObj().AddGraphWithCopy(sessionId_, graph_id, graph, str_options);
  365. if (ret != SUCCESS) {
  366. GELOGE(ret, "AddGraph failed in Session.");
  367. return FAILED;
  368. }
  369. GELOGD("AddGraph finished in Session.");
  370. return ret;
  371. }
  372. Status Session::RemoveGraph(uint32_t graph_id) {
  373. GELOGT(TRACE_INIT, "Session RemoveGraph start");
  374. ErrorManager::GetInstance().GenWorkStreamIdBySessionGraph(sessionId_, graph_id);
  375. // call RemoveGraph
  376. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  377. if (!instance_ptr || !instance_ptr->InitFlag()) {
  378. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "Session RemoveGraph failed");
  379. return FAILED;
  380. }
  381. GELOGT(TRACE_RUNNING, "Removing Graph from session");
  382. Status ret = instance_ptr->SessionManagerObj().RemoveGraph(sessionId_, graph_id);
  383. // check return status, return
  384. if (ret != SUCCESS) {
  385. GELOGE(ret, "session RemoveGraph failed");
  386. return FAILED;
  387. }
  388. GELOGT(TRACE_STOP, "Session RemoveGraph finished");
  389. return ret;
  390. }
  391. void PrintOutputResult(std::vector<Tensor> &outputs) {
  392. if (outputs.empty() || outputs[0].GetData() == nullptr) {
  393. GELOGW("outputs is empty or data is nullptr.");
  394. return;
  395. }
  396. size_t out_buf_size = outputs[0].GetSize();
  397. TensorDesc desc(outputs[0].GetTensorDesc());
  398. DataType data_type = desc.GetDataType();
  399. auto iter = CONST_OPDATA_TYPE_SIZE_MAP.find(data_type);
  400. if (iter == CONST_OPDATA_TYPE_SIZE_MAP.end()) {
  401. GELOGI("DataType %s has not defined size", TypeUtils::DataTypeToSerialString(data_type).c_str());
  402. return;
  403. }
  404. size_t length = CONST_OPDATA_TYPE_SIZE_MAP[data_type];
  405. for (size_t i = 0; i < 10 && i < (out_buf_size / length); ++i) { // take first 10 at most
  406. switch (data_type) {
  407. case DT_BOOL:
  408. case DT_INT8:
  409. case DT_UINT8:
  410. GELOGI("output data[%zu]=%d", i, *(reinterpret_cast<int8_t *>(outputs[0].GetData()) + i));
  411. break;
  412. case DT_INT16:
  413. case DT_UINT16:
  414. GELOGI("output data[%zu]=%d", i, *(reinterpret_cast<int16_t *>(outputs[0].GetData()) + i));
  415. break;
  416. case DT_INT32:
  417. case DT_UINT32:
  418. GELOGI("output data[%zu]=%d", i, *(reinterpret_cast<int32_t *>(outputs[0].GetData()) + i));
  419. break;
  420. case DT_INT64:
  421. case DT_UINT64:
  422. GELOGI("output data[%zu]=%ld", i, *(reinterpret_cast<int64_t *>(outputs[0].GetData()) + i));
  423. break;
  424. case DT_FLOAT:
  425. GELOGI("output data[%zu]=%f", i, *(reinterpret_cast<float *>(outputs[0].GetData()) + i));
  426. break;
  427. case DT_DOUBLE:
  428. GELOGI("output data[%zu]=%lf", i, *(reinterpret_cast<double *>(outputs[0].GetData()) + i));
  429. break;
  430. default:
  431. GELOGI("Output datatype %s is not supported.", TypeUtils::DataTypeToSerialString(data_type).c_str());
  432. return;
  433. }
  434. }
  435. }
  436. Status Session::RunGraph(uint32_t graph_id, const std::vector<Tensor> &inputs, std::vector<Tensor> &outputs) {
  437. GELOGT(TRACE_INIT, "Session RunGraph start");
  438. ErrorManager::GetInstance().GenWorkStreamIdBySessionGraph(sessionId_, graph_id);
  439. std::vector<Tensor> graph_inputs = inputs;
  440. // call RunGraph
  441. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  442. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  443. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "Session RunGraph failed");
  444. return FAILED;
  445. }
  446. GELOGT(TRACE_RUNNING, "Running Graph");
  447. Status ret = instance_ptr->SessionManagerObj().RunGraph(sessionId_, graph_id, graph_inputs, outputs);
  448. // check return status
  449. if (ret != SUCCESS) {
  450. GELOGE(ret, "Session RunGraph failed");
  451. return FAILED;
  452. }
  453. // print output
  454. if (outputs.size() > 0) {
  455. PrintOutputResult(outputs);
  456. }
  457. // return
  458. GELOGT(TRACE_STOP, "Session RunGraph finished");
  459. return ret;
  460. }
  461. Status Session::RegisterCallBackFunc(const std::string &key, const pCallBackFunc &callback) {
  462. ErrorManager::GetInstance().GenWorkStreamIdDefault();
  463. return ge::GELib::GetInstance()->SessionManagerObj().RegisterCallBackFunc(sessionId_, key, callback);
  464. }
  465. Status Session::RegisterCallBackFunc(const char *key, const session::pCallBackFunc &callback) {
  466. ErrorManager::GetInstance().GenWorkStreamIdDefault();
  467. std::string str_key;
  468. if (key != nullptr) {
  469. str_key = key;
  470. }
  471. return ge::GELib::GetInstance()->SessionManagerObj().RegisterCallBackFunc(sessionId_, str_key, callback);
  472. }
  473. Status Session::BuildGraph(uint32_t graph_id, const std::vector<InputTensorInfo> &inputs) {
  474. ErrorManager::GetInstance().GenWorkStreamIdBySessionGraph(sessionId_, graph_id);
  475. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  476. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  477. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "SessionConstructor failed");
  478. return FAILED;
  479. }
  480. GELOGT(TRACE_RUNNING, "Building Graph");
  481. Status ret = instance_ptr->SessionManagerObj().BuildGraph(sessionId_, graph_id, inputs);
  482. if (ret != SUCCESS) {
  483. GELOGE(ret, "Session BuildGraph failed");
  484. return FAILED;
  485. }
  486. return SUCCESS;
  487. }
  488. Status Session::RunGraphAsync(uint32_t graph_id, const std::vector<InputTensorInfo> &inputs,
  489. RunAsyncCallback callback) {
  490. ErrorManager::GetInstance().GenWorkStreamIdBySessionGraph(sessionId_, graph_id);
  491. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  492. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  493. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "SessionConstructor failed");
  494. return FAILED;
  495. }
  496. GELOGT(TRACE_RUNNING, "Run Graph Asynchronously");
  497. GELOGW(
  498. "The callback function will not be checked. Please ensure that the implementation of the function is trusted.");
  499. Status ret = ge::GELib::GetInstance()->SessionManagerObj().RunGraphAsync(sessionId_, graph_id, inputs, callback);
  500. if (ret != SUCCESS) {
  501. GELOGE(ret, "SessionManager RunGraphAsync failed");
  502. return FAILED;
  503. }
  504. return SUCCESS;
  505. }
  506. Status Session::GetVariables(const std::vector<std::string> &var_names, std::vector<Tensor> &var_values) {
  507. ErrorManager::GetInstance().GenWorkStreamIdDefault();
  508. auto instance_ptr = ge::GELib::GetInstance();
  509. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  510. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "SessionConstructor failed");
  511. return FAILED;
  512. }
  513. GELOGT(TRACE_RUNNING, "Get Variables");
  514. Status ret = ge::GELib::GetInstance()->SessionManagerObj().GetVariables(sessionId_, var_names, var_values);
  515. if (ret != SUCCESS) {
  516. GELOGE(ret, "SessionManager RunGraphAsync failed");
  517. return FAILED;
  518. }
  519. return SUCCESS;
  520. }
  521. Status Session::GetVariables(const std::vector<AscendString> &var_names, std::vector<Tensor> &var_values) {
  522. ErrorManager::GetInstance().GenWorkStreamIdDefault();
  523. auto instance_ptr = ge::GELib::GetInstance();
  524. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  525. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "SessionConstructor failed");
  526. return FAILED;
  527. }
  528. GELOGT(TRACE_RUNNING, "Get Variables");
  529. std::vector<ge::string> str_var_names;
  530. for (auto &var_name : var_names) {
  531. if (var_name.GetString() == nullptr) {
  532. GELOGE(FAILED, "GetVariables name is nullptr.");
  533. return FAILED;
  534. }
  535. str_var_names.emplace_back(var_name.GetString());
  536. }
  537. Status ret = ge::GELib::GetInstance()->SessionManagerObj().GetVariables(sessionId_, str_var_names, var_values);
  538. if (ret != SUCCESS) {
  539. GELOGE(ret, "SessionManager RunGraphAsync failed");
  540. return FAILED;
  541. }
  542. return SUCCESS;
  543. }
  544. bool Session::IsGraphNeedRebuild(uint32_t graph_id) {
  545. return ge::GELib::GetInstance()->SessionManagerObj().IsGraphNeedRebuild(sessionId_, graph_id);
  546. }
  547. } // namespace ge

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