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

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