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
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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. ///
  241. /// @ingroup ge
  242. /// @brief get model input and output format
  243. /// @return ccTensorFormat_t current model input and output format
  244. ///
  245. Format GetFormat();
  246. rtModel_t GetRtModelHandle() const { return rt_model_handle_; }
  247. rtStream_t GetRtModelStream() const { return rt_model_stream_; }
  248. uint64_t GetRtBaseAddr() const { return runtime_param_.logic_mem_base; }
  249. uint64_t GetRtWeightAddr() const { return runtime_param_.logic_weight_base; }
  250. uint64_t GetRtVarAddr() const { return runtime_param_.logic_var_base; }
  251. uint32_t GetFlowctrlIndex(uint32_t op_index);
  252. void PushHcclStream(rtStream_t value);
  253. bool IsBroadCastOpData(const NodePtr &var_node);
  254. ///
  255. /// @ingroup ge
  256. /// @brief For TVM Op, avoid Addr Reuse.
  257. /// @return void*
  258. ///
  259. const char *GetRegisterStub(const string &tvm_binfile_key, const string &session_graph_model_id = "");
  260. ///
  261. /// @ingroup ge
  262. /// @brief get model input and output desc info
  263. /// @param [out] input_shape model input size
  264. /// @param [out] output_shape model output size
  265. /// @return execute result
  266. ///
  267. Status GetInputOutputDescInfo(vector<InputOutputDescInfo> &input_desc, vector<InputOutputDescInfo> &output_desc);
  268. Status GetInputOutputDescInfo(vector<InputOutputDescInfo> &input_desc, vector<InputOutputDescInfo> &output_desc,
  269. vector<uint32_t> &inputFormats, vector<uint32_t> &output_formats);
  270. ///
  271. /// @ingroup ge
  272. /// @brief Get dynamic batch_info
  273. /// @param [out] batch_info
  274. /// @param [out] dynamic_type
  275. /// @return execute result
  276. ///
  277. Status GetDynamicBatchInfo(vector<vector<int64_t>> &batch_info, int32_t &dynamic_type) const;
  278. ///
  279. /// @ingroup ge
  280. /// @brief Get combined dynamic dims info
  281. /// @param [out] batch_info
  282. /// @return None
  283. ///
  284. void GetCombinedDynamicDims(vector<vector<int64_t>> &batch_info) const;
  285. void GetUserDesignateShapeOrder(vector<string> &user_input_shape_order) const;
  286. void GetCurShape(vector<int64_t> &batch_info, int32_t &dynamic_type);
  287. void GetModelAttr(vector<string> &dynamic_output_shape_info);
  288. ///
  289. /// @ingroup ge
  290. /// @brief Get AIPP input info
  291. /// @param [in] index
  292. /// @param [out] aipp_info
  293. /// @return execute result
  294. ///
  295. Status GetAIPPInfo(uint32_t index, AippConfigInfo &aipp_info);
  296. Status GetAippType(uint32_t index, InputAippType &type, size_t &aipp_index);
  297. ///
  298. /// @ingroup ge
  299. /// @brief Get model_id.
  300. /// @return model_id
  301. ///
  302. uint32_t GetModelId() const { return model_id_; }
  303. ///
  304. /// @ingroup ge
  305. /// @brief get unique identification for op when load two or more models
  306. /// @param [in] op_desc : current op.
  307. /// @param [in] string identification: unique identification for current op.
  308. /// @return None
  309. ///
  310. void GetUniqueId(const OpDescPtr &op_desc, string &unique_identification);
  311. ///
  312. /// @ingroup ge
  313. /// @brief get model input and output desc for zero copy
  314. /// @param [out] input_shape model input size
  315. /// @param [out] output_shape model output size
  316. /// @return execute result
  317. ///
  318. Status GetInputOutputDescInfoForZeroCopy(vector<InputOutputDescInfo> &input_desc,
  319. vector<InputOutputDescInfo> &output_desc,
  320. vector<uint32_t> &inputFormats, vector<uint32_t> &output_formats);
  321. Status ReturnResult(uint32_t data_id, const bool rslt_flg, const bool seq_end_flg, OutputData *output_data);
  322. Status ReturnNoOutput(uint32_t data_id);
  323. Status ModelRunStart();
  324. ///
  325. /// @ingroup ge
  326. /// @brief stop run model
  327. /// @return Status
  328. ///
  329. Status ModelRunStop();
  330. ///
  331. /// @ingroup ge
  332. /// @brief model run flag
  333. /// @return Status
  334. ///
  335. bool RunFlag() const { return run_flg_; }
  336. ///
  337. /// @ingroup ge
  338. /// @brief Set Session Id
  339. /// @return void
  340. ///
  341. void SetSessionId(uint64_t session_id) { session_id_ = session_id; }
  342. ///
  343. /// @ingroup ge
  344. /// @brief Get Session Id
  345. /// @return sessionID
  346. ///
  347. uint64_t GetSessionId() const { return session_id_; }
  348. ///
  349. /// @ingroup ge
  350. /// @brief SetDeviceId
  351. /// @return void
  352. ///
  353. void SetDeviceId(uint32_t device_id) { device_id_ = device_id; }
  354. ///
  355. /// @ingroup ge
  356. /// @brief Get device Id
  357. /// @return device id
  358. ///
  359. uint32_t GetDeviceId() const { return device_id_; }
  360. bool NeedDestroyAicpuKernel() const { return need_destroy_aicpu_kernel_; }
  361. Status UpdateSessionId(uint64_t session_id);
  362. const RuntimeParam &GetRuntimeParam() { return runtime_param_; }
  363. int32_t GetDataInputTid() const { return dataInputTid; }
  364. void SetDataInputTid(int32_t data_input_tid) { dataInputTid = data_input_tid; }
  365. void DisableZeroCopy(const void *addr);
  366. bool GetOpDugReg() const { return is_op_debug_reg_; }
  367. ///
  368. /// @ingroup ge
  369. /// @brief Save outside address of Data or NetOutput used info for ZeroCopy.
  370. /// @param [in] const OpDescPtr &op_desc: current op desc
  371. /// @param [in] const vector<void *> &outside_addrs: address of task
  372. /// @param [in] const void *args_offset: arguments address save the address.
  373. /// @return None.
  374. ///
  375. void SetZeroCopyAddr(const OpDescPtr &op_desc, const vector<void *> &outside_addrs, const void *info, void *args,
  376. size_t size, size_t offset);
  377. void SetDynamicSize(const vector<uint64_t> &batch_num, int32_t dynamic_type);
  378. bool GetL1FusionEnableOption() { return is_l1_fusion_enable_; }
  379. void SetProfileTime(ModelProcStage stage, int64_t endTime = 0);
  380. int64_t GetLoadBeginTime() { return load_begin_time_; }
  381. int64_t GetLoadEndTime() { return load_end_time_; }
  382. Status ReportProfilingData();
  383. void SaveDumpOpInfo(const RuntimeParam &model_param, const OpDescPtr &op, uint32_t task_id, uint32_t stream_id) {
  384. data_dumper_.SaveDumpOpInfo(model_param, op, task_id, stream_id);
  385. }
  386. void SaveDumpTask(uint32_t task_id, uint32_t stream_id, const shared_ptr<OpDesc> &op_desc, uintptr_t args) {
  387. data_dumper_.SaveDumpTask(task_id, stream_id, op_desc, args);
  388. }
  389. void SetEndGraphId(uint32_t task_id, uint32_t stream_id);
  390. DavinciModel &operator=(const DavinciModel &model) = delete;
  391. DavinciModel(const DavinciModel &model) = delete;
  392. const map<int64_t, vector<rtStream_t>> &GetHcclFolowStream() {
  393. return main_follow_stream_mapping_;
  394. }
  395. void SaveHcclFollowStream(int64_t main_stream_id, rtStream_t stream);
  396. void InitRuntimeParams();
  397. Status InitVariableMem();
  398. void UpdateMemBase(uint8_t *mem_base) {
  399. runtime_param_.mem_base = mem_base;
  400. mem_base_ = mem_base;
  401. }
  402. void SetTotalArgsSize(uint32_t args_size) { total_args_size_ += args_size; }
  403. uint32_t GetTotalArgsSize() { return total_args_size_; }
  404. void *GetCurrentArgsAddr(uint32_t offset) {
  405. void *cur_args = static_cast<char *>(args_) + offset;
  406. return cur_args;
  407. }
  408. void SetTotalIOAddrs(const vector<void *> &io_addrs);
  409. void SetHybridArgsSize(uint32_t args_size) { total_hybrid_args_size_ += args_size; }
  410. uint32_t GetHybridArgsSize() {
  411. return total_hybrid_args_size_;
  412. }
  413. void *GetCurrentHybridArgsAddr(uint32_t offset) {
  414. void *cur_args = static_cast<char *>(hybrid_addrs_) + offset;
  415. return cur_args;
  416. }
  417. void SetTotalFixedAddrsSize(string tensor_name, int64_t fix_addr_size);
  418. int64_t GetFixedAddrsSize(string tensor_name);
  419. void *GetCurrentFixedAddr(int64_t offset) const {
  420. void *cur_addr = static_cast<char *>(fixed_addrs_) + offset;
  421. return cur_addr;
  422. }
  423. uint32_t GetFixedAddrOutputIndex(string tensor_name) {
  424. if (tensor_name_to_peer_output_index_.find(tensor_name) != tensor_name_to_peer_output_index_.end()) {
  425. return tensor_name_to_peer_output_index_[tensor_name];
  426. }
  427. return UINT32_MAX;
  428. }
  429. void SetKnownNode(bool known_node) { known_node_ = known_node; }
  430. bool IsKnownNode() { return known_node_; }
  431. Status MallocKnownArgs();
  432. Status UpdateKnownNodeArgs(const vector<void *> &inputs, const vector<void *> &outputs);
  433. Status CreateKnownZeroCopyMap(const vector<void *> &inputs, const vector<void *> &outputs);
  434. Status UpdateKnownZeroCopyAddr(vector<void *> &total_io_addrs, bool update_args = true);
  435. void SetKnownNodeAddrNotChanged(bool base_addr_not_changed) { base_addr_not_changed_ = base_addr_not_changed; }
  436. Status GetOrigInputInfo(uint32_t index, OriginInputInfo &orig_input_info);
  437. Status GetAllAippInputOutputDims(uint32_t index, vector<InputOutputDims> &input_dims,
  438. vector<InputOutputDims> &output_dims);
  439. void SetModelDescVersion(bool is_new_model_desc) { is_new_model_desc_ = is_new_model_desc; }
  440. // om file name
  441. void SetOmName(string om_name) { om_name_ = om_name; }
  442. void SetDumpProperties(const DumpProperties &dump_properties) { data_dumper_.SetDumpProperties(dump_properties); }
  443. const DumpProperties &GetDumpProperties() const { return data_dumper_.GetDumpProperties(); }
  444. bool GetOpDescInfo(uint32_t stream_id, uint32_t task_id, OpDescInfo &op_desc_info) const {
  445. return data_dumper_.GetOpDescInfo(stream_id, task_id, op_desc_info);
  446. }
  447. private:
  448. // memory address of weights
  449. uint8_t *weights_mem_base_;
  450. uint8_t *var_mem_base_;
  451. // memory address of model
  452. uintptr_t fixed_mem_base_; // Initial of mem_base_, keep forever.
  453. uint8_t *mem_base_;
  454. uint8_t *p2p_mem_base_;
  455. bool is_inner_mem_base_;
  456. bool is_inner_weight_base_;
  457. bool is_inner_p2p_mem_base_;
  458. // input data manager
  459. DataInputer *data_inputer_;
  460. int64_t load_begin_time_;
  461. int64_t load_end_time_;
  462. struct timeInfo time_info_;
  463. int32_t dataInputTid;
  464. void *GetRunAddress(void *addr) const;
  465. ///
  466. /// @ingroup ge
  467. /// @brief Copy Check input size and model op size.
  468. /// @param [in] const int64_t &input_size: input size.
  469. /// @param [in] const int64_t &op_size: model op size.
  470. /// @param [in] is_dynamic: dynamic batch input flag.
  471. /// @return true if success
  472. ///
  473. bool CheckInputAndModelSize(const int64_t &input_size, const int64_t &op_size, bool is_dynamic);
  474. ///
  475. /// @ingroup ge
  476. /// @brief Set copy only for No task feed NetOutput address.
  477. /// @return None.
  478. ///
  479. void SetCopyOnlyOutput();
  480. ///
  481. /// @ingroup ge
  482. /// @brief Copy Input/Output to model for direct use.
  483. /// @param [in] const InputData &input_data: user input data info.
  484. /// @param [in/out] OutputData &output_data: user output data info.
  485. /// @param [in] bool is_dynamic: whether is dynamic input, true: is dynamic input; false: not is dynamic input
  486. /// @return SUCCESS handle successfully / others handle failed
  487. ///
  488. Status CopyModelData(const InputData &input_data, OutputData &output_data, bool is_dynamic);
  489. ///
  490. /// @ingroup ge
  491. /// @brief Copy Data addr to model for direct use.
  492. /// @param [in] data_info: model memory addr/size map { data_index, { tensor_size, tensor_addr } }.
  493. /// @param [in] is_input: input data or output data
  494. /// @param [in] blobs: user input/output data list.
  495. /// @param [in] is_dynamic: whether is dynamic input, true: is dynamic input; false: not is dynamic input
  496. /// @param [in] batch_label: batch label for multi-batch scenes
  497. /// @return SUCCESS handle successfully / others handle failed
  498. ///
  499. Status UpdateIoTaskArgs(const map<uint32_t, ZeroCopyOffset> &data_info, bool is_input,
  500. const vector<DataBuffer> &blobs, bool is_dynamic, const string &batch_label);
  501. Status CopyInputData(const InputData &input_data, bool device_data = false);
  502. Status CopyOutputData(uint32_t data_id, OutputData &output_data, rtMemcpyKind_t kind);
  503. Status SyncVarData();
  504. Status InitWeightMem(void *dev_ptr, void *weight_ptr, size_t weight_size);
  505. Status InitFeatureMapAndP2PMem(void *dev_ptr, size_t mem_size);
  506. void CreateInputDimsInfo(const OpDescPtr &op_desc, Format format, InputOutputDescInfo &input);
  507. void SetInputDimsInfo(const vector<int64_t> &model_input_dims, Format &format, InputOutputDescInfo &input);
  508. Status GetInputDescInfo(vector<InputOutputDescInfo> &input_desc, vector<uint32_t> &input_formats);
  509. Status GetOutputDescInfo(vector<InputOutputDescInfo> &output_desc, vector<uint32_t> &output_formats);
  510. Status InitTaskInfo(domi::ModelTaskDef &modelTaskInfo);
  511. void UnbindHcomStream();
  512. Status DistributeTask();
  513. uint8_t *MallocFeatureMapMem(size_t data_size);
  514. uint8_t *MallocWeightsMem(size_t weights_size);
  515. uint8_t *MallocP2PMem(size_t p2p_data_size);
  516. void FreeFeatureMapMem();
  517. void FreeWeightsMem();
  518. void FreeP2PMem();
  519. void ReleaseTask();
  520. void UnbindTaskSinkStream();
  521. bool IsAicpuKernelConnectSpecifiedLayer();
  522. ///
  523. /// @ingroup ge
  524. /// @brief Reduce memory usage after task sink.
  525. /// @return: void
  526. ///
  527. void Shrink();
  528. ///
  529. /// @ingroup ge
  530. /// @brief Travel all nodes and do some init.
  531. /// @param [in] compute_graph: ComputeGraph to load.
  532. /// @return Status
  533. ///
  534. Status InitNodes(const ComputeGraphPtr &compute_graph);
  535. ///
  536. /// @ingroup ge
  537. /// @brief Data Op Initialize.
  538. /// @param [in] ComputeGraphPtr: root graph of the model.
  539. /// @param [in] NodePtr: Data Op.
  540. /// @param [in/out] data_op_index: index of courrent count.
  541. /// @param [in/out] data_by_index: Data ordered by index.
  542. /// @return Status
  543. ///
  544. Status InitDataOp(const ComputeGraphPtr &graph, const NodePtr &node, uint32_t &data_op_index,
  545. map<uint32_t, OpDescPtr> &data_by_index);
  546. ///
  547. /// @ingroup ge
  548. /// @brief Sort Data op list by index.
  549. /// @param [in] data_by_index: map of Data Op.
  550. /// @param [in] output_op_list: list of NetOutput op.
  551. /// @return Status
  552. ///
  553. Status OptInputOutputInfo(const map<uint32_t, OpDescPtr> &data_by_index, const vector<OpDescPtr> &output_op_list);
  554. ///
  555. /// @ingroup ge
  556. /// @brief NetOutput Op Initialize.
  557. /// @param [in] ComputeGraphPtr: root graph of the model.
  558. /// @param [in] NodePtr: NetOutput Op.
  559. /// @param [in/out] vector<OpDescPtr>: All NetOutput node in model.
  560. /// @return Status
  561. ///
  562. Status InitNetOutput(const ComputeGraphPtr &graph, const NodePtr &node, vector<OpDescPtr> &output_op_list);
  563. ///
  564. /// @ingroup ge
  565. /// @brief Constant Op Init.
  566. /// @return Status
  567. ///
  568. Status InitConstant(const OpDescPtr &op_desc);
  569. Status InitVariable(const OpDescPtr &op_desc);
  570. /// @ingroup ge
  571. /// @brief LabelSet Op Initialize.
  572. /// @param [in] op_desc: LabelSet Op descriptor.
  573. /// @return Status
  574. Status InitLabelSet(const OpDescPtr &op_desc);
  575. Status InitStreamSwitch(const OpDescPtr &op_desc);
  576. Status InitStreamActive(const OpDescPtr &op_desc);
  577. Status InitStreamSwitchN(const OpDescPtr &op_desc);
  578. ///
  579. /// @ingroup ge
  580. /// @brief Case Op Init.
  581. /// @return Status
  582. ///
  583. Status InitCase(const OpDescPtr &op_desc);
  584. Status SetDynamicBatchInfo(const OpDescPtr &op_desc, uint32_t batch_num);
  585. ///
  586. /// @ingroup ge
  587. /// @brief TVM Op Init.
  588. /// @return Status
  589. ///
  590. Status InitTbeHandle(const OpDescPtr &op_desc);
  591. void StoreTbeHandle(const string &handle_key);
  592. void CleanTbeHandle();
  593. ///
  594. /// @ingroup ge
  595. /// @brief Make active stream list and bind to model.
  596. /// @return: 0 for success / others for fail
  597. ///
  598. Status BindModelStream();
  599. ///
  600. /// @ingroup ge
  601. /// @brief Init model stream for NN model.
  602. /// @return Status
  603. ///
  604. Status InitModelStream(rtStream_t stream);
  605. ///
  606. /// @ingroup ge
  607. /// @brief ACL, Load task list with queue entrance.
  608. /// @return: 0 for success / others for fail
  609. ///
  610. Status LoadWithQueue();
  611. ///
  612. /// @ingroup ge
  613. /// @brief ACL, Bind Data Op addr to input queue.
  614. /// @return: 0 for success / others for fail
  615. ///
  616. Status BindInputQueue();
  617. Status CpuTaskModelZeroCopy(vector<uintptr_t> &mbuf_list, map<const void *, ZeroCopyOffset> &outside_addrs);
  618. ///
  619. /// @ingroup ge
  620. /// @brief ACL, Bind NetOutput Op addr to output queue.
  621. /// @return: 0 for success / others for fail
  622. ///
  623. Status BindOutputQueue();
  624. Status CpuModelPrepareOutput(uintptr_t addr, uint32_t size);
  625. ///
  626. /// @ingroup ge
  627. /// @brief definiteness queue schedule, bind input queue to task.
  628. /// @param [in] queue_id: input queue id from user.
  629. /// @param [in] addr: Data Op output tensor address.
  630. /// @param [in] size: Data Op output tensor size.
  631. /// @return: 0 for success / others for fail
  632. ///
  633. Status CpuModelDequeue(uint32_t queue_id);
  634. ///
  635. /// @ingroup ge
  636. /// @brief definiteness queue schedule, bind output queue to task.
  637. /// @param [in] queue_id: output queue id from user.
  638. /// @param [in] addr: NetOutput Op input tensor address.
  639. /// @param [in] size: NetOutput Op input tensor size.
  640. /// @return: 0 for success / others for fail
  641. ///
  642. Status CpuModelEnqueue(uint32_t queue_id, uintptr_t addr, uint32_t size);
  643. ///
  644. /// @ingroup ge
  645. /// @brief definiteness queue schedule, active original model stream.
  646. /// @return: 0 for success / others for fail
  647. ///
  648. Status CpuActiveStream();
  649. ///
  650. /// @ingroup ge
  651. /// @brief definiteness queue schedule, wait for end graph.
  652. /// @return: 0 for success / others for fail
  653. ///
  654. Status CpuWaitEndGraph();
  655. Status BindEnqueue();
  656. Status CpuModelEnqueue(uint32_t queue_id, uintptr_t out_mbuf);
  657. ///
  658. /// @ingroup ge
  659. /// @brief definiteness queue schedule, repeat run model.
  660. /// @return: 0 for success / others for fail
  661. ///
  662. Status CpuModelRepeat();
  663. Status InitEntryTask();
  664. Status AddHeadStream();
  665. ///
  666. /// @ingroup ge
  667. /// @brief set ts device.
  668. /// @return: 0 for success / others for fail
  669. ///
  670. Status SetTSDevice();
  671. Status OpDebugRegister();
  672. void OpDebugUnRegister();
  673. void CheckHasHcomOp();
  674. Status DoTaskSink();
  675. void CreateOutput(uint32_t index, const OpDescPtr &op_desc, InputOutputDescInfo &output, uint32_t &format_result);
  676. Status TransAllVarData(ComputeGraphPtr &graph, uint32_t graph_id);
  677. // get desc info of graph for profiling
  678. Status GetComputeGraphInfo(vector<ComputeGraphDescInfo> &graph_desc_info);
  679. void SetDataDumperArgs(const ComputeGraphPtr &compute_graph);
  680. Status InitModelProfile();
  681. Status SinkModelProfile();
  682. Status SinkTimeProfile(const InputData &current_data);
  683. Status InitOutputTensorInfo(const OpDescPtr &op_desc);
  684. Status GenOutputTensorInfo(OutputData *output_data, vector<OutputTensorInfo> &outputs);
  685. Status InitOutputDescInfo(const vector<OpDescPtr> &output_op_list,
  686. vector<InputOutputDescInfo> &output_desc, vector<uint32_t> &formats);
  687. void ParseAIPPInfo(string in_out_info, InputOutputDims &dims_info);
  688. void SetLabelForDynamic(const NodePtr &node);
  689. void ParseDynamicOutShape(const vector<string> &str_info, vector<vector<int64_t>> &vec_info);
  690. bool IsGetNextSinkDynamic(const OpDescPtr &op_desc);
  691. void GetAllGearsInfo(const NodePtr &node);
  692. Status GetGetDynamicDimsNodeInfo(const NodePtr &node);
  693. Status GetGearAndRealOutSizeInfo(size_t input_count, const NodePtr &node);
  694. Status GetRealOutputSizeOfMerge(size_t input_index, const NodePtr &merge_node);
  695. Status GetGearAndRealOutShapeInfo(size_t input_count, const OpDescPtr &op_desc);
  696. bool is_weight_mem_has_inited_;
  697. bool is_feature_map_mem_has_inited_;
  698. uint32_t model_id_;
  699. uint32_t runtime_model_id_;
  700. string name_;
  701. // used for inference data dump
  702. string om_name_;
  703. uint32_t version_;
  704. GeModelPtr ge_model_;
  705. bool need_destroy_aicpu_kernel_{false};
  706. vector<string> out_node_name_;
  707. map<uint32_t, OpDescPtr> op_list_;
  708. // data op_desc
  709. vector<OpDescPtr> data_op_list_;
  710. vector<OpDescPtr> variable_op_list_;
  711. map<uint32_t, ZeroCopyOffset> new_input_data_info_;
  712. map<uint32_t, ZeroCopyOffset> new_output_data_info_;
  713. map<const void *, ZeroCopyOffset> new_input_outside_addrs_;
  714. map<const void *, ZeroCopyOffset> new_output_outside_addrs_;
  715. set<const void *> real_virtual_addrs_;
  716. // output op: save cce op actual needed memory size
  717. vector<int64_t> output_memory_size_list_;
  718. thread thread_id_;
  719. shared_ptr<ModelListener> listener_;
  720. bool run_flg_;
  721. mutex mux_run_flg_;
  722. int32_t priority_;
  723. vector<rtStream_t> stream_list_;
  724. mutex all_hccl_stream_list_mutex_;
  725. vector<rtStream_t> all_hccl_stream_list_;
  726. // for reuse hccl_follow_stream
  727. mutex capacity_of_stream_mutex_;
  728. map<int64_t, vector<rtStream_t>> main_follow_stream_mapping_;
  729. vector<rtEvent_t> event_list_;
  730. vector<rtLabel_t> label_list_;
  731. set<uint32_t> label_id_indication_;
  732. mutex outside_addrs_mutex_;
  733. vector<ZeroCopyTask> zero_copy_tasks_; // Task used Data or NetOutput addr.
  734. set<const void *> copy_only_addrs_; // Address need copy to original place.
  735. vector<TaskInfoPtr> task_list_;
  736. // rt_moodel_handle
  737. rtModel_t rt_model_handle_;
  738. rtStream_t rt_model_stream_;
  739. bool is_inner_model_stream_;
  740. bool is_async_mode_; // For NN execute, Async mode use rtMemcpyAsync on rt_model_stream_.
  741. ExecuteMode last_execute_mode_;
  742. bool is_stream_list_bind_{false};
  743. bool is_pure_head_stream_{false};
  744. rtStream_t rt_head_stream_{nullptr};
  745. rtStream_t rt_entry_stream_{nullptr};
  746. rtAicpuDeployType_t deploy_type_{AICPU_DEPLOY_RESERVED};
  747. // ACL queue schedule, save queue ids for Init.
  748. vector<TaskInfoPtr> cpu_task_list_;
  749. vector<uint32_t> input_queue_ids_; // input queue ids created by caller.
  750. vector<uint32_t> output_queue_ids_; // output queue ids created by caller.
  751. vector<uintptr_t> input_mbuf_list_; // input mbuf created by dequeue task.
  752. vector<uintptr_t> output_mbuf_list_; // output mbuf created by dequeue task.
  753. uint64_t session_id_;
  754. uint32_t device_id_;
  755. mutex flowctrl_op_index_internal_map_mutex_;
  756. map<uint32_t, uint32_t> flowctrl_op_index_internal_map_;
  757. vector<rtStream_t> active_stream_list_;
  758. set<uint32_t> active_stream_indication_;
  759. set<uint32_t> hcom_streams_;
  760. RuntimeParam runtime_param_;
  761. static mutex tvm_bin_mutex_;
  762. set<string> tvm_bin_kernel_;
  763. map<string, uint32_t> used_tbe_handle_map_;
  764. // for profiling task and graph info
  765. vector<TaskDescInfo> task_desc_info_;
  766. int64_t maxDumpOpNum_;
  767. // for data dump
  768. DataDumper data_dumper_;
  769. uint64_t iterator_count_;
  770. bool is_l1_fusion_enable_;
  771. map<OpDescPtr, void *> saved_task_addrs_;
  772. void *l1_fusion_addr_ = nullptr;
  773. bool known_node_ = false;
  774. uint32_t total_args_size_ = 0;
  775. void *args_ = nullptr;
  776. void *args_host_ = nullptr;
  777. void *fixed_addrs_ = nullptr;
  778. void *hybrid_addrs_ = nullptr;
  779. uint32_t total_hybrid_args_size_ = 0;
  780. int64_t total_fixed_addr_size_ = 0;
  781. map<const void *, void *> known_input_data_info_;
  782. map<const void *, void *> known_output_data_info_;
  783. vector<void *> total_io_addrs_;
  784. vector<void *> orig_total_io_addrs_;
  785. bool base_addr_not_changed_ = false;
  786. vector<vector<int64_t>> batch_info_;
  787. vector<vector<int64_t>> combined_batch_info_;
  788. vector<string> user_designate_shape_order_;
  789. int32_t dynamic_type_ = 0;
  790. bool is_dynamic_ = false;
  791. vector<uint64_t> batch_size_;
  792. // key: input tensor name, generally rts op;
  793. // value: the fixed addr of input anchor, same as the peer output anchor addr of the peer op
  794. map<string, int64_t> tensor_name_to_fixed_addr_size_;
  795. // key: input tensor name, generally rts op; value: the peer output anchor of the peer op
  796. map<string, int64_t> tensor_name_to_peer_output_index_;
  797. // if model is first execute
  798. bool is_first_execute_;
  799. // for op debug
  800. mutex debug_reg_mutex_;
  801. bool is_op_debug_reg_ = false;
  802. void *op_debug_addr_ = nullptr;
  803. void *p2p_debug_addr_ = nullptr;
  804. bool is_new_model_desc_{false};
  805. bool is_online_infer_dynamic_ = false;
  806. bool is_getnext_sink_dynamic_ = false;
  807. vector<int64_t> cur_dynamic_dims_;
  808. void *netoutput_last_input_addr_ = nullptr;
  809. int64_t netoutput_last_input_size_ = 0;
  810. size_t shape_of_cur_dynamic_dims_ = 0;
  811. // key: input_index: input is merge node; value: each gear info and each output size
  812. map<size_t, map<vector<int64_t>, int64_t>> merge_nodes_gear_and_real_out_size_info_;
  813. // key: input_index: input is merge node; value: each gear info and each output shape
  814. map<size_t, map<vector<int64_t>, vector<int64_t>>> merge_nodes_gear_and_real_out_shape_info_;
  815. vector<vector<int64_t>> all_gears_info_;
  816. multimap<uint32_t, uint32_t> op_id_map_;
  817. vector<ProfileInfo> profile_list_;
  818. // For super kernel.
  819. SuperKernelTaskInfo skt_info_;
  820. bool is_dynamic_aipp_ = false;
  821. vector<string> dynamic_output_shape_info_;
  822. vector<vector<void *>> input_addrs_list_;
  823. vector<vector<void *>> output_addrs_list_;
  824. vector<int64_t> output_buffer_size_;
  825. vector<vector<int64_t>> output_shape_info_;
  826. vector<InputOutputDescInfo> output_descs_;
  827. vector<uint32_t> output_formats_;
  828. };
  829. } // namespace ge
  830. #endif // GE_GRAPH_LOAD_NEW_MODEL_MANAGER_DAVINCI_MODEL_H_

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