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

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

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