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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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] combined_dims: array of combined dimensions
  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> &combined_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. ge::Status GetCurShape(const uint32_t model_id, std::vector<int64_t> &batch_info, int32_t &dynamic_type);
  121. ///
  122. /// @ingroup ge
  123. /// @brief Set dynamic image info
  124. /// @param [in] model_id: model id allocate from manager
  125. /// @param [in] dynamic_input_addr: dynamic input addr created by user
  126. /// @param [in] length: length of dynamic input addr
  127. /// @param [in] aippBatchPara: kAippDynamicBatchPara vector by user in dynamic aipp
  128. /// @param [in] aippParms: kAippDynamicPara by user in dynamic aipp
  129. /// @return execute result
  130. ///
  131. ge::Status SetDynamicAippData(uint32_t model_id, void *dynamic_input_addr, uint64_t length,
  132. const std::vector<kAippDynamicBatchPara> &aippBatchPara,
  133. const kAippDynamicPara &aippParms);
  134. ge::Status GetAIPPInfo(uint32_t model_id, uint32_t index, AippConfigInfo &aipp_info);
  135. ge::Status GetModelAttr(uint32_t model_id, std::vector<std::string> &dynamic_output_shape_info);
  136. ge::Status GetModelDescInfoForZeroCopy(uint32_t model_id, std::vector<ge::TensorDesc> &input_desc,
  137. std::vector<ge::TensorDesc> &output_desc);
  138. ge::Status LoadModel(uint32_t &model_id, const ge::ModelData &model_data,
  139. std::shared_ptr<ge::ModelListener> listener);
  140. ge::Status CommandHandle(const ge::Command &command);
  141. ///
  142. /// @ingroup ge
  143. /// @brief Query model memory consuming interface
  144. /// @param [in] model_id Offline model ID
  145. /// @param [out] max_size Memory size
  146. /// @return SUCCESS
  147. /// @return FAILED
  148. ///
  149. ge::Status GetMaxUsedMemory(uint32_t model_id, uint32_t &max_size);
  150. ///
  151. /// @ingroup ge
  152. /// @brief Load data from model file to memory
  153. /// @param [in] const std::string &path: Offline model file path
  154. /// @param [out] ModelData &model_data: Offline model memory data
  155. /// @return SUCCESS handle successfully / others handle failed
  156. ///
  157. ge::Status LoadDataFromFile(const std::string &path, ge::ModelData &model_data);
  158. ///
  159. /// @ingroup ge
  160. /// @brief Load model from offline model memory data
  161. /// @param [in] ModelData &model_data: Offline model data
  162. /// @param [in] void *dev_ptr: Input/Output memory address
  163. /// @param [in] size_t mem_size: Input/Output memory length
  164. /// @param [in] void *weight_ptr: Weight memory address
  165. /// @param [in] size_t weight_size: Weight memory length
  166. /// @param [out] uint32_t &model_id: Corresponding identification after model loading
  167. /// @return SUCCESS handle successfully / others handle failed
  168. ///
  169. ge::Status LoadModelFromData(uint32_t &model_id, const ge::ModelData &model_data, void *dev_ptr, size_t mem_size,
  170. void *weight_ptr, size_t weight_size);
  171. ///
  172. /// @ingroup ge
  173. /// @brief Load task list from ModelData with queue.
  174. /// @param [out] model_id: model id allocate from manager.
  175. /// @param [in] model_data: Model data load from offline model.
  176. /// @param [in] input_queue_ids: input queue ids create from user.
  177. /// @param [in] output_queue_ids: input queue ids create from user.
  178. /// @return: 0 for success / others for fail
  179. ///
  180. ge::Status LoadModelWithQ(uint32_t &model_id, const ge::ModelData &model_data,
  181. const std::vector<uint32_t> &input_queue_ids,
  182. const std::vector<uint32_t> &output_queue_ids);
  183. ///
  184. /// @ingroup ge
  185. /// @brief Synchronous execution of offline model(Do not create thread)
  186. /// @param [in] uint32_t model_id: Model ID to execute
  187. /// @param [in] void* stream: stream to execute
  188. /// @param [in] bool async_mode: is asynchronize mode.
  189. /// @param [in] const domi::InputData *input_data: Model input data
  190. /// @param [out] domi::OutputData *output_data: Model output data
  191. /// @return SUCCESS handle successfully / others handle failed
  192. ///
  193. ge::Status ExecModel(uint32_t model_id, void *stream, const ge::RunModelData &input_data,
  194. ge::RunModelData &output_data, bool async_mode = false);
  195. ///
  196. /// @ingroup ge
  197. /// @brief Get weight memory size from model file
  198. /// @param [in] const std::string &path: Offline model file path
  199. /// @param [out] size_t &mem_size Execution memory size
  200. /// @param [out] size_t &weight_size Weight memory space size
  201. /// @return SUCCESS handle successfully / others handle failed
  202. ///
  203. ge::Status GetMemAndWeightSize(const std::string &path, size_t &mem_size, size_t &weight_size);
  204. ///
  205. /// @ingroup ge
  206. /// @brief Get weight memory size from model file
  207. /// @param [in] const void *model_data Offline model buffer
  208. /// @param [in] size_t model_size Offline model buffer length
  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 void *model_data, size_t model_size, size_t &mem_size, size_t &weight_size);
  214. static ge::Status LoadSingleOp(const std::string &modelName, const ge::ModelData &modelData, void *stream,
  215. SingleOp **single_op);
  216. static ge::Status ExecuteAsync(SingleOp *executor, const std::vector<DataBuffer> &inputs,
  217. std::vector<DataBuffer> &outputs);
  218. static ge::Status LoadDynamicSingleOp(const std::string &model_name, const ge::ModelData &modelData, void *stream,
  219. DynamicSingleOp **single_op);
  220. static ge::Status ExecuteAsync(DynamicSingleOp *executor, const std::vector<GeTensorDesc> &input_desc,
  221. const std::vector<DataBuffer> &inputs, std::vector<GeTensorDesc> &output_desc,
  222. std::vector<DataBuffer> &outputs);
  223. static ge::Status ReleaseSingleOpResource(void *stream);
  224. ge::Status GetBatchInfoSize(uint32_t model_id, size_t &shape_count);
  225. ge::Status GetOrigInputInfo(uint32_t model_id, uint32_t index, OriginInputInfo &orig_input_info);
  226. ge::Status GetAllAippInputOutputDims(uint32_t model_id, uint32_t index, std::vector<InputOutputDims> &input_dims,
  227. std::vector<InputOutputDims> &output_dims);
  228. private:
  229. static bool isInit_;
  230. };
  231. ge::Status ModelInfoParser(const ge::ModelData &model, ge::ModelInfo &model_info);
  232. } // namespace ge
  233. #endif // INC_FRAMEWORK_EXECUTOR_GE_EXECUTOR_H_

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