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.

runtime_model.cc 17 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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 "ge_runtime/runtime_model.h"
  17. #include <set>
  18. #include "./model_context.h"
  19. #include "./task/task.h"
  20. #include "common/ge_inner_error_codes.h"
  21. #include "common/types.h"
  22. #include "common/util.h"
  23. #include "framework/common/debug/ge_log.h"
  24. #include "framework/common/op/op_parser_util.h"
  25. #include "graph/types.h"
  26. #include "task/task_factory.h"
  27. namespace ge {
  28. namespace model_runner {
  29. RuntimeModel::~RuntimeModel() {
  30. GELOGI("RuntimeModel destructor start");
  31. // Release task first, hccl task hold stream
  32. task_list_.clear();
  33. // Unbind rtModel from all task related streams
  34. RtModelUnbindStream();
  35. // Release all task related streams
  36. RtStreamDestory();
  37. // Release rtlabel resource
  38. RtLabelDestory();
  39. // Release rtEvent resourece
  40. RtEventDestory();
  41. GELOGI("Do RtModelDestory");
  42. // Release all rt_model
  43. RtModelDestory();
  44. }
  45. bool RuntimeModel::InitStream(std::shared_ptr<DavinciModel> &davinci_model) {
  46. if (davinci_model == nullptr) {
  47. GELOGE(PARAM_INVALID, "Davinci model is null.");
  48. return false;
  49. }
  50. std::set<int64_t> wait_active_streams;
  51. std::set<int64_t> force_copy_streams;
  52. for (const auto &stream_id : davinci_model->GetWaitActiveStreams()) {
  53. GELOGI("stream id %u is wait active stream.", stream_id);
  54. (void)wait_active_streams.insert(stream_id);
  55. }
  56. for (const auto &stream_id : davinci_model->GetForceCopyStreams()) {
  57. GELOGI("stream id %u is force copy stream.", stream_id);
  58. (void)force_copy_streams.insert(stream_id);
  59. }
  60. GELOGI("stream number:%u", davinci_model->GetStreamNum());
  61. for (uint32_t i = 0; i < davinci_model->GetStreamNum(); ++i) {
  62. rtStream_t stream = nullptr;
  63. uint32_t flag = (force_copy_streams.find(i) != force_copy_streams.end())
  64. ? (RT_STREAM_PERSISTENT | RT_STREAM_FORCE_COPY)
  65. : (RT_STREAM_PERSISTENT);
  66. rtError_t rt_ret = rtStreamCreateWithFlags(&stream, davinci_model->GetPriority(), flag);
  67. if (rt_ret != RT_ERROR_NONE) {
  68. GELOGE(RT_FAILED, "Call rt api rtStreamCreate failed, ret: 0x%X", rt_ret);
  69. return false;
  70. }
  71. GELOGI("rtStreamCreateWithFlags end.");
  72. stream_list_.emplace_back(stream);
  73. // Bind rt_model_handle_ to all task related streams
  74. flag = (wait_active_streams.find(i) != wait_active_streams.end()) ? (static_cast<uint32_t>(RT_INVALID_FLAG))
  75. : (static_cast<uint32_t>(RT_HEAD_STREAM));
  76. rt_ret = rtModelBindStream(rt_model_handle_, stream, flag);
  77. if (rt_ret != RT_ERROR_NONE) {
  78. GELOGE(RT_FAILED, "Call rt api rtModelBindStream failed, ret: 0x%X", rt_ret);
  79. return false;
  80. }
  81. GELOGI("stream index:%u, stream:%p.", i, stream);
  82. }
  83. return true;
  84. }
  85. bool RuntimeModel::InitEvent(uint32_t event_num) {
  86. GELOGI("event number:%u.", event_num);
  87. for (uint32_t i = 0; i < event_num; ++i) {
  88. rtEvent_t rt_event;
  89. rtError_t rt_ret = rtEventCreate(&rt_event);
  90. if (rt_ret != RT_ERROR_NONE) {
  91. GELOGE(RT_FAILED, "Call rt api rtEventCreate failed, i; %u; ret: 0x%X", i, rt_ret);
  92. return false;
  93. }
  94. event_list_.push_back(rt_event);
  95. }
  96. return true;
  97. }
  98. bool RuntimeModel::InitLabel(uint32_t batch_num) {
  99. GELOGI("batch number:%u.", batch_num);
  100. for (uint32_t i = 0; (batch_num != 0 && i <= batch_num); ++i) {
  101. rtLabel_t rt_lLabel = nullptr;
  102. rtError_t rt_ret = rtLabelCreate(&rt_lLabel);
  103. if (rt_ret != RT_ERROR_NONE) {
  104. GELOGE(RT_FAILED, "Call rt api rtLabelCreate failed, i; %u; ret: 0x%X", i, rt_ret);
  105. return false;
  106. }
  107. if (rt_lLabel == nullptr) {
  108. GELOGE(RT_FAILED, "rtLabel is nullptr!");
  109. return false;
  110. }
  111. label_list_.emplace_back(rt_lLabel);
  112. }
  113. return true;
  114. }
  115. bool RuntimeModel::InitResource(std::shared_ptr<DavinciModel> &davinci_model) {
  116. GELOGI("InitResource start");
  117. if (davinci_model == nullptr) {
  118. GELOGE(PARAM_INVALID, "davinci model is null");
  119. return false;
  120. }
  121. rtError_t rt_ret = rtModelCreate(&rt_model_handle_, 0);
  122. if (rt_ret != RT_ERROR_NONE) {
  123. GELOGE(RT_FAILED, "Call rt api rtModelCreate failed, ret: 0x%X", rt_ret);
  124. return false;
  125. }
  126. // Create rtStream for rt_model_handle_
  127. rt_ret = rtStreamCreate(&rt_model_stream_, davinci_model->GetPriority());
  128. if (rt_ret != RT_ERROR_NONE) {
  129. GELOGE(RT_FAILED, "Call rt api rtStreamCreate failed, ret: 0x%X", rt_ret);
  130. return false;
  131. }
  132. GELOGI("rtStreamCreate end");
  133. if (!InitStream(davinci_model)) {
  134. return false;
  135. }
  136. if (!InitEvent(davinci_model->GetEventNum())) {
  137. return false;
  138. }
  139. if (!InitLabel(davinci_model->GetBatchNum())) {
  140. return false;
  141. }
  142. GELOGI("InitResource succ");
  143. return true;
  144. }
  145. void RuntimeModel::GenerateTask(uint32_t device_id, uint64_t session_id, std::shared_ptr<DavinciModel> &davinci_model) {
  146. GELOGI("GenerateTask start.");
  147. if (davinci_model == nullptr) {
  148. GELOGE(PARAM_INVALID, "davinci model is null");
  149. return;
  150. }
  151. auto task_infos = davinci_model->GetTaskInfoList();
  152. ModelContext model_context(device_id, session_id, davinci_model->GetPriority(), rt_model_handle_, rt_model_stream_,
  153. stream_list_, label_list_, event_list_);
  154. for (auto &task_info : task_infos) {
  155. auto task = TaskFactory::GetInstance().Create(model_context, task_info);
  156. task_list_.push_back(task);
  157. }
  158. GELOGI("GenerateTask succ.");
  159. }
  160. bool RuntimeModel::LoadTask() {
  161. GELOGI("LoadTask start.");
  162. for (auto &task : task_list_) {
  163. if (task == nullptr) {
  164. GELOGE(PARAM_INVALID, "task is null.");
  165. continue;
  166. }
  167. bool ret = task->Distribute();
  168. if (!ret) {
  169. GELOGE(FAILED, "task distribute fail.");
  170. return false;
  171. }
  172. uint32_t task_id = 0;
  173. uint32_t stream_id = 0;
  174. rtError_t rt_ret = rtModelGetTaskId(rt_model_handle_, &task_id, &stream_id);
  175. if (rt_ret != RT_ERROR_NONE) {
  176. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X.", rt_ret);
  177. return false;
  178. }
  179. task_id_list_.push_back(task_id);
  180. stream_id_list_.push_back(stream_id);
  181. if (task->Args() != nullptr) {
  182. std::shared_ptr<RuntimeInfo> runtime_tuple = nullptr;
  183. GE_MAKE_SHARED(runtime_tuple = std::make_shared<RuntimeInfo>(task_id, stream_id, task->Args()), return false);
  184. auto emplace_ret = runtime_info_map_.emplace(task->task_name(), runtime_tuple);
  185. if (!emplace_ret.second) {
  186. GELOGW("Task name exist:%s", task->task_name().c_str());
  187. }
  188. }
  189. }
  190. if (task_list_.empty()) {
  191. GELOGE(FAILED, "Task list is empty");
  192. return false;
  193. }
  194. GELOGI("LoadTask succ.");
  195. return true;
  196. }
  197. bool RuntimeModel::LoadComplete() {
  198. uint32_t task_id = 0;
  199. uint32_t stream_id = 0;
  200. auto rt_ret = rtModelGetTaskId(rt_model_handle_, &task_id, &stream_id);
  201. if (rt_ret != RT_ERROR_NONE) {
  202. GELOGE(RT_FAILED, "Call rtModelGetTaskId failed, ret:0x%X", rt_ret);
  203. return RT_FAILED;
  204. }
  205. task_id_list_.push_back(task_id);
  206. stream_id_list_.push_back(stream_id);
  207. rt_ret = rtModelLoadComplete(rt_model_handle_);
  208. if (rt_ret != RT_ERROR_NONE) {
  209. GELOGE(RT_FAILED, "Call rt api rtModelLoadComplete failed, ret: 0x%X.", rt_ret);
  210. return false;
  211. }
  212. return true;
  213. }
  214. bool RuntimeModel::Load(uint32_t device_id, uint64_t session_id, std::shared_ptr<DavinciModel> &davinci_model) {
  215. bool status = InitResource(davinci_model);
  216. if (!status) {
  217. GELOGE(FAILED, "InitResource failed.");
  218. return status;
  219. }
  220. status = InitDataInfo(davinci_model);
  221. if (!status) {
  222. GELOGE(FAILED, "InitDataInfo failed.");
  223. return status;
  224. }
  225. status = InitOutputInfo(davinci_model);
  226. if (!status) {
  227. GELOGE(FAILED, "InitOutputInfo failed.");
  228. return status;
  229. }
  230. status = InitConstantInfo(davinci_model);
  231. if (!status) {
  232. GELOGE(FAILED, "InitConstantInfo failed.");
  233. return status;
  234. }
  235. GenerateTask(device_id, session_id, davinci_model);
  236. return status;
  237. }
  238. bool RuntimeModel::DistributeTask() {
  239. bool status = LoadTask();
  240. if (!status) {
  241. GELOGE(FAILED, "DistributeTask failed");
  242. return false;
  243. }
  244. return true;
  245. }
  246. bool RuntimeModel::Run() {
  247. GELOGI("Davinci task run start");
  248. rtError_t ret = rtModelExecute(rt_model_handle_, rt_model_stream_, 0);
  249. if (ret != RT_ERROR_NONE) {
  250. GELOGE(RT_FAILED, "Model execute failed, ret = 0x%X", ret);
  251. return false;
  252. }
  253. GELOGI("Run rtModelExecute success");
  254. ret = rtStreamSynchronize(rt_model_stream_);
  255. if (ret != RT_ERROR_NONE) {
  256. GELOGE(RT_FAILED, "Model stream sync failed, ret = 0x%X", ret);
  257. return false;
  258. }
  259. GELOGI("Davinci task run succ.");
  260. return true;
  261. }
  262. void RuntimeModel::RtModelUnbindStream() noexcept {
  263. for (size_t i = 0; i < stream_list_.size(); i++) {
  264. if (rtModelUnbindStream(rt_model_handle_, stream_list_[i]) != RT_ERROR_NONE) {
  265. GELOGE(RT_FAILED, "Unbind stream from model failed! Index: %zu", i);
  266. return;
  267. }
  268. }
  269. }
  270. void RuntimeModel::RtStreamDestory() noexcept {
  271. if (rtStreamDestroy(rt_model_stream_) != RT_ERROR_NONE) {
  272. GELOGE(RT_FAILED, "Destroy stream for rt_model failed!");
  273. return;
  274. }
  275. for (size_t i = 0; i < stream_list_.size(); i++) {
  276. if (rtStreamDestroy(stream_list_[i]) != RT_ERROR_NONE) {
  277. GELOGE(RT_FAILED, "Destroy stream failed! Index: %zu", i);
  278. return;
  279. }
  280. }
  281. }
  282. void RuntimeModel::RtLabelDestory() noexcept {
  283. for (size_t i = 0; i < label_list_.size(); i++) {
  284. if (rtLabelDestroy(label_list_[i]) != RT_ERROR_NONE) {
  285. GELOGE(RT_FAILED, "Destroy label failed! Index: %zu.", i);
  286. return;
  287. }
  288. }
  289. }
  290. void RuntimeModel::RtModelDestory() noexcept {
  291. rtError_t ret = rtModelDestroy(rt_model_handle_);
  292. if (ret != RT_ERROR_NONE) {
  293. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", ret);
  294. return;
  295. }
  296. }
  297. void RuntimeModel::RtEventDestory() noexcept {
  298. for (size_t i = 0; i < event_list_.size(); i++) {
  299. if (rtEventDestroy(event_list_[i]) != RT_ERROR_NONE) {
  300. GELOGE(RT_FAILED, "Destroy event failed! Index: %zu", i);
  301. return;
  302. }
  303. }
  304. }
  305. bool RuntimeModel::InitDataInfo(std::shared_ptr<DavinciModel> &davinci_model) { return true; }
  306. bool RuntimeModel::InitOutputInfo(std::shared_ptr<DavinciModel> &davinci_model) {
  307. if (davinci_model == nullptr) {
  308. GELOGE(PARAM_INVALID, "davinci model is null");
  309. return false;
  310. }
  311. output_info_list_ = davinci_model->GetOutputInfoList();
  312. return true;
  313. }
  314. bool RuntimeModel::CopyInputData(const InputData &input_data) {
  315. if (input_data.blobs.size() != data_info_list_.size()) {
  316. GELOGE(PARAM_INVALID, "The input data list size (%zu) does not match the model input list size (%zu)",
  317. input_data.blobs.size(), data_info_list_.size());
  318. return false;
  319. }
  320. for (const auto &data_info : data_info_list_) {
  321. if (data_info == nullptr) {
  322. GELOGE(PARAM_INVALID, "data info is null.");
  323. return false;
  324. }
  325. bool ret = CopyInputDataToModel(input_data.blobs, data_info);
  326. if (!ret) {
  327. GELOGE(FAILED, "Copy input data to model ret fail, data_info: %s, model id: %u", data_info->name.c_str(),
  328. input_data.model_id);
  329. return false;
  330. }
  331. }
  332. return true;
  333. }
  334. bool RuntimeModel::CopyInputDataToModel(const std::vector<DataBuffer> &data, const std::shared_ptr<OpInfo> &data_info) {
  335. return true;
  336. }
  337. bool RuntimeModel::CopyHostData(const std::vector<DataBuffer> &data, const std::shared_ptr<OpInfo> &data_info) const {
  338. GELOGI("Start CopyHostData.");
  339. if (data.empty()) {
  340. GELOGE(PARAM_INVALID, "data buffer is empty.");
  341. return false;
  342. }
  343. if (data_info == nullptr) {
  344. GELOGE(PARAM_INVALID, "data info is null.");
  345. return false;
  346. }
  347. void *host_data_addr = data[data_info->index].data;
  348. uint32_t copy_size = data[data_info->index].length;
  349. GELOGD("data output tensor is aipp tensor,copy data only.");
  350. const std::vector<uintptr_t> &outputs = data_info->output_addrs;
  351. if (outputs.empty()) {
  352. GELOGE(PARAM_INVALID, "Output addrs is empty.");
  353. return false;
  354. }
  355. // Copy input data to data nodes
  356. void *data_out_addr = reinterpret_cast<void *>(outputs[0]);
  357. rtError_t rt_ret = rtMemcpy(data_out_addr, copy_size, host_data_addr, copy_size, RT_MEMCPY_HOST_TO_DEVICE);
  358. if (rt_ret != RT_ERROR_NONE) {
  359. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  360. return false;
  361. }
  362. return true;
  363. }
  364. bool RuntimeModel::CopyTransData(const std::vector<DataBuffer> &data, const std::shared_ptr<OpInfo> &data_info) {
  365. return true;
  366. }
  367. bool RuntimeModel::InitConstantInfo(std::shared_ptr<DavinciModel> &davinci_model) {
  368. // Const no input, only 1 output, and this output has no data
  369. // weight data copy to output mem
  370. if (davinci_model == nullptr) {
  371. GELOGE(PARAM_INVALID, "Davinci model is null.");
  372. return false;
  373. }
  374. constant_info_list_ = davinci_model->GetConstantInfoList();
  375. for (const auto &constant : constant_info_list_) {
  376. if (constant == nullptr) {
  377. GELOGE(PARAM_INVALID, "constant is null");
  378. continue;
  379. }
  380. if (constant->output_tensors.empty()) {
  381. GELOGE(PARAM_INVALID, "Output tensors is empty");
  382. return false;
  383. }
  384. if (constant->weight_tensors.empty()) {
  385. GELOGE(PARAM_INVALID, "Weight tensors is empty");
  386. return false;
  387. }
  388. if (constant->output_tensors[0].size < constant->weight_data.size()) {
  389. GELOGE(PARAM_INVALID, "Output size:%u is less than weight data size:%zu", constant->output_tensors[0].size,
  390. constant->weight_data.size());
  391. return false;
  392. }
  393. if (constant->weight_data.empty()) {
  394. GELOGW("Const op:%s has no weight data.", constant->name.c_str());
  395. continue;
  396. }
  397. if (constant->weight_tensors[0].datatype == DT_STRING) {
  398. /// If tensor is a scaler, it's shape size if zero, according ge_tensor.cc.
  399. /// The logic of GetShapeSize is wrong, the scaler tensor's GetShapeSize is zero
  400. /// and that of unknown shape is zero too.
  401. /// Unknown shape will not appear here, so we can use zero judge a tensor is scaler or not.
  402. int64_t elem_num = constant->weight_tensors[0].GetShapeSize();
  403. if (elem_num == 0 && constant->weight_tensors[0].size == 0) {
  404. elem_num = 1;
  405. }
  406. if (constant->weight_data.size() < sizeof(uint64_t)) {
  407. GELOGE(FAILED, "weight_data size is smaller than sizeof(uint64_t)");
  408. return false;
  409. }
  410. uint64_t *buff = reinterpret_cast<uint64_t *>(const_cast<char *>(constant->weight_data.data()));
  411. int64_t offset = elem_num * 8;
  412. uintptr_t hbm_raw_data_base_addr = reinterpret_cast<uintptr_t>(constant->output_addrs[0]) + offset;
  413. for (int64_t i = elem_num - 1; i >= 0; --i) {
  414. buff[i] = hbm_raw_data_base_addr + (buff[i] - buff[0]);
  415. }
  416. }
  417. rtError_t rt_ret = rtMemcpy(reinterpret_cast<void *>(constant->output_addrs[0]), constant->output_tensors[0].size,
  418. constant->weight_data.data(), constant->weight_data.size(), RT_MEMCPY_HOST_TO_DEVICE);
  419. if (rt_ret != RT_ERROR_NONE) {
  420. GELOGE(RT_FAILED, "rtGetFunctionByName failed, ret: 0x%X", rt_ret);
  421. return false;
  422. }
  423. }
  424. return true;
  425. }
  426. bool RuntimeModel::GetInputOutputDescInfo(bool zero_copy, std::vector<InputOutputDescInfo> *input_desc,
  427. std::vector<InputOutputDescInfo> *output_desc,
  428. std::vector<uint32_t> *input_format, std::vector<uint32_t> *output_format) {
  429. return true;
  430. }
  431. bool RuntimeModel::GetInputDescInfo(std::vector<InputOutputDescInfo> *input_desc, std::vector<uint32_t> *formats) {
  432. return true;
  433. }
  434. bool RuntimeModel::GetOutputDescInfo(std::vector<InputOutputDescInfo> *output_desc, std::vector<uint32_t> *formats) {
  435. return true;
  436. }
  437. void RuntimeModel::CreateOutput(uint32_t index, const OpInfo &op_info, InputOutputDescInfo *output,
  438. uint32_t *format_result) {}
  439. const std::vector<uint32_t> &RuntimeModel::GetTaskIdList() const { return task_id_list_; }
  440. const std::vector<uint32_t> &RuntimeModel::GetStreamIdList() const { return stream_id_list_; }
  441. } // namespace model_runner
  442. } // namespace ge

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