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.

davinci_model.h 30 kB

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
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
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
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
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
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
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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_DAVINCI_MODEL_H_
  17. #define GE_GRAPH_LOAD_NEW_MODEL_MANAGER_DAVINCI_MODEL_H_
  18. #include <map>
  19. #include <memory>
  20. #include <set>
  21. #include <string>
  22. #include <thread>
  23. #include <vector>
  24. #include "common/ge_types.h"
  25. #include "common/helper/model_helper.h"
  26. #include "common/helper/om_file_helper.h"
  27. #include "common/opskernel/ge_task_info.h"
  28. #include "common/properties_manager.h"
  29. #include "common/types.h"
  30. #include "framework/common/util.h"
  31. #include "graph/debug/ge_attr_define.h"
  32. #include "graph/load/new_model_manager/aipp_utils.h"
  33. #include "graph/load/new_model_manager/data_dumper.h"
  34. #include "graph/load/new_model_manager/data_inputer.h"
  35. #include "graph/load/new_model_manager/model_utils.h"
  36. #include "graph/load/new_model_manager/zero_copy_offset.h"
  37. #include "graph/load/new_model_manager/zero_copy_task.h"
  38. #include "graph/model.h"
  39. #include "graph/node.h"
  40. #include "graph/op_desc.h"
  41. #include "graph/operator.h"
  42. #include "graph/utils/attr_utils.h"
  43. #include "graph/utils/tensor_utils.h"
  44. #include "mmpa/mmpa_api.h"
  45. #include "proto/task.pb.h"
  46. #include "task_info/task_info.h"
  47. namespace ge {
  48. // op debug need 2048 bits buffer
  49. const size_t kOpDebugMemorySize = 2048UL;
  50. const size_t kDebugP2pSize = 8UL;
  51. typedef enum tagModelProcStage {
  52. MODEL_LOAD_START = 1,
  53. MODEL_LOAD_END,
  54. MODEL_PRE_PROC_START,
  55. MODEL_PRE_PROC_END,
  56. MODEL_INFER_START,
  57. MODEL_INFER_END,
  58. MODEL_AFTER_PROC_START,
  59. MODEL_AFTER_PROC_END,
  60. MODEL_PROC_INVALID,
  61. } ModelProcStage;
  62. struct timeInfo {
  63. uint32_t modelId;
  64. int64_t processBeginTime;
  65. int64_t processEndTime;
  66. int64_t inferenceBeginTime;
  67. int64_t inferenceEndTime;
  68. int64_t dumpBeginTime;
  69. int64_t dumpEndTime;
  70. };
  71. enum ExecuteMode {
  72. INITIALIZATION,
  73. SYNCHRONIZATION,
  74. ASYNCHRONIZATION,
  75. };
  76. // comments
  77. class DavinciModel {
  78. public:
  79. ///
  80. /// @ingroup ge
  81. /// @brief DavinciModel constructor
  82. /// @author
  83. ///
  84. DavinciModel(int32_t priority, const std::shared_ptr<ModelListener> &listener);
  85. ///
  86. /// @ingroup ge
  87. /// @brief DavinciModel desctructor, free Parse and Init resources
  88. /// @author
  89. ///
  90. ~DavinciModel();
  91. ///
  92. /// @ingroup ge
  93. /// @brief apply model to model_def_
  94. ///
  95. Status Assign(const GeModelPtr &ge_model);
  96. ///
  97. /// @ingroup ge
  98. /// @brief DavinciModel initialization, including Stream, ccHandle, Event, DataInputer, etc
  99. /// @return execute result
  100. /// @author
  101. ///
  102. Status Init(void *dev_ptr = nullptr, size_t memsize = 0, void *weight_ptr = nullptr, size_t weightsize = 0);
  103. ///
  104. /// @ingroup ge
  105. /// @brief ACL case, Load task list with queue.
  106. /// @param [in] input_que_ids: input queue ids from user, nums equal Data Op.
  107. /// @param [in] output_que_ids: input queue ids from user, nums equal NetOutput Op.
  108. /// @return: 0 for success / others for fail
  109. ///
  110. Status SetQueIds(const std::vector<uint32_t> &input_queue_ids, const std::vector<uint32_t> &output_queue_ids);
  111. ///
  112. /// @ingroup ge
  113. /// @brief Get DataInputer
  114. /// @return model ID
  115. ///
  116. uint32_t Id() const { return model_id_; }
  117. ///
  118. /// @ingroup ge
  119. /// @brief Get DataInputer
  120. /// @return model ID
  121. ///
  122. void SetId(uint32_t model_id) { model_id_ = model_id; }
  123. static void *Run(DavinciModel *model_pointer);
  124. ///
  125. /// @ingroup ge
  126. /// @brief NnExecute
  127. /// @param [in] stream execute stream
  128. /// @param [in] async_mode is asynchronize mode.
  129. /// @param [in] input_data model input data
  130. /// @param [out] output_data model output data
  131. ///
  132. Status NnExecute(rtStream_t stream, bool async_mode, const InputData &input_data, OutputData &output_data);
  133. ///
  134. /// @ingroup ge
  135. /// @brief lock mutex run flag
  136. /// @author
  137. ///
  138. void LockRunFlg() { mux_run_flg_.lock(); }
  139. ///
  140. /// @ingroup ge
  141. /// @brief unlock mutex run flag
  142. /// @author
  143. ///
  144. void UnlockRunFlg() { mux_run_flg_.unlock(); }
  145. ///
  146. /// @ingroup ge
  147. /// @brief get DataInputer
  148. /// @return DataInputer pointer
  149. ///
  150. DataInputer *const GetDataInputer() const { return data_inputer_; }
  151. // get Stream number
  152. uint32_t StreamNum() const { return runtime_param_.stream_num; }
  153. // get Event number
  154. uint32_t EventNum() const { return runtime_param_.event_num; }
  155. // get Lable number
  156. uint32_t LabelNum() const { return runtime_param_.label_num; }
  157. // get batch number
  158. uint32_t BatchNum() const { return runtime_param_.batch_num; }
  159. // get session id
  160. uint64_t SessionId() const { return runtime_param_.session_id; }
  161. // get model priority
  162. int32_t Priority() const { return priority_; }
  163. // get total mem size
  164. size_t TotalMemSize() const { return runtime_param_.mem_size; }
  165. const std::map<uint32_t, MemInfo> &P2PMemInfos() const {return runtime_param_.memory_infos;}
  166. // model name
  167. string Name() const { return name_; }
  168. // om_name
  169. string OmName() const { return om_name_; }
  170. // version
  171. uint32_t Version() const { return version_; }
  172. // get total weights mem size
  173. size_t TotalWeightsMemSize() const { return runtime_param_.weight_size; }
  174. size_t TotalVarMemSize() const { return runtime_param_.var_size; }
  175. // get base memory address
  176. uint8_t *MemBase() { return mem_base_; }
  177. // get weight base memory address
  178. uint8_t *WeightsMemBase() { return weights_mem_base_; }
  179. uint8_t *VarMemBase() { return var_mem_base_; }
  180. // get Event list
  181. const vector<rtEvent_t> &GetEventList() const { return event_list_; }
  182. const vector<rtStream_t> &GetStreamList() const { return stream_list_; }
  183. const vector<rtLabel_t> &GetLabelList() const { return label_list_; }
  184. Status DestroyThread();
  185. // Get Data Op.
  186. const vector<OpDescPtr> &GetDataList() const { return data_op_list_; }
  187. // get Op
  188. const map<uint32_t, OpDescPtr> &GetOpList() const { return op_list_; }
  189. OpDescPtr GetOpByIndex(uint32_t index) const {
  190. if (op_list_.find(index) == op_list_.end()) {
  191. return nullptr;
  192. }
  193. return op_list_.at(index);
  194. }
  195. OpDescPtr GetVariableOp(const string &name) {
  196. for (auto op_desc : variable_op_list_) {
  197. if (op_desc != nullptr && op_desc->GetName() == name) {
  198. return op_desc;
  199. }
  200. }
  201. return nullptr;
  202. }
  203. // get task info for profiling
  204. const std::vector<TaskDescInfo> &GetTaskDescInfo() const { return task_desc_info_; }
  205. // get updated task info list
  206. std::vector<TaskInfoPtr> GetTaskList() { return task_list_; }
  207. ///
  208. /// @ingroup ge
  209. /// @brief get model input and output format
  210. /// @return ccTensorFormat_t current model input and output format
  211. ///
  212. Format GetFormat();
  213. rtModel_t GetRtModelHandle() const { return rt_model_handle_; }
  214. rtStream_t GetRtModelStream() const { return rt_model_stream_; }
  215. uint64_t GetRtBaseAddr() const { return runtime_param_.logic_mem_base; }
  216. uint64_t GetRtWeightAddr() const { return runtime_param_.logic_weight_base; }
  217. uint64_t GetRtVarAddr() const { return runtime_param_.logic_var_base; }
  218. uint32_t GetFlowctrlIndex(uint32_t op_index);
  219. void PushHcclStream(rtStream_t value);
  220. bool IsBroadCastOpData(const NodePtr &var_node);
  221. ///
  222. /// @ingroup ge
  223. /// @brief For TVM Op, avoid Addr Reuse.
  224. /// @return void*
  225. ///
  226. const char *GetRegisterStub(const string &tvm_binfile_key, const string &session_graph_model_id = "");
  227. ///
  228. /// @ingroup ge
  229. /// @brief get model input and output desc info
  230. /// @param [out] input_shape model input size
  231. /// @param [out] output_shape model output size
  232. /// @return execute result
  233. ///
  234. Status GetInputOutputDescInfo(vector<InputOutputDescInfo> &input_desc, vector<InputOutputDescInfo> &output_desc);
  235. Status GetInputOutputDescInfo(vector<InputOutputDescInfo> &input_desc, vector<InputOutputDescInfo> &output_desc,
  236. std::vector<uint32_t> &inputFormats, std::vector<uint32_t> &output_formats);
  237. ///
  238. /// @ingroup ge
  239. /// @brief Get dynamic batch_info
  240. /// @param [out] batch_info
  241. /// @param [out] dynamic_type
  242. /// @return execute result
  243. ///
  244. Status GetDynamicBatchInfo(std::vector<std::vector<int64_t>> &batch_info, int32_t &dynamic_type) const;
  245. ///
  246. /// @ingroup ge
  247. /// @brief Get combined dynamic dims info
  248. /// @param [out] batch_info
  249. /// @return None
  250. ///
  251. void GetCombinedDynamicDims(std::vector<std::vector<int64_t>> &batch_info) const;
  252. void GetUserDesignateShapeOrder(std::vector<std::string> &user_input_shape_order) const;
  253. void GetCurShape(std::vector<int64_t> &batch_info, int32_t &dynamic_type);
  254. void GetModelAttr(std::vector<std::string> &dynamic_output_shape_info);
  255. ///
  256. /// @ingroup ge
  257. /// @brief Get AIPP input info
  258. /// @param [in] index
  259. /// @param [out] aipp_info
  260. /// @return execute result
  261. ///
  262. Status GetAIPPInfo(uint32_t index, AippConfigInfo &aipp_info);
  263. Status GetAippType(uint32_t index, InputAippType &type, size_t &aipp_index);
  264. ///
  265. /// @ingroup ge
  266. /// @brief Get model_id.
  267. /// @return model_id
  268. ///
  269. uint32_t GetModelId() const { return model_id_; }
  270. ///
  271. /// @ingroup ge
  272. /// @brief get unique identification for op when load two or more models
  273. /// @param [in] op_desc : current op.
  274. /// @param [in] string identification: unique identification for current op.
  275. /// @return None
  276. ///
  277. void GetUniqueId(const OpDescPtr &op_desc, std::string &unique_identification);
  278. ///
  279. /// @ingroup ge
  280. /// @brief get model input and output desc for zero copy
  281. /// @param [out] input_shape model input size
  282. /// @param [out] output_shape model output size
  283. /// @return execute result
  284. ///
  285. Status GetInputOutputDescInfoForZeroCopy(vector<InputOutputDescInfo> &input_desc,
  286. vector<InputOutputDescInfo> &output_desc,
  287. std::vector<uint32_t> &inputFormats, std::vector<uint32_t> &output_formats);
  288. Status ReturnResult(uint32_t data_id, const bool rslt_flg, const bool seq_end_flg, OutputData *output_data);
  289. Status ReturnNoOutput(uint32_t data_id);
  290. Status ModelRunStart();
  291. ///
  292. /// @ingroup ge
  293. /// @brief stop run model
  294. /// @return Status
  295. ///
  296. Status ModelRunStop();
  297. ///
  298. /// @ingroup ge
  299. /// @brief model run flag
  300. /// @return Status
  301. ///
  302. bool RunFlag() const { return run_flg_; }
  303. Status GetOutputDescInfo(vector<InputOutputDescInfo> &output_desc, std::vector<uint32_t> &formats);
  304. ///
  305. /// @ingroup ge
  306. /// @brief Set Session Id
  307. /// @return void
  308. ///
  309. void SetSessionId(uint64_t session_id) { session_id_ = session_id; }
  310. ///
  311. /// @ingroup ge
  312. /// @brief Get Session Id
  313. /// @return sessionID
  314. ///
  315. uint64_t GetSessionId() const { return session_id_; }
  316. ///
  317. /// @ingroup ge
  318. /// @brief SetDeviceId
  319. /// @return void
  320. ///
  321. void SetDeviceId(uint32_t device_id) { device_id_ = device_id; }
  322. ///
  323. /// @ingroup ge
  324. /// @brief Get device Id
  325. /// @return device id
  326. ///
  327. uint32_t GetDeviceId() const { return device_id_; }
  328. bool NeedDestroyAicpuKernel() const { return need_destroy_aicpu_kernel_; }
  329. Status UpdateSessionId(uint64_t session_id);
  330. const RuntimeParam &GetRuntimeParam() { return runtime_param_; }
  331. int32_t GetDataInputTid() const { return dataInputTid; }
  332. void SetDataInputTid(int32_t data_input_tid) { dataInputTid = data_input_tid; }
  333. void DisableZeroCopy(const void *addr);
  334. bool GetOpDugReg() const { return is_op_debug_reg_; }
  335. ///
  336. /// @ingroup ge
  337. /// @brief Save outside address of Data or NetOutput used info for ZeroCopy.
  338. /// @param [in] const OpDescPtr &op_desc: current op desc
  339. /// @param [in] const std::vector<void *> &outside_addrs: address of task
  340. /// @param [in] const void *args_offset: arguments address save the address.
  341. /// @return None.
  342. ///
  343. void SetZeroCopyAddr(const OpDescPtr &op_desc, const std::vector<void *> &outside_addrs, const void *info, void *args,
  344. size_t size, size_t offset);
  345. void SetDynamicSize(const std::vector<uint64_t> &batch_num, int32_t dynamic_type);
  346. bool GetL1FusionEnableOption() { return is_l1_fusion_enable_; }
  347. void SetProfileTime(ModelProcStage stage, int64_t endTime = 0);
  348. int64_t GetLoadBeginTime() { return load_begin_time_; }
  349. int64_t GetLoadEndTime() { return load_end_time_; }
  350. Status SinkModelProfile();
  351. Status SinkTimeProfile(const InputData &current_data);
  352. Status ReportProfilingData(bool check_device = true);
  353. void SaveDumpOpInfo(const RuntimeParam &model_param, const OpDescPtr &op, uint32_t task_id, uint32_t stream_id) {
  354. data_dumper_.SaveDumpOpInfo(model_param, op, task_id, stream_id);
  355. }
  356. void SaveDumpTask(uint32_t task_id, uint32_t stream_id, const std::shared_ptr<OpDesc> &op_desc, uintptr_t args) {
  357. data_dumper_.SaveDumpTask(task_id, stream_id, op_desc, args);
  358. }
  359. void SetEndGraphId(uint32_t task_id, uint32_t stream_id);
  360. DavinciModel &operator=(const DavinciModel &model) = delete;
  361. DavinciModel(const DavinciModel &model) = delete;
  362. const map<int64_t, std::vector<rtStream_t>> &GetHcclFolowStream() {
  363. return main_follow_stream_mapping_;
  364. }
  365. void SaveHcclFollowStream(int64_t main_stream_id, rtStream_t stream);
  366. void InitRuntimeParams();
  367. Status InitVariableMem();
  368. void UpdateMemBase(uint8_t *mem_base) {
  369. runtime_param_.mem_base = mem_base;
  370. mem_base_ = mem_base;
  371. }
  372. void SetTotalArgsSize(uint32_t args_size) { total_args_size_ += args_size; }
  373. uint32_t GetTotalArgsSize() { return total_args_size_; }
  374. void *GetCurrentArgsAddr(uint32_t offset) {
  375. void *cur_args = static_cast<char *>(args_) + offset;
  376. return cur_args;
  377. }
  378. void SetTotalIOAddrs(vector<void *> &io_addrs) {
  379. total_io_addrs_.insert(total_io_addrs_.end(), io_addrs.begin(), io_addrs.end());
  380. }
  381. void SetTotalFixedAddrsSize(string tensor_name, int64_t fix_addr_size);
  382. int64_t GetFixedAddrsSize(string tensor_name);
  383. void *GetCurrentFixedAddr(int64_t offset) const {
  384. void *cur_addr = static_cast<char *>(fixed_addrs_) + offset;
  385. return cur_addr;
  386. }
  387. uint32_t GetFixedAddrOutputIndex(string tensor_name) {
  388. if (tensor_name_to_peer_output_index_.find(tensor_name) != tensor_name_to_peer_output_index_.end()) {
  389. return tensor_name_to_peer_output_index_[tensor_name];
  390. }
  391. return UINT32_MAX;
  392. }
  393. void SetKnownNode(bool known_node) { known_node_ = known_node; }
  394. bool IsKnownNode() { return known_node_; }
  395. Status MallocKnownArgs();
  396. Status UpdateKnownNodeArgs(const vector<void *> &inputs, const vector<void *> &outputs);
  397. Status CreateKnownZeroCopyMap(const vector<void *> &inputs, const vector<void *> &outputs);
  398. Status UpdateKnownZeroCopyAddr();
  399. void SetKnownNodeAddrNotChanged(bool base_addr_not_changed) { base_addr_not_changed_ = base_addr_not_changed; }
  400. Status GetOrigInputInfo(uint32_t index, OriginInputInfo &orig_input_info);
  401. Status GetAllAippInputOutputDims(uint32_t index, std::vector<InputOutputDims> &input_dims,
  402. std::vector<InputOutputDims> &output_dims);
  403. void SetModelDescVersion(bool is_new_model_desc) { is_new_model_desc_ = is_new_model_desc; }
  404. // om file name
  405. void SetOmName(string om_name) { om_name_ = om_name; }
  406. void SetDumpProperties(const DumpProperties &dump_properties) { data_dumper_.SetDumpProperties(dump_properties); }
  407. const DumpProperties &GetDumpProperties() const { return data_dumper_.GetDumpProperties(); }
  408. bool GetOpDescInfo(uint32_t stream_id, uint32_t task_id, OpDescInfo &op_desc_info) const {
  409. return data_dumper_.GetOpDescInfo(stream_id, task_id, op_desc_info);
  410. }
  411. Status InitInputOutputForDynamic(const ComputeGraphPtr &compute_graph);
  412. private:
  413. // memory address of weights
  414. uint8_t *weights_mem_base_;
  415. uint8_t *var_mem_base_;
  416. // memory address of model
  417. uint8_t *mem_base_;
  418. uint8_t *p2p_mem_base_;
  419. bool is_inner_mem_base_;
  420. bool is_inner_weight_base_;
  421. bool is_inner_p2p_mem_base_;
  422. // input data manager
  423. DataInputer *data_inputer_;
  424. int64_t load_begin_time_;
  425. int64_t load_end_time_;
  426. struct timeInfo time_info_;
  427. int32_t dataInputTid;
  428. ///
  429. /// @ingroup ge
  430. /// @brief Save Batch label Info.
  431. /// @param [in] const OpDescPtr &op_desc
  432. /// @param [in] uintptr_t addr: address value in args block.
  433. /// @return None.
  434. ///
  435. void SetBatchLabelAddr(const OpDescPtr &op_desc, uintptr_t addr);
  436. ///
  437. /// @ingroup ge
  438. /// @brief Copy Check input size and model op size.
  439. /// @param [in] const int64_t &input_size: input size.
  440. /// @param [in] const int64_t &op_size: model op size.
  441. /// @param [in] is_dynamic: dynamic batch input flag.
  442. /// @return true if success
  443. ///
  444. bool CheckInputAndModelSize(const int64_t &input_size, const int64_t &op_size, bool is_dynamic);
  445. ///
  446. /// @ingroup ge
  447. /// @brief Set copy only for No task feed NetOutput address.
  448. /// @return None.
  449. ///
  450. void SetCopyOnlyOutput();
  451. ///
  452. /// @ingroup ge
  453. /// @brief Copy Input/Output to model for direct use.
  454. /// @param [in] const InputData &input_data: user input data info.
  455. /// @param [in/out] OutputData &output_data: user output data info.
  456. /// @param [in] bool is_dynamic: whether is dynamic input, true: is dynamic input; false: not is dynamic input
  457. /// @return SUCCESS handle successfully / others handle failed
  458. ///
  459. Status CopyModelData(const InputData &input_data, OutputData &output_data, bool is_dynamic);
  460. ///
  461. /// @ingroup ge
  462. /// @brief Copy Data addr to model for direct use.
  463. /// @param [in] data_info: model memory addr/size map { data_index, { tensor_size, tensor_addr } }.
  464. /// @param [in] is_input: input data or output data
  465. /// @param [in] blobs: user input/output data list.
  466. /// @param [in] is_dynamic: whether is dynamic input, true: is dynamic input; false: not is dynamic input
  467. /// @param [in] batch_label: batch label for multi-batch scenes
  468. /// @return SUCCESS handle successfully / others handle failed
  469. ///
  470. Status UpdateIoTaskArgs(const std::map<uint32_t, ZeroCopyOffset> &data_info, bool is_input,
  471. const vector<DataBuffer> &blobs, bool is_dynamic, const string &batch_label);
  472. Status CopyInputData(const InputData &input_data, bool device_data = false);
  473. Status CopyOutputData(uint32_t data_id, OutputData &output_data, rtMemcpyKind_t kind);
  474. Status SyncVarData();
  475. Status InitModelMem(void *dev_ptr, size_t memsize, void *weight_ptr, size_t weightsize);
  476. void CreateInputDimsInfo(const OpDescPtr &op_desc, Format format, InputOutputDescInfo &input);
  477. void SetInputDimsInfo(const vector<int64_t> &model_input_dims, Format &format, InputOutputDescInfo &input);
  478. Status GetInputDescInfo(vector<InputOutputDescInfo> &input_desc, std::vector<uint32_t> &formats);
  479. Status InitTaskInfo(domi::ModelTaskDef &modelTaskInfo);
  480. void UnbindHcomStream();
  481. Status DistributeTask();
  482. uint8_t *MallocFeatureMapMem(size_t data_size);
  483. uint8_t *MallocWeightsMem(size_t weights_size);
  484. uint8_t* MallocP2PMem(size_t p2p_data_size);
  485. void FreeFeatureMapMem();
  486. void FreeWeightsMem();
  487. void FreeP2PMem();
  488. void ReleaseTask();
  489. void UnbindTaskSinkStream();
  490. bool IsAicpuKernelConnectSpecifiedLayer();
  491. ///
  492. /// @ingroup ge
  493. /// @brief Reduce memory usage after task sink.
  494. /// @return: void
  495. ///
  496. void Shrink();
  497. ///
  498. /// @ingroup ge
  499. /// @brief Travel all nodes and do some init.
  500. /// @param [in] compute_graph: ComputeGraph to load.
  501. /// @return Status
  502. ///
  503. Status InitNodes(const ComputeGraphPtr &compute_graph);
  504. ///
  505. /// @ingroup ge
  506. /// @brief Data Op Initialize.
  507. /// @param [in] NodePtr: Data Op.
  508. /// @param [in/out] data_op_index: NetOutput addr size info.
  509. /// @return Status
  510. ///
  511. Status InitDataOp(const NodePtr &node, uint32_t &data_op_index, map<uint32_t, OpDescPtr> &data_by_index);
  512. ///
  513. /// @ingroup ge
  514. /// @brief Sort Data op list by index.
  515. /// @param [in] data_by_index: map of Data Op.
  516. /// @return
  517. ///
  518. void AdjustDataOpList(const map<uint32_t, OpDescPtr> &data_by_index);
  519. ///
  520. /// @ingroup ge
  521. /// @brief input zero copy node Initialize.
  522. /// @param [in] NodePtr: Data Op.
  523. /// @return Status
  524. ///
  525. Status InitInputZeroCopyNodes(const NodePtr &node);
  526. ///
  527. /// @ingroup ge
  528. /// @brief NetOutput Op Initialize.
  529. /// @param [in] NodePtr: NetOutput Op.
  530. /// @return Status
  531. ///
  532. Status InitNetOutput(const NodePtr &node);
  533. ///
  534. /// @ingroup ge
  535. /// @brief output zero copy node Initialize.
  536. /// @param [in] NodePtr: Data Op.
  537. /// @return Status
  538. ///
  539. Status InitOutputZeroCopyNodes(const NodePtr &node);
  540. ///
  541. /// @ingroup ge
  542. /// @brief input zero copy node Initialize for Case.
  543. /// @param [in] NodePtr: Data Op.
  544. /// @return Status
  545. ///
  546. Status InitInputBatchLabel(const NodePtr &node);
  547. ///
  548. /// @ingroup ge
  549. /// @brief output zero copy node Initialize for Case.
  550. /// @param [in] NodePtr: netoutput Op.
  551. /// @return Status
  552. ///
  553. Status InitOutputBatchLabel(const NodePtr &node);
  554. ///
  555. /// @ingroup ge
  556. /// @brief Constant Op Init.
  557. /// @return Status
  558. ///
  559. Status InitConstant(const OpDescPtr &op_desc);
  560. Status InitVariable(const OpDescPtr &op_desc);
  561. /// @ingroup ge
  562. /// @brief LabelSet Op Initialize.
  563. /// @param [in] op_desc: LabelSet Op descriptor.
  564. /// @return Status
  565. Status InitLabelSet(const OpDescPtr &op_desc);
  566. Status InitStreamSwitch(const OpDescPtr &op_desc);
  567. Status InitStreamActive(const OpDescPtr &op_desc);
  568. Status InitStreamSwitchN(const OpDescPtr &op_desc);
  569. ///
  570. /// @ingroup ge
  571. /// @brief Case Op Init.
  572. /// @return Status
  573. ///
  574. Status InitCase(const OpDescPtr &op_desc);
  575. Status SetDynamicBatchInfo(const OpDescPtr &op_desc, uint32_t batch_num);
  576. ///
  577. /// @ingroup ge
  578. /// @brief TVM Op Init.
  579. /// @return Status
  580. ///
  581. Status InitTbeHandle(const OpDescPtr &op_desc);
  582. void StoreTbeHandle(const std::string &handle_key);
  583. void CleanTbeHandle();
  584. ///
  585. /// @ingroup ge
  586. /// @brief Make active stream list and bind to model.
  587. /// @return: 0 for success / others for fail
  588. ///
  589. Status BindModelStream();
  590. ///
  591. /// @ingroup ge
  592. /// @brief Init model stream for NN model.
  593. /// @return Status
  594. ///
  595. Status InitModelStream(rtStream_t stream);
  596. ///
  597. /// @ingroup ge
  598. /// @brief ACL, Load task list with queue entrance.
  599. /// @return: 0 for success / others for fail
  600. ///
  601. Status LoadWithQueue();
  602. ///
  603. /// @ingroup ge
  604. /// @brief ACL, Bind Data Op addr to input queue.
  605. /// @return: 0 for success / others for fail
  606. ///
  607. Status BindInputQueue();
  608. Status CpuTaskModelZeroCopy(std::vector<uintptr_t> &mbuf_list, std::map<const void *, ZeroCopyOffset> &outside_addrs);
  609. ///
  610. /// @ingroup ge
  611. /// @brief ACL, Bind NetOutput Op addr to output queue.
  612. /// @return: 0 for success / others for fail
  613. ///
  614. Status BindOutputQueue();
  615. Status CpuModelPrepareOutput(uintptr_t addr, uint32_t size);
  616. ///
  617. /// @ingroup ge
  618. /// @brief definiteness queue schedule, bind input queue to task.
  619. /// @param [in] queue_id: input queue id from user.
  620. /// @param [in] addr: Data Op output tensor address.
  621. /// @param [in] size: Data Op output tensor size.
  622. /// @return: 0 for success / others for fail
  623. ///
  624. Status CpuModelDequeue(uint32_t queue_id);
  625. ///
  626. /// @ingroup ge
  627. /// @brief definiteness queue schedule, bind output queue to task.
  628. /// @param [in] queue_id: output queue id from user.
  629. /// @param [in] addr: NetOutput Op input tensor address.
  630. /// @param [in] size: NetOutput Op input tensor size.
  631. /// @return: 0 for success / others for fail
  632. ///
  633. Status CpuModelEnqueue(uint32_t queue_id, uintptr_t addr, uint32_t size);
  634. ///
  635. /// @ingroup ge
  636. /// @brief definiteness queue schedule, active original model stream.
  637. /// @return: 0 for success / others for fail
  638. ///
  639. Status CpuActiveStream();
  640. ///
  641. /// @ingroup ge
  642. /// @brief definiteness queue schedule, wait for end graph.
  643. /// @return: 0 for success / others for fail
  644. ///
  645. Status CpuWaitEndGraph();
  646. Status BindEnqueue();
  647. Status CpuModelEnqueue(uint32_t queue_id, uintptr_t out_mbuf);
  648. ///
  649. /// @ingroup ge
  650. /// @brief definiteness queue schedule, repeat run model.
  651. /// @return: 0 for success / others for fail
  652. ///
  653. Status CpuModelRepeat();
  654. Status InitEntryTask();
  655. Status AddHeadStream();
  656. ///
  657. /// @ingroup ge
  658. /// @brief set ts device.
  659. /// @return: 0 for success / others for fail
  660. ///
  661. Status SetTSDevice();
  662. Status OpDebugRegister();
  663. void OpDebugUnRegister();
  664. void CheckHasHcomOp();
  665. Status DoTaskSink();
  666. void CreateOutput(uint32_t index, OpDescPtr &op_desc, InputOutputDescInfo &output, uint32_t &format_result);
  667. Status TransAllVarData(ComputeGraphPtr &graph, uint32_t graph_id);
  668. // get desc info of graph for profiling
  669. Status GetComputeGraphInfo(vector<ComputeGraphDescInfo> &graph_desc_info);
  670. void SetDataDumperArgs(const ComputeGraphPtr &compute_graph);
  671. Status GenOutputTensorInfo(const OpDescPtr &op_desc, uint32_t data_index, OutputData *output_data,
  672. std::vector<ge::OutputTensorInfo> &outputs);
  673. void ParseAIPPInfo(std::string in_out_info, InputOutputDims &dims_info);
  674. void SetLabelForDynamic(const NodePtr &node);
  675. bool is_model_has_inited_;
  676. uint32_t model_id_;
  677. uint32_t runtime_model_id_;
  678. string name_;
  679. // used for inference data dump
  680. string om_name_;
  681. uint32_t version_;
  682. GeModelPtr ge_model_;
  683. bool need_destroy_aicpu_kernel_{false};
  684. vector<std::string> out_node_name_;
  685. map<uint32_t, OpDescPtr> op_list_;
  686. // data op_desc
  687. vector<OpDescPtr> data_op_list_;
  688. vector<OpDescPtr> output_op_list_;
  689. vector<OpDescPtr> variable_op_list_;
  690. std::map<uint32_t, ZeroCopyOffset> new_input_data_info_;
  691. std::map<uint32_t, ZeroCopyOffset> new_output_data_info_;
  692. std::map<const void *, ZeroCopyOffset> new_input_outside_addrs_;
  693. std::map<const void *, ZeroCopyOffset> new_output_outside_addrs_;
  694. std::set<const void *> real_virtual_addrs_;
  695. // output op: save cce op actual needed memory size
  696. vector<int64_t> output_memory_size_list_;
  697. std::thread thread_id_;
  698. std::shared_ptr<ModelListener> listener_;
  699. bool run_flg_;
  700. std::mutex mux_run_flg_;
  701. int32_t priority_;
  702. vector<rtStream_t> stream_list_;
  703. std::mutex all_hccl_stream_list_mutex_;
  704. vector<rtStream_t> all_hccl_stream_list_;
  705. // for reuse hccl_follow_stream
  706. std::mutex capacity_of_stream_mutex_;
  707. std::map<int64_t, std::vector<rtStream_t>> main_follow_stream_mapping_;
  708. vector<rtEvent_t> event_list_;
  709. vector<rtLabel_t> label_list_;
  710. set<uint32_t> label_id_indication_;
  711. std::mutex outside_addrs_mutex_;
  712. std::vector<ZeroCopyTask> zero_copy_tasks_; // Task used Data or NetOutput addr.
  713. std::set<const void *> copy_only_addrs_; // Address need copy to original place.
  714. // {op_id, batch_label}
  715. std::map<int64_t, std::string> zero_copy_op_id_batch_label_;
  716. // {batch_label, addrs}
  717. std::map<std::string, std::set<uintptr_t>> zero_copy_batch_label_addrs_;
  718. std::vector<TaskInfoPtr> task_list_;
  719. // rt_moodel_handle
  720. rtModel_t rt_model_handle_;
  721. rtStream_t rt_model_stream_;
  722. bool is_inner_model_stream_;
  723. bool is_async_mode_; // For NN execute, Async mode use rtMemcpyAsync on rt_model_stream_.
  724. ExecuteMode last_execute_mode_;
  725. bool is_stream_list_bind_{false};
  726. bool is_pure_head_stream_{false};
  727. rtStream_t rt_head_stream_{nullptr};
  728. rtStream_t rt_entry_stream_{nullptr};
  729. rtAicpuDeployType_t deploy_type_{AICPU_DEPLOY_RESERVED};
  730. // ACL queue schedule, save queue ids for Init.
  731. std::vector<TaskInfoPtr> cpu_task_list_;
  732. std::vector<uint32_t> input_queue_ids_; // input queue ids created by caller.
  733. std::vector<uint32_t> output_queue_ids_; // output queue ids created by caller.
  734. std::vector<uintptr_t> input_mbuf_list_; // input mbuf created by dequeue task.
  735. std::vector<uintptr_t> output_mbuf_list_; // output mbuf created by dequeue task.
  736. uint64_t session_id_;
  737. uint32_t device_id_;
  738. std::mutex flowctrl_op_index_internal_map_mutex_;
  739. std::map<uint32_t, uint32_t> flowctrl_op_index_internal_map_;
  740. std::vector<rtStream_t> active_stream_list_;
  741. std::set<uint32_t> active_stream_indication_;
  742. std::set<uint32_t> hcom_streams_;
  743. RuntimeParam runtime_param_;
  744. static std::mutex tvm_bin_mutex_;
  745. std::set<std::string> tvm_bin_kernel_;
  746. std::map<std::string, uint32_t> used_tbe_handle_map_;
  747. // for profiling task and graph info
  748. std::vector<TaskDescInfo> task_desc_info_;
  749. int64_t maxDumpOpNum_;
  750. // for data dump
  751. DataDumper data_dumper_;
  752. uint64_t iterator_count_;
  753. bool is_l1_fusion_enable_;
  754. std::map<OpDescPtr, void *> saved_task_addrs_;
  755. void *l1_fusion_addr_ = nullptr;
  756. bool known_node_ = false;
  757. uint32_t total_args_size_ = 0;
  758. void *args_ = nullptr;
  759. void *args_host_ = nullptr;
  760. void *fixed_addrs_ = nullptr;
  761. int64_t total_fixed_addr_size_ = 0;
  762. std::map<const void *, void *> knonw_input_data_info_;
  763. std::map<const void *, void *> knonw_output_data_info_;
  764. vector<void *> total_io_addrs_;
  765. vector<void *> orig_total_io_addrs_;
  766. bool base_addr_not_changed_ = false;
  767. vector<vector<int64_t>> batch_info_;
  768. std::vector<std::vector<int64_t>> combined_batch_info_;
  769. vector<string> user_designate_shape_order_;
  770. int32_t dynamic_type_ = 0;
  771. bool is_dynamic_ = false;
  772. vector<uint64_t> batch_size_;
  773. // key: input tensor name, generally rts op;
  774. // value: the fixed addr of input anchor, same as the peer output anchor addr of the peer op
  775. std::map<string, int64_t> tensor_name_to_fixed_addr_size_;
  776. // key: input tensor name, generally rts op; value: the peer output anchor of the peer op
  777. std::map<string, int64_t> tensor_name_to_peer_output_index_;
  778. // if model is first execute
  779. bool is_first_execute_;
  780. // for op debug
  781. std::mutex debug_reg_mutex_;
  782. bool is_op_debug_reg_ = false;
  783. void *op_debug_addr_ = nullptr;
  784. void *p2p_debug_addr_ = nullptr;
  785. bool is_new_model_desc_{false};
  786. };
  787. } // namespace ge
  788. #endif // GE_GRAPH_LOAD_NEW_MODEL_MANAGER_DAVINCI_MODEL_H_

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