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.

ge_executor.cc 30 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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 "executor/ge_executor.h"
  17. #include <cce/cce.h>
  18. #include <cce/compiler_stub.h>
  19. #include <ctime>
  20. #include <iostream>
  21. #include "common/debug/log.h"
  22. #include "common/ge/ge_util.h"
  23. #include "common/helper/model_helper.h"
  24. #include "common/profiling/profiling_manager.h"
  25. #include "common/util.h"
  26. #include "framework/common/debug/ge_log.h"
  27. #include "framework/common/util.h"
  28. #include "graph/execute/graph_execute.h"
  29. #include "graph/load/graph_loader.h"
  30. #include "graph/load/new_model_manager/davinci_model_parser.h"
  31. #include "graph/load/new_model_manager/model_manager.h"
  32. #include "graph/manager/graph_mem_allocator.h"
  33. #include "graph/model.h"
  34. #include "graph/utils/graph_utils.h"
  35. #include "mmpa/mmpa_api.h"
  36. #include "single_op/single_op_manager.h"
  37. namespace {
  38. const size_t kDynamicBatchSizeVecSize = 1;
  39. const size_t kStaticBatchInfoSize = 1;
  40. const size_t kDynamicImageSizeVecSize = 2;
  41. const size_t kDynamicImageSizeInputSize = 2;
  42. const char *const kBatchLabel = "Batch_";
  43. ge::Status TransferDomiErrorCode(const uint32_t errorCode) {
  44. switch (errorCode) {
  45. case ge::PARAM_INVALID:
  46. case domi::PARAM_INVALID:
  47. return ge::PARAM_INVALID;
  48. case ge::INTERNAL_ERROR:
  49. case domi::INTERNAL_ERROR:
  50. return ge::INTERNAL_ERROR;
  51. default:
  52. return ge::FAILED;
  53. }
  54. }
  55. void GetGeTensorDescFromDomiInfo(std::vector<ge::TensorDesc> &ge_descs,
  56. const std::vector<ge::InputOutputDescInfo> &domi_descs,
  57. const std::vector<uint32_t> &formats) {
  58. uint32_t idx = 0;
  59. for (auto desc_item : domi_descs) {
  60. ge::TensorDesc ge_desc;
  61. ge_desc.SetName(desc_item.name);
  62. ge_desc.SetDataType(static_cast<ge::DataType>(desc_item.data_type));
  63. ge_desc.SetFormat(static_cast<ge::Format>(formats[idx]));
  64. std::vector<int64_t> shape_dims;
  65. for (auto dim : desc_item.shape_info.dims) {
  66. shape_dims.push_back(dim);
  67. }
  68. ge::Shape ge_shape(shape_dims);
  69. ge_desc.SetShape(ge_shape);
  70. ge_desc.SetSize(desc_item.size);
  71. ge_descs.emplace_back(ge_desc);
  72. ++idx;
  73. }
  74. }
  75. void GetDomiInputData(const ge::RunModelData &input_data, ge::InputData &inputs) {
  76. inputs.index = input_data.index;
  77. inputs.model_id = input_data.modelId;
  78. inputs.timestamp = input_data.timestamp;
  79. inputs.timeout = input_data.timeout;
  80. inputs.request_id = input_data.request_id;
  81. for (const auto &data_item : input_data.blobs) {
  82. ge::DataBuffer dataBuf{data_item.data, data_item.length, data_item.isDataSupportMemShare};
  83. inputs.blobs.emplace_back(dataBuf);
  84. }
  85. }
  86. void GetDomiOutputData(const ge::RunModelData &output_data, ge::OutputData &outputs) {
  87. outputs.index = output_data.index;
  88. outputs.model_id = output_data.modelId;
  89. for (const auto &data_item : output_data.blobs) {
  90. ge::DataBuffer dataBuf(data_item.data, data_item.length, data_item.isDataSupportMemShare);
  91. outputs.blobs.emplace_back(dataBuf);
  92. }
  93. }
  94. void SetDynamicInputDataFlag(const ge::RunModelData &input_data, const std::vector<std::vector<int64_t>> batch_info,
  95. ge::InputData &inputs) {
  96. inputs.is_dynamic_batch = true;
  97. std::string batch_label;
  98. for (size_t i = 0; i < batch_info.size(); ++i) {
  99. if (batch_info[i].size() == kDynamicBatchSizeVecSize &&
  100. batch_info[i][0] == static_cast<int64_t>(input_data.dynamic_batch_size)) {
  101. batch_label = kBatchLabel + std::to_string(i);
  102. inputs.batch_label = batch_label;
  103. break;
  104. } else if (batch_info[i].size() == kDynamicImageSizeVecSize &&
  105. batch_info[i][0] == static_cast<int64_t>(input_data.dynamic_image_height) &&
  106. batch_info[i][1] == static_cast<int64_t>(input_data.dynamic_image_width)) {
  107. batch_label = kBatchLabel + std::to_string(i);
  108. inputs.batch_label = batch_label;
  109. break;
  110. }
  111. }
  112. GELOGI("current batch label:%s", batch_label.c_str());
  113. }
  114. bool IsDynamicBatchSizeMatchModel(uint64_t batch_size, const vector<std::vector<int64_t>> &batch_info) {
  115. if (batch_info.empty()) {
  116. GELOGE(ge::FAILED, "Dynamic batch info is empty.");
  117. return false;
  118. }
  119. for (auto batch : batch_info) {
  120. if (batch.size() != kDynamicBatchSizeVecSize) {
  121. GELOGE(ge::FAILED, "Dynamic batch param num is %zu, current batch size is %zu.", kDynamicBatchSizeVecSize,
  122. batch.size());
  123. return false;
  124. }
  125. if (batch[0] == static_cast<int64_t>(batch_size)) {
  126. return true;
  127. }
  128. }
  129. GELOGE(ge::FAILED, "Dynamic batch %lu can not match the gear of model.", batch_size);
  130. return false;
  131. }
  132. bool IsDynamicImageSizeMatchModel(uint64_t image_height, uint64_t image_width,
  133. const vector<std::vector<int64_t>> &batch_info) {
  134. if (batch_info.empty()) {
  135. GELOGE(ge::FAILED, "Dynamic batch info is empty.");
  136. return false;
  137. }
  138. for (auto resolution : batch_info) {
  139. if (resolution.size() != kDynamicImageSizeVecSize) {
  140. GELOGE(ge::FAILED, "Dynamic resolution param num is %zu, current resolution size is %zu.",
  141. kDynamicImageSizeVecSize, resolution.size());
  142. return false;
  143. }
  144. if (resolution[0] == static_cast<int64_t>(image_height) && resolution[1] == static_cast<int64_t>(image_width)) {
  145. return true;
  146. }
  147. }
  148. GELOGE(ge::FAILED, "Dynamic resolution (%lu,%lu) can not match the gear of model.", image_height, image_width);
  149. return false;
  150. }
  151. } // namespace
  152. namespace ge {
  153. bool GeExecutor::isInit_ = false;
  154. class ModelListenerAdapter : public ModelListener {
  155. public:
  156. domi::Status OnComputeDone(uint32_t model_id, uint32_t dataIndex, uint32_t resultCode,
  157. std::vector<ge::OutputTensorInfo> &outputs) {
  158. if (listener == nullptr) {
  159. GELOGE(ge::FAILED, "listener is null.");
  160. return FAILED;
  161. }
  162. return listener->OnComputeDone(model_id, dataIndex, resultCode, outputs);
  163. }
  164. std::shared_ptr<ge::ModelListener> listener;
  165. };
  166. GeExecutor::GeExecutor() {}
  167. Status GeExecutor::Initialize() {
  168. GELOGI("Init GeExecutor begin.");
  169. if (isInit_) {
  170. GELOGW("Already initialized, no need to be initialized again.");
  171. return ge::SUCCESS;
  172. }
  173. std::vector<rtMemType_t> mem_type(1, RT_MEMORY_HBM);
  174. auto ret = MemManager::Instance().Initialize(mem_type);
  175. if (ret != SUCCESS) {
  176. GELOGE(ret, "Memory Manager init failed.");
  177. return ret;
  178. }
  179. // Start profiling
  180. Options profiling_options;
  181. profiling_options.device_id = 0;
  182. profiling_options.job_id = "";
  183. ProfilingManager::Instance().Init(profiling_options);
  184. isInit_ = true;
  185. GELOGI("Init GeExecutor over.");
  186. return ge::SUCCESS;
  187. }
  188. Status GeExecutor::Finalize() {
  189. GELOGI("Uninit GeExecutor begin.");
  190. if (isInit_ == false) {
  191. GELOGW("GeExecutor has not been initialized.");
  192. return ge::SUCCESS;
  193. }
  194. // Stop profiling
  195. if (ProfilingManager::Instance().ProfilingOn()) {
  196. ProfilingManager::Instance().StopProfiling();
  197. ProfilingManager::Instance().PluginUnInit(GE_PROFILING_MODULE);
  198. }
  199. GELOGI("Uninit GeExecutor over.");
  200. return ge::SUCCESS;
  201. }
  202. Status GeExecutor::SetDynamicBatchSize(uint32_t model_id, void *dynamic_input_addr, uint64_t length,
  203. uint64_t batch_size) {
  204. if (dynamic_input_addr == nullptr) {
  205. GELOGE(FAILED, "Dynamic input addr is nullptr!");
  206. return FAILED;
  207. }
  208. uint64_t size = sizeof(uint64_t);
  209. if (length < size) {
  210. GELOGE(FAILED, "Dynamic input size [%lu] is less than [%lu]!", length, size);
  211. return FAILED;
  212. }
  213. // Verify whether the input dynamic batch matches the model gear
  214. std::vector<std::vector<int64_t>> batch_info;
  215. std::vector<uint64_t> batch_num{batch_size};
  216. Status ret = GraphExecutor::GetDynamicBatchInfo(model_id, batch_info);
  217. if (ret != SUCCESS) {
  218. GELOGE(FAILED, "Get dynamic input info failed.");
  219. return FAILED;
  220. }
  221. if (!IsDynamicBatchSizeMatchModel(batch_size, batch_info)) {
  222. GELOGE(FAILED, "The current dynamic input does not match the gear of the model.");
  223. return FAILED;
  224. }
  225. ret = GraphExecutor::SetDynamicSize(model_id, batch_num);
  226. if (ret != SUCCESS) {
  227. GELOGE(FAILED, "Set dynamic size failed");
  228. return FAILED;
  229. }
  230. // memcpy dynamic_batch_size from host to device
  231. if (rtMemcpy(dynamic_input_addr, length, &batch_size, size, RT_MEMCPY_HOST_TO_DEVICE) != RT_ERROR_NONE) {
  232. GELOGE(FAILED, "memcpy dynamic batch input data failed!");
  233. return FAILED;
  234. }
  235. return SUCCESS;
  236. }
  237. Status GeExecutor::SetDynamicImageSize(uint32_t model_id, void *dynamic_input_addr, uint64_t length,
  238. uint64_t image_height, uint64_t image_width) {
  239. if (dynamic_input_addr == nullptr) {
  240. GELOGE(FAILED, "Dynamic input addr is nullptr!");
  241. return FAILED;
  242. }
  243. uint64_t dynamic_input_size = kDynamicImageSizeInputSize * sizeof(uint64_t);
  244. if (length < dynamic_input_size) {
  245. GELOGE(FAILED, "Dynamic input size [%lu] is less than [%lu]!", length, dynamic_input_size);
  246. return FAILED;
  247. }
  248. // Verify whether the input dynamic resolution matches the model gear
  249. std::vector<std::vector<int64_t>> batch_info;
  250. std::vector<uint64_t> batch_num{image_height, image_width};
  251. Status ret = GraphExecutor::GetDynamicBatchInfo(model_id, batch_info);
  252. if (ret != SUCCESS) {
  253. GELOGE(FAILED, "Get dynamic input info failed.");
  254. return FAILED;
  255. }
  256. if (!IsDynamicImageSizeMatchModel(image_height, image_width, batch_info)) {
  257. GELOGE(FAILED, "The current dynamic input does not match the gear of the model.");
  258. return FAILED;
  259. }
  260. ret = GraphExecutor::SetDynamicSize(model_id, batch_num);
  261. if (ret != SUCCESS) {
  262. GELOGE(FAILED, "Set dynamic size failed");
  263. return FAILED;
  264. }
  265. // Memcpy dynamic resolution height from host to device
  266. if (rtMemcpy(dynamic_input_addr, sizeof(uint64_t), &image_height, sizeof(uint64_t), RT_MEMCPY_HOST_TO_DEVICE) !=
  267. RT_ERROR_NONE) {
  268. GELOGE(FAILED, "memcpy dynamic resolution input data failed!");
  269. return FAILED;
  270. }
  271. uint64_t remain_size = length - sizeof(uint64_t);
  272. // Memcpy dynamic resolution width from host to device
  273. if (rtMemcpy(reinterpret_cast<void *>(reinterpret_cast<uint8_t *>(dynamic_input_addr) + sizeof(uint64_t)),
  274. remain_size, &image_width, sizeof(uint64_t), RT_MEMCPY_HOST_TO_DEVICE) != RT_ERROR_NONE) {
  275. GELOGE(FAILED, "memcpy dynamic resolution input data failed!");
  276. return FAILED;
  277. }
  278. return SUCCESS;
  279. }
  280. Status GeExecutor::GetCurShape(const uint32_t model_id, std::vector<int64_t> &batch_info) {
  281. GELOGI("Begin to get current shape");
  282. if (!isInit_) {
  283. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  284. return GE_EXEC_NOT_INIT;
  285. }
  286. Status ret = GraphExecutor::GetCurShape(model_id, batch_info);
  287. if (ret != SUCCESS) {
  288. GELOGE(FAILED, "Get current shape failed");
  289. return FAILED;
  290. }
  291. return SUCCESS;
  292. }
  293. Status GeExecutor::SetDynamicAippData(uint32_t model_id, void *dynamic_input_addr, uint64_t length,
  294. const std::vector<kAippDynamicBatchPara> &aippBatchPara,
  295. const kAippDynamicPara &aippParms) {
  296. GELOGI("Enter to SetDynamicAippData.");
  297. if (dynamic_input_addr == nullptr) {
  298. GELOGE(FAILED, "Dynamic aipp input addr is nullptr!");
  299. return FAILED;
  300. }
  301. if (aippBatchPara.empty()) {
  302. GELOGE(FAILED, "aippBatchPara is empty.");
  303. return FAILED;
  304. }
  305. uint64_t batch_num = aippBatchPara.size();
  306. uint64_t real_aippParms_size = sizeof(kAippDynamicPara) - sizeof(kAippDynamicBatchPara);
  307. uint64_t struct_len = batch_num * sizeof(kAippDynamicBatchPara) + real_aippParms_size;
  308. GELOGI(
  309. "Get acl input dynamic aipp data, model_id is %u, length is %lu,"
  310. "batch num is %lu, struct_len is %lu",
  311. model_id, length, batch_num, struct_len);
  312. if (struct_len > length) {
  313. GELOGE(FAILED, "input dynamic aipp param len [%lu] is larger than aipp_data size [%lu]", struct_len, length);
  314. return FAILED;
  315. }
  316. // Memcpy real kAippDynamicBatchPara from host to device
  317. if (rtMemcpy(dynamic_input_addr, length, &aippParms, real_aippParms_size, RT_MEMCPY_HOST_TO_DEVICE) !=
  318. RT_ERROR_NONE) {
  319. GELOGE(FAILED, "memcpy real_aippParms_size failed!");
  320. return FAILED;
  321. }
  322. uint64_t remain_len = length - real_aippParms_size;
  323. uint8_t *aipp_batch_para_dev = reinterpret_cast<uint8_t *>(dynamic_input_addr) + real_aippParms_size;
  324. for (uint64_t i = 0; i < batch_num; ++i) {
  325. if (rtMemcpy(reinterpret_cast<void *>(aipp_batch_para_dev + i * sizeof(kAippDynamicBatchPara)),
  326. (remain_len - i * sizeof(kAippDynamicBatchPara)), &(aippBatchPara[i]), sizeof(kAippDynamicBatchPara),
  327. RT_MEMCPY_HOST_TO_DEVICE) != RT_ERROR_NONE) {
  328. GELOGE(FAILED, "memcpy kAippDynamicBatchPara input data failed!");
  329. return FAILED;
  330. }
  331. }
  332. return SUCCESS;
  333. }
  334. // Load model
  335. Status GeExecutor::LoadModelOffline(uint32_t &model_id, const std::string &path, const std::string &key,
  336. int32_t priority, std::shared_ptr<ge::ModelListener> listener) {
  337. GELOGI("load model offline begin.");
  338. if (!isInit_) {
  339. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  340. return GE_EXEC_NOT_INIT;
  341. }
  342. string filePath = RealPath(path.c_str());
  343. if (filePath.empty()) {
  344. GELOGE(ge::FAILED, "File path is invalid. please check your text file '%s'.", path.c_str());
  345. return ge::FAILED;
  346. }
  347. std::shared_ptr<ModelListenerAdapter> listener_adapter = MakeShared<ModelListenerAdapter>();
  348. if (listener_adapter == nullptr) {
  349. GELOGE(MEMALLOC_FAILED, "ModelListenerAdapter make shared failed!");
  350. return ge::FAILED;
  351. }
  352. listener_adapter->listener = listener;
  353. Status ret = GraphLoader::LoadModelFromFile(path, key, priority, listener_adapter, model_id);
  354. if (ret != SUCCESS) {
  355. GELOGE(ret, "[GeExecutor] LoadModelFromFile failed");
  356. return TransferDomiErrorCode(ret);
  357. }
  358. return SUCCESS;
  359. }
  360. Status GeExecutor::LoadModel(uint32_t &model_id, const ModelData &model_data,
  361. std::shared_ptr<ge::ModelListener> listener) {
  362. GELOGI("Load model begin.");
  363. if (!isInit_) {
  364. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  365. return GE_EXEC_NOT_INIT;
  366. }
  367. std::shared_ptr<ModelListenerAdapter> listener_adapter = MakeShared<ModelListenerAdapter>();
  368. if (listener_adapter == nullptr) {
  369. GELOGE(MEMALLOC_FAILED, "ModelListenerAdapter make shared failed!");
  370. return ge::FAILED;
  371. }
  372. listener_adapter->listener = listener;
  373. Status ret = GraphLoader::LoadModel(model_data, listener_adapter, model_id);
  374. if (ret != SUCCESS) {
  375. GELOGE(ret, "[GeExecutor] LoadModel failed.");
  376. return TransferDomiErrorCode(ret);
  377. }
  378. return ret;
  379. }
  380. Status GeExecutor::UnloadModel(uint32_t model_id) {
  381. GELOGI("unload model %u begin.", model_id);
  382. if (!isInit_) {
  383. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  384. return GE_EXEC_NOT_INIT;
  385. }
  386. Status ret = GraphLoader::DestroyAicpuSessionForInfer(model_id);
  387. if (ret != SUCCESS) {
  388. GELOGE(ret, "[GraphLoader] DestroyAicpuSessionForInfer failed.");
  389. return FAILED;
  390. }
  391. return GraphLoader::UnloadModel(model_id);
  392. }
  393. Status GeExecutor::RunModel(const ge::RunModelData &input_data, ge::RunModelData &output_data) {
  394. GELOGI("run model begin.");
  395. if (!isInit_) {
  396. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  397. return GE_EXEC_NOT_INIT;
  398. }
  399. InputData inputs;
  400. GetDomiInputData(input_data, inputs);
  401. OutputData outputs;
  402. GetDomiOutputData(output_data, outputs);
  403. return GraphExecutor::DataInput(inputs, outputs);
  404. }
  405. // Get input and output descriptor
  406. Status GeExecutor::GetModelDescInfo(uint32_t model_id, std::vector<ge::TensorDesc> &input_desc,
  407. std::vector<ge::TensorDesc> &output_desc, bool new_model_desc) {
  408. GELOGI("get model desc info begin.");
  409. if (!isInit_) {
  410. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  411. return GE_EXEC_NOT_INIT;
  412. }
  413. std::vector<InputOutputDescInfo> input_desc_infos;
  414. std::vector<InputOutputDescInfo> output_desc_infos;
  415. std::vector<uint32_t> input_formats;
  416. std::vector<uint32_t> output_formats;
  417. Status ret = GraphExecutor::GetInputOutputDescInfo(model_id, input_desc_infos, output_desc_infos, input_formats,
  418. output_formats, new_model_desc);
  419. if (ret != domi::SUCCESS) {
  420. GELOGE(ret, "GetInputOutputDescInfo failed. ret = %u", ret);
  421. return TransferDomiErrorCode(ret);
  422. }
  423. if (input_formats.size() != input_desc_infos.size()) {
  424. GELOGE(ge::FAILED, "input_formats.size() != input_desc_infos.size().");
  425. return ge::FAILED;
  426. }
  427. if (output_formats.size() != output_desc_infos.size()) {
  428. GELOGE(ge::FAILED, "output_formats.size() != output_desc_infos.size().");
  429. return ge::FAILED;
  430. }
  431. // Transfer data to TensorDesc
  432. GetGeTensorDescFromDomiInfo(input_desc, input_desc_infos, input_formats);
  433. GetGeTensorDescFromDomiInfo(output_desc, output_desc_infos, output_formats);
  434. GELOGI("get model desc info end.");
  435. return ge::SUCCESS;
  436. }
  437. ///
  438. /// @ingroup ge
  439. /// @brief Get dynamic batch_info
  440. /// @param [in] model_id
  441. /// @param [out] batch_info
  442. /// @return execute result
  443. ///
  444. Status GeExecutor::GetDynamicBatchInfo(uint32_t model_id, std::vector<std::vector<int64_t>> &batch_info) {
  445. GELOGI("Begin to get dynamic batch info.");
  446. if (!isInit_) {
  447. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  448. return GE_EXEC_NOT_INIT;
  449. }
  450. Status ret = GraphExecutor::GetDynamicBatchInfo(model_id, batch_info);
  451. if (ret != SUCCESS) {
  452. GELOGE(ret, "GetDynamicBatchInfo failed.");
  453. return ret;
  454. }
  455. GELOGI("Get dynamic batch info succ.");
  456. return SUCCESS;
  457. }
  458. ///
  459. /// @ingroup ge
  460. /// @brief Get AIPP input format
  461. /// @param [in] model_id
  462. /// @param [in] index
  463. /// @param [out] input_format
  464. /// @return execute result
  465. ///
  466. Status GeExecutor::GetAIPPInfo(uint32_t model_id, uint32_t index, AippConfigInfo &aipp_info) {
  467. GELOGI("Begin to GetAIPPInfo.");
  468. if (!isInit_) {
  469. GELOGE(GE_EXEC_NOT_INIT, "not inited yet!");
  470. return GE_EXEC_NOT_INIT;
  471. }
  472. Status ret = GraphExecutor::GetAIPPInfo(model_id, index, aipp_info);
  473. if (ret != SUCCESS) {
  474. GELOGE(ret, "GetAIPPInfo failed.");
  475. return ret;
  476. }
  477. GELOGI("GetAIPPInfo succ.");
  478. return SUCCESS;
  479. }
  480. Status GeExecutor::GetModelAttr(uint32_t model_id, std::vector<std::string> &dynamic_output_shape_info) {
  481. GELOGI("Begin to get dynamic batch output shape info");
  482. if (!isInit_) {
  483. GELOGE(GE_EXEC_NOT_INIT, "not inited yet!");
  484. return GE_EXEC_NOT_INIT;
  485. }
  486. Status ret = GraphExecutor::GetModelAttr(model_id, dynamic_output_shape_info);
  487. if (ret != SUCCESS) {
  488. GELOGE(ret, "Get dynamic batch output shape info failed.");
  489. return ret;
  490. }
  491. GELOGI("Get dynamic batch output shape info succ.");
  492. return SUCCESS;
  493. }
  494. Status GeExecutor::GetModelDescInfoForZeroCopy(uint32_t model_id, std::vector<ge::TensorDesc> &input_desc,
  495. std::vector<TensorDesc> &output_desc) {
  496. GELOGI("get model desc info for zero copy begin.");
  497. if (!isInit_) {
  498. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  499. return GE_EXEC_NOT_INIT;
  500. }
  501. std::vector<InputOutputDescInfo> input_desc_infos;
  502. std::vector<InputOutputDescInfo> output_desc_infos;
  503. std::vector<uint32_t> input_formats;
  504. std::vector<uint32_t> output_formats;
  505. Status ret = GraphExecutor::GetInputOutputDescInfoForZeroCopy(model_id, input_desc_infos, output_desc_infos,
  506. input_formats, output_formats);
  507. if (ret != domi::SUCCESS) {
  508. GELOGE(ret, "Get DescInfo from zero copy failed. ret = %u", ret);
  509. return TransferDomiErrorCode(ret);
  510. }
  511. if (input_formats.size() != input_desc_infos.size()) {
  512. GELOGE(ge::FAILED, "input_formats.size() != input_desc_infos.size().");
  513. return ge::FAILED;
  514. }
  515. if (output_formats.size() != output_desc_infos.size()) {
  516. GELOGE(ge::FAILED, "output_formats.size() != output_desc_infos.size().");
  517. return ge::FAILED;
  518. }
  519. GetGeTensorDescFromDomiInfo(input_desc, input_desc_infos, input_formats);
  520. GetGeTensorDescFromDomiInfo(output_desc, output_desc_infos, output_formats);
  521. GELOGI("get model desc info from zero copy end.");
  522. return ge::SUCCESS;
  523. }
  524. Status GeExecutor::CommandHandle(const Command &command) {
  525. GELOGI("command handle begin.");
  526. Status ret = GraphLoader::CommandHandle(command);
  527. if (ret != SUCCESS) {
  528. GELOGE(ret, "CommandHandle: Command Handle failed.");
  529. return TransferDomiErrorCode(ret);
  530. }
  531. return SUCCESS;
  532. }
  533. Status GeExecutor::GetMaxUsedMemory(uint32_t model_id, uint32_t &max_size) {
  534. GELOGI("Get max used memory begin.");
  535. if (!isInit_) {
  536. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  537. return GE_EXEC_NOT_INIT;
  538. }
  539. uint64_t max_mem_size = 0;
  540. Status ret = GraphLoader::GetMaxUsedMemory(model_id, max_mem_size);
  541. max_size = static_cast<uint32_t>(max_mem_size);
  542. return ret;
  543. }
  544. /**
  545. * @ingroup ge
  546. * @brief Load data from model file to memory
  547. * @param [in] const std::string &path: Offline model file path
  548. * @param [out] domi::ModelData &model_data: Offline model memory data
  549. * @return SUCCESS handle successfully / others handle failed
  550. */
  551. Status GeExecutor::LoadDataFromFile(const std::string &path, ModelData &model_data) {
  552. GELOGI("Load data from file begin.");
  553. if (!isInit_) {
  554. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  555. return GE_EXEC_NOT_INIT;
  556. }
  557. string filePath = RealPath(path.c_str());
  558. if (filePath.empty()) {
  559. GELOGE(ge::FAILED, "File path is invalid. please check your text file '%s'.", path.c_str());
  560. return ge::FAILED;
  561. }
  562. GELOGI("load modelData from file: %s.", path.c_str());
  563. std::string key_path;
  564. int32_t priority = 0;
  565. Status ret = GraphLoader::LoadDataFromFile(path, key_path, priority, model_data);
  566. if (ret != SUCCESS) {
  567. if (model_data.model_data != nullptr) {
  568. delete[] static_cast<char *>(model_data.model_data);
  569. model_data.model_data = nullptr;
  570. }
  571. }
  572. return ret;
  573. }
  574. /**
  575. * @ingroup ge
  576. * @brief Load model from offline model memory data
  577. * @param [in] domi::ModelData &model_data: Offline model data
  578. void *dev_ptr: Input/Output memory start address
  579. size_t memsize: Input/Output memory length
  580. void *weight_ptr: Weight memory start address
  581. size_t weightsize: Weight memory length
  582. * @param [out] uint32_t &model_id: identification after model loading
  583. * @return SUCCESS handle successfully / others handle failed
  584. */
  585. Status GeExecutor::LoadModelFromData(uint32_t &model_id, const ModelData &model_data, void *dev_ptr, size_t mem_size,
  586. void *weight_ptr, size_t weight_size) {
  587. GELOGI("Load model from data begin.");
  588. if (!isInit_) {
  589. GELOGE(GE_EXEC_NOT_INIT, "not inited yet!");
  590. return GE_EXEC_NOT_INIT;
  591. }
  592. return GraphLoader::LoadModelFromData(model_id, model_data, dev_ptr, mem_size, weight_ptr, weight_size);
  593. }
  594. /**
  595. * @ingroup ge
  596. * @brief Load task list from ModelData with queue.
  597. * @param [out] model_id: model id allocate from manager.
  598. * @param [in] ge_model_data: Model data load from offline model.
  599. * @param [in] input_queue_ids: input queue ids create from user.
  600. * @param [in] output_queue_ids: input queue ids create from user.
  601. * @return: 0 for success / others for fail
  602. */
  603. Status GeExecutor::LoadModelWithQ(uint32_t &model_id, const ModelData &model_data,
  604. const std::vector<uint32_t> &input_queue_ids,
  605. const std::vector<uint32_t> &output_queue_ids) {
  606. GELOGI("Load model with queue begin.");
  607. if (!isInit_) {
  608. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  609. return GE_EXEC_NOT_INIT;
  610. }
  611. return GraphLoader::LoadModelWithQ(model_id, model_data, input_queue_ids, output_queue_ids);
  612. }
  613. /**
  614. * @ingroup ge
  615. * @brief Synchronous execution of offline model(Do not create thread)
  616. * @param [in] uint32_t model_id: Model ID to execute
  617. void* stream: stream to execute
  618. const domi::InputData *input_data: Model input data
  619. bool async_mode: is asynchronize mode.
  620. * @param [out] domi::OutputData *output_data: Model output data
  621. * @return SUCCESS handle successfully / others handle failed
  622. */
  623. Status GeExecutor::ExecModel(uint32_t model_id, void *stream, const ge::RunModelData &run_input_data,
  624. ge::RunModelData &run_output_data, bool async_mode) {
  625. GELOGI("Execute model begin.");
  626. if (!isInit_) {
  627. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  628. return GE_EXEC_NOT_INIT;
  629. }
  630. InputData input_data;
  631. OutputData output_data;
  632. GetDomiInputData(run_input_data, input_data);
  633. GetDomiOutputData(run_output_data, output_data);
  634. if ((run_input_data.dynamic_batch_size != 0) || (run_input_data.dynamic_image_width != 0) ||
  635. (run_input_data.dynamic_image_height != 0)) {
  636. std::vector<std::vector<int64_t>> batch_info;
  637. Status ret = GraphExecutor::GetDynamicBatchInfo(model_id, batch_info);
  638. if (ret != SUCCESS) {
  639. GELOGE(FAILED, "Get dynamic input info failed.");
  640. return FAILED;
  641. }
  642. if (!batch_info.empty()) {
  643. SetDynamicInputDataFlag(run_input_data, batch_info, input_data);
  644. }
  645. }
  646. return GraphLoader::ExecuteModel(model_id, stream, async_mode, input_data, output_data);
  647. }
  648. /**
  649. * @ingroup ge
  650. * @brief Get weight memory size from model file
  651. * @param [in] const std::string &path: Offline model file path
  652. * @param [out] size_t &mem_size Execution memory size
  653. size_t &weight_size Weight memory space size
  654. * @return SUCCESS handle successfully / others handle failed
  655. */
  656. Status GeExecutor::GetMemAndWeightSize(const std::string &path, size_t &mem_size, size_t &weight_size) {
  657. GELOGI("Get memory and weight size from file begin.");
  658. if (!isInit_) {
  659. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  660. return GE_EXEC_NOT_INIT;
  661. }
  662. ModelData model;
  663. std::string key;
  664. Status ret = ge::GraphLoader::LoadDataFromFile(path, key, 0, model);
  665. if ((ret != SUCCESS) || (model.model_data == nullptr)) {
  666. GELOGE(ret, "Load data from file failed. ret = %d", ret);
  667. return ret;
  668. }
  669. ret = ge::ModelManager::GetModelMemAndWeightSize(model, mem_size, weight_size);
  670. delete[] static_cast<char *>(model.model_data);
  671. model.model_data = nullptr;
  672. return ret;
  673. }
  674. /**
  675. * @ingroup ge
  676. * @brief Get weight memory size from model file
  677. * @param [in] const void *model_data Offline model buffer
  678. size_t model_size Offline model buffer length
  679. * @param [out] size_t &mem_size Execution memory size
  680. size_t &weight_size Weight memory space size
  681. * @return SUCCESS handle successfully / others handle failed
  682. */
  683. Status GeExecutor::GetMemAndWeightSize(const void *model_data, size_t model_size, size_t &mem_size,
  684. size_t &weight_size) {
  685. GELOGI("Get memory and weight size from data begin.");
  686. if (!isInit_) {
  687. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  688. return GE_EXEC_NOT_INIT;
  689. }
  690. if (model_data == nullptr) {
  691. GELOGE(PARAM_INVALID, "invalid model data!");
  692. return PARAM_INVALID;
  693. }
  694. ModelData model;
  695. model.model_data = const_cast<void *>(model_data);
  696. model.model_len = static_cast<uint32_t>(model_size);
  697. return ge::ModelManager::GetModelMemAndWeightSize(model, mem_size, weight_size);
  698. }
  699. Status GeExecutor::LoadSingleOp(const std::string &model_name, const ge::ModelData &modelData, void *stream,
  700. SingleOp **single_op) {
  701. return SingleOpManager::GetInstance().GetOpFromModel(model_name, modelData, stream, single_op);
  702. }
  703. Status GeExecutor::ExecuteAsync(SingleOp *executor, const std::vector<DataBuffer> &inputs,
  704. std::vector<DataBuffer> &outputs) {
  705. if (executor == nullptr) {
  706. GELOGE(PARAM_INVALID, "param is NULL");
  707. return PARAM_INVALID;
  708. }
  709. return executor->ExecuteAsync(inputs, outputs);
  710. }
  711. Status GeExecutor::ReleaseSingleOpResource(void *stream) {
  712. return SingleOpManager::GetInstance().ReleaseResource(stream);
  713. }
  714. Status GeExecutor::GetBatchInfoSize(uint32_t model_id, size_t &shape_count) {
  715. std::vector<std::vector<int64_t>> batch_info;
  716. Status ret = GetDynamicBatchInfo(model_id, batch_info);
  717. if (ret != SUCCESS) {
  718. GELOGE(ret, "Calc batch info size failed. ret = %d", ret);
  719. return ret;
  720. }
  721. if (batch_info.empty()) {
  722. shape_count = kStaticBatchInfoSize;
  723. } else {
  724. shape_count = batch_info.size();
  725. }
  726. return SUCCESS;
  727. }
  728. Status GeExecutor::GetOrigInputInfo(uint32_t model_id, uint32_t index, OriginInputInfo &orig_input_info) {
  729. GELOGI("Begin to GetOrigInputInfo.");
  730. if (!isInit_) {
  731. GELOGE(GE_EXEC_NOT_INIT, "not inited yet!");
  732. return GE_EXEC_NOT_INIT;
  733. }
  734. Status ret = GraphExecutor::GetOrigInputInfo(model_id, index, orig_input_info);
  735. if (ret != SUCCESS) {
  736. GELOGE(ret, "GetOrigInputInfo failed.");
  737. return ret;
  738. }
  739. GELOGI("GetOrigInputInfo succ.");
  740. return SUCCESS;
  741. }
  742. Status GeExecutor::GetAllAippInputOutputDims(uint32_t model_id, uint32_t index,
  743. std::vector<InputOutputDims> &input_dims,
  744. std::vector<InputOutputDims> &output_dims) {
  745. GELOGI("Begin to GetAllAippInputOutputDims.");
  746. if (!isInit_) {
  747. GELOGE(GE_EXEC_NOT_INIT, "not inited yet!");
  748. return GE_EXEC_NOT_INIT;
  749. }
  750. Status ret = GraphExecutor::GetAllAippInputOutputDims(model_id, index, input_dims, output_dims);
  751. if (ret != SUCCESS) {
  752. GELOGE(ret, "GetAllAippInputOutputDims failed.");
  753. return ret;
  754. }
  755. GELOGI("GetAllAippInputOutputDims succ.");
  756. return SUCCESS;
  757. }
  758. } // namespace ge

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