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

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