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

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