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

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