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

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

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