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 10 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #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/types.h"
  33. #include "ge/ge_api_types.h"
  34. #include "graph/ge_context.h"
  35. #include "graph/model.h"
  36. #include "hybrid/hybrid_davinci_model.h"
  37. #include "runtime/base.h"
  38. namespace ge {
  39. class DavinciModel;
  40. class FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ModelManager {
  41. public:
  42. static std::shared_ptr<ModelManager> GetInstance();
  43. static void FinalizeForPtr(ModelManager *) {}
  44. ///
  45. /// @ingroup domi_ome
  46. /// @brief load and init model
  47. /// @param [in] model_id model id
  48. /// @param [in] model including model ptr and size
  49. /// @param [in] listener used to return result
  50. /// @param [in/out] info model task generate info
  51. /// @return Status run result
  52. /// @author
  53. ///
  54. ge::Status LoadModelOffline(uint32_t &model_id, const ModelData &model,
  55. std::shared_ptr<ModelListener> listener = nullptr, void *dev_ptr = nullptr,
  56. size_t mem_size = 0, void *weight_ptr = nullptr, size_t weight_size = 0);
  57. ///
  58. /// @ingroup domi_ome
  59. /// @brief load and init model
  60. /// @param [out] model_id model id
  61. /// @param [in] model modeldef datatype
  62. /// @param [in] listener used to return result
  63. /// @param [in] isTrainMode model type
  64. /// @return Status run result
  65. /// @author @
  66. ///
  67. ge::Status LoadModelOnline(uint32_t &model_id, const std::shared_ptr<ge::GeRootModel> &ge_root_model,
  68. std::shared_ptr<ModelListener> listener);
  69. ge::Status DoLoadHybridModelOnline(uint32_t model_id, const shared_ptr<ge::GeRootModel> &ge_root_model,
  70. const std::shared_ptr<ModelListener> &listener);
  71. ///
  72. /// @ingroup ge
  73. /// @brief ACL case, Load task list with queue.
  74. /// @param [out] model_id: model id for manager.
  75. /// @param [in] model_data: Model data load from offline model file.
  76. /// @param [in] input_que_ids: input queue ids from user, num equals Data Op.
  77. /// @param [in] output_que_ids: input queue ids from user, num equals NetOutput Op.
  78. /// @return: 0 for success / others for fail
  79. ///
  80. ge::Status LoadModelWithQ(uint32_t &model_id, const ModelData &model_data,
  81. const std::vector<uint32_t> &input_queue_ids,
  82. const std::vector<uint32_t> &output_queue_ids);
  83. ///
  84. /// @ingroup domi_ome
  85. /// @brief unload model and free resources
  86. /// @param [in] model_id model id
  87. /// @return Status run result
  88. /// @author
  89. ///
  90. ge::Status Unload(uint32_t model_id);
  91. ///
  92. /// @ingroup omm
  93. /// @brief unload model and free resources
  94. /// @param [in] model_id model id
  95. /// @return Status run result
  96. /// @author
  97. ///
  98. ge::Status UnloadModeldef(uint32_t model_id);
  99. ///
  100. /// @ingroup domi_ome
  101. /// @brief process input data asynchronously
  102. /// cannot be invoked by multiple thread
  103. /// if one fails, other continue
  104. /// @param [in] input_data input data
  105. /// @return SUCCESS success
  106. /// @return PARAM_INVALID parameter invalid
  107. /// @return MODEL_NOT_READY model not ready
  108. /// @return PUSH_DATA_FAILED push data into model queue failed
  109. /// @author
  110. ///
  111. ge::Status DataInput(const InputData &input_data, OutputData &output_data);
  112. ge::Status DataInputTensor(uint32_t model_id, const std::vector<InputTensorInfo> &inputs);
  113. ///
  114. /// @ingroup domi_ome
  115. /// @brief model start to run
  116. ///
  117. ge::Status Start(uint32_t model_id);
  118. ///
  119. /// @ingroup domi_ome
  120. /// @brief ACL case, do not start new thread, return result
  121. /// @param [in] model_id model id
  122. /// @param [in] stream model stream
  123. /// @param [in] async_mode is asynchronize mode.
  124. /// @param [in] input_data model input data
  125. /// @param [out] output_data model output data
  126. ///
  127. ge::Status ExecuteModel(uint32_t model_id, rtStream_t stream, bool async_mode, const InputData &input_data,
  128. OutputData &output_data);
  129. ///
  130. /// @ingroup domi_ome
  131. /// @brief model stop
  132. ///
  133. ge::Status Stop(uint32_t model_id);
  134. ///
  135. /// @ingroup domi_ome
  136. /// @brief comment handle function
  137. ///
  138. ge::Status HandleCommand(const Command &command);
  139. static ge::Status HandleAclProfilingCommand(const Command &command);
  140. static ge::Status HandleProfileCommand(const Command &command);
  141. static ge::Status HandleDumpCommand(const Command &command);
  142. ///
  143. /// @ingroup domi_ome
  144. /// @brief get model memory usage
  145. /// @param [in] model_id model id
  146. /// @return SUCCESS success
  147. /// @return PARAM_INVALID parameter invalid
  148. ///
  149. ge::Status GetMaxUsedMemory(const uint32_t model_id, uint64_t &max_size);
  150. ///
  151. /// @ingroup domi_ome
  152. /// @brief get model input and output size
  153. /// @param [in] model_id model id
  154. /// @param [out] input_shape input tensor
  155. /// @param [out] output_shape output tensor
  156. /// @return SUCCESS success
  157. /// @return PARAM_INVALID parameter invalid
  158. ///
  159. ge::Status GetInputOutputDescInfo(const uint32_t model_id, std::vector<InputOutputDescInfo> &input_desc,
  160. std::vector<InputOutputDescInfo> &output_desc);
  161. ge::Status GetInputOutputDescInfo(const uint32_t model_id, std::vector<InputOutputDescInfo> &input_desc,
  162. std::vector<InputOutputDescInfo> &output_desc, std::vector<uint32_t> &inputFormats,
  163. std::vector<uint32_t> &outputFormats);
  164. ///
  165. /// @ingroup ge
  166. /// @brief Get dynamic batch_info
  167. /// @param [in] model_id
  168. /// @param [out] batch_info
  169. /// @return execute result
  170. ///
  171. ge::Status GetDynamicBatchInfo(const uint32_t model_id, std::vector<std::vector<int64_t>> &batch_info);
  172. ///
  173. /// @ingroup ge
  174. /// @brief Get AIPP info
  175. /// @param [in] model_id
  176. /// @param [in] index
  177. /// @param [out] aipp_info
  178. /// @return execute result
  179. ///
  180. ge::Status GetAIPPInfo(const uint32_t model_id, uint32_t index, AippConfigInfo &aipp_info);
  181. ///
  182. /// @ingroup domi_ome
  183. /// @brief set model input and output size zero copy
  184. /// @param [in] model_id model id
  185. /// @param [out] input_shape input tensor
  186. /// @param [out] output_shape output tensor
  187. /// @return SUCCESS success
  188. /// @return PARAM_INVALID parameter invalid
  189. ///
  190. ge::Status GetInputOutputDescInfoForZeroCopy(const uint32_t model_id, std::vector<InputOutputDescInfo> &input_desc,
  191. std::vector<InputOutputDescInfo> &output_desc,
  192. std::vector<uint32_t> &inputFormats,
  193. std::vector<uint32_t> &outputFormats);
  194. ge::Status GetCurShape(const uint32_t model_id, std::vector<int64_t> &batch_info);
  195. ge::Status GetModelAttr(uint32_t model_id, std::vector<string> &dynamic_output_shape_info);
  196. ge::Status SetDevice(int32_t deviceId) const;
  197. ge::Status SetDynamicSize(uint32_t model_id, const std::vector<uint64_t> &batch_num);
  198. ///
  199. /// @ingroup domi_ome
  200. /// @brief Get model according to given id
  201. ///
  202. std::shared_ptr<DavinciModel> GetModel(uint32_t id);
  203. std::shared_ptr<hybrid::HybridDavinciModel> GetHybridModel(uint32_t id);
  204. ge::Status KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType op_type, uint64_t session_id, uint32_t model_id);
  205. ge::Status CreateAicpuSession(uint64_t session_id);
  206. static ge::Status GetModelMemAndWeightSize(const ModelData &model, size_t &mem_size, size_t &weight_size);
  207. void DestroyAicpuSession(uint64_t session_id);
  208. ge::Status DestroyAicpuKernel(uint64_t session_id, uint32_t model_id);
  209. ge::Status CreateAicpuKernel(uint64_t session_id, uint32_t model_id, uint64_t kernel_id);
  210. ge::Status DestroyAicpuSessionForInfer(uint32_t model_id);
  211. ge::Status GetOrigInputInfo(uint32_t model_id, uint32_t index, OriginInputInfo &orig_input_info);
  212. ge::Status GenSessionId(uint64_t &session_id);
  213. ge::Status GetAllAippInputOutputDims(uint32_t model_id, uint32_t index, std::vector<InputOutputDims> &input_dims,
  214. std::vector<InputOutputDims> &output_dims);
  215. private:
  216. ///
  217. /// @ingroup domi_ome
  218. /// @brief constructor
  219. ///
  220. ModelManager();
  221. ///
  222. /// @ingroup domi_ome
  223. /// @brief destructor
  224. ///
  225. ~ModelManager();
  226. ///
  227. /// @ingroup domi_ome
  228. /// @brief insert new model into model manager set
  229. ///
  230. void InsertModel(uint32_t id, std::shared_ptr<DavinciModel> &davinci_model);
  231. void InsertModel(uint32_t id, std::shared_ptr<hybrid::HybridDavinciModel> &hybrid_model);
  232. ///
  233. /// @ingroup domi_ome
  234. /// @brief delete model from model manager set
  235. ///
  236. ge::Status DeleteModel(uint32_t id);
  237. void GenModelId(uint32_t *id);
  238. ge::Status UpdateSessionId(std::shared_ptr<DavinciModel> &davinci_model, uint64_t session_id);
  239. std::map<uint32_t, std::shared_ptr<DavinciModel>> model_map_;
  240. std::map<uint32_t, std::shared_ptr<hybrid::HybridDavinciModel>> hybrid_model_map_;
  241. std::map<std::string, std::vector<uint64_t>> model_aicpu_kernel_;
  242. uint32_t max_model_id_;
  243. std::mutex map_mutex_;
  244. std::mutex sess_ids_mutex_;
  245. std::mutex session_id_create_mutex_;
  246. uint64_t session_id_bias_;
  247. std::set<uint64_t> sess_ids_;
  248. };
  249. } // namespace ge
  250. #endif // GE_GRAPH_LOAD_NEW_MODEL_MANAGER_MODEL_MANAGER_H_

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