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

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