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.

model_manager.h 13 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
4 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
4 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. #ifndef GE_GRAPH_LOAD_NEW_MODEL_MANAGER_MODEL_MANAGER_H_
  17. #define GE_GRAPH_LOAD_NEW_MODEL_MANAGER_MODEL_MANAGER_H_
  18. #include <model/ge_root_model.h>
  19. #include <pthread.h>
  20. #include <stdint.h>
  21. #include <algorithm>
  22. #include <map>
  23. #include <memory>
  24. #include <set>
  25. #include <string>
  26. #include <vector>
  27. #include "cce/aicpu_engine_struct.h"
  28. #include "common/ge_inner_error_codes.h"
  29. #include "common/ge_types.h"
  30. #include "common/helper/model_helper.h"
  31. #include "common/helper/om_file_helper.h"
  32. #include "common/properties_manager.h"
  33. #include "common/types.h"
  34. #include "ge/ge_api_types.h"
  35. #include "graph/ge_context.h"
  36. #include "graph/model.h"
  37. #include "hybrid/hybrid_davinci_model.h"
  38. #include "runtime/base.h"
  39. namespace ge {
  40. class DavinciModel;
  41. class FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ModelManager {
  42. public:
  43. static std::shared_ptr<ModelManager> GetInstance();
  44. static void FinalizeForPtr(ModelManager *) {}
  45. ///
  46. /// @ingroup domi_ome
  47. /// @brief load and init model
  48. /// @param [in] model_id model id
  49. /// @param [in] model including model ptr and size
  50. /// @param [in] listener used to return result
  51. /// @param [in/out] info model task generate info
  52. /// @return Status run result
  53. /// @author
  54. ///
  55. ge::Status LoadModelOffline(uint32_t &model_id, const ModelData &model,
  56. std::shared_ptr<ModelListener> listener = nullptr, void *dev_ptr = nullptr,
  57. size_t mem_size = 0, void *weight_ptr = nullptr, size_t weight_size = 0);
  58. ///
  59. /// @ingroup domi_ome
  60. /// @brief load and init model
  61. /// @param [out] model_id model id
  62. /// @param [in] model modeldef datatype
  63. /// @param [in] listener used to return result
  64. /// @param [in] isTrainMode model type
  65. /// @return Status run result
  66. /// @author @
  67. ///
  68. ge::Status LoadModelOnline(uint32_t &model_id, const std::shared_ptr<ge::GeRootModel> &ge_root_model,
  69. std::shared_ptr<ModelListener> listener);
  70. ge::Status DoLoadHybridModelOnline(uint32_t model_id, const shared_ptr<ge::GeRootModel> &ge_root_model,
  71. const std::shared_ptr<ModelListener> &listener);
  72. ///
  73. /// @ingroup ge
  74. /// @brief ACL case, Load task list with queue.
  75. /// @param [out] model_id: model id for manager.
  76. /// @param [in] model_data: Model data load from offline model file.
  77. /// @param [in] input_que_ids: input queue ids from user, num equals Data Op.
  78. /// @param [in] output_que_ids: input queue ids from user, num equals NetOutput Op.
  79. /// @return: 0 for success / others for fail
  80. ///
  81. ge::Status LoadModelWithQ(uint32_t &model_id, const ModelData &model_data,
  82. const std::vector<uint32_t> &input_queue_ids,
  83. const std::vector<uint32_t> &output_queue_ids);
  84. ///
  85. /// @ingroup domi_ome
  86. /// @brief unload model and free resources
  87. /// @param [in] model_id model id
  88. /// @return Status run result
  89. /// @author
  90. ///
  91. ge::Status Unload(uint32_t model_id);
  92. ///
  93. /// @ingroup omm
  94. /// @brief unload model and free resources
  95. /// @param [in] model_id model id
  96. /// @return Status run result
  97. /// @author
  98. ///
  99. ge::Status UnloadModeldef(uint32_t model_id);
  100. ///
  101. /// @ingroup domi_ome
  102. /// @brief process input data asynchronously
  103. /// cannot be invoked by multiple thread
  104. /// if one fails, other continue
  105. /// @param [in] input_data input data
  106. /// @return SUCCESS success
  107. /// @return PARAM_INVALID parameter invalid
  108. /// @return MODEL_NOT_READY model not ready
  109. /// @return PUSH_DATA_FAILED push data into model queue failed
  110. /// @author
  111. ///
  112. ge::Status DataInput(const InputData &input_data, OutputData &output_data);
  113. ge::Status DataInputTensor(uint32_t model_id, const std::vector<InputTensorInfo> &inputs);
  114. ///
  115. /// @ingroup domi_ome
  116. /// @brief Get cur_dynamic_dims for all input.
  117. /// @param [in] vector<vector<uint64_t>> &user_real_input_dims: dims info of all user_inputs.
  118. /// @param [in] vector<pair<string, vector<int64_t>>> &user_input_dims: key:name. value:dynamic dims from option.
  119. /// @param [out] vector<uint64_t> &cur_dynamic_dims: real dims gather, where the index of -1.
  120. /// @return 0: SUCCESS / others: INTERNAL_ERROR
  121. ///
  122. Status GetCurDynamicDims(const vector<vector<int64_t>> &user_real_input_dims,
  123. const vector<pair<string, vector<int64_t>>> &user_input_dims,
  124. vector<int64_t> &cur_dynamic_dims);
  125. ///
  126. /// @ingroup domi_ome
  127. /// @brief model start to run
  128. ///
  129. ge::Status Start(uint32_t model_id);
  130. ///
  131. /// @ingroup domi_ome
  132. /// @brief ACL case, do not start new thread, return result
  133. /// @param [in] model_id model id
  134. /// @param [in] stream model stream
  135. /// @param [in] async_mode is asynchronize mode.
  136. /// @param [in] input_data model input data
  137. /// @param [out] output_data model output data
  138. ///
  139. ge::Status ExecuteModel(uint32_t model_id, rtStream_t stream, bool async_mode, const InputData &input_data,
  140. OutputData &output_data);
  141. ge::Status SyncExecuteModel(uint32_t model_id, const std::vector<GeTensor> &inputs, std::vector<GeTensor> &outputs);
  142. ///
  143. /// @ingroup domi_ome
  144. /// @brief model stop
  145. ///
  146. ge::Status Stop(uint32_t model_id);
  147. ///
  148. /// @ingroup domi_ome
  149. /// @brief comment handle function
  150. ///
  151. ge::Status HandleCommand(const Command &command);
  152. static ge::Status HandleAclProfilingCommand(const Command &command);
  153. static ge::Status HandleProfileCommand(const Command &command);
  154. static ge::Status HandleDumpCommand(const Command &command);
  155. static ge::Status HandleProfInitCommand(const Command &command);
  156. static ge::Status HandleProfFinalizeCommand(const Command &command);
  157. static ge::Status HandleProfStartCommand(const Command &command);
  158. static ge::Status HandleProfStopCommand(const Command &command);
  159. ///
  160. /// @ingroup domi_ome
  161. /// @brief get model memory usage
  162. /// @param [in] model_id model id
  163. /// @return SUCCESS success
  164. /// @return PARAM_INVALID parameter invalid
  165. ///
  166. ge::Status GetMaxUsedMemory(const uint32_t model_id, uint64_t &max_size);
  167. ///
  168. /// @ingroup domi_ome
  169. /// @brief get model input and output size
  170. /// @param [in] model_id model id
  171. /// @param [out] input_shape input tensor
  172. /// @param [out] output_shape output tensor
  173. /// @return SUCCESS success
  174. /// @return PARAM_INVALID parameter invalid
  175. ///
  176. ge::Status GetInputOutputDescInfo(const uint32_t model_id, std::vector<InputOutputDescInfo> &input_desc,
  177. std::vector<InputOutputDescInfo> &output_desc);
  178. ge::Status GetInputOutputDescInfo(const uint32_t model_id, std::vector<InputOutputDescInfo> &input_desc,
  179. std::vector<InputOutputDescInfo> &output_desc, std::vector<uint32_t> &inputFormats,
  180. std::vector<uint32_t> &outputFormats, bool new_model_desc = false);
  181. ///
  182. /// @ingroup ge
  183. /// @brief Get dynamic batch_info
  184. /// @param [in] model_id
  185. /// @param [out] batch_info
  186. /// @param [out] dynamic_type
  187. /// @return execute result
  188. ///
  189. ge::Status GetDynamicBatchInfo(const uint32_t model_id, std::vector<std::vector<int64_t>> &batch_info,
  190. int32_t &dynamic_type);
  191. ///
  192. /// @ingroup ge
  193. /// @brief Get combined dynamic dims info
  194. /// @param [in] model_id
  195. /// @param [out] batch_info
  196. /// @return execute result
  197. ///
  198. ge::Status GetCombinedDynamicDims(const uint32_t model_id, std::vector<std::vector<int64_t>> &batch_info);
  199. ///
  200. /// @ingroup ge
  201. /// @brief Get user designate shape order
  202. /// @param [in] model_id
  203. /// @param [out] user_input_shape_order
  204. /// @return execute result
  205. ///
  206. Status GetUserDesignateShapeOrder(const uint32_t model_id, std::vector<std::string> &user_input_shape_order);
  207. ///
  208. /// @ingroup ge
  209. /// @brief Get AIPP info
  210. /// @param [in] model_id
  211. /// @param [in] index
  212. /// @param [out] aipp_info
  213. /// @return execute result
  214. ///
  215. ge::Status GetAIPPInfo(const uint32_t model_id, uint32_t index, AippConfigInfo &aipp_info);
  216. ge::Status GetAippType(uint32_t model_id, uint32_t index, InputAippType &type, size_t &aipp_index);
  217. ///
  218. /// @ingroup domi_ome
  219. /// @brief set model input and output size zero copy
  220. /// @param [in] model_id model id
  221. /// @param [out] input_shape input tensor
  222. /// @param [out] output_shape output tensor
  223. /// @return SUCCESS success
  224. /// @return PARAM_INVALID parameter invalid
  225. ///
  226. ge::Status GetInputOutputDescInfoForZeroCopy(const uint32_t model_id, std::vector<InputOutputDescInfo> &input_desc,
  227. std::vector<InputOutputDescInfo> &output_desc,
  228. std::vector<uint32_t> &inputFormats,
  229. std::vector<uint32_t> &outputFormats);
  230. ge::Status GetCurShape(const uint32_t model_id, std::vector<int64_t> &batch_info, int32_t &dynamic_type);
  231. ge::Status GetModelAttr(uint32_t model_id, std::vector<string> &dynamic_output_shape_info);
  232. ge::Status SetDevice(int32_t deviceId) const;
  233. ge::Status SetDynamicSize(uint32_t model_id, const std::vector<uint64_t> &batch_num, int32_t dynamic_type);
  234. ///
  235. /// @ingroup domi_ome
  236. /// @brief Get model according to given id
  237. ///
  238. std::shared_ptr<DavinciModel> GetModel(uint32_t id);
  239. std::shared_ptr<hybrid::HybridDavinciModel> GetHybridModel(uint32_t id);
  240. ge::Status KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType op_type, uint64_t session_id, uint32_t model_id);
  241. ge::Status CreateAicpuSession(uint64_t session_id);
  242. static ge::Status GetModelMemAndWeightSize(const ModelData &model, size_t &mem_size, size_t &weight_size);
  243. void DestroyAicpuSession(uint64_t session_id);
  244. ge::Status DestroyAicpuKernel(uint64_t session_id, uint32_t model_id);
  245. ge::Status CreateAicpuKernel(uint64_t session_id, uint32_t model_id, uint64_t kernel_id);
  246. ge::Status DestroyAicpuSessionForInfer(uint32_t model_id);
  247. ge::Status LoadCustAicpuSo(const OpDescPtr &op_desc, const string &so_name);
  248. ge::Status LaunchCustAicpuSo();
  249. ge::Status ClearAicpuSo();
  250. ge::Status LaunchKernelCustAicpuSo(const string &kernel_name);
  251. ge::Status GetOrigInputInfo(uint32_t model_id, uint32_t index, OriginInputInfo &orig_input_info);
  252. ge::Status GenSessionId(uint64_t &session_id);
  253. ge::Status GetAllAippInputOutputDims(uint32_t model_id, uint32_t index, std::vector<InputOutputDims> &input_dims,
  254. std::vector<InputOutputDims> &output_dims);
  255. bool IsDynamicShape(uint32_t model_id);
  256. ge::Status GetOpDescInfo(uint32_t device_id, uint32_t stream_id, uint32_t task_id, OpDescInfo &op_desc_info);
  257. ge::Status EnableExceptionDump(const std::map<string, string> &options);
  258. const std::vector<rtExceptionInfo> &GetExceptionInfos() { return exception_infos_; }
  259. void AddExceptionInfo(const rtExceptionInfo &exception_info) { exception_infos_.emplace_back(exception_info); }
  260. static void ExceptionCallback(rtExceptionInfo *exception_info) {
  261. std::lock_guard<std::mutex> lock(exeception_infos_mutex_);
  262. auto instance = ModelManager::GetInstance();
  263. if (instance == nullptr) {
  264. GELOGE(FAILED, "Instance is nullptr");
  265. return;
  266. }
  267. instance->AddExceptionInfo(*exception_info);
  268. }
  269. private:
  270. ///
  271. /// @ingroup domi_ome
  272. /// @brief constructor
  273. ///
  274. ModelManager();
  275. ///
  276. /// @ingroup domi_ome
  277. /// @brief destructor
  278. ///
  279. ~ModelManager();
  280. ///
  281. /// @ingroup domi_ome
  282. /// @brief insert new model into model manager set
  283. ///
  284. void InsertModel(uint32_t id, std::shared_ptr<DavinciModel> &davinci_model);
  285. void InsertModel(uint32_t id, std::shared_ptr<hybrid::HybridDavinciModel> &hybrid_model);
  286. ///
  287. /// @ingroup domi_ome
  288. /// @brief delete model from model manager set
  289. ///
  290. ge::Status DeleteModel(uint32_t id);
  291. void GenModelId(uint32_t *id);
  292. std::map<uint32_t, std::shared_ptr<DavinciModel>> model_map_;
  293. std::map<uint32_t, std::shared_ptr<hybrid::HybridDavinciModel>> hybrid_model_map_;
  294. std::map<std::string, std::vector<uint64_t>> model_aicpu_kernel_;
  295. uint32_t max_model_id_;
  296. std::mutex map_mutex_;
  297. std::mutex sess_ids_mutex_;
  298. std::mutex session_id_create_mutex_;
  299. static::std::mutex exeception_infos_mutex_;
  300. uint64_t session_id_bias_;
  301. std::set<uint64_t> sess_ids_;
  302. std::vector<rtExceptionInfo> exception_infos_;
  303. std::mutex cust_aicpu_mutex_;
  304. std::map<uintptr_t, std::map<std::string, CustAICPUKernelPtr>> cust_aicpu_so_;
  305. static DumpProperties dump_properties_;
  306. std::map<uint32_t, std::vector<int64_t *>> shape_data_addrs_;
  307. };
  308. } // namespace ge
  309. #endif // GE_GRAPH_LOAD_NEW_MODEL_MANAGER_MODEL_MANAGER_H_

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