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.h 12 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 INC_FRAMEWORK_EXECUTOR_GE_EXECUTOR_H_
  17. #define INC_FRAMEWORK_EXECUTOR_GE_EXECUTOR_H_
  18. #include <memory>
  19. #include <string>
  20. #include <vector>
  21. #include "common/dynamic_aipp.h"
  22. #include "common/ge_inner_error_codes.h"
  23. #include "common/ge_types.h"
  24. #include "common/types.h"
  25. #include "graph/tensor.h"
  26. #include "graph/ge_tensor.h"
  27. #include "runtime/base.h"
  28. namespace ge {
  29. class ModelListenerAdapter;
  30. class SingleOp;
  31. class DynamicSingleOp;
  32. struct RunModelData {
  33. uint32_t index; // Data index
  34. uint32_t modelId;
  35. std::vector<DataBuffer> blobs; // All input/output data buffer
  36. uint32_t timestamp; // Data creation time
  37. uint32_t timeout; // Processing timeout
  38. uint64_t request_id = 0; // Request ID
  39. uint64_t dynamic_batch_size = 0; // Dynamic batch size scene, set dynamic size, not supported by default:0
  40. uint64_t dynamic_image_height = 0; // Dynamic image size scene, set image height, not supported by default:0
  41. uint64_t dynamic_image_width = 0; // Dynamic image size scene, set image width, not supported by default:0
  42. std::vector<uint64_t> dynamic_dims; // Dynamic dims scene, set dynamic dims, not supported by default:empty
  43. };
  44. class GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY GeExecutor {
  45. public:
  46. GeExecutor();
  47. ~GeExecutor() = default;
  48. ge::Status Initialize();
  49. ge::Status Finalize();
  50. // Load model
  51. ge::Status LoadModelOffline(uint32_t &model_id, const std::string &path, const std::string &key, int32_t priority,
  52. std::shared_ptr<ge::ModelListener> listener);
  53. ge::Status UnloadModel(uint32_t modelId);
  54. ge::Status RunModel(const ge::RunModelData &input_data, ge::RunModelData &output_data);
  55. // Get input and output descriptor
  56. ge::Status GetModelDescInfo(uint32_t model_id, std::vector<ge::TensorDesc> &input_desc,
  57. std::vector<ge::TensorDesc> &output_desc, bool new_model_desc = false);
  58. ///
  59. /// @ingroup ge
  60. /// @brief Set dynamic batch size
  61. /// @param [in] model_id: model id allocate from manager
  62. /// @param [in] dynamic_input_addr: dynamic input addr created by user
  63. /// @param [in] length: length of dynamic input addr
  64. /// @param [in] batch_size: batch size entered by user in dynamic multi-batch scenario
  65. /// @return execute result
  66. ///
  67. ge::Status SetDynamicBatchSize(uint32_t model_id, void *dynamic_input_addr, uint64_t length, uint64_t batch_size);
  68. ///
  69. /// @ingroup ge
  70. /// @brief Set dynamic image info
  71. /// @param [in] model_id: model id allocate from manager
  72. /// @param [in] dynamic_input_addr: dynamic input addr created by user
  73. /// @param [in] length: length of dynamic input addr
  74. /// @param [in] image_height: image height entered by user in dynamic multi-resolution scenario
  75. /// @param [in] image_width: image width entered by user in dynamic multi-resolution scenario
  76. /// @return execute result
  77. ///
  78. ge::Status SetDynamicImageSize(uint32_t model_id, void *dynamic_input_addr, uint64_t length, uint64_t image_height,
  79. uint64_t image_width);
  80. ///
  81. /// @ingroup ge
  82. /// @brief Set dynamic dims info
  83. /// @param [in] model_id: model id allocate from manager
  84. /// @param [in] dynamic_input_addr: dynamic input addr created by user
  85. /// @param [in] length: length of dynamic input addr
  86. /// @param [in] dynamic_dim_num: number of dynamic dimension
  87. /// @param [in] dynamic_dims: array of dynamic dimensions
  88. /// @return execute result
  89. ///
  90. ge::Status SetDynamicDims(uint32_t model_id, void *dynamic_input_addr, uint64_t length,
  91. const std::vector<uint64_t> &dynamic_dims);
  92. ///
  93. /// @ingroup ge
  94. /// @brief Get current dynamic dims info by combined dims
  95. /// @param [in] model_id: model id allocate from manager
  96. /// @param [in] dynamic_dims: cur gear dynamic dims value
  97. /// @param [out] cur_dynamic_dims: current dynamic dims
  98. /// @return execute result
  99. ///
  100. ge::Status GetCurDynamicDims(uint32_t model_id, const std::vector<uint64_t> &dynamic_dims,
  101. std::vector<uint64_t> &cur_dynamic_dims);
  102. ///
  103. /// @ingroup ge
  104. /// @brief Get dynamic batch_info
  105. /// @param [in] model_id
  106. /// @param [out] batch_info
  107. /// @param [out] dynamic_type
  108. /// @return execute result
  109. ///
  110. ge::Status GetDynamicBatchInfo(uint32_t model_id, std::vector<std::vector<int64_t>> &batch_info,
  111. int32_t &dynamic_type);
  112. ///
  113. /// @ingroup ge
  114. /// @brief Get combined dynamic dims info
  115. /// @param [in] model_id
  116. /// @param [out] batch_info
  117. /// @return execute result
  118. ///
  119. ge::Status GetCombinedDynamicDims(uint32_t model_id, std::vector<std::vector<int64_t>> &batch_info);
  120. ///
  121. /// @ingroup ge
  122. /// @brief Get user designeate shape order
  123. /// @param [in] model_id
  124. /// @param [out] user_designate_shape_order
  125. /// @return execute result
  126. ///
  127. ge::Status GetUserDesignateShapeOrder(uint32_t model_id, std::vector<std::string> &user_designate_shape_order);
  128. ge::Status GetCurShape(const uint32_t model_id, std::vector<int64_t> &batch_info, int32_t &dynamic_type);
  129. ///
  130. /// @ingroup ge
  131. /// @brief Set dynamic image info
  132. /// @param [in] model_id: model id allocate from manager
  133. /// @param [in] dynamic_input_addr: dynamic input addr created by user
  134. /// @param [in] length: length of dynamic input addr
  135. /// @param [in] aippBatchPara: kAippDynamicBatchPara vector by user in dynamic aipp
  136. /// @param [in] aippParms: kAippDynamicPara by user in dynamic aipp
  137. /// @return execute result
  138. ///
  139. ge::Status SetDynamicAippData(uint32_t model_id, void *dynamic_input_addr, uint64_t length,
  140. const std::vector<kAippDynamicBatchPara> &aippBatchPara,
  141. const kAippDynamicPara &aippParms);
  142. ge::Status GetAIPPInfo(uint32_t model_id, uint32_t index, AippConfigInfo &aipp_info);
  143. ge::Status GetModelAttr(uint32_t model_id, std::vector<std::string> &dynamic_output_shape_info);
  144. ge::Status GetAippType(uint32_t model_id, uint32_t index, InputAippType &type, size_t &aipp_index);
  145. ge::Status GetModelDescInfoForZeroCopy(uint32_t model_id, std::vector<ge::TensorDesc> &input_desc,
  146. std::vector<ge::TensorDesc> &output_desc);
  147. ge::Status LoadModel(uint32_t &model_id, const ge::ModelData &model_data,
  148. std::shared_ptr<ge::ModelListener> listener);
  149. ge::Status CommandHandle(const ge::Command &command);
  150. ge::Status SetDump(const DumpConfig &dump_config);
  151. ///
  152. /// @ingroup ge
  153. /// @brief Query model memory consuming interface
  154. /// @param [in] model_id Offline model ID
  155. /// @param [out] max_size Memory size
  156. /// @return SUCCESS
  157. /// @return FAILED
  158. ///
  159. ge::Status GetMaxUsedMemory(uint32_t model_id, uint32_t &max_size);
  160. ///
  161. /// @ingroup ge
  162. /// @brief Load data from model file to memory
  163. /// @param [in] const std::string &path: Offline model file path
  164. /// @param [out] ModelData &model_data: Offline model memory data
  165. /// @return SUCCESS handle successfully / others handle failed
  166. ///
  167. ge::Status LoadDataFromFile(const std::string &path, ge::ModelData &model_data);
  168. ///
  169. /// @ingroup ge
  170. /// @brief Load model from offline model memory data
  171. /// @param [in] ModelData &model_data: Offline model data
  172. /// @param [in] void *dev_ptr: Input/Output memory address
  173. /// @param [in] size_t mem_size: Input/Output memory length
  174. /// @param [in] void *weight_ptr: Weight memory address
  175. /// @param [in] size_t weight_size: Weight memory length
  176. /// @param [out] uint32_t &model_id: Corresponding identification after model loading
  177. /// @return SUCCESS handle successfully / others handle failed
  178. ///
  179. ge::Status LoadModelFromData(uint32_t &model_id, const ge::ModelData &model_data, void *dev_ptr, size_t mem_size,
  180. void *weight_ptr, size_t weight_size);
  181. ///
  182. /// @ingroup ge
  183. /// @brief Load task list from ModelData with queue.
  184. /// @param [out] model_id: model id allocate from manager.
  185. /// @param [in] model_data: Model data load from offline model.
  186. /// @param [in] input_queue_ids: input queue ids create from user.
  187. /// @param [in] output_queue_ids: input queue ids create from user.
  188. /// @return: 0 for success / others for fail
  189. ///
  190. ge::Status LoadModelWithQ(uint32_t &model_id, const ge::ModelData &model_data,
  191. const std::vector<uint32_t> &input_queue_ids,
  192. const std::vector<uint32_t> &output_queue_ids);
  193. ///
  194. /// @ingroup ge
  195. /// @brief Synchronous execution of offline model(Do not create thread)
  196. /// @param [in] uint32_t model_id: Model ID to execute
  197. /// @param [in] void* stream: stream to execute
  198. /// @param [in] bool async_mode: is asynchronize mode.
  199. /// @param [in] const domi::InputData *input_data: Model input data
  200. /// @param [out] domi::OutputData *output_data: Model output data
  201. /// @return SUCCESS handle successfully / others handle failed
  202. ///
  203. ge::Status ExecModel(uint32_t model_id, void *stream, const ge::RunModelData &input_data,
  204. ge::RunModelData &output_data, bool async_mode = false);
  205. ///
  206. /// @ingroup ge
  207. /// @brief Get weight memory size from model file
  208. /// @param [in] const std::string &path: Offline model file path
  209. /// @param [out] size_t &mem_size Execution memory size
  210. /// @param [out] size_t &weight_size Weight memory space size
  211. /// @return SUCCESS handle successfully / others handle failed
  212. ///
  213. ge::Status GetMemAndWeightSize(const std::string &path, size_t &mem_size, size_t &weight_size);
  214. ///
  215. /// @ingroup ge
  216. /// @brief Get weight memory size from model file
  217. /// @param [in] const void *model_data Offline model buffer
  218. /// @param [in] size_t model_size Offline model buffer length
  219. /// @param [out] size_t &mem_size Execution memory size
  220. /// @param [out] size_t &weight_size Weight memory space size
  221. /// @return SUCCESS handle successfully / others handle failed
  222. ///
  223. ge::Status GetMemAndWeightSize(const void *model_data, size_t model_size, size_t &mem_size, size_t &weight_size);
  224. static ge::Status LoadSingleOp(const std::string &modelName, const ge::ModelData &modelData, void *stream,
  225. SingleOp **single_op);
  226. static ge::Status ExecuteAsync(SingleOp *executor, const std::vector<DataBuffer> &inputs,
  227. std::vector<DataBuffer> &outputs);
  228. static ge::Status LoadDynamicSingleOp(const std::string &model_name, const ge::ModelData &modelData, void *stream,
  229. DynamicSingleOp **single_op);
  230. static ge::Status ExecuteAsync(DynamicSingleOp *executor, const std::vector<GeTensorDesc> &input_desc,
  231. const std::vector<DataBuffer> &inputs, std::vector<GeTensorDesc> &output_desc,
  232. std::vector<DataBuffer> &outputs);
  233. static ge::Status ReleaseSingleOpResource(void *stream);
  234. static ge::Status GetDeviceIdByModelId(uint32_t model_id, uint32_t &device_id);
  235. ge::Status GetBatchInfoSize(uint32_t model_id, size_t &shape_count);
  236. ge::Status GetOrigInputInfo(uint32_t model_id, uint32_t index, OriginInputInfo &orig_input_info);
  237. ge::Status GetAllAippInputOutputDims(uint32_t model_id, uint32_t index, std::vector<InputOutputDims> &input_dims,
  238. std::vector<InputOutputDims> &output_dims);
  239. ge::Status GetOpDescInfo(uint32_t device_id, uint32_t stream_id, uint32_t task_id, OpDescInfo &op_desc_info);
  240. private:
  241. static bool isInit_;
  242. };
  243. ge::Status ModelInfoParser(const ge::ModelData &model, ge::ModelInfo &model_info);
  244. } // namespace ge
  245. #endif // INC_FRAMEWORK_EXECUTOR_GE_EXECUTOR_H_

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