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

davinci_model.h 35 kB

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

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