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.

hybrid_model_async_executor.cc 20 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 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
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
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /**
  2. * Copyright 2019-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 "hybrid/executor/hybrid_model_async_executor.h"
  17. #include "graph/load/new_model_manager/model_utils.h"
  18. #include "graph/utils/tensor_utils.h"
  19. #include "graph/utils/type_utils.h"
  20. #include "graph/ge_context.h"
  21. #include "omm/csa_interact.h"
  22. namespace ge {
  23. namespace hybrid {
  24. namespace {
  25. const int kDataOutputIndex = 0;
  26. }
  27. HybridModelAsyncExecutor::HybridModelAsyncExecutor(HybridModel *model)
  28. : model_(model), run_flag_(false) {
  29. }
  30. HybridModelAsyncExecutor::~HybridModelAsyncExecutor() {
  31. if (stream_ != nullptr) {
  32. GE_CHK_RT(rtStreamDestroy(stream_));
  33. }
  34. }
  35. void HybridModelAsyncExecutor::SetDeviceId(uint32_t device_id) {
  36. device_id_ = device_id;
  37. }
  38. void HybridModelAsyncExecutor::SetModelId(uint32_t model_id) {
  39. model_id_ = model_id;
  40. }
  41. Status HybridModelAsyncExecutor::EnqueueData(const shared_ptr<InputDataWrapper> &data) {
  42. GE_CHK_STATUS_EXEC(data_inputer_->Push(data), return domi::DATA_QUEUE_ISFULL,
  43. "Data queue is full, please call again later, model_id %u ", model_id_);
  44. GELOGD("EnqueueData successfully. model_id = %u, data_index = %u", data->GetInput().model_id, data->GetInput().index);
  45. return SUCCESS;
  46. }
  47. Status HybridModelAsyncExecutor::Start(const std::shared_ptr<ModelListener> &listener) {
  48. GELOGD("HybridModelExecutor::Start IN, has listener = %d", listener != nullptr);
  49. std::lock_guard<std::mutex> lk(mu_);
  50. GE_CHK_BOOL_RET_STATUS(!run_flag_, INTERNAL_ERROR, "Model already started.");
  51. run_flag_ = true;
  52. listener_ = listener;
  53. future_ = std::async(std::launch::async, [&]() -> Status {
  54. GetContext().SetSessionId(executor_->GetContext()->session_id);
  55. return RunInternal();
  56. });
  57. GE_CHK_BOOL_RET_STATUS(future_.valid(), INTERNAL_ERROR, "Failed to start.");
  58. GELOGD("HybridModelExecutor::Start successfully");
  59. return SUCCESS;
  60. }
  61. Status HybridModelAsyncExecutor::Stop() {
  62. std::lock_guard<std::mutex> lk(mu_);
  63. run_flag_ = false;
  64. data_inputer_->Stop();
  65. Status ret = SUCCESS;
  66. if (future_.valid()) {
  67. ret = future_.get();
  68. }
  69. if (stream_ != nullptr) {
  70. GE_CHK_RT(rtStreamDestroy(stream_));
  71. stream_ = nullptr;
  72. }
  73. return ret;
  74. }
  75. Status HybridModelAsyncExecutor::Init() {
  76. data_inputer_ = std::unique_ptr<DataInputer>(new(std::nothrow) DataInputer());
  77. GE_CHECK_NOTNULL(data_inputer_);
  78. GE_CHK_RT_RET(rtStreamCreate(&stream_, RT_STREAM_PRIORITY_DEFAULT));
  79. executor_ = std::unique_ptr<HybridModelExecutor>(new(std::nothrow) HybridModelExecutor(model_, device_id_, stream_));
  80. GE_CHECK_NOTNULL(executor_);
  81. GE_CHK_STATUS_RET(executor_->Init(), "Failed to init hybrid engine");
  82. GE_CHK_STATUS_RET(InitInputDesc(), "Failed to init input tensors");
  83. return SUCCESS;
  84. }
  85. Status HybridModelAsyncExecutor::PreRun(InputData &current_data, HybridModelExecutor::ExecuteArgs &args) {
  86. GE_CHK_STATUS_RET(SyncVarData(), "Failed to sync var data");
  87. RECORD_MODEL_EXECUTION_EVENT(executor_->GetContext(), "[SyncVarData] End");
  88. GE_CHK_STATUS_RET(PrepareInputs(current_data, args), "Failed to copy input data to model");
  89. RECORD_MODEL_EXECUTION_EVENT(executor_->GetContext(), "[CopyInputData] End");
  90. return SUCCESS;
  91. }
  92. Status HybridModelAsyncExecutor::RunInternal() {
  93. auto device_id = static_cast<int32_t>(device_id_);
  94. GELOGD("Hybrid model start. model_id = %u, device_id = %u", model_id_, device_id_);
  95. GE_CHK_RT_RET(rtSetDevice(device_id));
  96. // DeviceReset before thread run finished!
  97. GE_MAKE_GUARD(not_used_var, [&] { GE_CHK_RT(rtDeviceReset(device_id)); });
  98. while (run_flag_) {
  99. std::shared_ptr<InputDataWrapper> data_wrapper;
  100. Status ret = data_inputer_->Pop(data_wrapper);
  101. if (data_wrapper == nullptr || ret != SUCCESS) {
  102. GELOGI("data_wrapper is null!, ret = %u", ret);
  103. continue;
  104. }
  105. GELOGI("Getting the input data, model_id:%u", model_id_);
  106. GE_IF_BOOL_EXEC(!run_flag_, break);
  107. InputData current_data = data_wrapper->GetInput();
  108. GELOGI("Model thread Run begin, model id:%u, data index:%u.", model_id_, current_data.index);
  109. RECORD_MODEL_EXECUTION_EVENT(executor_->GetContext(), "[RunInternal] [iteration = %d] Start", iterator_count_);
  110. HybridModelExecutor::ExecuteArgs args;
  111. ret = PreRun(current_data, args);
  112. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
  113. ret != SUCCESS, (void) HandleResult(ret, current_data.index, args, data_wrapper->GetOutput());
  114. CsaInteract::GetInstance().StoreInternalErrorCode(ret, ERROR_MODULE_FMK, JOBSUBSTATE_GRAPH_EXEC);
  115. continue, "PreRun failed."); // [No need to check value]
  116. ret = executor_->Execute(args);
  117. ret = HandleResult(ret, current_data.index, args, data_wrapper->GetOutput());
  118. if (ret != SUCCESS) {
  119. CsaInteract::GetInstance().StoreInternalErrorCode(ret, ERROR_MODULE_RUNTIME, JOBSUBSTATE_GRAPH_EXEC);
  120. continue;
  121. }
  122. RECORD_MODEL_EXECUTION_EVENT(executor_->GetContext(), "[RunInternal] [iteration = %d] End", iterator_count_);
  123. iterator_count_++;
  124. GELOGI("run iterator count is %lu", iterator_count_);
  125. }
  126. CsaInteract::GetInstance().WriteInternalErrorCode();
  127. GELOGI("Model run end, model id:%u", model_id_);
  128. return SUCCESS;
  129. }
  130. Status HybridModelAsyncExecutor::HandleResult(Status exec_ret,
  131. uint32_t data_id,
  132. HybridModelExecutor::ExecuteArgs &args,
  133. OutputData *output_data) {
  134. GELOGD("Start to handle result. model id = %u, data index = %u, execution ret = %u", model_id_, data_id, exec_ret);
  135. std::vector<ge::OutputTensorInfo> output_tensor_info_list;
  136. if (args.is_eos) {
  137. GELOGI("End of sequence, model id = %u", model_id_);
  138. GE_CHK_STATUS_RET_NOLOG(OnComputeDone(data_id, END_OF_SEQUENCE, output_tensor_info_list));
  139. return SUCCESS;
  140. }
  141. if (exec_ret != SUCCESS) {
  142. GELOGE(exec_ret, "Failed to execute graph. model_id = %u", model_id_);
  143. return OnComputeDone(data_id, INTERNAL_ERROR, output_tensor_info_list);
  144. }
  145. GE_CHECK_NOTNULL(output_data);
  146. auto ret = CopyOutputs(args, output_data, output_tensor_info_list);
  147. if (ret != SUCCESS) {
  148. OnComputeDone(data_id, INTERNAL_ERROR, output_tensor_info_list);
  149. return INTERNAL_ERROR;
  150. }
  151. GELOGD("Executed graph successfully, model id = %u, data_index = %u", model_id_, data_id);
  152. return OnComputeDone(data_id, SUCCESS, output_tensor_info_list);
  153. }
  154. Status HybridModelAsyncExecutor::SyncVarData() {
  155. GELOGI("Sync var data, model id:%u", model_id_);
  156. TensorValue *global_step_var = model_->GetVariable(NODE_NAME_GLOBAL_STEP);
  157. if (global_step_var != nullptr) {
  158. std::vector<uint64_t> v_step;
  159. v_step.push_back(iterator_count_);
  160. GE_CHK_RT_RET(rtMemcpy(global_step_var->MutableData(),
  161. global_step_var->GetSize(),
  162. v_step.data(),
  163. v_step.size() * sizeof(uint64_t),
  164. RT_MEMCPY_HOST_TO_DEVICE));
  165. } else {
  166. GELOGD("No GLOBAL_STEP variable was found.");
  167. }
  168. return SUCCESS;
  169. }
  170. Status HybridModelAsyncExecutor::PrepareInputs(const InputData &current_data, HybridModelExecutor::ExecuteArgs &args) {
  171. if (current_data.blobs.size() < input_tensor_desc_.size()) {
  172. GELOGE(PARAM_INVALID, "Blob size mismatches, expect at least %zu, but got %zu",
  173. input_tensor_desc_.size(), current_data.blobs.size());
  174. return PARAM_INVALID;
  175. }
  176. auto allocator = NpuMemoryAllocator::GetAllocator(device_id_);
  177. GE_CHECK_NOTNULL(allocator);
  178. args.input_desc.resize(input_tensor_desc_.size());
  179. const std::vector<DataBuffer> &blobs = current_data.blobs;
  180. for (size_t input_index = 0; input_index < input_tensor_desc_.size(); ++input_index) {
  181. auto tensor_size = input_sizes_[input_index];
  182. if (is_input_dynamic_[input_index]) {
  183. if (input_index >= current_data.shapes.size()) {
  184. GELOGE(PARAM_INVALID, "Shape index out of range, index = %zu, shape size = %zu",
  185. input_index, current_data.shapes.size());
  186. return PARAM_INVALID;
  187. }
  188. auto &tensor_desc = input_tensor_desc_[input_index];
  189. tensor_desc->SetShape(GeShape(current_data.shapes[input_index]));
  190. args.input_desc[input_index] = tensor_desc;
  191. GELOGD("Update shape of input[%u] to [%s]", input_index, tensor_desc->MutableShape().ToString().c_str());
  192. GE_CHK_GRAPH_STATUS_RET(TensorUtils::GetTensorMemorySizeInBytes(*tensor_desc, tensor_size),
  193. "Failed to calc tensor size, index = %zu, shape = [%s]",
  194. input_index,
  195. tensor_desc->GetShape().ToString().c_str());
  196. GELOGD("Input tensor[%zu] size = %zu", input_index, tensor_size);
  197. }
  198. GE_CHECK_GE(tensor_size, 0);
  199. auto tensor_buffer = TensorBuffer::Create(allocator, tensor_size);
  200. GE_CHECK_NOTNULL(tensor_buffer);
  201. args.inputs.emplace_back(std::shared_ptr<TensorBuffer>(tensor_buffer.release()));
  202. GELOGD("To copy input data for input[%u]", input_index);
  203. const DataBuffer &data_buf = blobs[input_index];
  204. auto mem_size = static_cast<uint64_t>(tensor_size);
  205. GE_CHK_BOOL_RET_STATUS(mem_size >= data_buf.length,
  206. PARAM_INVALID,
  207. "input data size(%lu) does not match model required size(%lu), ret failed.",
  208. data_buf.length,
  209. mem_size);
  210. GELOGI("[IMAS]CopyPlainData memcpy graph_%u type[F] output[%u] memaddr[%p] mem_size[%u] datasize[%lu]",
  211. model_->root_runtime_param_.graph_id,
  212. input_index,
  213. args.inputs[input_index].GetData(),
  214. mem_size,
  215. data_buf.length);
  216. GE_CHK_RT_RET(rtMemcpy(args.inputs[input_index].MutableData(),
  217. mem_size,
  218. data_buf.data,
  219. data_buf.length,
  220. RT_MEMCPY_HOST_TO_DEVICE));
  221. }
  222. return SUCCESS;
  223. }
  224. Status HybridModelAsyncExecutor::InitInputDesc() {
  225. int input_index = 0;
  226. for (const auto &input_node : model_->GetRootGraphItem()->GetInputNodes()) {
  227. GELOGD("Init input[%u], node = %s, is_dynamic = %d",
  228. input_index,
  229. input_node->NodeName().c_str(),
  230. input_node->is_dynamic);
  231. auto output_desc = input_node->MutableOutputDesc(kDataOutputIndex);
  232. GE_CHECK_NOTNULL(output_desc);
  233. int64_t tensor_size = -1;
  234. if (!input_node->is_dynamic) {
  235. GE_CHK_GRAPH_STATUS_RET(TensorUtils::GetSize(*output_desc, tensor_size),
  236. "Failed to get size from %s",
  237. input_node->NodeName().c_str());
  238. if (tensor_size == 0) {
  239. GELOGW("[%s] Tensor size == 0", input_node->NodeName().c_str());
  240. GE_CHK_GRAPH_STATUS_RET(TensorUtils::GetTensorMemorySizeInBytes(*output_desc, tensor_size),
  241. "Failed to calc tensor size");
  242. GELOGD("[%s] Tensor size updated to %ld", input_node->NodeName().c_str(), tensor_size);
  243. }
  244. }
  245. input_sizes_.emplace(input_index, tensor_size);
  246. input_tensor_desc_.emplace(input_index, output_desc);
  247. is_input_dynamic_.push_back(input_node->is_dynamic);
  248. input_index += 1;
  249. }
  250. return SUCCESS;
  251. }
  252. Status HybridModelAsyncExecutor::OnComputeDone(uint32_t data_index, uint32_t result_code,
  253. std::vector<ge::OutputTensorInfo> &outputs) {
  254. GELOGD("OnComputeDone. model id = %u, data index = %u, execution ret = %u", model_id_, data_index, result_code);
  255. if (listener_ != nullptr) {
  256. GE_CHK_STATUS(listener_->OnComputeDone(model_id_, data_index, result_code, outputs),
  257. "OnComputeDone failed");
  258. }
  259. return result_code;
  260. }
  261. Status HybridModelAsyncExecutor::CopyOutputs(HybridModelExecutor::ExecuteArgs &args,
  262. OutputData *output_data,
  263. std::vector<ge::OutputTensorInfo> &outputs) {
  264. // copy output data from op to designated position
  265. std::vector<ConstGeTensorDescPtr> &output_tensor_desc_list = args.output_desc;
  266. std::vector<TensorValue> &output_tensors = args.outputs;
  267. if (output_tensor_desc_list.size() != output_tensors.size()) {
  268. GELOGE(INTERNAL_ERROR,
  269. "Output sizes mismatch. From op_desc = %zu, and from output tensors = %zu",
  270. output_tensor_desc_list.size(),
  271. output_tensors.size());
  272. return INTERNAL_ERROR;
  273. }
  274. GELOGD("Number of outputs = %zu", output_tensor_desc_list.size());
  275. for (size_t i = 0; i < output_tensors.size(); ++i) {
  276. GELOGD("Start to process output[%zu]", i);
  277. auto &output_tensor = output_tensors[i];
  278. auto &tensor_desc = output_tensor_desc_list.at(i);
  279. GE_CHECK_NOTNULL(tensor_desc);
  280. int64_t output_size = -1;
  281. GE_CHK_GRAPH_STATUS_RET(TensorUtils::CalcTensorMemSize(tensor_desc->GetShape(),
  282. tensor_desc->GetFormat(),
  283. tensor_desc->GetDataType(),
  284. output_size),
  285. "Failed to calc tensor size for output[%zu]. shape = [%s], type = %s, format = %s",
  286. i,
  287. tensor_desc->GetShape().ToString().c_str(),
  288. TypeUtils::DataTypeToSerialString(tensor_desc->GetDataType()).c_str(),
  289. TypeUtils::FormatToSerialString(tensor_desc->GetFormat()).c_str());
  290. GELOGD("Got tensor size for output[%zu] successfully. shape = [%s], type = %s, format = %s, size = %ld",
  291. i,
  292. tensor_desc->GetShape().ToString().c_str(),
  293. TypeUtils::DataTypeToSerialString(tensor_desc->GetDataType()).c_str(),
  294. TypeUtils::FormatToSerialString(tensor_desc->GetFormat()).c_str(),
  295. output_size);
  296. GE_CHECK_GE(output_size, 0);
  297. GE_CHECK_LE(output_size, UINT32_MAX);
  298. if (output_tensor.GetSize() < static_cast<size_t>(output_size)) {
  299. GELOGE(INTERNAL_ERROR,
  300. "output[%zu] tensor size(%zu) is not enough for output shape [%s]",
  301. i, output_tensor.GetSize(), tensor_desc->GetShape().ToString().c_str());
  302. return INTERNAL_ERROR;
  303. }
  304. ge::OutputTensorInfo output;
  305. output.data_type = static_cast<uint32_t>(tensor_desc->GetDataType());
  306. output.dims = tensor_desc->GetShape().GetDims();
  307. output.length = output_size;
  308. if (output_size > 0) {
  309. std::unique_ptr<uint8_t[]> data_buf(new(std::nothrow) uint8_t[output_size]);
  310. GE_CHECK_NOTNULL(data_buf);
  311. GE_CHK_RT_RET(rtMemcpy(data_buf.get(),
  312. output_size,
  313. output_tensor.GetData(),
  314. output_size,
  315. RT_MEMCPY_DEVICE_TO_HOST));
  316. output.data = std::move(data_buf);
  317. output_data->blobs.emplace_back(data_buf.get(), static_cast<uint32_t>(output_size), false);
  318. } else {
  319. GELOGW("Output[%zu] is empty. shape = [%s]", i, tensor_desc->GetShape().ToString().c_str());
  320. output.data = nullptr;
  321. output_data->blobs.emplace_back(nullptr, 0U, false);
  322. }
  323. outputs.emplace_back(std::move(output));
  324. GELOGD("Output[%zu] added, type = %s, shape = [%s], size = %ld",
  325. i,
  326. TypeUtils::DataTypeToSerialString(tensor_desc->GetDataType()).c_str(),
  327. tensor_desc->GetShape().ToString().c_str(),
  328. output_size);
  329. }
  330. return SUCCESS;
  331. }
  332. Status HybridModelAsyncExecutor::Execute(const std::vector<DataBuffer> &inputs,
  333. const std::vector<GeTensorDesc> &input_desc,
  334. std::vector<DataBuffer> &outputs,
  335. std::vector<GeTensorDesc> &output_desc) {
  336. GELOGI("Start to execute model.");
  337. HybridModelExecutor::ExecuteArgs args;
  338. args.inputs.resize(inputs.size());
  339. for (size_t i = 0; i < inputs.size(); ++i) {
  340. TensorValue tensor_value(inputs[i].data, inputs[i].length);
  341. args.inputs[i] = tensor_value;
  342. }
  343. GE_CHK_STATUS_RET(executor_->Execute(args), "Failed to execute model.");
  344. for (const auto &output_tensor_desc : args.output_desc) {
  345. output_desc.emplace_back(*output_tensor_desc);
  346. }
  347. for (size_t i = 0; i < args.outputs.size(); ++i) {
  348. int64_t output_real_size = 0;
  349. ge::graphStatus graph_status = TensorUtils::GetTensorSizeInBytes(output_desc[i], output_real_size);
  350. if (graph_status != GRAPH_SUCCESS) {
  351. GELOGE(FAILED, "Get tensor size in bytes failed.");
  352. return FAILED;
  353. }
  354. if (output_real_size > 0) {
  355. if (outputs[i].length < static_cast<uint64_t>(output_real_size)) {
  356. GELOGE(FAILED, "output idx[%zu], the memory size of output[%lu] given by "
  357. "user should be greater than or equal to the real size of output[%ld]",
  358. i, outputs[i].length, output_real_size);
  359. return FAILED;
  360. }
  361. GE_CHK_RT_RET(rtMemcpy(outputs[i].data, outputs[i].length, args.outputs[i].GetData(), output_real_size,
  362. RT_MEMCPY_DEVICE_TO_DEVICE));
  363. }
  364. outputs[i].length = output_real_size;
  365. }
  366. return SUCCESS;
  367. }
  368. Status HybridModelAsyncExecutor::Execute(const vector<GeTensor> &inputs, vector<GeTensor> &outputs) {
  369. GELOGD("Start to execute model.");
  370. // prepare inputs
  371. InputData input_data;
  372. for (auto &tensor : inputs) {
  373. DataBuffer buffer;
  374. buffer.data = const_cast<uint8_t *>(tensor.GetData().GetData());
  375. buffer.length = tensor.GetData().size();
  376. input_data.blobs.emplace_back(buffer);
  377. input_data.shapes.emplace_back(tensor.GetTensorDesc().GetShape().GetDims());
  378. }
  379. HybridModelExecutor::ExecuteArgs args;
  380. GE_CHK_STATUS_RET(PrepareInputs(input_data, args), "Failed to copy input data to model");
  381. GELOGD("Done copying input data successfully.");
  382. GE_CHK_STATUS_RET(executor_->Execute(args), "Failed to execute model.");
  383. std::vector<ge::OutputTensorInfo> output_tensor_info_list;
  384. OutputData output_data;
  385. GE_CHK_STATUS_RET(CopyOutputs(args, &output_data, output_tensor_info_list), "Failed to copy outputs.");
  386. GELOGD("Done copying output data successfully. output count = %zu", output_tensor_info_list.size());
  387. int out_index = 0;
  388. outputs.resize(output_tensor_info_list.size());
  389. for (auto &out_tensor_info : output_tensor_info_list) {
  390. auto &ge_tensor = outputs[out_index];
  391. if (out_tensor_info.length > 0) {
  392. GE_CHK_GRAPH_STATUS_RET(ge_tensor.SetData(out_tensor_info.data.get(), out_tensor_info.length),
  393. "Failed to set output[%d].", out_index);
  394. }
  395. ge_tensor.MutableTensorDesc() = *args.output_desc[out_index];
  396. GELOGD("Set output[%d], tensor size = %ld, shape = [%s]",
  397. out_index,
  398. out_tensor_info.length,
  399. ge_tensor.MutableTensorDesc().MutableShape().ToString().c_str());
  400. ++out_index;
  401. }
  402. return SUCCESS;
  403. }
  404. } // namespace hybrid
  405. } // namespace ge

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