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 35 kB

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

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