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

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

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