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

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