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

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