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

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