You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

davinci_model.h 32 kB

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

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