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

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