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

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