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.

graph_execute.cc 32 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 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
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
4 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
4 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
4 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
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
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. /**
  2. * Copyright 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 "graph/execute/graph_execute.h"
  17. #include <memory>
  18. #include <string>
  19. #include "graph/load/model_manager/model_manager.h"
  20. #include "graph/load/model_manager/davinci_model.h"
  21. #include "common/profiling/profiling_manager.h"
  22. namespace ge {
  23. using Uint32Pair = pair<uint32_t, uint32_t>;
  24. const uint32_t kInvalidModelId = UINT32_MAX;
  25. GraphExecutor::GraphExecutor()
  26. : init_flag_(false),
  27. train_graph_flag_(false),
  28. sync_run_mutex_(nullptr),
  29. condition_(nullptr),
  30. graph_run_listener_(nullptr),
  31. last_graph_id_(UINT32_MAX),
  32. malloc_flag_(false) {}
  33. GraphExecutor::~GraphExecutor() {
  34. outputs_desc_.clear();
  35. if (malloc_flag_) {
  36. for (auto &buffer_addr : buffer_addr_) {
  37. rtError_t rt_ret;
  38. rt_ret = rtFreeHost(buffer_addr);
  39. if (rt_ret != RT_ERROR_NONE) {
  40. REPORT_CALL_ERROR("E19999", "Call rtFreeHost failed, ret:0x%X", rt_ret);
  41. GELOGE(RT_FAILED, "[Call][RtFreeHost] subgraph free buffer failed, ret: 0x%X", rt_ret);
  42. }
  43. }
  44. }
  45. malloc_flag_ = false;
  46. buffer_addr_.clear();
  47. }
  48. Status GraphExecutor::SetCondition(std::mutex *mutex, std::condition_variable *cond,
  49. std::shared_ptr<GraphModelListener> listener) {
  50. if (mutex == nullptr) {
  51. REPORT_INNER_ERROR("E19999", "Check param mutex nullptr");
  52. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Check][Param] input param mutex is nullptr.");
  53. return GE_GRAPH_PARAM_NULLPTR;
  54. }
  55. if (cond == nullptr) {
  56. REPORT_INNER_ERROR("E19999", "Check param cond nullptr");
  57. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Check][Param] input param cond is nullptr.");
  58. return GE_GRAPH_PARAM_NULLPTR;
  59. }
  60. if (listener == nullptr) {
  61. REPORT_INNER_ERROR("E19999", "Check param listener nullptr");
  62. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Check][Param] input param listener is nullptr.");
  63. return GE_GRAPH_PARAM_NULLPTR;
  64. }
  65. sync_run_mutex_ = mutex;
  66. condition_ = cond;
  67. graph_run_listener_ = listener;
  68. init_flag_ = true;
  69. return SUCCESS;
  70. }
  71. Status GraphExecutor::SetDynamicSize(uint32_t model_id, const std::vector<uint64_t> &batch_num, int32_t dynamic_type) {
  72. auto model_manager = ge::ModelManager::GetInstance();
  73. GE_CHECK_NOTNULL(model_manager);
  74. Status ret = model_manager->SetDynamicSize(model_id, batch_num, dynamic_type);
  75. if (ret != SUCCESS) {
  76. GELOGE(ret, "[Set][DynamicSize] failed, model_id:%u", model_id);
  77. return ret;
  78. }
  79. return SUCCESS;
  80. }
  81. void GraphExecutor::SetTrainFlag(bool is_train_graph) { train_graph_flag_ = is_train_graph; }
  82. Status GraphExecutor::FreeInOutBuffer() {
  83. if (malloc_flag_) {
  84. for (auto iter = buffer_addr_.begin(); iter != buffer_addr_.end(); ++iter) {
  85. rtError_t rt_ret;
  86. rt_ret = rtFreeHost(*iter);
  87. if (rt_ret != RT_ERROR_NONE) {
  88. REPORT_CALL_ERROR("E19999", "Call rtFreeHost failed, ret:0x%X", rt_ret);
  89. GELOGE(RT_FAILED, "[Call][RtFreeHost] subgraph free buffer failed, ret: 0x%X", rt_ret);
  90. (void)buffer_addr_.erase(buffer_addr_.begin(), iter);
  91. return GE_GRAPH_FREE_FAILED;
  92. }
  93. }
  94. buffer_addr_.clear();
  95. malloc_flag_ = false;
  96. return SUCCESS;
  97. } else {
  98. GELOGD("[GraphManager] not malloc buffer.");
  99. return SUCCESS;
  100. }
  101. }
  102. Status GraphExecutor::MallocInOutBuffer(const std::vector<uint64_t> &buffer_size, std::vector<void *> &data_addr) {
  103. if (malloc_flag_) {
  104. auto all_size_same = true;
  105. if (buffer_size.size() == buffer_size_.size()) {
  106. for (size_t i = 0; i < buffer_size.size(); i++) {
  107. if (buffer_size[i] != buffer_size_[i]) {
  108. all_size_same = false;
  109. break;
  110. }
  111. }
  112. } else {
  113. all_size_same = false;
  114. }
  115. if (all_size_same) {
  116. data_addr = buffer_addr_;
  117. return SUCCESS;
  118. }
  119. buffer_size_.clear();
  120. auto rt_ret = FreeInOutBuffer();
  121. if (rt_ret != SUCCESS) {
  122. GELOGE(RT_FAILED, "[Free][Buffer] failed, ret: 0x%X", rt_ret);
  123. return RT_FAILED;
  124. }
  125. }
  126. rtError_t rt_ret;
  127. for (size_t i = 0; i < buffer_size.size(); ++i) {
  128. void *tmp_buf = nullptr;
  129. rt_ret = rtMallocHost(&tmp_buf, buffer_size[i]);
  130. if (rt_ret != RT_ERROR_NONE) {
  131. REPORT_CALL_ERROR("E19999", "Call rtMallocHost failed, size:%lu, ret:0x%X", buffer_size[i], rt_ret);
  132. GELOGE(RT_FAILED, "[Malloc][Buffer] failed, size:%lu, ret:0x%X", buffer_size[i], rt_ret);
  133. return GE_GRAPH_MALLOC_FAILED;
  134. }
  135. malloc_flag_ = true;
  136. data_addr.push_back(tmp_buf);
  137. buffer_addr_.push_back(tmp_buf);
  138. }
  139. buffer_size_ = buffer_size;
  140. return SUCCESS;
  141. }
  142. Status GraphExecutor::PrepareInputData(const std::vector<GeTensor> &input_tensor, InputData &graph_input_data,
  143. OutputData &graph_output_data, std::vector<InputOutputDescInfo> &output_desc) {
  144. // Preprocessing input data
  145. graph_input_data.index = 0;
  146. graph_input_data.timeout = 0;
  147. graph_input_data.timestamp = 0;
  148. std::size_t inputSize = input_tensor.size();
  149. std::size_t output_size = output_desc.size();
  150. std::vector<uint64_t> bufferSizeVec;
  151. std::vector<void *> addrVec;
  152. for (std::size_t i = 0; i < inputSize; ++i) {
  153. const GeTensor *InTensor = &input_tensor[i];
  154. GE_CHECK_NOTNULL(InTensor);
  155. bufferSizeVec.push_back(InTensor->GetData().size());
  156. }
  157. for (const auto &desc : output_desc) {
  158. bufferSizeVec.push_back(desc.size);
  159. }
  160. Status ret = MallocInOutBuffer(bufferSizeVec, addrVec);
  161. if (ret != SUCCESS) {
  162. GELOGE(GE_GRAPH_MALLOC_FAILED, "[Malloc][Mem] failed");
  163. return GE_GRAPH_MALLOC_FAILED;
  164. }
  165. for (std::size_t i = 0; i < input_tensor.size() && i < addrVec.size(); ++i) {
  166. const GeTensor *in_tensor = &input_tensor[i];
  167. GE_CHECK_NOTNULL(in_tensor);
  168. if ((addrVec[i] != nullptr) && (in_tensor->GetData().data() != nullptr)) {
  169. rtError_t rt_ret = rtMemcpy(addrVec[i], bufferSizeVec[i], in_tensor->GetData().data(),
  170. in_tensor->GetData().size(), RT_MEMCPY_HOST_TO_HOST);
  171. if (rt_ret != RT_ERROR_NONE) {
  172. REPORT_CALL_ERROR("E19999", "Call rtMemcpy failed, dst_size:%lu, src_size:%zu, ret:0x%X",
  173. bufferSizeVec[i], in_tensor->GetData().size(), rt_ret);
  174. GELOGE(RT_FAILED, "[Call][RtMemcpy] failed, dst_size:%lu, src_size:%zu, ret:0x%X",
  175. bufferSizeVec[i], in_tensor->GetData().size(), rt_ret);
  176. return RT_FAILED;
  177. }
  178. }
  179. DataBuffer in_data_buf;
  180. in_data_buf.data = reinterpret_cast<uint8_t *>(addrVec[i]);
  181. in_data_buf.length = in_tensor->GetData().size();
  182. in_data_buf.isDataSupportMemShare = false;
  183. graph_input_data.blobs.push_back(in_data_buf);
  184. }
  185. graph_output_data.index = 0;
  186. for (std::size_t j = 0; j < output_size; j++) {
  187. auto desc = output_desc[j];
  188. uint64_t buffer_size = desc.size;
  189. DataBuffer out_data_buf;
  190. out_data_buf.data = reinterpret_cast<uint8_t *>(addrVec[inputSize + j]);
  191. out_data_buf.length = buffer_size;
  192. out_data_buf.isDataSupportMemShare = false;
  193. graph_output_data.blobs.push_back(out_data_buf);
  194. }
  195. return SUCCESS;
  196. }
  197. Status GraphExecutor::SyncExecuteModel(uint32_t model_id, const std::vector<GeTensor> &input_tensor,
  198. std::vector<GeTensor> &output_tensor) {
  199. auto model_manager = ge::ModelManager::GetInstance();
  200. GE_CHECK_NOTNULL(model_manager);
  201. if (model_manager->IsDynamicShape(model_id)) {
  202. GELOGI("[ExecuteGraph] GetInputOutputDescInfo via dynamic shape model executor, modelId=%u", model_id);
  203. return model_manager->SyncExecuteModel(model_id, input_tensor, output_tensor);
  204. }
  205. // Prepare input and output
  206. std::vector<InputOutputDescInfo> inputs_desc;
  207. std::vector<InputOutputDescInfo> output_desc;
  208. GELOGI("[ExecuteGraph] GetInputOutputDescInfo via new ome begin.");
  209. Status ret = GetInputOutputDescInfo(model_id, inputs_desc, output_desc);
  210. if (ret != SUCCESS) {
  211. GELOGE(GE_GRAPH_GET_IN_OUT_FAILED, "[Get][InputOutputDescInfo] failed, modelId=%u.", model_id);
  212. return GE_GRAPH_GET_IN_OUT_FAILED;
  213. }
  214. outputs_desc_.assign(output_desc.begin(), output_desc.end());
  215. InputData input_data;
  216. OutputData output_data;
  217. input_data.model_id = model_id;
  218. ret = PrepareInputData(input_tensor, input_data, output_data, output_desc);
  219. if (ret != SUCCESS) {
  220. GELOGE(GE_GRAPH_PREPARE_FAILED, "[Prepare][InputData] failed, modelId=%u.", model_id);
  221. return GE_GRAPH_PREPARE_FAILED;
  222. }
  223. if (graph_run_listener_->ResetResult() != SUCCESS) {
  224. REPORT_CALL_ERROR("E19999", "Call graph_run_listener_.ResetResult fail, model_id:%u", model_id);
  225. GELOGE(GE_GRAPH_EXECUTE_FAILED, "[Reset][Result] failed, model_id:%u", model_id);
  226. return GE_GRAPH_EXECUTE_FAILED;
  227. }
  228. // Run mode async
  229. GELOGI("[ExecuteGraph] DataInput via new ome begin.");
  230. ret = DataInput(input_data, output_data);
  231. if (ret != SUCCESS) {
  232. GELOGE(GE_GRAPH_DATA_INPUT_FAILED, "[Call][DataInput] push data failed, modelId=%u.", model_id);
  233. return GE_GRAPH_DATA_INPUT_FAILED;
  234. }
  235. GELOGI("[GraphExecutor] input data push to wrapper finish, waiting for result...");
  236. // Pending until async execute graph complete
  237. {
  238. std::unique_lock<std::mutex> ulock(*sync_run_mutex_);
  239. if (!graph_run_listener_->IsFinished()) {
  240. (*condition_).wait(ulock);
  241. }
  242. // Run graph return
  243. uint32_t result_code = graph_run_listener_->GetResultCode();
  244. if (result_code != SUCCESS && result_code != END_OF_SEQUENCE) {
  245. REPORT_CALL_ERROR("E19999", "Graph_run_listener_ run fail, result:%u, model_id:%u", result_code, model_id);
  246. GELOGE(GE_GRAPH_EXECUTE_FAILED, "[Execute][Model] failed, ret=%u, modelId=%u.", result_code, model_id);
  247. return GE_GRAPH_EXECUTE_FAILED;
  248. }
  249. }
  250. for (size_t i = 0; i < output_data.blobs.size(); ++i) {
  251. DataBuffer outputDataTmp = output_data.blobs[i];
  252. CHECK_FALSE_EXEC(outputDataTmp.length != 0,
  253. REPORT_INNER_ERROR("E19999", "Param output_data.length is 0 in model:%u, check invalid",
  254. model_id);
  255. GELOGE(GE_GRAPH_EXECUTE_FAILED, "[Check][Param] Failed to allocate memory, "
  256. "length is 0, model id:%u", model_id);
  257. return GE_GRAPH_EXECUTE_FAILED);
  258. std::unique_ptr<uint8_t> outBufTmp(new (std::nothrow) uint8_t[outputDataTmp.length]);
  259. if (outBufTmp == nullptr) {
  260. REPORT_CALL_ERROR("E19999", "New output buffer fail, length:%lu, model:%u", outputDataTmp.length, model_id);
  261. GELOGE(FAILED, "[Allocate][Memory] failed, length:%lu, model:%u", outputDataTmp.length, model_id);
  262. return FAILED;
  263. }
  264. GE_PRINT_DYNAMIC_MEMORY(new, "the output memory of data on training.", sizeof(uint8_t) * outputDataTmp.length)
  265. rtError_t ret_value = rtMemcpy(outBufTmp.get(), outputDataTmp.length, outputDataTmp.data, outputDataTmp.length,
  266. RT_MEMCPY_HOST_TO_HOST);
  267. CHECK_FALSE_EXEC(ret_value == RT_ERROR_NONE,
  268. REPORT_CALL_ERROR("E19999", "Call rtMemcpy failed, dst_size:%lu, src_size:%zu, ret:0x%X",
  269. outputDataTmp.length, outputDataTmp.length, ret_value);
  270. GELOGE(GE_GRAPH_EXECUTE_FAILED, "[Call][RtMemcpy] failed, dst_size:%lu, src_size:%zu, ret:0x%X",
  271. outputDataTmp.length, outputDataTmp.length, ret_value);
  272. return GE_GRAPH_EXECUTE_FAILED);
  273. GeTensor outTensor;
  274. std::vector<int64_t> shapeDims;
  275. for (const auto &dim : output_desc[i].shape_info.dims) {
  276. shapeDims.push_back(dim);
  277. }
  278. GeShape outShape(shapeDims);
  279. outTensor.MutableTensorDesc().SetShape(outShape);
  280. outTensor.MutableTensorDesc().SetDataType((DataType)output_desc[i].data_type);
  281. (void)outTensor.SetData(outBufTmp.get(), outputDataTmp.length);
  282. output_tensor.push_back(outTensor);
  283. }
  284. GELOGI("[GraphExecutor] execute model success, modelId=%u.", model_id);
  285. return SUCCESS;
  286. }
  287. void GraphExecutor::InitModelIdInfo(std::vector<uint32_t> &out_model_id_info,
  288. std::vector<SubGraphInfoPtr> &sub_graph_vec, uint32_t output_size) {
  289. for (uint32_t i = 0; i < output_size; i++) {
  290. for (size_t j = 0; j < sub_graph_vec.size(); j++) {
  291. if (sub_graph_vec[j]->GetOutputFlag().size() == output_size && sub_graph_vec[j]->GetOutputFlag().at(i)) {
  292. out_model_id_info.push_back(sub_graph_vec[j]->GetModelIdInfo().model_id);
  293. }
  294. }
  295. }
  296. }
  297. Status GraphExecutor::FreeExecuteMemory() {
  298. auto ret = FreeInOutBuffer();
  299. if (ret != SUCCESS) {
  300. GELOGE(ret, "[Free][InOutBuffer] Error!");
  301. return ret;
  302. }
  303. return SUCCESS;
  304. }
  305. Status GraphExecutor::ExecuteGraph(GraphId graph_id, const GeRootModelPtr &ge_root_model,
  306. const std::vector<GeTensor> &input_tensor, std::vector<GeTensor> &output_tensor) {
  307. if (graph_id != last_graph_id_) {
  308. auto ret = FreeExecuteMemory();
  309. if (ret != SUCCESS) {
  310. return ret;
  311. }
  312. }
  313. last_graph_id_ = graph_id;
  314. if (!init_flag_) {
  315. REPORT_INNER_ERROR("E19999", "No SetCondition called before, graph:%u, check invalid",
  316. graph_id);
  317. GELOGE(GE_GRAPH_EXECUTE_NOT_INIT, "[Check][Param] AI Core Engine without calling SetCondition! graph id:%u",
  318. graph_id);
  319. return GE_GRAPH_EXECUTE_NOT_INIT;
  320. }
  321. GE_CHECK_NOTNULL_EXEC(ge_root_model, return FAILED);
  322. Status ret = SyncExecuteModel(ge_root_model->GetModelId(), input_tensor, output_tensor);
  323. if (ret != SUCCESS) {
  324. GELOGE(GE_GRAPH_SYNC_MODEL_FAILED, "[SyncExecute][Model] Error! graph id:%u", graph_id);
  325. return GE_GRAPH_SYNC_MODEL_FAILED;
  326. }
  327. ret = ModelSubscribe(graph_id);
  328. if (ret != SUCCESS) {
  329. GELOGE(ret, "[Call][ModelSubscribe] failed, graph_id:%u", graph_id);
  330. return ret;
  331. }
  332. return SUCCESS;
  333. }
  334. Status GraphExecutor::ExecuteGraphAsync(GraphId graph_id, const GeRootModelPtr &ge_root_model,
  335. const std::vector<ge::Tensor> &input_tensor,
  336. const RunAsyncCallback& callback) {
  337. GELOGI("[GraphExecutor] Start to async execute graph, graph_id=%u", graph_id);
  338. if (graph_id != last_graph_id_) {
  339. auto ret = FreeExecuteMemory();
  340. if (ret != SUCCESS) {
  341. return ret;
  342. }
  343. }
  344. last_graph_id_ = graph_id;
  345. GE_CHECK_NOTNULL_EXEC(ge_root_model, return FAILED);
  346. Status ret = AsyncExecuteModel(ge_root_model, input_tensor, callback);
  347. if (ret != SUCCESS) {
  348. GELOGE(GE_GRAPH_SYNC_MODEL_FAILED, "[AsyncExecute][Model] Error! graph id:%u", graph_id);
  349. return GE_GRAPH_SYNC_MODEL_FAILED;
  350. }
  351. GELOGI("[GraphExecutor] Async execute graph success, graph_id=%u", graph_id);
  352. return SUCCESS;
  353. }
  354. Status GraphExecutor::GetExecuteData(const std::vector<GeTensor> &input_tensor, std::vector<DataBuffer> &blobs,
  355. std::vector<GeTensorDesc> &tensor_desc) {
  356. for (const auto &tensor : input_tensor) {
  357. DataBuffer in_data_buf;
  358. // check placement
  359. in_data_buf.data = const_cast<uint8_t *>(tensor.GetData().data());
  360. in_data_buf.length = tensor.GetData().size();
  361. in_data_buf.isDataSupportMemShare = false;
  362. blobs.emplace_back(in_data_buf);
  363. tensor_desc.emplace_back(tensor.GetTensorDesc());
  364. }
  365. return SUCCESS;
  366. }
  367. Status GraphExecutor::ExecuteGraphWithStream(GraphId graph_id,
  368. rtStream_t stream,
  369. const GeRootModelPtr &ge_root_model,
  370. const std::vector<GeTensor> &input_tensor,
  371. std::vector<GeTensor> &output_tensor) {
  372. GELOGI("[GraphExecutor] Start to execute graph with stream, graph id = %u, stream = %p.", graph_id, stream);
  373. if (!init_flag_) {
  374. REPORT_INNER_ERROR("E19999", "No SetCondition called before, graph id = %u, stream = %p, check invalid.",
  375. graph_id, stream);
  376. GELOGE(GE_GRAPH_EXECUTE_NOT_INIT, "[Check][Param] AI Core Engine without calling SetCondition! graph id = %u",
  377. graph_id);
  378. return GE_GRAPH_EXECUTE_NOT_INIT;
  379. }
  380. if (graph_id != last_graph_id_) {
  381. auto ret = FreeExecuteMemory();
  382. if (ret != SUCCESS) {
  383. return ret;
  384. }
  385. }
  386. last_graph_id_ = graph_id;
  387. GE_CHECK_NOTNULL_EXEC(ge_root_model, return FAILED);
  388. auto model_id = ge_root_model->GetModelId();
  389. auto async_mode = true;
  390. auto model_manager = ge::ModelManager::GetInstance();
  391. GE_CHECK_NOTNULL(model_manager);
  392. auto ret = model_manager->ExecuteModel(model_id, stream, async_mode, input_tensor, output_tensor);
  393. if (ret != SUCCESS) {
  394. return ret;
  395. }
  396. GELOGI("[GraphExecutor] Async execute graph with stream success graph id = %u, stream = %p.", graph_id, stream);
  397. return SUCCESS;
  398. }
  399. bool CompareByLoad(const Uint32Pair &lhs, const Uint32Pair &rhs) {
  400. return lhs.second < rhs.second;
  401. }
  402. uint32_t GraphExecutor::GetExecuteModelId(const GeRootModelPtr &ge_root_model) {
  403. std::vector<uint32_t> model_ids = ge_root_model->GetAllModelId();
  404. if (model_ids.empty()) {
  405. return kInvalidModelId;
  406. }
  407. if (model_ids.size() == 1) {
  408. return ge_root_model->GetModelId();
  409. }
  410. std::vector<Uint32Pair> model_id_to_loads;
  411. auto model_manager = ModelManager::GetInstance();
  412. GE_CHECK_NOTNULL(model_manager);
  413. for (auto model_id : model_ids) {
  414. auto davinci_model = model_manager->GetModel(model_id);
  415. auto hybrid_model = model_manager->GetHybridModel(model_id);
  416. if (hybrid_model == nullptr) {
  417. GE_CHECK_NOTNULL(davinci_model);
  418. }
  419. uint32_t input_load = hybrid_model != nullptr ? hybrid_model->GetDataInputerSize() :
  420. davinci_model->GetDataInputerSize();
  421. uint32_t running_load = hybrid_model != nullptr ? static_cast<uint32_t>(hybrid_model->GetRunningFlag()) :
  422. static_cast<uint32_t>(davinci_model->GetRunningFlag());
  423. uint32_t load = input_load + running_load;
  424. if (load == 0) {
  425. return model_id;
  426. }
  427. model_id_to_loads.emplace_back(model_id, load);
  428. }
  429. sort(model_id_to_loads.begin(), model_id_to_loads.end(), CompareByLoad);
  430. if (model_id_to_loads.empty()) {
  431. return kInvalidModelId;
  432. }
  433. return model_id_to_loads.begin()->first;
  434. }
  435. Status GraphExecutor::SetCallback(uint32_t model_id, const GeRootModelPtr &ge_root_model,
  436. const RunAsyncCallback &callback) {
  437. auto model_manager = ge::ModelManager::GetInstance();
  438. GE_CHECK_NOTNULL(model_manager);
  439. if (model_manager->IsNeedHybridLoad(*ge_root_model)) {
  440. auto model = model_manager->GetHybridModel(model_id);
  441. GE_CHECK_NOTNULL(model);
  442. if (model->SetRunAsyncListenerCallback(callback) != SUCCESS) {
  443. GELOGE(FAILED, "[Set][RunAsyncListenerCallback] failed, model_id %u", model_id);
  444. return FAILED;
  445. }
  446. } else {
  447. auto model = model_manager->GetModel(model_id);
  448. GE_CHECK_NOTNULL(model);
  449. if (model->SetRunAsyncListenerCallback(callback) != SUCCESS) {
  450. GELOGE(FAILED, "[Set][RunAsyncListenerCallback] failed, model_id %u", model_id);
  451. return FAILED;
  452. }
  453. }
  454. return SUCCESS;
  455. }
  456. Status GraphExecutor::AsyncExecuteModel(const GeRootModelPtr &ge_root_model, const std::vector<ge::Tensor> &inputs,
  457. const RunAsyncCallback &callback) {
  458. uint32_t model_id = GetExecuteModelId(ge_root_model);
  459. if (model_id == kInvalidModelId) {
  460. GELOGE(INTERNAL_ERROR, "No valid model id.");
  461. return INTERNAL_ERROR;
  462. }
  463. try {
  464. auto model_manager = ge::ModelManager::GetInstance();
  465. GE_CHECK_NOTNULL(model_manager);
  466. GELOGI("RunAsync begin.model_id %u", model_id);
  467. if (SetCallback(model_id, ge_root_model, callback) != SUCCESS) {
  468. GELOGE(FAILED, "[Set][CallBack] for model fail, model_id %u", model_id);
  469. return FAILED;
  470. }
  471. Status ret = model_manager->DataInputTensor(model_id, inputs);
  472. if (ret != SUCCESS) {
  473. GELOGE(ret, "[Call][DataInputTensor] RunAsync: DataInput fail, model_id %u", model_id);
  474. return ret;
  475. }
  476. GELOGI("RunAsync success.");
  477. } catch (std::bad_alloc &) {
  478. REPORT_INNER_ERROR("E19999", "Bad memory allocation exception occur failed, model_id %u", model_id);
  479. GELOGE(MEMALLOC_FAILED, "[Run][Async] failed, bad memory allocation occur, model_id %u", model_id);
  480. return MEMALLOC_FAILED;
  481. } catch (...) {
  482. REPORT_INNER_ERROR("E19999", "Some exceptions occur failed, model_id %u", model_id);
  483. GELOGE(FAILED, "[Run][Async] failed, some exceptions occur, model_id %u", model_id);
  484. return FAILED;
  485. }
  486. return SUCCESS;
  487. }
  488. Status GraphExecutor::DataInput(const InputData &input_data, OutputData &output_data) {
  489. try {
  490. auto model_manager = ge::ModelManager::GetInstance();
  491. GE_CHECK_NOTNULL(model_manager);
  492. Status ret = model_manager->DataInput(input_data, output_data);
  493. if (ret != SUCCESS) {
  494. GELOGE(ret, "[Call][DataInput] failed.");
  495. return ret;
  496. }
  497. } catch (std::bad_alloc &) {
  498. REPORT_INNER_ERROR("E19999", "Bad memory allocation exception occur failed");
  499. GELOGE(MEMALLOC_FAILED, "[Call][DataInput] failed, bad memory allocation occur !");
  500. return MEMALLOC_FAILED;
  501. } catch (...) {
  502. REPORT_INNER_ERROR("E19999", "Some exceptions occur failed");
  503. GELOGE(FAILED, "[Call][DataInput] failed, some exceptions occur !");
  504. return FAILED;
  505. }
  506. return SUCCESS;
  507. }
  508. Status GraphExecutor::GetInputOutputDescInfo(const uint32_t model_id, vector<InputOutputDescInfo> &input_desc,
  509. vector<InputOutputDescInfo> &output_desc) {
  510. try {
  511. auto model_manager = ge::ModelManager::GetInstance();
  512. GE_CHECK_NOTNULL(model_manager);
  513. Status ret = model_manager->GetInputOutputDescInfo(model_id, input_desc, output_desc);
  514. if (ret != SUCCESS) {
  515. GELOGE(ret, "[Get][InputOutputDescInfo] failed, model_id:%u.", model_id);
  516. return ret;
  517. }
  518. } catch (std::bad_alloc &) {
  519. REPORT_INNER_ERROR("E19999", "Bad memory allocation exception occur failed, model_id:%u.", model_id);
  520. GELOGE(MEMALLOC_FAILED, "[Get][InputOutputDescInfo] failed, bad memory allocation occur, model_id:%u.", model_id);
  521. return MEMALLOC_FAILED;
  522. } catch (...) {
  523. REPORT_INNER_ERROR("E19999", "Some exceptions occur failed, model_id:%u.", model_id);
  524. GELOGE(FAILED, "[Get][InputOutputDescInfo] failed, some exceptions occur, model_id:%u.", model_id);
  525. return FAILED;
  526. }
  527. return SUCCESS;
  528. }
  529. Status GraphExecutor::GetInputOutputDescInfo(const uint32_t model_id, vector<InputOutputDescInfo> &input_desc,
  530. vector<InputOutputDescInfo> &output_desc,
  531. std::vector<uint32_t> &input_formats, std::vector<uint32_t> &out_formats,
  532. bool new_model_desc) {
  533. try {
  534. auto model_manager = ge::ModelManager::GetInstance();
  535. GE_CHECK_NOTNULL(model_manager);
  536. Status ret = model_manager->GetInputOutputDescInfo(model_id, input_desc, output_desc, input_formats, out_formats,
  537. new_model_desc);
  538. if (ret != SUCCESS) {
  539. GELOGE(ret, "[Get][InputOutputDescInfo] failed, model_id:%u.", model_id);
  540. return ret;
  541. }
  542. } catch (std::bad_alloc &) {
  543. REPORT_INNER_ERROR("E19999", "Bad memory allocation exception occur failed, model_id:%u.", model_id);
  544. GELOGE(MEMALLOC_FAILED, "[Get][InputOutputDescInfo] failed, bad memory allocation occur, model_id:%u.", model_id);
  545. return MEMALLOC_FAILED;
  546. } catch (...) {
  547. REPORT_INNER_ERROR("E19999", "Some exceptions occur failed, model_id:%u.", model_id);
  548. GELOGE(FAILED, "[Get][InputOutputDescInfo] failed, some exceptions occur, model_id:%u.", model_id);
  549. return FAILED;
  550. }
  551. return SUCCESS;
  552. }
  553. ///
  554. /// @ingroup ge
  555. /// @brief Get dynamic batch_info
  556. /// @param [in] model_id
  557. /// @param [out] batch_info
  558. /// @param [out] dynamic_type
  559. /// @return execute result
  560. ///
  561. Status GraphExecutor::GetDynamicBatchInfo(uint32_t model_id, std::vector<std::vector<int64_t>> &batch_info,
  562. int32_t &dynamic_type) {
  563. auto model_manager = ge::ModelManager::GetInstance();
  564. GE_CHECK_NOTNULL(model_manager);
  565. Status ret = model_manager->GetDynamicBatchInfo(model_id, batch_info, dynamic_type);
  566. if (ret != SUCCESS) {
  567. GELOGE(ret, "[Get][DynamicBatchInfo] failed, model_id:%u.", model_id);
  568. return ret;
  569. }
  570. return SUCCESS;
  571. }
  572. ///
  573. /// @ingroup ge
  574. /// @brief Get combined dynamic dims info
  575. /// @param [in] model_id
  576. /// @param [out] batch_info
  577. /// @return execute result
  578. ///
  579. Status GraphExecutor::GetCombinedDynamicDims(uint32_t model_id, std::vector<std::vector<int64_t>> &batch_info) {
  580. auto model_manager = ge::ModelManager::GetInstance();
  581. GE_CHECK_NOTNULL(model_manager);
  582. Status ret = model_manager->GetCombinedDynamicDims(model_id, batch_info);
  583. if (ret != SUCCESS) {
  584. GELOGE(ret, "[Call][GetCombinedDynamicDims] failed, model_id:%u.", model_id);
  585. return ret;
  586. }
  587. return SUCCESS;
  588. }
  589. ///
  590. /// @ingroup ge
  591. /// @brief Get user designate shape order
  592. /// @param [in] model_id
  593. /// @param [out] user_input_shape_order
  594. /// @return execute result
  595. ///
  596. ge::Status GraphExecutor::GetUserDesignateShapeOrder(uint32_t model_id,
  597. std::vector<std::string> &user_input_shape_order) {
  598. auto model_manager = ge::ModelManager::GetInstance();
  599. GE_CHECK_NOTNULL(model_manager);
  600. Status ret = model_manager->GetUserDesignateShapeOrder(model_id, user_input_shape_order);
  601. if (ret != SUCCESS) {
  602. GELOGE(ret, "[Get][UserDesignateShapeOrder] failed, model_id:%u.", model_id);
  603. return ret;
  604. }
  605. return SUCCESS;
  606. }
  607. Status GraphExecutor::GetCurShape(const uint32_t model_id, std::vector<int64_t> &batch_info, int32_t &dynamic_type) {
  608. auto model_manager = ge::ModelManager::GetInstance();
  609. GE_CHECK_NOTNULL(model_manager);
  610. Status ret = model_manager->GetCurShape(model_id, batch_info, dynamic_type);
  611. if (ret != SUCCESS) {
  612. GELOGE(ret, "[Get][CurShape] failed, model_id:%u", model_id);
  613. return ret;
  614. }
  615. return SUCCESS;
  616. }
  617. Status GraphExecutor::GetOpAttr(uint32_t model_id, const std::string &op_name, const std::string &attr_name,
  618. std::string &attr_value) {
  619. auto model_manager = ge::ModelManager::GetInstance();
  620. GE_CHECK_NOTNULL(model_manager);
  621. Status ret = model_manager->GetOpAttr(model_id, op_name, attr_name, attr_value);
  622. if (ret != SUCCESS) {
  623. GELOGE(ret, "[Get][OpAttr]Get op:%s attr:%s failed.", op_name.c_str(), attr_name.c_str());
  624. REPORT_CALL_ERROR("E19999", "Get op:%s attr:%s failed.", op_name.c_str(), attr_name.c_str());
  625. return ret;
  626. }
  627. return SUCCESS;
  628. }
  629. Status GraphExecutor::GetModelAttr(uint32_t model_id, std::vector<string> &dynamic_output_shape_info) {
  630. auto model_manager = ge::ModelManager::GetInstance();
  631. GE_CHECK_NOTNULL(model_manager);
  632. Status ret = model_manager->GetModelAttr(model_id, dynamic_output_shape_info);
  633. if (ret != SUCCESS) {
  634. GELOGE(FAILED, "[Get][ModelAttr] failed, model_id:%u", model_id);
  635. return ret;
  636. }
  637. return SUCCESS;
  638. }
  639. Status GraphExecutor::GetAippInfo(uint32_t model_id, uint32_t index, AippConfigInfo &aipp_info) {
  640. auto model_manager = ge::ModelManager::GetInstance();
  641. GE_CHECK_NOTNULL(model_manager);
  642. Status ret = model_manager->GetAippInfo(model_id, index, aipp_info);
  643. if (ret != SUCCESS) {
  644. GELOGW("GetAIPPInfo is not success.");
  645. return ret;
  646. }
  647. return SUCCESS;
  648. }
  649. Status GraphExecutor::GetAippType(uint32_t model_id, uint32_t index, InputAippType &type, size_t &aipp_index) {
  650. auto model_manager = ge::ModelManager::GetInstance();
  651. GE_CHECK_NOTNULL(model_manager);
  652. Status ret = model_manager->GetAippType(model_id, index, type, aipp_index);
  653. if (ret != SUCCESS) {
  654. GELOGW("Get aipp type is not success.");
  655. return ret;
  656. }
  657. return SUCCESS;
  658. }
  659. Status GraphExecutor::GetOrigInputInfo(uint32_t model_id, uint32_t index, OriginInputInfo &orig_input_info) {
  660. auto model_manager = ge::ModelManager::GetInstance();
  661. GE_CHECK_NOTNULL(model_manager);
  662. Status ret = model_manager->GetOrigInputInfo(model_id, index, orig_input_info);
  663. if (ret != SUCCESS) {
  664. GELOGE(ret, "[Get][OrigInputInfo] failed, model_id:%u, index:%u.", model_id, index);
  665. return ret;
  666. }
  667. return SUCCESS;
  668. }
  669. Status GraphExecutor::GetAllAippInputOutputDims(uint32_t model_id, uint32_t index,
  670. std::vector<InputOutputDims> &input_dims,
  671. std::vector<InputOutputDims> &output_dims) {
  672. auto model_manager = ge::ModelManager::GetInstance();
  673. GE_CHECK_NOTNULL(model_manager);
  674. Status ret = model_manager->GetAllAippInputOutputDims(model_id, index, input_dims, output_dims);
  675. if (ret != SUCCESS) {
  676. GELOGE(ret, "[Get][AllAippInputOutputDims] failed, model_id:%u, index:%u.", model_id, index);
  677. return ret;
  678. }
  679. return SUCCESS;
  680. }
  681. Status GraphExecutor::GetOpDescInfo(uint32_t device_id, uint32_t stream_id, uint32_t task_id,
  682. OpDescInfo &op_desc_info) {
  683. auto model_manager = ge::ModelManager::GetInstance();
  684. GE_CHECK_NOTNULL(model_manager);
  685. Status ret = model_manager->GetOpDescInfo(device_id, stream_id, task_id, op_desc_info);
  686. if (ret != SUCCESS) {
  687. GELOGE(ret, "[Get][OpDescInfo] failed, device_id:%u, stream_id:%u, task_id:%u.",
  688. device_id, stream_id, task_id);
  689. return ret;
  690. }
  691. return SUCCESS;
  692. }
  693. Status GraphExecutor::GetModelByID(uint32_t model_id, std::shared_ptr<DavinciModel> &davinci_model) {
  694. auto model_manager = ge::ModelManager::GetInstance();
  695. GE_CHECK_NOTNULL(model_manager);
  696. davinci_model = model_manager->GetModel(static_cast<uint32_t>(model_id));
  697. if (davinci_model == nullptr) {
  698. REPORT_INNER_ERROR("E19999", "GetModel from model_manager fail, model_id:%u", model_id);
  699. GELOGE(ge::FAILED, "[Get][Model] failed, Model id:%d is invaild or model is not loaded.", model_id);
  700. return ge::FAILED;
  701. }
  702. return ge::SUCCESS;
  703. }
  704. Status GraphExecutor::ModelSubscribe(uint32_t graph_id) {
  705. auto &profiling_manager = ProfilingManager::Instance();
  706. const auto &subcribe_info = profiling_manager.GetSubscribeInfo();
  707. if (subcribe_info.is_subscribe) {
  708. std::shared_ptr<DavinciModel> davinci_model = nullptr;
  709. uint32_t model_id = 0;
  710. Status ret = profiling_manager.GetModelIdFromGraph(graph_id, model_id);
  711. if (ret != SUCCESS) {
  712. GELOGE(ret, "[Call][GetModelIdFromGraph] failed, graph_id:%u", graph_id);
  713. return ret;
  714. }
  715. ret = GetModelByID(model_id, davinci_model);
  716. if (ret != SUCCESS) {
  717. GELOGE(ret, "[Call][GetModelByID] failed, model_id:%u", model_id);
  718. return ret;
  719. }
  720. ret = profiling_manager.ProfModelSubscribe(subcribe_info.prof_switch, davinci_model.get());
  721. if (ret != SUCCESS) {
  722. GELOGE(ret, "[Call][ProfModelSubscribe] failed");
  723. return ret;
  724. }
  725. }
  726. return SUCCESS;
  727. }
  728. } // namespace ge

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