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

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