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

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