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 21 kB

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

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