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

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