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 41 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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/dump/dump_manager.h"
  26. #include "common/util.h"
  27. #include "framework/common/debug/ge_log.h"
  28. #include "framework/common/util.h"
  29. #include "graph/execute/graph_execute.h"
  30. #include "graph/load/graph_loader.h"
  31. #include "graph/load/new_model_manager/davinci_model_parser.h"
  32. #include "graph/load/new_model_manager/model_manager.h"
  33. #include "graph/manager/graph_mem_allocator.h"
  34. #include "graph/model.h"
  35. #include "graph/utils/graph_utils.h"
  36. #include "mmpa/mmpa_api.h"
  37. #include "single_op/single_op_manager.h"
  38. #include "graph/manager/graph_var_manager.h"
  39. #include "graph/load/new_model_manager/davinci_model.h"
  40. #include "opskernel_manager/ops_kernel_builder_manager.h"
  41. using std::string;
  42. using std::vector;
  43. namespace {
  44. const size_t kDynamicBatchSizeVecSize = 1;
  45. const size_t kStaticBatchInfoSize = 1;
  46. const size_t kDynamicImageSizeVecSize = 2;
  47. const size_t kDynamicImageSizeInputSize = 2;
  48. const char *const kBatchLabel = "Batch_";
  49. ge::Status TransferDomiErrorCode(const uint32_t errorCode) {
  50. switch (errorCode) {
  51. case ge::PARAM_INVALID:
  52. case domi::PARAM_INVALID:
  53. return ge::PARAM_INVALID;
  54. case ge::INTERNAL_ERROR:
  55. case domi::INTERNAL_ERROR:
  56. return ge::INTERNAL_ERROR;
  57. default:
  58. return ge::FAILED;
  59. }
  60. }
  61. void GetGeTensorDescFromDomiInfo(std::vector<ge::TensorDesc> &ge_descs,
  62. const std::vector<ge::InputOutputDescInfo> &domi_descs,
  63. const std::vector<uint32_t> &formats) {
  64. uint32_t idx = 0;
  65. for (auto desc_item : domi_descs) {
  66. ge::TensorDesc ge_desc;
  67. ge_desc.SetName(desc_item.name);
  68. ge_desc.SetDataType(static_cast<ge::DataType>(desc_item.data_type));
  69. ge_desc.SetFormat(static_cast<ge::Format>(formats[idx]));
  70. std::vector<int64_t> shape_dims;
  71. for (auto dim : desc_item.shape_info.dims) {
  72. shape_dims.push_back(dim);
  73. }
  74. ge::Shape ge_shape(shape_dims);
  75. ge_desc.SetShape(ge_shape);
  76. ge_desc.SetSize(desc_item.size);
  77. ge_descs.emplace_back(ge_desc);
  78. ++idx;
  79. }
  80. }
  81. void GetDomiInputData(const ge::RunModelData &input_data, ge::InputData &inputs) {
  82. inputs.index = input_data.index;
  83. inputs.model_id = input_data.modelId;
  84. inputs.timestamp = input_data.timestamp;
  85. inputs.timeout = input_data.timeout;
  86. inputs.request_id = input_data.request_id;
  87. for (const auto &data_item : input_data.blobs) {
  88. ge::DataBuffer dataBuf{data_item.data, data_item.length, data_item.isDataSupportMemShare};
  89. inputs.blobs.emplace_back(dataBuf);
  90. }
  91. }
  92. void GetDomiOutputData(const ge::RunModelData &output_data, ge::OutputData &outputs) {
  93. outputs.index = output_data.index;
  94. outputs.model_id = output_data.modelId;
  95. for (const auto &data_item : output_data.blobs) {
  96. ge::DataBuffer dataBuf(data_item.data, data_item.length, data_item.isDataSupportMemShare);
  97. outputs.blobs.emplace_back(dataBuf);
  98. }
  99. }
  100. void SetDynamicInputDataFlag(const ge::RunModelData &input_data, const std::vector<std::vector<int64_t>> batch_info,
  101. ge::InputData &inputs) {
  102. inputs.is_dynamic_batch = true;
  103. std::string batch_label;
  104. size_t match_idx = 0;
  105. for (size_t i = 0; i < batch_info.size(); ++i) {
  106. // dynamic_dims
  107. if (input_data.dynamic_dims.size() != 0) {
  108. bool is_match = true;
  109. for (size_t j = 0; j < static_cast<size_t>(input_data.dynamic_dims.size()); ++j) {
  110. if (static_cast<uint64_t>(batch_info[i][j]) != input_data.dynamic_dims[j]) {
  111. is_match = false;
  112. break;
  113. }
  114. }
  115. if (is_match) {
  116. match_idx = i;
  117. break;
  118. }
  119. // dynamic_batch_size
  120. } else if (batch_info[i].size() == kDynamicBatchSizeVecSize &&
  121. batch_info[i][0] == static_cast<int64_t>(input_data.dynamic_batch_size)) {
  122. match_idx = i;
  123. break;
  124. // dynamic_image_size
  125. } else if (batch_info[i].size() == kDynamicImageSizeVecSize &&
  126. batch_info[i][0] == static_cast<int64_t>(input_data.dynamic_image_height) &&
  127. batch_info[i][1] == static_cast<int64_t>(input_data.dynamic_image_width)) {
  128. match_idx = i;
  129. break;
  130. }
  131. }
  132. batch_label = kBatchLabel + std::to_string(match_idx);
  133. inputs.batch_label = batch_label;
  134. GELOGI("current batch label:%s", batch_label.c_str());
  135. }
  136. bool IsDynamicBatchSizeMatchModel(uint64_t batch_size, const vector<std::vector<int64_t>> &batch_info) {
  137. if (batch_info.empty()) {
  138. GELOGE(ge::FAILED, "Dynamic batch info is empty.");
  139. return false;
  140. }
  141. for (auto batch : batch_info) {
  142. if (batch.size() != kDynamicBatchSizeVecSize) {
  143. GELOGE(ge::FAILED, "Dynamic batch param num is %zu, current batch size is %zu.", kDynamicBatchSizeVecSize,
  144. batch.size());
  145. return false;
  146. }
  147. if (batch[0] == static_cast<int64_t>(batch_size)) {
  148. return true;
  149. }
  150. }
  151. GELOGE(ge::FAILED, "Dynamic batch %lu can not match the gear of model.", batch_size);
  152. return false;
  153. }
  154. bool IsDynamicImageSizeMatchModel(uint64_t image_height, uint64_t image_width,
  155. const vector<std::vector<int64_t>> &batch_info) {
  156. if (batch_info.empty()) {
  157. GELOGE(ge::FAILED, "Dynamic batch info is empty.");
  158. return false;
  159. }
  160. for (auto resolution : batch_info) {
  161. if (resolution.size() != kDynamicImageSizeVecSize) {
  162. GELOGE(ge::FAILED, "Dynamic resolution param num is %zu, current resolution size is %zu.",
  163. kDynamicImageSizeVecSize, resolution.size());
  164. return false;
  165. }
  166. if (resolution[0] == static_cast<int64_t>(image_height) && resolution[1] == static_cast<int64_t>(image_width)) {
  167. return true;
  168. }
  169. }
  170. GELOGE(ge::FAILED, "Dynamic resolution (%lu,%lu) can not match the gear of model.", image_height, image_width);
  171. return false;
  172. }
  173. bool IsDynmaicDimsSizeMatchModel(const vector<uint64_t> cur_dynamic_dims, const vector<vector<int64_t>> &batch_info) {
  174. if (batch_info.empty()) {
  175. GELOGE(ge::FAILED, "Dynamic batch info is empty.");
  176. return false;
  177. }
  178. bool find_match = false;
  179. for (auto resolution : batch_info) {
  180. if (cur_dynamic_dims.size() != resolution.size()) {
  181. GELOGE(ge::FAILED, "Cur dynamic dims param num is %zu, current resolution size is %zu.", cur_dynamic_dims.size(),
  182. resolution.size());
  183. return false;
  184. }
  185. bool flag = true;
  186. for (std::size_t i = 0; i < resolution.size(); ++i) {
  187. if (cur_dynamic_dims[i] != static_cast<uint64_t>(resolution[i])) {
  188. flag = false;
  189. break;
  190. }
  191. }
  192. if (flag) {
  193. find_match = true;
  194. break;
  195. }
  196. }
  197. if (!find_match) {
  198. GELOGE(ge::FAILED, "choose dynamic dims can not match the gear of model.");
  199. }
  200. return find_match;
  201. }
  202. } // namespace
  203. namespace ge {
  204. bool GeExecutor::isInit_ = false;
  205. class ModelListenerAdapter : public ModelListener {
  206. public:
  207. domi::Status OnComputeDone(uint32_t model_id, uint32_t dataIndex, uint32_t resultCode,
  208. std::vector<ge::OutputTensorInfo> &outputs) {
  209. if (listener == nullptr) {
  210. GELOGE(ge::FAILED, "listener is null.");
  211. return FAILED;
  212. }
  213. return listener->OnComputeDone(model_id, dataIndex, resultCode, outputs);
  214. }
  215. std::shared_ptr<ge::ModelListener> listener;
  216. };
  217. GeExecutor::GeExecutor() {}
  218. Status GeExecutor::Initialize() {
  219. GELOGI("Init GeExecutor begin.");
  220. if (isInit_) {
  221. GELOGW("Already initialized, no need to be initialized again.");
  222. return ge::SUCCESS;
  223. }
  224. std::vector<rtMemType_t> mem_type(1, RT_MEMORY_HBM);
  225. mem_type.push_back(RT_MEMORY_P2P_DDR);
  226. auto ret = MemManager::Instance().Initialize(mem_type);
  227. if (ret != SUCCESS) {
  228. GELOGE(ret, "Memory Manager init failed.");
  229. return ret;
  230. }
  231. GE_CHK_STATUS_RET(OpsKernelBuilderManager::Instance().Initialize({}, false),
  232. "Failed to initialize OpsKernelBuilders");
  233. // Start profiling
  234. Options profiling_options;
  235. profiling_options.device_id = 0;
  236. profiling_options.job_id = "";
  237. ProfilingManager::Instance().Init(profiling_options);
  238. isInit_ = true;
  239. GELOGI("Init GeExecutor over.");
  240. return ge::SUCCESS;
  241. }
  242. Status GeExecutor::Finalize() {
  243. GELOGI("Uninit GeExecutor begin.");
  244. if (isInit_ == false) {
  245. GELOGW("GeExecutor has not been initialized.");
  246. return ge::SUCCESS;
  247. }
  248. (void)OpsKernelBuilderManager::Instance().Finalize();
  249. // Stop profiling
  250. if (ProfilingManager::Instance().ProfilingOn()) {
  251. ProfilingManager::Instance().StopProfiling();
  252. ProfilingManager::Instance().PluginUnInit(GE_PROFILING_MODULE);
  253. }
  254. GELOGI("Uninit GeExecutor over.");
  255. return ge::SUCCESS;
  256. }
  257. Status GeExecutor::SetDynamicBatchSize(uint32_t model_id, void *dynamic_input_addr, uint64_t length,
  258. uint64_t batch_size) {
  259. if (dynamic_input_addr == nullptr) {
  260. GELOGE(PARAM_INVALID, "Dynamic input addr is nullptr!");
  261. return PARAM_INVALID;
  262. }
  263. uint64_t size = sizeof(uint32_t);
  264. if (length < size) {
  265. GELOGE(PARAM_INVALID, "Dynamic input size [%lu] is less than [%lu]!", length, size);
  266. return PARAM_INVALID;
  267. }
  268. if (length >= sizeof(uint64_t)) {
  269. size = sizeof(uint64_t);
  270. }
  271. // Verify whether the input dynamic batch matches the model gear
  272. std::vector<std::vector<int64_t>> batch_info;
  273. std::vector<uint64_t> batch_num{batch_size};
  274. int32_t dynamic_type = static_cast<int32_t>(FIXED);
  275. Status ret = GraphExecutor::GetDynamicBatchInfo(model_id, batch_info, dynamic_type);
  276. if (ret != SUCCESS) {
  277. GELOGE(ret, "Get dynamic input info failed.");
  278. return ret;
  279. }
  280. if (!IsDynamicBatchSizeMatchModel(batch_size, batch_info)) {
  281. GELOGE(PARAM_INVALID, "The current dynamic input does not match the gear of the model.");
  282. return PARAM_INVALID;
  283. }
  284. ret = GraphExecutor::SetDynamicSize(model_id, batch_num, static_cast<int32_t>(DYNAMIC_BATCH));
  285. if (ret != SUCCESS) {
  286. GELOGE(ret, "Set dynamic size failed");
  287. return ret;
  288. }
  289. // memcpy dynamic_batch_size from host to device
  290. rtError_t rt_ret = rtMemcpy(dynamic_input_addr, length, &batch_size, size, RT_MEMCPY_HOST_TO_DEVICE);
  291. if (rt_ret != RT_ERROR_NONE) {
  292. GELOGE(RT_FAILED, "memcpy dynamic batch input data failed! ret: 0x%X", rt_ret);
  293. return RT_ERROR_TO_GE_STATUS(rt_ret);
  294. }
  295. return SUCCESS;
  296. }
  297. Status GeExecutor::SetDynamicImageSize(uint32_t model_id, void *dynamic_input_addr, uint64_t length,
  298. uint64_t image_height, uint64_t image_width) {
  299. if (dynamic_input_addr == nullptr) {
  300. GELOGE(PARAM_INVALID, "Dynamic input addr is nullptr!");
  301. return PARAM_INVALID;
  302. }
  303. uint64_t dynamic_input_size = kDynamicImageSizeInputSize * sizeof(uint32_t);
  304. if (length < dynamic_input_size) {
  305. GELOGE(PARAM_INVALID, "Dynamic input size [%lu] is less than [%lu]!", length, dynamic_input_size);
  306. return PARAM_INVALID;
  307. }
  308. uint64_t size = sizeof(uint32_t);
  309. if (length >= kDynamicImageSizeInputSize * sizeof(uint64_t)) {
  310. size = sizeof(uint64_t);
  311. }
  312. // Verify whether the input dynamic resolution matches the model gear
  313. std::vector<std::vector<int64_t>> batch_info;
  314. std::vector<uint64_t> batch_num{image_height, image_width};
  315. int32_t dynamic_type = static_cast<int32_t>(FIXED);
  316. Status ret = GraphExecutor::GetDynamicBatchInfo(model_id, batch_info, dynamic_type);
  317. if (ret != SUCCESS) {
  318. GELOGE(ret, "Get dynamic input info failed.");
  319. return ret;
  320. }
  321. if (!IsDynamicImageSizeMatchModel(image_height, image_width, batch_info)) {
  322. GELOGE(PARAM_INVALID, "The current dynamic input does not match the gear of the model.");
  323. return PARAM_INVALID;
  324. }
  325. ret = GraphExecutor::SetDynamicSize(model_id, batch_num, static_cast<int32_t>(DYNAMIC_IMAGE));
  326. if (ret != SUCCESS) {
  327. GELOGE(ret, "Set dynamic size failed");
  328. return ret;
  329. }
  330. // Memcpy dynamic resolution height from host to device
  331. rtError_t rt_ret = rtMemcpy(dynamic_input_addr, size, &image_height, size, RT_MEMCPY_HOST_TO_DEVICE);
  332. if (rt_ret != RT_ERROR_NONE) {
  333. GELOGE(RT_FAILED, "memcpy dynamic resolution input data failed! ret: 0x%X", rt_ret);
  334. return RT_ERROR_TO_GE_STATUS(rt_ret);
  335. }
  336. uint64_t remain_size = length - size;
  337. // Memcpy dynamic resolution width from host to device
  338. if (rtMemcpy(reinterpret_cast<void *>(reinterpret_cast<uint8_t *>(dynamic_input_addr) + size), remain_size,
  339. &image_width, size, RT_MEMCPY_HOST_TO_DEVICE) != RT_ERROR_NONE) {
  340. GELOGE(FAILED, "memcpy dynamic resolution input data failed!");
  341. return FAILED;
  342. }
  343. return SUCCESS;
  344. }
  345. Status GeExecutor::SetDynamicDims(uint32_t model_id, void *dynamic_input_addr, uint64_t length,
  346. const vector<uint64_t> &dynamic_dims) {
  347. if (dynamic_input_addr == nullptr) {
  348. GELOGE(FAILED, "Dynamic input addr is nullptr!");
  349. return FAILED;
  350. }
  351. vector<uint64_t> cur_dynamic_dims;
  352. Status ret = GetCurDynamicDims(model_id, dynamic_dims, cur_dynamic_dims);
  353. if (ret != SUCCESS) {
  354. GELOGE(FAILED, "Set cur gear dynamic dims failed");
  355. return FAILED;
  356. }
  357. std::vector<std::vector<int64_t>> batch_info;
  358. int32_t dynamic_type = static_cast<int32_t>(FIXED);
  359. ret = GraphExecutor::GetDynamicBatchInfo(model_id, batch_info, dynamic_type);
  360. if (ret != SUCCESS) {
  361. GELOGE(ret, "Get dynamic input info failed.");
  362. return ret;
  363. }
  364. if (!IsDynmaicDimsSizeMatchModel(cur_dynamic_dims, batch_info)) {
  365. GELOGE(PARAM_INVALID, "The current dynamic input does not match the gear of the model.");
  366. return PARAM_INVALID;
  367. }
  368. ret = GraphExecutor::SetDynamicSize(model_id, cur_dynamic_dims, static_cast<int32_t>(DYNAMIC_DIMS));
  369. if (ret != SUCCESS) {
  370. GELOGE(FAILED, "Set dynamic size failed");
  371. return FAILED;
  372. }
  373. size_t dynamic_dim_num = cur_dynamic_dims.size();
  374. uint64_t dynamic_input_size = static_cast<uint64_t>(dynamic_dim_num * sizeof(uint32_t));
  375. if (length < dynamic_input_size) {
  376. GELOGE(FAILED, "Dynamic input size [%lu] is less than [%lu]!", length, dynamic_input_size);
  377. return FAILED;
  378. }
  379. uint64_t size = sizeof(uint32_t);
  380. if (length >= dynamic_dim_num * sizeof(uint64_t)) {
  381. size = sizeof(uint64_t);
  382. }
  383. for (uint32_t i = 0; i < dynamic_dim_num; ++i) {
  384. // Memcpy dynamic dim[i] from host to device
  385. if (rtMemcpy(reinterpret_cast<void *>(reinterpret_cast<uint8_t *>(dynamic_input_addr) + size * i),
  386. length - size * i, &cur_dynamic_dims[i], size, RT_MEMCPY_HOST_TO_DEVICE) != RT_ERROR_NONE) {
  387. GELOGE(FAILED, "memcpy dynamic resolution input data failed!");
  388. return FAILED;
  389. }
  390. }
  391. return SUCCESS;
  392. }
  393. Status GeExecutor::GetCurDynamicDims(uint32_t model_id, const vector<uint64_t> &dynamic_dims,
  394. vector<uint64_t> &cur_dynamic_dims) {
  395. cur_dynamic_dims.clear();
  396. vector<ge::TensorDesc> input_desc;
  397. vector<ge::TensorDesc> output_desc;
  398. auto ret = GetModelDescInfo(model_id, input_desc, output_desc);
  399. if (ret != ge::SUCCESS) {
  400. GELOGE(FAILED, "GetModelDescInfo failed.");
  401. return FAILED;
  402. }
  403. vector<string> user_designate_shape_order;
  404. vector<int64_t> all_data_dims;
  405. ret = GetUserDesignateShapeOrder(model_id, user_designate_shape_order);
  406. if (ret != ge::SUCCESS) {
  407. GELOGE(FAILED, "GetUserDesignateShapeOrder failed.");
  408. return FAILED;
  409. }
  410. for (auto &data_name : user_designate_shape_order) {
  411. for (auto &desc : input_desc) {
  412. if (desc.GetName() == data_name) {
  413. for (auto dim : desc.GetShape().GetDims()) {
  414. all_data_dims.push_back(dim);
  415. }
  416. break;
  417. }
  418. }
  419. }
  420. if (dynamic_dims.size() != all_data_dims.size()) {
  421. GELOGE(FAILED, "Dynamic input size [%lu] is not equal with all data dims size [%lu]!", dynamic_dims.size(),
  422. all_data_dims.size());
  423. return FAILED;
  424. }
  425. for (std::size_t i = 0; i < all_data_dims.size(); ++i) {
  426. if (all_data_dims[i] < 0) {
  427. cur_dynamic_dims.push_back(dynamic_dims[i]);
  428. } else if (static_cast<uint64_t>(all_data_dims[i]) != dynamic_dims[i]) {
  429. GELOGE(PARAM_INVALID, "Static dims should be same, index: %zu value: %d should be %d", i, dynamic_dims[i],
  430. all_data_dims[i]);
  431. return PARAM_INVALID;
  432. }
  433. }
  434. return SUCCESS;
  435. }
  436. Status GeExecutor::GetCurShape(const uint32_t model_id, std::vector<int64_t> &batch_info, int32_t &dynamic_type) {
  437. GELOGI("Begin to get current shape");
  438. if (!isInit_) {
  439. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  440. return GE_EXEC_NOT_INIT;
  441. }
  442. Status ret = GraphExecutor::GetCurShape(model_id, batch_info, dynamic_type);
  443. if (ret != SUCCESS) {
  444. GELOGE(ret, "Get current shape failed");
  445. return ret;
  446. }
  447. return SUCCESS;
  448. }
  449. Status GeExecutor::SetDynamicAippData(uint32_t model_id, void *dynamic_input_addr, uint64_t length,
  450. const std::vector<kAippDynamicBatchPara> &aippBatchPara,
  451. const kAippDynamicPara &aippParms) {
  452. GELOGI("Enter to SetDynamicAippData.");
  453. if (dynamic_input_addr == nullptr) {
  454. GELOGE(PARAM_INVALID, "Dynamic aipp input addr is nullptr!");
  455. return PARAM_INVALID;
  456. }
  457. if (aippBatchPara.empty()) {
  458. GELOGE(PARAM_INVALID, "aippBatchPara is empty.");
  459. return PARAM_INVALID;
  460. }
  461. uint64_t batch_num = aippBatchPara.size();
  462. uint64_t real_aippParms_size = sizeof(kAippDynamicPara) - sizeof(kAippDynamicBatchPara);
  463. uint64_t struct_len = batch_num * sizeof(kAippDynamicBatchPara) + real_aippParms_size;
  464. GELOGI(
  465. "Get acl input dynamic aipp data, model_id is %u, length is %lu,"
  466. "batch num is %lu, struct_len is %lu",
  467. model_id, length, batch_num, struct_len);
  468. if (struct_len > length) {
  469. GELOGE(PARAM_INVALID, "input dynamic aipp param len [%lu] is larger than aipp_data size [%lu]", struct_len, length);
  470. return PARAM_INVALID;
  471. }
  472. // Memcpy real kAippDynamicBatchPara from host to device
  473. rtError_t rt_ret = rtMemcpy(dynamic_input_addr, length, &aippParms, real_aippParms_size, RT_MEMCPY_HOST_TO_DEVICE);
  474. if (rt_ret != RT_ERROR_NONE) {
  475. GELOGE(RT_FAILED, "memcpy real_aippParms_size failed! ret: 0x%X", rt_ret);
  476. return RT_ERROR_TO_GE_STATUS(rt_ret);
  477. }
  478. uint64_t remain_len = length - real_aippParms_size;
  479. uint8_t *aipp_batch_para_dev = reinterpret_cast<uint8_t *>(dynamic_input_addr) + real_aippParms_size;
  480. for (uint64_t i = 0; i < batch_num; ++i) {
  481. rt_ret = rtMemcpy(reinterpret_cast<void *>(aipp_batch_para_dev + i * sizeof(kAippDynamicBatchPara)),
  482. (remain_len - i * sizeof(kAippDynamicBatchPara)), &(aippBatchPara[i]),
  483. sizeof(kAippDynamicBatchPara), RT_MEMCPY_HOST_TO_DEVICE);
  484. if (rt_ret != RT_ERROR_NONE) {
  485. GELOGE(RT_FAILED, "memcpy kAippDynamicBatchPara input data failed! ret: 0x%X", rt_ret);
  486. return RT_ERROR_TO_GE_STATUS(rt_ret);
  487. }
  488. }
  489. return SUCCESS;
  490. }
  491. // Load model
  492. Status GeExecutor::LoadModelOffline(uint32_t &model_id, const std::string &path, const std::string &key,
  493. int32_t priority, std::shared_ptr<ge::ModelListener> listener) {
  494. GELOGI("load model offline begin.");
  495. if (!isInit_) {
  496. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  497. return GE_EXEC_NOT_INIT;
  498. }
  499. string filePath = RealPath(path.c_str());
  500. if (filePath.empty()) {
  501. GELOGE(ge::FAILED, "File path is invalid. please check your text file '%s'.", path.c_str());
  502. return ge::FAILED;
  503. }
  504. std::shared_ptr<ModelListenerAdapter> listener_adapter = MakeShared<ModelListenerAdapter>();
  505. if (listener_adapter == nullptr) {
  506. GELOGE(MEMALLOC_FAILED, "ModelListenerAdapter make shared failed!");
  507. return ge::FAILED;
  508. }
  509. listener_adapter->listener = listener;
  510. Status ret = GraphLoader::LoadModelFromFile(path, key, priority, listener_adapter, model_id);
  511. if (ret != SUCCESS) {
  512. GELOGE(ret, "[GeExecutor] LoadModelFromFile failed");
  513. return TransferDomiErrorCode(ret);
  514. }
  515. return SUCCESS;
  516. }
  517. Status GeExecutor::LoadModel(uint32_t &model_id, const ModelData &model_data,
  518. std::shared_ptr<ge::ModelListener> listener) {
  519. GELOGI("Load model begin.");
  520. if (!isInit_) {
  521. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  522. return GE_EXEC_NOT_INIT;
  523. }
  524. std::shared_ptr<ModelListenerAdapter> listener_adapter = MakeShared<ModelListenerAdapter>();
  525. if (listener_adapter == nullptr) {
  526. GELOGE(MEMALLOC_FAILED, "ModelListenerAdapter make shared failed!");
  527. return ge::FAILED;
  528. }
  529. listener_adapter->listener = listener;
  530. Status ret = GraphLoader::LoadModel(model_data, listener_adapter, model_id);
  531. if (ret != SUCCESS) {
  532. GELOGE(ret, "[GeExecutor] LoadModel failed.");
  533. return TransferDomiErrorCode(ret);
  534. }
  535. return ret;
  536. }
  537. Status GeExecutor::UnloadModel(uint32_t model_id) {
  538. GELOGI("unload model %u begin.", model_id);
  539. if (!isInit_) {
  540. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  541. return GE_EXEC_NOT_INIT;
  542. }
  543. Status ret = GraphLoader::DestroyAicpuSessionForInfer(model_id);
  544. if (ret != SUCCESS) {
  545. GELOGE(ret, "[GraphLoader] DestroyAicpuSessionForInfer failed. model id: %u", model_id);
  546. return FAILED;
  547. }
  548. std::shared_ptr<DavinciModel> davinci_model = ModelManager::GetInstance()->GetModel(model_id);
  549. if (davinci_model != nullptr) {
  550. uint64_t session_id = davinci_model->GetSessionId();
  551. VarManagerPool::Instance().RemoveVarManager(session_id);
  552. }
  553. return GraphLoader::UnloadModel(model_id);
  554. }
  555. Status GeExecutor::RunModel(const ge::RunModelData &input_data, ge::RunModelData &output_data) {
  556. GELOGI("run model begin.");
  557. if (!isInit_) {
  558. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  559. return GE_EXEC_NOT_INIT;
  560. }
  561. InputData inputs;
  562. GetDomiInputData(input_data, inputs);
  563. OutputData outputs;
  564. GetDomiOutputData(output_data, outputs);
  565. return GraphExecutor::DataInput(inputs, outputs);
  566. }
  567. // Get input and output descriptor
  568. Status GeExecutor::GetModelDescInfo(uint32_t model_id, std::vector<ge::TensorDesc> &input_desc,
  569. std::vector<ge::TensorDesc> &output_desc, bool new_model_desc) {
  570. GELOGI("get model desc info begin.");
  571. if (!isInit_) {
  572. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  573. return GE_EXEC_NOT_INIT;
  574. }
  575. std::vector<InputOutputDescInfo> input_desc_infos;
  576. std::vector<InputOutputDescInfo> output_desc_infos;
  577. std::vector<uint32_t> input_formats;
  578. std::vector<uint32_t> output_formats;
  579. Status ret = GraphExecutor::GetInputOutputDescInfo(model_id, input_desc_infos, output_desc_infos, input_formats,
  580. output_formats, new_model_desc);
  581. if (ret != domi::SUCCESS) {
  582. GELOGE(ret, "GetInputOutputDescInfo failed. ret = %u", ret);
  583. return ret;
  584. }
  585. if (input_formats.size() != input_desc_infos.size()) {
  586. GELOGE(ge::PARAM_INVALID, "input_formats size %zu is not equal to input_desc_infos size %zu.", input_formats.size(),
  587. input_desc_infos.size());
  588. return ge::PARAM_INVALID;
  589. }
  590. if (output_formats.size() != output_desc_infos.size()) {
  591. GELOGE(ge::PARAM_INVALID, "output_formats size %zu is not equal to output_desc_infos size %zu.",
  592. output_formats.size(), output_desc_infos.size());
  593. return ge::PARAM_INVALID;
  594. }
  595. // Transfer data to TensorDesc
  596. GetGeTensorDescFromDomiInfo(input_desc, input_desc_infos, input_formats);
  597. GetGeTensorDescFromDomiInfo(output_desc, output_desc_infos, output_formats);
  598. GELOGI("get model desc info end.");
  599. return ge::SUCCESS;
  600. }
  601. ///
  602. /// @ingroup ge
  603. /// @brief Get dynamic batch_info
  604. /// @param [in] model_id
  605. /// @param [out] batch_info
  606. /// @param [out] dynamic_type
  607. /// @return execute result
  608. ///
  609. Status GeExecutor::GetDynamicBatchInfo(uint32_t model_id, std::vector<std::vector<int64_t>> &batch_info,
  610. int32_t &dynamic_type) {
  611. GELOGI("Begin to get dynamic batch info.");
  612. if (!isInit_) {
  613. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  614. return GE_EXEC_NOT_INIT;
  615. }
  616. Status ret = GraphExecutor::GetDynamicBatchInfo(model_id, batch_info, dynamic_type);
  617. if (ret != SUCCESS) {
  618. GELOGE(ret, "GetDynamicBatchInfo failed.");
  619. return ret;
  620. }
  621. GELOGI("Get dynamic batch info succ.");
  622. return SUCCESS;
  623. }
  624. ///
  625. /// @ingroup ge
  626. /// @brief Get combined dynamic dims info
  627. /// @param [in] model_id
  628. /// @param [out] batch_info
  629. /// @return execute result
  630. ///
  631. Status GeExecutor::GetCombinedDynamicDims(uint32_t model_id, vector<vector<int64_t>> &batch_info) {
  632. GELOGI("Begin to get combined dynamic dims info.");
  633. if (!isInit_) {
  634. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  635. return GE_EXEC_NOT_INIT;
  636. }
  637. Status ret = GraphExecutor::GetCombinedDynamicDims(model_id, batch_info);
  638. if (ret != SUCCESS) {
  639. GELOGE(ret, "GetCombinedDynamicDims failed.");
  640. return ret;
  641. }
  642. GELOGI("Get combined dynamic dims succ.");
  643. return SUCCESS;
  644. }
  645. ///
  646. /// @ingroup ge
  647. /// @brief Get user designeate shape order
  648. /// @param [in] model_id
  649. /// @param [out] user_designate_shape_order
  650. /// @return execute result
  651. ///
  652. Status GeExecutor::GetUserDesignateShapeOrder(uint32_t model_id, vector<string> &user_designate_shape_order) {
  653. GELOGI("Begin to get user designate shape info.");
  654. if (!isInit_) {
  655. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  656. return GE_EXEC_NOT_INIT;
  657. }
  658. Status ret = GraphExecutor::GetUserDesignateShapeOrder(model_id, user_designate_shape_order);
  659. if (ret != SUCCESS) {
  660. GELOGE(ret, "GetUserDesignateShapeOrder failed.");
  661. return ret;
  662. }
  663. GELOGI("Get user designate shape order succ.");
  664. return SUCCESS;
  665. }
  666. ///
  667. /// @ingroup ge
  668. /// @brief Get AIPP input format
  669. /// @param [in] model_id
  670. /// @param [in] index
  671. /// @param [out] input_format
  672. /// @return execute result
  673. ///
  674. Status GeExecutor::GetAIPPInfo(uint32_t model_id, uint32_t index, AippConfigInfo &aipp_info) {
  675. GELOGI("Begin to GetAIPPInfo.");
  676. if (!isInit_) {
  677. GELOGE(GE_EXEC_NOT_INIT, "not inited yet!");
  678. return GE_EXEC_NOT_INIT;
  679. }
  680. Status ret = GraphExecutor::GetAIPPInfo(model_id, index, aipp_info);
  681. if (ret != SUCCESS) {
  682. GELOGW("GetAIPPInfo is not success.");
  683. return ret;
  684. }
  685. GELOGI("GetAIPPInfo succ.");
  686. return SUCCESS;
  687. }
  688. Status GeExecutor::GetAippType(uint32_t model_id, uint32_t index, InputAippType &type, size_t &aipp_index) {
  689. GELOGI("Begin to get aipp type.");
  690. if (!isInit_) {
  691. GELOGE(GE_EXEC_NOT_INIT, "not inited yet!");
  692. return GE_EXEC_NOT_INIT;
  693. }
  694. Status ret = GraphExecutor::GetAippType(model_id, index, type, aipp_index);
  695. if (ret != SUCCESS) {
  696. GELOGW("Get aipp type is not success.");
  697. return ret;
  698. }
  699. GELOGI("Get aipp type success.");
  700. return SUCCESS;
  701. }
  702. Status GeExecutor::GetModelAttr(uint32_t model_id, std::vector<std::string> &dynamic_output_shape_info) {
  703. GELOGI("Begin to get dynamic batch output shape info");
  704. if (!isInit_) {
  705. GELOGE(GE_EXEC_NOT_INIT, "not inited yet!");
  706. return GE_EXEC_NOT_INIT;
  707. }
  708. Status ret = GraphExecutor::GetModelAttr(model_id, dynamic_output_shape_info);
  709. if (ret != SUCCESS) {
  710. GELOGE(ret, "Get dynamic batch output shape info failed.");
  711. return ret;
  712. }
  713. GELOGI("Get dynamic batch output shape info succ.");
  714. return SUCCESS;
  715. }
  716. Status GeExecutor::GetModelDescInfoForZeroCopy(uint32_t model_id, std::vector<ge::TensorDesc> &input_desc,
  717. std::vector<TensorDesc> &output_desc) {
  718. GELOGI("get model desc info for zero copy begin.");
  719. if (!isInit_) {
  720. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  721. return GE_EXEC_NOT_INIT;
  722. }
  723. std::vector<InputOutputDescInfo> input_desc_infos;
  724. std::vector<InputOutputDescInfo> output_desc_infos;
  725. std::vector<uint32_t> input_formats;
  726. std::vector<uint32_t> output_formats;
  727. Status ret = GraphExecutor::GetInputOutputDescInfoForZeroCopy(model_id, input_desc_infos, output_desc_infos,
  728. input_formats, output_formats);
  729. if (ret != domi::SUCCESS) {
  730. GELOGE(ret, "Get DescInfo from zero copy failed. ret = %u", ret);
  731. return TransferDomiErrorCode(ret);
  732. }
  733. if (input_formats.size() != input_desc_infos.size()) {
  734. GELOGE(ge::FAILED, "input_formats.size() != input_desc_infos.size().");
  735. return ge::FAILED;
  736. }
  737. if (output_formats.size() != output_desc_infos.size()) {
  738. GELOGE(ge::FAILED, "output_formats.size() != output_desc_infos.size().");
  739. return ge::FAILED;
  740. }
  741. GetGeTensorDescFromDomiInfo(input_desc, input_desc_infos, input_formats);
  742. GetGeTensorDescFromDomiInfo(output_desc, output_desc_infos, output_formats);
  743. GELOGI("get model desc info from zero copy end.");
  744. return ge::SUCCESS;
  745. }
  746. Status GeExecutor::CommandHandle(const Command &command) {
  747. GELOGI("command handle begin.");
  748. Status ret = GraphLoader::CommandHandle(command);
  749. if (ret != SUCCESS) {
  750. GELOGE(ret, "CommandHandle: Command Handle failed.");
  751. return TransferDomiErrorCode(ret);
  752. }
  753. return SUCCESS;
  754. }
  755. Status GeExecutor::GetMaxUsedMemory(uint32_t model_id, uint32_t &max_size) {
  756. GELOGI("Get max used memory begin.");
  757. if (!isInit_) {
  758. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  759. return GE_EXEC_NOT_INIT;
  760. }
  761. uint64_t max_mem_size = 0;
  762. Status ret = GraphLoader::GetMaxUsedMemory(model_id, max_mem_size);
  763. max_size = static_cast<uint32_t>(max_mem_size);
  764. return ret;
  765. }
  766. /**
  767. * @ingroup ge
  768. * @brief Load data from model file to memory
  769. * @param [in] const std::string &path: Offline model file path
  770. * @param [out] domi::ModelData &model_data: Offline model memory data
  771. * @return SUCCESS handle successfully / others handle failed
  772. */
  773. Status GeExecutor::LoadDataFromFile(const std::string &path, ModelData &model_data) {
  774. GELOGI("Load data from file begin.");
  775. if (!isInit_) {
  776. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  777. return GE_EXEC_NOT_INIT;
  778. }
  779. string filePath = RealPath(path.c_str());
  780. if (filePath.empty()) {
  781. GELOGE(GE_EXEC_MODEL_PATH_INVALID, "File path is invalid. please check your text file '%s'.", path.c_str());
  782. return GE_EXEC_MODEL_PATH_INVALID;
  783. }
  784. GELOGI("load modelData from file: %s.", path.c_str());
  785. std::string key_path;
  786. int32_t priority = 0;
  787. Status ret = GraphLoader::LoadDataFromFile(path, key_path, priority, model_data);
  788. if (ret != SUCCESS) {
  789. if (model_data.model_data != nullptr) {
  790. delete[] static_cast<char *>(model_data.model_data);
  791. model_data.model_data = nullptr;
  792. }
  793. }
  794. return ret;
  795. }
  796. /**
  797. * @ingroup ge
  798. * @brief Load model from offline model memory data
  799. * @param [in] domi::ModelData &model_data: Offline model data
  800. void *dev_ptr: Input/Output memory start address
  801. size_t memsize: Input/Output memory length
  802. void *weight_ptr: Weight memory start address
  803. size_t weightsize: Weight memory length
  804. * @param [out] uint32_t &model_id: identification after model loading
  805. * @return SUCCESS handle successfully / others handle failed
  806. */
  807. Status GeExecutor::LoadModelFromData(uint32_t &model_id, const ModelData &model_data, void *dev_ptr, size_t mem_size,
  808. void *weight_ptr, size_t weight_size) {
  809. GELOGI("Load model from data begin.");
  810. if (!isInit_) {
  811. GELOGE(GE_EXEC_NOT_INIT, "not inited yet!");
  812. return GE_EXEC_NOT_INIT;
  813. }
  814. return GraphLoader::LoadModelFromData(model_id, model_data, dev_ptr, mem_size, weight_ptr, weight_size);
  815. }
  816. /**
  817. * @ingroup ge
  818. * @brief Load task list from ModelData with queue.
  819. * @param [out] model_id: model id allocate from manager.
  820. * @param [in] ge_model_data: Model data load from offline model.
  821. * @param [in] input_queue_ids: input queue ids create from user.
  822. * @param [in] output_queue_ids: input queue ids create from user.
  823. * @return: 0 for success / others for fail
  824. */
  825. Status GeExecutor::LoadModelWithQ(uint32_t &model_id, const ModelData &model_data,
  826. const std::vector<uint32_t> &input_queue_ids,
  827. const std::vector<uint32_t> &output_queue_ids) {
  828. GELOGI("Load model with queue begin.");
  829. if (!isInit_) {
  830. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  831. return GE_EXEC_NOT_INIT;
  832. }
  833. return GraphLoader::LoadModelWithQ(model_id, model_data, input_queue_ids, output_queue_ids);
  834. }
  835. /**
  836. * @ingroup ge
  837. * @brief Synchronous execution of offline model(Do not create thread)
  838. * @param [in] uint32_t model_id: Model ID to execute
  839. void* stream: stream to execute
  840. const domi::InputData *input_data: Model input data
  841. bool async_mode: is asynchronize mode.
  842. * @param [out] domi::OutputData *output_data: Model output data
  843. * @return SUCCESS handle successfully / others handle failed
  844. */
  845. Status GeExecutor::ExecModel(uint32_t model_id, void *stream, const ge::RunModelData &run_input_data,
  846. ge::RunModelData &run_output_data, bool async_mode) {
  847. GELOGI("Execute model begin.");
  848. if (!isInit_) {
  849. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  850. return GE_EXEC_NOT_INIT;
  851. }
  852. InputData input_data;
  853. OutputData output_data;
  854. GetDomiInputData(run_input_data, input_data);
  855. GetDomiOutputData(run_output_data, output_data);
  856. if ((run_input_data.dynamic_batch_size != 0) || (run_input_data.dynamic_image_width != 0) ||
  857. (run_input_data.dynamic_image_height != 0) || (run_input_data.dynamic_dims.size() != 0)) {
  858. std::vector<std::vector<int64_t>> batch_info;
  859. int32_t dynamic_type = static_cast<int32_t>(FIXED);
  860. Status ret = GraphExecutor::GetDynamicBatchInfo(model_id, batch_info, dynamic_type);
  861. if (ret != SUCCESS) {
  862. GELOGE(ret, "Get dynamic input info failed.");
  863. return ret;
  864. }
  865. if (!batch_info.empty()) {
  866. SetDynamicInputDataFlag(run_input_data, batch_info, input_data);
  867. }
  868. }
  869. return GraphLoader::ExecuteModel(model_id, stream, async_mode, input_data, output_data);
  870. }
  871. /**
  872. * @ingroup ge
  873. * @brief Get weight memory size from model file
  874. * @param [in] const std::string &path: Offline model file path
  875. * @param [out] size_t &mem_size Execution memory size
  876. size_t &weight_size Weight memory space size
  877. * @return SUCCESS handle successfully / others handle failed
  878. */
  879. Status GeExecutor::GetMemAndWeightSize(const std::string &path, size_t &mem_size, size_t &weight_size) {
  880. GELOGI("Get memory and weight size from file begin.");
  881. if (!isInit_) {
  882. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  883. return GE_EXEC_NOT_INIT;
  884. }
  885. ModelData model;
  886. std::string key;
  887. Status ret = ge::GraphLoader::LoadDataFromFile(path, key, 0, model);
  888. if ((ret != SUCCESS) || (model.model_data == nullptr)) {
  889. GELOGE(ret, "Load data from file failed. ret = %d", ret);
  890. return ret;
  891. }
  892. ret = ge::ModelManager::GetModelMemAndWeightSize(model, mem_size, weight_size);
  893. delete[] static_cast<char *>(model.model_data);
  894. model.model_data = nullptr;
  895. return ret;
  896. }
  897. /**
  898. * @ingroup ge
  899. * @brief Get weight memory size from model file
  900. * @param [in] const void *model_data Offline model buffer
  901. size_t model_size Offline model buffer length
  902. * @param [out] size_t &mem_size Execution memory size
  903. size_t &weight_size Weight memory space size
  904. * @return SUCCESS handle successfully / others handle failed
  905. */
  906. Status GeExecutor::GetMemAndWeightSize(const void *model_data, size_t model_size, size_t &mem_size,
  907. size_t &weight_size) {
  908. GELOGI("Get memory and weight size from data begin.");
  909. if (!isInit_) {
  910. GELOGE(GE_EXEC_NOT_INIT, "GeExecutor has not been initialized!");
  911. return GE_EXEC_NOT_INIT;
  912. }
  913. if (model_data == nullptr) {
  914. GELOGE(PARAM_INVALID, "invalid model data!");
  915. return PARAM_INVALID;
  916. }
  917. ModelData model;
  918. model.model_data = const_cast<void *>(model_data);
  919. model.model_len = static_cast<uint32_t>(model_size);
  920. return ge::ModelManager::GetModelMemAndWeightSize(model, mem_size, weight_size);
  921. }
  922. Status GeExecutor::LoadSingleOp(const std::string &model_name, const ge::ModelData &modelData, void *stream,
  923. SingleOp **single_op) {
  924. return SingleOpManager::GetInstance().GetOpFromModel(model_name, modelData, stream, single_op);
  925. }
  926. Status GeExecutor::LoadDynamicSingleOp(const std::string &model_name, const ge::ModelData &modelData, void *stream,
  927. DynamicSingleOp **single_op) {
  928. return SingleOpManager::GetInstance().GetDynamicOpFromModel(model_name, modelData, stream, single_op);
  929. }
  930. Status GeExecutor::ExecuteAsync(SingleOp *executor, const std::vector<DataBuffer> &inputs,
  931. std::vector<DataBuffer> &outputs) {
  932. if (executor == nullptr) {
  933. GELOGE(PARAM_INVALID, "param is NULL");
  934. return PARAM_INVALID;
  935. }
  936. return executor->ExecuteAsync(inputs, outputs);
  937. }
  938. ge::Status GeExecutor::ExecuteAsync(DynamicSingleOp *executor, const vector<GeTensorDesc> &input_desc,
  939. const vector<DataBuffer> &inputs, vector<GeTensorDesc> &output_desc,
  940. vector<DataBuffer> &outputs) {
  941. GE_CHECK_NOTNULL(executor);
  942. return executor->ExecuteAsync(input_desc, inputs, output_desc, outputs);
  943. }
  944. Status GeExecutor::ReleaseSingleOpResource(void *stream) {
  945. ModelManager::GetInstance()->ClearAicpuSo();
  946. return SingleOpManager::GetInstance().ReleaseResource(stream);
  947. }
  948. Status GeExecutor::GetDeviceIdByModelId(uint32_t model_id, uint32_t &device_id) {
  949. auto model_manager = ModelManager::GetInstance();
  950. GE_CHECK_NOTNULL(model_manager);
  951. auto davinci_model = model_manager->GetModel(model_id);
  952. if (davinci_model == nullptr) {
  953. GELOGE(FAILED, "Model id: %d is invaild or model is not loaded.", model_id);
  954. return FAILED;
  955. }
  956. device_id = davinci_model->GetDeviceId();
  957. return SUCCESS;
  958. }
  959. Status GeExecutor::GetBatchInfoSize(uint32_t model_id, size_t &shape_count) {
  960. std::vector<std::vector<int64_t>> batch_info;
  961. int32_t dynamic_type = static_cast<int32_t>(FIXED);
  962. Status ret = GetDynamicBatchInfo(model_id, batch_info, dynamic_type);
  963. if (ret != SUCCESS) {
  964. GELOGE(ret, "Calc batch info size failed. ret = %d", ret);
  965. return ret;
  966. }
  967. if (batch_info.empty()) {
  968. shape_count = kStaticBatchInfoSize;
  969. } else {
  970. shape_count = batch_info.size();
  971. }
  972. return SUCCESS;
  973. }
  974. Status GeExecutor::GetOrigInputInfo(uint32_t model_id, uint32_t index, OriginInputInfo &orig_input_info) {
  975. GELOGI("Begin to GetOrigInputInfo.");
  976. if (!isInit_) {
  977. GELOGE(GE_EXEC_NOT_INIT, "not inited yet!");
  978. return GE_EXEC_NOT_INIT;
  979. }
  980. Status ret = GraphExecutor::GetOrigInputInfo(model_id, index, orig_input_info);
  981. if (ret != SUCCESS) {
  982. GELOGE(ret, "GetOrigInputInfo failed.");
  983. return ret;
  984. }
  985. GELOGI("GetOrigInputInfo succ.");
  986. return SUCCESS;
  987. }
  988. Status GeExecutor::GetAllAippInputOutputDims(uint32_t model_id, uint32_t index,
  989. std::vector<InputOutputDims> &input_dims,
  990. std::vector<InputOutputDims> &output_dims) {
  991. GELOGI("Begin to GetAllAippInputOutputDims.");
  992. if (!isInit_) {
  993. GELOGE(GE_EXEC_NOT_INIT, "not inited yet!");
  994. return GE_EXEC_NOT_INIT;
  995. }
  996. Status ret = GraphExecutor::GetAllAippInputOutputDims(model_id, index, input_dims, output_dims);
  997. if (ret != SUCCESS) {
  998. GELOGE(ret, "GetAllAippInputOutputDims failed.");
  999. return ret;
  1000. }
  1001. GELOGI("GetAllAippInputOutputDims succ.");
  1002. return SUCCESS;
  1003. }
  1004. Status GeExecutor::GetOpDescInfo(uint32_t device_id, uint32_t stream_id, uint32_t task_id, OpDescInfo &op_desc_info) {
  1005. GELOGI("Begin to GetOpDescInfo.");
  1006. Status ret = GraphExecutor::GetOpDescInfo(device_id, stream_id, task_id, op_desc_info);
  1007. if (ret != SUCCESS) {
  1008. GELOGE(ret, "GetOpDescInfo failed.");
  1009. return ret;
  1010. }
  1011. GELOGI("GetOpDescInfo succ.");
  1012. return SUCCESS;
  1013. }
  1014. Status GeExecutor::SetDump(const DumpConfig &dump_config) {
  1015. GELOGI("Start to set dump config");
  1016. auto ret = DumpManager::GetInstance().SetDumpConf(dump_config);
  1017. if (ret != SUCCESS) {
  1018. GELOGE(ret, "Set dump conf failed");
  1019. return ret;
  1020. }
  1021. GELOGI("Set dump config successfully");
  1022. return SUCCESS;
  1023. }
  1024. } // namespace ge

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