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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 model start to run
  117. ///
  118. ge::Status Start(uint32_t model_id);
  119. ///
  120. /// @ingroup domi_ome
  121. /// @brief ACL case, do not start new thread, return result
  122. /// @param [in] model_id model id
  123. /// @param [in] stream model stream
  124. /// @param [in] async_mode is asynchronize mode.
  125. /// @param [in] input_data model input data
  126. /// @param [out] output_data model output data
  127. ///
  128. ge::Status ExecuteModel(uint32_t model_id, rtStream_t stream, bool async_mode, const InputData &input_data,
  129. OutputData &output_data);
  130. ge::Status SyncExecuteModel(uint32_t model_id, const std::vector<GeTensor> &inputs, std::vector<GeTensor> &outputs);
  131. ///
  132. /// @ingroup domi_ome
  133. /// @brief model stop
  134. ///
  135. ge::Status Stop(uint32_t model_id);
  136. ///
  137. /// @ingroup domi_ome
  138. /// @brief comment handle function
  139. ///
  140. ge::Status HandleCommand(const Command &command);
  141. static ge::Status HandleAclProfilingCommand(const Command &command);
  142. static ge::Status HandleProfileCommand(const Command &command);
  143. static ge::Status HandleDumpCommand(const Command &command);
  144. static ge::Status HandleProfInitCommand(const Command &command);
  145. static ge::Status HandleProfFinalizeCommand(const Command &command);
  146. static ge::Status HandleProfStartCommand(const Command &command);
  147. static ge::Status HandleProfStopCommand(const Command &command);
  148. ///
  149. /// @ingroup domi_ome
  150. /// @brief get model memory usage
  151. /// @param [in] model_id model id
  152. /// @return SUCCESS success
  153. /// @return PARAM_INVALID parameter invalid
  154. ///
  155. ge::Status GetMaxUsedMemory(const uint32_t model_id, uint64_t &max_size);
  156. ///
  157. /// @ingroup domi_ome
  158. /// @brief get model input and output size
  159. /// @param [in] model_id model id
  160. /// @param [out] input_shape input tensor
  161. /// @param [out] output_shape output tensor
  162. /// @return SUCCESS success
  163. /// @return PARAM_INVALID parameter invalid
  164. ///
  165. ge::Status GetInputOutputDescInfo(const uint32_t model_id, std::vector<InputOutputDescInfo> &input_desc,
  166. std::vector<InputOutputDescInfo> &output_desc);
  167. ge::Status GetInputOutputDescInfo(const uint32_t model_id, std::vector<InputOutputDescInfo> &input_desc,
  168. std::vector<InputOutputDescInfo> &output_desc, std::vector<uint32_t> &inputFormats,
  169. std::vector<uint32_t> &outputFormats, bool new_model_desc = false);
  170. ///
  171. /// @ingroup ge
  172. /// @brief Get dynamic batch_info
  173. /// @param [in] model_id
  174. /// @param [out] batch_info
  175. /// @param [out] dynamic_type
  176. /// @return execute result
  177. ///
  178. ge::Status GetDynamicBatchInfo(const uint32_t model_id, std::vector<std::vector<int64_t>> &batch_info,
  179. int32_t &dynamic_type);
  180. ///
  181. /// @ingroup ge
  182. /// @brief Get combined dynamic dims info
  183. /// @param [in] model_id
  184. /// @param [out] batch_info
  185. /// @return execute result
  186. ///
  187. ge::Status GetCombinedDynamicDims(const uint32_t model_id, std::vector<std::vector<int64_t>> &batch_info);
  188. ///
  189. /// @ingroup ge
  190. /// @brief Get user designate shape order
  191. /// @param [in] model_id
  192. /// @param [out] user_input_shape_order
  193. /// @return execute result
  194. ///
  195. Status GetUserDesignateShapeOrder(const uint32_t model_id, std::vector<std::string> &user_input_shape_order);
  196. ///
  197. /// @ingroup ge
  198. /// @brief Get AIPP info
  199. /// @param [in] model_id
  200. /// @param [in] index
  201. /// @param [out] aipp_info
  202. /// @return execute result
  203. ///
  204. ge::Status GetAIPPInfo(const uint32_t model_id, uint32_t index, AippConfigInfo &aipp_info);
  205. ge::Status GetAippType(uint32_t model_id, uint32_t index, InputAippType &type, size_t &aipp_index);
  206. ///
  207. /// @ingroup domi_ome
  208. /// @brief set model input and output size zero copy
  209. /// @param [in] model_id model id
  210. /// @param [out] input_shape input tensor
  211. /// @param [out] output_shape output tensor
  212. /// @return SUCCESS success
  213. /// @return PARAM_INVALID parameter invalid
  214. ///
  215. ge::Status GetInputOutputDescInfoForZeroCopy(const uint32_t model_id, std::vector<InputOutputDescInfo> &input_desc,
  216. std::vector<InputOutputDescInfo> &output_desc,
  217. std::vector<uint32_t> &inputFormats,
  218. std::vector<uint32_t> &outputFormats);
  219. ge::Status GetCurShape(const uint32_t model_id, std::vector<int64_t> &batch_info, int32_t &dynamic_type);
  220. ge::Status GetModelAttr(uint32_t model_id, std::vector<string> &dynamic_output_shape_info);
  221. ge::Status SetDevice(int32_t deviceId) const;
  222. ge::Status SetDynamicSize(uint32_t model_id, const std::vector<uint64_t> &batch_num, int32_t dynamic_type);
  223. ///
  224. /// @ingroup domi_ome
  225. /// @brief Get model according to given id
  226. ///
  227. std::shared_ptr<DavinciModel> GetModel(uint32_t id);
  228. std::shared_ptr<hybrid::HybridDavinciModel> GetHybridModel(uint32_t id);
  229. ge::Status KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType op_type, uint64_t session_id, uint32_t model_id);
  230. ge::Status CreateAicpuSession(uint64_t session_id);
  231. static ge::Status GetModelMemAndWeightSize(const ModelData &model, size_t &mem_size, size_t &weight_size);
  232. void DestroyAicpuSession(uint64_t session_id);
  233. ge::Status DestroyAicpuKernel(uint64_t session_id, uint32_t model_id);
  234. ge::Status CreateAicpuKernel(uint64_t session_id, uint32_t model_id, uint64_t kernel_id);
  235. ge::Status DestroyAicpuSessionForInfer(uint32_t model_id);
  236. ge::Status LoadCustAicpuSo(const OpDescPtr &op_desc, const string &so_name);
  237. ge::Status LaunchCustAicpuSo();
  238. ge::Status ClearAicpuSo();
  239. ge::Status LaunchKernelCustAicpuSo(const string &kernel_name);
  240. ge::Status GetOrigInputInfo(uint32_t model_id, uint32_t index, OriginInputInfo &orig_input_info);
  241. ge::Status GenSessionId(uint64_t &session_id);
  242. ge::Status GetAllAippInputOutputDims(uint32_t model_id, uint32_t index, std::vector<InputOutputDims> &input_dims,
  243. std::vector<InputOutputDims> &output_dims);
  244. bool IsDynamicShape(uint32_t model_id);
  245. ge::Status GetOpDescInfo(uint32_t device_id, uint32_t stream_id, uint32_t task_id, OpDescInfo &op_desc_info);
  246. ge::Status EnableExceptionDump(const std::map<string, string> &options);
  247. const std::vector<rtExceptionInfo> &GetExceptionInfos() { return exception_infos_; }
  248. void AddExceptionInfo(const rtExceptionInfo &exception_info) { exception_infos_.emplace_back(exception_info); }
  249. static void ExceptionCallback(rtExceptionInfo *exception_info) {
  250. std::lock_guard<std::mutex> lock(exeception_infos_mutex_);
  251. auto instance = ModelManager::GetInstance();
  252. if (instance == nullptr) {
  253. GELOGE(FAILED, "Instance is nullptr");
  254. return;
  255. }
  256. instance->AddExceptionInfo(*exception_info);
  257. }
  258. private:
  259. ///
  260. /// @ingroup domi_ome
  261. /// @brief constructor
  262. ///
  263. ModelManager();
  264. ///
  265. /// @ingroup domi_ome
  266. /// @brief destructor
  267. ///
  268. ~ModelManager();
  269. ///
  270. /// @ingroup domi_ome
  271. /// @brief insert new model into model manager set
  272. ///
  273. void InsertModel(uint32_t id, std::shared_ptr<DavinciModel> &davinci_model);
  274. void InsertModel(uint32_t id, std::shared_ptr<hybrid::HybridDavinciModel> &hybrid_model);
  275. ///
  276. /// @ingroup domi_ome
  277. /// @brief delete model from model manager set
  278. ///
  279. ge::Status DeleteModel(uint32_t id);
  280. void GenModelId(uint32_t *id);
  281. std::map<uint32_t, std::shared_ptr<DavinciModel>> model_map_;
  282. std::map<uint32_t, std::shared_ptr<hybrid::HybridDavinciModel>> hybrid_model_map_;
  283. std::map<std::string, std::vector<uint64_t>> model_aicpu_kernel_;
  284. uint32_t max_model_id_;
  285. std::mutex map_mutex_;
  286. std::mutex sess_ids_mutex_;
  287. std::mutex session_id_create_mutex_;
  288. static::std::mutex exeception_infos_mutex_;
  289. uint64_t session_id_bias_;
  290. std::set<uint64_t> sess_ids_;
  291. std::vector<rtExceptionInfo> exception_infos_;
  292. std::mutex cust_aicpu_mutex_;
  293. std::map<uintptr_t, std::map<std::string, CustAICPUKernelPtr>> cust_aicpu_so_;
  294. static DumpProperties dump_properties_;
  295. };
  296. } // namespace ge
  297. #endif // GE_GRAPH_LOAD_NEW_MODEL_MANAGER_MODEL_MANAGER_H_

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