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

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