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.

model_executor.cc 22 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /**
  2. * Copyright 2021 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 "graph/execute/model_executor.h"
  17. #include "graph/ge_context.h"
  18. #include "graph/debug/ge_attr_define.h"
  19. #include "graph/common/ge_call_wrapper.h"
  20. #include "graph/common/local_context.h"
  21. #include "graph/manager/graph_var_manager.h"
  22. #include "graph/utils/tensor_adapter.h"
  23. #include "graph/load/graph_loader.h"
  24. #include "graph/load/model_manager/model_manager.h"
  25. #include "common/math/math_util.h"
  26. #include "common/formats/utils/formats_trans_utils.h"
  27. namespace {
  28. constexpr int32_t kBase = 10;
  29. constexpr uint8_t kNeverLoaded = 0;
  30. }
  31. namespace ge {
  32. ///
  33. /// @ingroup ge
  34. /// @brief graph executor init
  35. /// @param [in] options user config params
  36. /// @return Status result of function
  37. ///
  38. Status ModelExecutor::Initialize(const map<string, string> &options, uint64_t session_id) {
  39. graph_run_listener_ = MakeShared<GraphModelListener>(sync_run_mutex_, condition_);
  40. if (graph_run_listener_ == nullptr) {
  41. REPORT_CALL_ERROR("E19999", "New GraphModelListener fail");
  42. GELOGE(MEMALLOC_FAILED, "[New][GraphModelListener] failed");
  43. return MEMALLOC_FAILED;
  44. }
  45. session_id_ = session_id;
  46. train_graph_flag_ = ParseTrainGraphFlag();
  47. thread_run_flag_.store(true);
  48. run_thread_ = std::thread(&ModelExecutor::RunThread, this);
  49. init_flag_ = true;
  50. return SUCCESS;
  51. }
  52. ///
  53. /// @ingroup ge
  54. /// @brief graph executor finalize
  55. /// @return Status result of function
  56. ///
  57. Status ModelExecutor::Finalize() {
  58. if (!init_flag_) {
  59. GELOGW("ModelExecutor has not been initialized.");
  60. return SUCCESS;
  61. }
  62. StopQueue();
  63. if (run_thread_.joinable()) {
  64. run_thread_.join();
  65. }
  66. if (graph_executor_.FreeExecuteMemory() != SUCCESS) {
  67. GELOGW("Graph executor FreeExecuteMemory failed, resources may not be released correctly.");
  68. }
  69. ModelManager::GetInstance()->DestroyAicpuSession(session_id_);
  70. return SUCCESS;
  71. }
  72. // OPTION_GRAPH_RUN_MODE is supposed to be a session-level option, but it used to be set to global-level in the past.
  73. // If can not parse from session, it can parse from global by GetContext().
  74. bool ModelExecutor::ParseTrainGraphFlag() {
  75. string run_mode;
  76. if (GetContext().GetOption(OPTION_GRAPH_RUN_MODE, run_mode) == SUCCESS && !run_mode.empty()) {
  77. if (GraphRunMode(std::strtol(run_mode.c_str(), nullptr, kBase)) >= TRAIN) {
  78. GELOGI("Graph train flag set.");
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. void ModelExecutor::AddGraphNode(GraphId graph_id, const GraphNodePtr &graph_node) {
  85. std::lock_guard<std::mutex> lock(mutex_);
  86. graph_nodes_.emplace(graph_id, graph_node);
  87. }
  88. void ModelExecutor::RemoveGraphNode(GraphId graph_id) {
  89. std::lock_guard<std::mutex> lock(mutex_);
  90. graph_nodes_.erase(graph_id);
  91. }
  92. ///
  93. /// @ingroup ge
  94. /// @brief Load mode for graph.
  95. /// @param [in] GeRootModel: root model of graph compiled.
  96. /// @param [in] GraphNode: node of graph.
  97. /// @return Status result of function
  98. ///
  99. Status ModelExecutor::LoadGraph(const GeRootModelPtr &ge_root_model, const GraphNodePtr &graph_node) {
  100. GE_CHECK_NOTNULL(graph_node);
  101. if (ge_root_model == nullptr) {
  102. return SUCCESS;
  103. }
  104. UpdateLocalOmeContext(graph_node);
  105. return graph_node->IsAsync() ? ModelLoadAsync(ge_root_model, graph_node) : ModelLoadSync(ge_root_model, graph_node);
  106. }
  107. ///
  108. /// @ingroup ge
  109. /// @brief Unload mode for graph.
  110. /// @param [in] GeRootModel: root model of graph compiled.
  111. /// @param [in] graph_id: graph identifier.
  112. /// @return Status result of function
  113. ///
  114. Status ModelExecutor::UnloadGraph(const GeRootModelPtr &ge_root_model, uint32_t graph_id) {
  115. GE_CHECK_NOTNULL(ge_root_model);
  116. rtError_t rt_ret = rtSetDevice(GetContext().DeviceId());
  117. if (rt_ret != RT_ERROR_NONE) {
  118. GELOGW("[GraphExecutor] rtSetDevice failed, modelId=%u, graphId=%u.", ge_root_model->GetModelId(), graph_id);
  119. return FAILED;
  120. }
  121. RemoveGraphNode(graph_id);
  122. Status ret = UnloadModel(ge_root_model, graph_id);
  123. if (ret != SUCCESS) {
  124. GELOGW("[GraphExecutor] unload model failed, graph_id=%u.", graph_id);
  125. }
  126. rt_ret = rtDeviceReset(GetContext().DeviceId());
  127. if (rt_ret != RT_ERROR_NONE) {
  128. GELOGW("[GraphExecutor] rtDeviceReset failed, graphId=%u.", graph_id);
  129. }
  130. return ret;
  131. }
  132. Status ModelExecutor::UnloadModel(const GeRootModelPtr &ge_root_model, uint32_t graph_id) {
  133. GE_CHECK_NOTNULL(ge_root_model);
  134. for (size_t i = 0; i < ge_root_model->GetAllModelId().size(); ++i) {
  135. uint32_t model_id = ge_root_model->GetAllModelId()[i];
  136. GELOGI("Unload model %u.", model_id);
  137. Status ret = GraphLoader::UnloadModel(model_id);
  138. if (ret != SUCCESS) {
  139. GELOGE(ret, "[GraphExecutor] unload model failed, modelId=%u, graphId=%u.", model_id, graph_id);
  140. return ret;
  141. }
  142. }
  143. return SUCCESS;
  144. }
  145. void ModelExecutor::StopQueue() {
  146. thread_run_flag_.store(false);
  147. run_args_q_.Stop();
  148. }
  149. void ModelExecutor::ReturnError(RunAsyncCallback callback, Status ret, const string &log) {
  150. StopQueue();
  151. GELOGE(ret, "%s.", log.c_str());
  152. std::vector<ge::Tensor> outputs;
  153. if (callback != nullptr) {
  154. callback(ret, outputs);
  155. }
  156. }
  157. void ModelExecutor::UpdateLocalOmeContext(const GraphNodePtr &graph_node) {
  158. std::lock_guard<std::mutex> lock(mutex_);
  159. SetLocalOmeContext(graph_node->GetOmeContext());
  160. }
  161. ///
  162. /// @ingroup ge
  163. /// @brief Push model execution params to queue.
  164. /// @param [in] RunArgs of for model execution.
  165. /// @return Status result of function
  166. ///
  167. Status ModelExecutor::PushGraph(const RunArgs &args) {
  168. return run_args_q_.Push(args) ? SUCCESS : FAILED;
  169. }
  170. void ModelExecutor::RunThread() {
  171. ErrorManager::GetInstance().SetStage(error_message::kModelExecute, error_message::kModelExecute);
  172. if (mmSetCurrentThreadName("GE_Run") != EN_OK) {
  173. GELOGW("Set thread name failed.");
  174. }
  175. RunArgs args;
  176. while (thread_run_flag_) {
  177. if (!run_args_q_.Pop(args)) {
  178. continue;
  179. }
  180. GELOGI("[RunThread] A new loop start, graph_id:%u.", args.graph_id);
  181. ErrorManager::GetInstance().SetErrorContext(args.error_context);
  182. GetContext().SetSessionId(args.session_id);
  183. GetThreadLocalContext() = args.context;
  184. UpdateLocalOmeContext(args.graph_node);
  185. // parse inputs.dims to vector<vector<uint64_t>> dynamic_dims
  186. Status ret = ParseInputsDims(args.input_tensor);
  187. if (ret != SUCCESS) {
  188. ReturnError(args.callback, ret, "ParseInputsDims failed, thread exit.");
  189. args.graph_node->Unlock();
  190. return;
  191. }
  192. args.graph_node->UpdateLoadFlag();
  193. if (!args.graph_node->GetLoadFlag()) {
  194. ErrorManager::GetInstance().SetStage(error_message::kModelLoad, error_message::kModelLoad);
  195. args.ge_root_model->SetTrainFlag(train_graph_flag_);
  196. ret = ModelLoadAsync(args.ge_root_model, args.graph_node);
  197. if (ret != SUCCESS || args.ge_root_model == nullptr) {
  198. StopQueue();
  199. ReturnError(args.callback, ret, "LoadGraphAsync failed, thread exit.");
  200. args.graph_node->Unlock();
  201. return;
  202. }
  203. // control the times of graph loading in multi-thread scenario
  204. args.graph_node->DecreaseLoadCount();
  205. args.graph_node->IncreaseLoadRecord();
  206. args.graph_node->SetLoadFlag(true);
  207. GELOGI("LoadGraph[%u], model[%u] success and set LoadFlag to true.", args.graph_node->GetGraphId(),
  208. args.ge_root_model->GetModelId());
  209. }
  210. ErrorManager::GetInstance().SetStage(error_message::kModelExecute, error_message::kModelExecute);
  211. if (train_graph_flag_) {
  212. graph_executor_.SetTrainFlag(train_graph_flag_);
  213. }
  214. ret = graph_executor_.ExecuteGraphAsync(args.graph_id, args.graph_node->GetGeRootModel(),
  215. args.input_tensor, args.callback);
  216. args.graph_node->SetRunFlag(false);
  217. if (ret != SUCCESS) {
  218. ReturnError(args.callback, ret, "ExecuteGraphAsync failed, thread exit.");
  219. args.graph_node->Unlock();
  220. return;
  221. }
  222. args.graph_node->Unlock();
  223. GELOGI("[GraphExecutor] Run graph async success, graph_id=%u.", args.graph_id);
  224. }
  225. }
  226. ///
  227. /// @ingroup ge
  228. /// @brief Run graph for synchronize model.
  229. /// @param [in] graph_node: node of graph.
  230. /// @param [in] graph_id: graph identifier.
  231. /// @param [in] inputs: input data for the graph running.
  232. /// @param [out] outputs: output data of the graph running
  233. /// @return Status result of function
  234. ///
  235. Status ModelExecutor::RunGraph(const GraphNodePtr &graph_node, GraphId graph_id,
  236. const std::vector<GeTensor> &inputs, std::vector<GeTensor> &outputs) {
  237. Status ret = graph_executor_.SetCondition(&sync_run_mutex_, &condition_, graph_run_listener_);
  238. if (ret != SUCCESS) {
  239. GELOGE(GE_GRAPH_RUNGRAPH_FAILED, "[Set][Condition] failed, graph_id = %u.", graph_id);
  240. graph_node->SetRunFlag(false);
  241. return GE_GRAPH_RUNGRAPH_FAILED;
  242. }
  243. if (train_graph_flag_) {
  244. graph_executor_.SetTrainFlag(train_graph_flag_);
  245. }
  246. ret = graph_executor_.ExecuteGraph(graph_id, graph_node->GetGeRootModel(), inputs, outputs);
  247. graph_node->SetRunFlag(false);
  248. if (ret != SUCCESS) {
  249. GELOGE(ret, "[Execute][Graph] failed, graph_id = %u.", graph_id);
  250. return ret;
  251. }
  252. return SUCCESS;
  253. }
  254. ///
  255. /// @ingroup ge
  256. /// @brief Run graph for NN synchronize model.
  257. /// @param [in] graph_node: node of graph.
  258. /// @param [in] graph_id: graph identifier.
  259. /// @param [in] stream: Stream for model running.
  260. /// @param [in] inputs: input data for the graph running.
  261. /// @param [out] outputs: output data of the graph running
  262. /// @return Status result of function
  263. ///
  264. Status ModelExecutor::RunGraphWithStream(const GraphNodePtr &graph_node, GraphId graph_id, rtStream_t stream,
  265. const std::vector<GeTensor> &inputs, std::vector<GeTensor> &outputs) {
  266. auto ret = graph_executor_.SetCondition(&sync_run_mutex_, &condition_, graph_run_listener_);
  267. if (ret != SUCCESS) {
  268. GELOGE(GE_GRAPH_RUNGRAPH_FAILED, "[Set][Condition] failed, graph id = %u, stream = %p.", graph_id, stream);
  269. graph_node->SetRunFlag(false);
  270. return GE_GRAPH_RUNGRAPH_FAILED;
  271. }
  272. ret = graph_executor_.ExecuteGraphWithStream(graph_id, stream, graph_node->GetGeRootModel(), inputs, outputs);
  273. graph_node->SetRunFlag(false);
  274. graph_node->SetIsSpecificStream(false);
  275. if (ret != SUCCESS) {
  276. GELOGE(ret, "[Execute][Graph] With Stream failed, graph id = %u, stream = %p.", graph_id, stream);
  277. return ret;
  278. }
  279. GELOGI("[Run][GraphWithStreamAsync] run graph success, graph id = %u, stream = %p.", graph_id, stream);
  280. return SUCCESS;
  281. }
  282. Status ModelExecutor::ModelLoadSync(const GeRootModelPtr &ge_root_model, const GraphNodePtr &graph_node) {
  283. ge_root_model->SetIsSpecificStream(graph_node->IsSpecificStream());
  284. return ModelLoad(ge_root_model, graph_node, graph_run_listener_);
  285. }
  286. Status ModelExecutor::ModelLoadAsync(const GeRootModelPtr &ge_root_model, const GraphNodePtr &graph_node) {
  287. auto listener = MakeShared<RunAsyncListener>();
  288. GE_CHECK_NOTNULL(listener);
  289. return ModelLoad(ge_root_model, graph_node, listener);
  290. }
  291. Status ModelExecutor::ModelLoad(const GeRootModelPtr &ge_root_model, const GraphNodePtr &graph_node,
  292. const std::shared_ptr<ModelListener> &listener) {
  293. ge_root_model->SetTrainFlag(train_graph_flag_);
  294. bool is_unknown_shape = false;
  295. GE_CHK_STATUS_RET(ge_root_model->CheckIsUnknownShape(is_unknown_shape));
  296. if (!is_unknown_shape) {
  297. if (getenv(kEnvGeuseStaticMemory) != nullptr) {
  298. GELOGI("[LoadGraph] GE_USE_STATIC_MEMORY is seted.");
  299. } else {
  300. auto root_graph = ge_root_model->GetRootGraph();
  301. GE_CHECK_NOTNULL(root_graph);
  302. auto name_to_model = ge_root_model->GetSubgraphInstanceNameToModel();
  303. GeModelPtr ge_model = name_to_model[root_graph->GetName()];
  304. GE_CHK_STATUS_RET(CheckAndReleaseMemory(ge_model, graph_node));
  305. }
  306. }
  307. GE_TIMESTAMP_START(LoadModelOnline);
  308. uint32_t model_id = INVALID_MODEL_ID;
  309. Status ret = GraphLoader::LoadModelOnline(model_id, ge_root_model, listener);
  310. GE_TIMESTAMP_EVENT_END(LoadModelOnline, "GraphLoader::LoadModelOnline");
  311. if (ret != SUCCESS) {
  312. GELOGE(ret, "[Load][ModelOnline] Failed, model_id:%u", model_id);
  313. graph_node->SetRunFlag(false);
  314. return ret;
  315. }
  316. graph_node->SetLoadFlag(true);
  317. ge_root_model->SetModelId(model_id);
  318. graph_node->SetGeRootModel(ge_root_model);
  319. AddGraphNode(graph_node->GetGraphId(), graph_node);
  320. return SUCCESS;
  321. }
  322. void ModelExecutor::ReleaseMemory(const GeModelPtr &ge_model, const GraphNodePtr &graph_node,
  323. const std::vector<uint32_t> &model_ids, uint32_t graph_id, uint64_t session_id) {
  324. rtError_t rt_ret = rtSetDevice(GetContext().DeviceId());
  325. if (rt_ret != RT_ERROR_NONE) {
  326. REPORT_CALL_ERROR("E19999", "Call rtSetDevice failed, device_id:%u", GetContext().DeviceId());
  327. GELOGE(RT_FAILED, "[Call][RtSetDevice] failed, device_id=%u.", GetContext().DeviceId());
  328. return;
  329. }
  330. for (auto model_id : model_ids) {
  331. uint64_t max_memory_size = 0;
  332. Status result = GraphLoader::GetMaxUsedMemory(model_id, max_memory_size);
  333. if (result != SUCCESS) {
  334. continue;
  335. }
  336. GELOGI("try to UnloadGraph[%u], model[%u] which MaxUsedMemory[%lu].", graph_id, model_id, max_memory_size);
  337. if (model_ids.size() > 1) {
  338. result = ge_model->GetSessionId(model_id, session_id);
  339. if (result != SUCCESS) {
  340. GELOGW("[GraphExecutor:] get session failed when dynamic memory, modelId=%u, graphId=%u.", model_id,
  341. graph_id);
  342. continue;
  343. }
  344. }
  345. result = GraphLoader::DestroyAicpuKernel(session_id, model_id, 0);
  346. if (result != SUCCESS) {
  347. GELOGW("[GraphExecutor:] destroy aicpu kernel failed when dynamic memory, modelId=%u, graphId=%u.", model_id,
  348. graph_id);
  349. }
  350. result = GraphLoader::UnloadModel(model_id);
  351. if (result != SUCCESS) {
  352. GELOGW("[GraphExecutor:] unload model failed, modelId=%u, graphId=%u.", model_id, graph_id);
  353. }
  354. GELOGI("UnloadGraph[%u], model[%u] success.", graph_id, model_id);
  355. }
  356. graph_node->SetLoadFlag(false);
  357. // Allow model to be loaded agagin without adding graph again
  358. graph_node->SetLoadCount(graph_node->GetLoadRecord());
  359. graph_node->SetLoadRecord(kNeverLoaded);
  360. GeRootModelPtr ge_root_model = graph_node->GetGeRootModel();
  361. if (ge_root_model == nullptr) {
  362. GELOGW("ge_root_model is null, graph_id:%u", graph_id);
  363. return;
  364. }
  365. ge_root_model->ClearAllModelId();
  366. rt_ret = rtDeviceReset(GetContext().DeviceId());
  367. if (rt_ret != RT_ERROR_NONE) {
  368. REPORT_CALL_ERROR("E19999", "Call rtDeviceReset failed, device_id:%u", GetContext().DeviceId());
  369. GELOGE(RT_FAILED, "[Call][RtDeviceReset] failed, device_id:%u.", GetContext().DeviceId());
  370. return;
  371. }
  372. }
  373. Status ModelExecutor::CheckAndReleaseMemory(const GeModelPtr &ge_model, const GraphNodePtr &graph_node) {
  374. GELOGI("graph_id[%u]", graph_node->GetGraphId());
  375. int64_t free_memory = 0;
  376. Status result = GraphLoader::GetMemoryInfo(free_memory);
  377. if (result != SUCCESS) {
  378. return result;
  379. }
  380. int64_t value = 0;
  381. int64_t memory_size = AttrUtils::GetInt(ge_model, ATTR_MODEL_MEMORY_SIZE, value) ? value : 0;
  382. int64_t weight_size = AttrUtils::GetInt(ge_model, ATTR_MODEL_WEIGHT_SIZE, value) ? value : 0;
  383. int64_t session_id = AttrUtils::GetInt(ge_model, MODEL_ATTR_SESSION_ID, value) ? value : 0;
  384. GELOGI("Graph[%u] need memory_size[%ld], weight_size[%ld], Device[%u] free_memory_size[%ld]",
  385. graph_node->GetGraphId(), memory_size, weight_size, GetContext().DeviceId(), free_memory);
  386. if (CheckInt64AddOverflow(memory_size, weight_size) != SUCCESS) {
  387. REPORT_INNER_ERROR("E19999", "memory_size:%ld and weight_size:%ld will overflow after add, check invalid",
  388. memory_size, weight_size);
  389. GELOGE(INTERNAL_ERROR, "[Check][Param] memory_size:%ld and weight_size:%ld will overflow after add",
  390. memory_size, weight_size);
  391. return INTERNAL_ERROR;
  392. }
  393. if (free_memory >= (memory_size + weight_size)) {
  394. return SUCCESS;
  395. }
  396. std::lock_guard<std::mutex> lock(mutex_);
  397. for (const auto &it : graph_nodes_) {
  398. auto graph_id = it.second->GetGraphId();
  399. auto model = it.second->GetGeRootModel();
  400. if (model == nullptr) {
  401. continue;
  402. }
  403. auto model_id = model->GetModelId();
  404. auto model_ids = model->GetAllModelId();
  405. // unload model not release
  406. bool is_unknown_shape = false;
  407. GE_CHK_STATUS_RET(model->CheckIsUnknownShape(is_unknown_shape));
  408. if (is_unknown_shape) {
  409. GELOGD("model_id[%u] graph_id[%u] is unknown model, not release memory", model_id, graph_id);
  410. continue;
  411. }
  412. // not loaded,no need unload
  413. if (!it.second->GetLoadFlag()) {
  414. GELOGI("CheckAndReleaseMemory graph[%u] has not been loaded.", graph_id);
  415. continue;
  416. }
  417. ReleaseMemory(ge_model, it.second, model_ids, graph_id, static_cast<uint64_t>(session_id));
  418. }
  419. return SUCCESS;
  420. }
  421. void ModelExecutor::ParseInputsDimsForData(const std::vector<ge::Tensor> &input_tensor) {
  422. GELOGD("Start parse input dims from data.");
  423. for (size_t i = 0; i < input_tensor.size(); ++i) {
  424. const TensorDesc &tensor_desc = input_tensor[i].GetTensorDesc();
  425. const Shape &shape = tensor_desc.GetShape();
  426. const auto &shape_dims = shape.GetDims();
  427. GELOGD("Input tensor dims is %s.", formats::JoinToString(shape_dims).c_str());
  428. GetLocalOmeContext().user_real_input_dims.emplace_back(shape_dims);
  429. }
  430. }
  431. Status ModelExecutor::ParseInputsDimsForGetNextNoSinkAndData(const vector<NodePtr> &dynamic_nodes,
  432. const std::vector<ge::Tensor> &input_tensor) {
  433. GELOGD("Start parse inputs dims when coexist data and getnext sink.");
  434. for (size_t i = 0; i < dynamic_nodes.size(); ++i) {
  435. auto op_desc = dynamic_nodes.at(i)->GetOpDesc();
  436. if (op_desc == nullptr) {
  437. continue;
  438. }
  439. GeAttrValue::INT index = 0;
  440. if (!(AttrUtils::GetInt(op_desc, ATTR_NAME_INDEX, index))) {
  441. REPORT_CALL_ERROR("E19999", "Get Attr:%s from op:%s(%s) fail", ATTR_NAME_INDEX.c_str(),
  442. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  443. GELOGE(PARAM_INVALID, "[Get][Attr] %s from op:%s(%s) fail", ATTR_NAME_INDEX.c_str(),
  444. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  445. return PARAM_INVALID;
  446. }
  447. if (static_cast<size_t>(index) > input_tensor.size()) {
  448. REPORT_INNER_ERROR("E19999", "Attr:%s in op:%s(%s) value:%ld > param input_tensor.size:%zu, "
  449. "check invalid", ATTR_NAME_INDEX.c_str(),
  450. op_desc->GetName().c_str(), op_desc->GetType().c_str(),
  451. index, input_tensor.size());
  452. GELOGE(PARAM_INVALID, "[Check][Param] Attr:%s in op:%s(%s) value:%ld > param input_tensor.size:%zu",
  453. ATTR_NAME_INDEX.c_str(), op_desc->GetName().c_str(), op_desc->GetType().c_str(),
  454. index, input_tensor.size());
  455. return PARAM_INVALID;
  456. }
  457. const TensorDesc &tensor_desc = input_tensor[i].GetTensorDesc();
  458. const Shape &shape = tensor_desc.GetShape();
  459. const auto &shape_dims = shape.GetDims();
  460. GELOGI("Shape dims of %zu data is %s.", index, formats::JoinToString(shape_dims).c_str());
  461. GetLocalOmeContext().user_real_input_dims.emplace_back(std::move(shape_dims));
  462. }
  463. return SUCCESS;
  464. }
  465. Status ModelExecutor::ParseInputsDims(const std::vector<ge::Tensor> &input_tensor) {
  466. GELOGI("Start parse input dims of %zu input tensor.", input_tensor.size());
  467. GetLocalOmeContext().user_real_input_dims.clear();
  468. if (GetLocalOmeContext().dynamic_node_type.empty()) {
  469. return SUCCESS;
  470. }
  471. const vector<NodePtr> &data_nodes = GetLocalOmeContext().data_nodes;
  472. const vector<NodePtr> &getnext_nosink_nodes = GetLocalOmeContext().getnext_nosink_nodes;
  473. GELOGD("Data nodes count is %zu, getnext nosink nodes count is %zu.", data_nodes.size(),
  474. getnext_nosink_nodes.size());
  475. if (GetLocalOmeContext().dynamic_node_type == DATA) {
  476. if (getnext_nosink_nodes.empty()) {
  477. // just data or data+getnext_sink
  478. ParseInputsDimsForData(input_tensor);
  479. } else {
  480. // data+getnext_nosink, but only need to get shape_dims of data
  481. if (ParseInputsDimsForGetNextNoSinkAndData(data_nodes, input_tensor) != SUCCESS) {
  482. GELOGE(PARAM_INVALID, "[Parse][Dims] from data failed, when data coexist with getnext nosink.");
  483. return PARAM_INVALID;
  484. }
  485. }
  486. } else {
  487. if (getnext_nosink_nodes.empty()) {
  488. // just getnext_sink or getnext_sink+data, need to get shape_dims from aicpu op
  489. GELOGI("Need to get dims from aicpu op: GETDYNAMICDIMS.");
  490. return SUCCESS;
  491. } else {
  492. if (data_nodes.empty()) {
  493. // just getnext_nosink
  494. ParseInputsDimsForData(input_tensor);
  495. } else {
  496. // getnext_nosink + data, but only need to get shape_dims of getnext_nosink
  497. if (ParseInputsDimsForGetNextNoSinkAndData(getnext_nosink_nodes, input_tensor) != SUCCESS) {
  498. GELOGE(PARAM_INVALID, "[Parse][Dims] from getnext nosink failed, when data coexist with getnext nosink");
  499. return PARAM_INVALID;
  500. }
  501. }
  502. }
  503. }
  504. GELOGI("Parse %zu inputs dims success.", GetLocalOmeContext().user_real_input_dims.size());
  505. return SUCCESS;
  506. }
  507. } // namespace ge

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