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.

block_mem_assigner.h 13 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /**
  2. * Copyright 2019-2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef GE_GRAPH_BUILD_MEMORY_BLOCK_MEM_ASSIGNER_H_
  17. #define GE_GRAPH_BUILD_MEMORY_BLOCK_MEM_ASSIGNER_H_
  18. #include <map>
  19. #include <string>
  20. #include <unordered_map>
  21. #include <unordered_set>
  22. #include <utility>
  23. #include <vector>
  24. #include <list>
  25. #include "common/ge_inner_error_codes.h"
  26. #include "common/types.h"
  27. #include "common/util.h"
  28. #include "graph/build/memory/mem_assigner.h"
  29. #include "graph/compute_graph.h"
  30. #include "graph/utils/graph_utils.h"
  31. namespace ge {
  32. const size_t kMaxLifeTime = 0xffffffff;
  33. using DependStreamLife = std::map<int64_t, std::map<int64_t, size_t>>;
  34. enum MemoryType { kOutput, kWorkspace };
  35. struct NodeTypeIndex {
  36. NodeTypeIndex(ge::NodePtr node, MemoryType mem_type, uint32_t index, bool ref_input = false)
  37. : node(std::move(node)), mem_type(mem_type), index(index), ref_input(ref_input) {}
  38. ge::NodePtr node = nullptr;
  39. MemoryType mem_type = kOutput;
  40. uint32_t index = 0;
  41. size_t life_time_end = kMaxLifeTime;
  42. bool ref_input = false;
  43. const string GetMemType() const {
  44. if (mem_type == kOutput) {
  45. return "output";
  46. } else if (mem_type == kWorkspace) {
  47. return "workspace";
  48. }
  49. return "unknown";
  50. }
  51. };
  52. class MemoryBlock {
  53. public:
  54. explicit MemoryBlock(size_t block_size, int64_t stream_id = 0, bool reuse_mem = true)
  55. : ref_count_(0),
  56. stream_id_(stream_id),
  57. deleted_block_(false),
  58. reuse_mem_(reuse_mem),
  59. input_index_(0),
  60. continuous_block_(false),
  61. first_continuous_block_(false),
  62. last_continuous_block_(false),
  63. is_zero_copy_(false),
  64. block_size_(block_size),
  65. head_offset_(0),
  66. tail_offset_(0),
  67. child_offset_(0) {}
  68. MemoryBlock(const MemoryBlock &) = delete;
  69. MemoryBlock &operator=(const MemoryBlock &) = delete;
  70. ~MemoryBlock() {
  71. node_type_index_list_.clear();
  72. symbol_list_.clear();
  73. }
  74. void Init(size_t real_size, MemoryType type, const ge::NodePtr &node, uint32_t out_index, size_t no_align_size) {
  75. real_size_list_.emplace_back(real_size);
  76. no_align_size_list_.emplace_back(no_align_size);
  77. node_type_index_list_.emplace_back(node, type, out_index, false);
  78. }
  79. size_t Size() const { return block_size_; }
  80. size_t AlignSize() const;
  81. void SetHeadOffset(size_t offset);
  82. void SetTailOffset(size_t offset);
  83. size_t HeadOffset() const { return head_offset_; }
  84. size_t TailOffset() const { return tail_offset_; }
  85. void AddNodeTypeIndex(const NodeTypeIndex &node_type_index, size_t real_size, size_t no_align_size) {
  86. node_type_index_list_.emplace_back(node_type_index);
  87. real_size_list_.emplace_back(real_size);
  88. no_align_size_list_.emplace_back(no_align_size);
  89. }
  90. void AddSymbol(const std::string &symbol) { symbol_list_.emplace_back(symbol); }
  91. const std::vector<NodeTypeIndex> &NodeTypeIndexList() const { return node_type_index_list_; }
  92. const std::vector<std::string> &SymbolList() const { return symbol_list_; }
  93. const std::vector<size_t> &RealSizeList() const { return real_size_list_; }
  94. const std::vector<MemoryBlock *> &ChildBlockList() const { return child_blocks_; }
  95. const std::vector<size_t> &NoAlignSizeList() const { return no_align_size_list_; }
  96. void Resize();
  97. std::string String();
  98. bool IsSameLabel(std::string &first_batch_label);
  99. void AddContinuousLifeReuseBlock(MemoryBlock *block, DependStreamLife &total_node_depend_stream_life);
  100. void AddLifeReuseBlock(MemoryBlock *block, DependStreamLife &node_depend_stream_life);
  101. void SetLifeTimeEnd(size_t time);
  102. size_t GetLifeBegin();
  103. size_t GetLifeEnd();
  104. void AddDependLifeBegin(DependStreamLife &node_depend_stream_life);
  105. size_t GetDependLifeBegin(int64_t stream_id, DependStreamLife &node_depend_stream_life);
  106. int ref_count_;
  107. int64_t stream_id_;
  108. bool deleted_block_;
  109. bool reuse_mem_;
  110. uint32_t input_index_;
  111. bool continuous_block_;
  112. bool first_continuous_block_;
  113. bool last_continuous_block_;
  114. bool is_zero_copy_;
  115. std::map<int64_t, size_t> depend_stream_life_;
  116. private:
  117. size_t block_size_;
  118. std::vector<size_t> real_size_list_;
  119. std::vector<size_t> no_align_size_list_;
  120. size_t head_offset_;
  121. size_t tail_offset_;
  122. size_t child_offset_;
  123. std::vector<NodeTypeIndex> node_type_index_list_;
  124. std::vector<std::string> symbol_list_;
  125. std::vector<MemoryBlock *> child_blocks_;
  126. };
  127. class BlockMemAssigner : public MemAssigner {
  128. public:
  129. BlockMemAssigner(ComputeGraphPtr compute_graph, const std::map<std::string, std::string> &anchor_to_symbol,
  130. const std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors);
  131. BlockMemAssigner(const BlockMemAssigner &) = delete;
  132. BlockMemAssigner &operator=(const BlockMemAssigner &) = delete;
  133. ~BlockMemAssigner() override;
  134. Status Assign() override;
  135. size_t GetMemOffset() const { return mem_offset_; };
  136. int64_t GetAtomicAddrCleanId() const { return atomic_addr_clean_id_; };
  137. std::vector<MemoryBlock *> GetMemoryBlocks() const { return memory_blocks_; };
  138. ///
  139. /// @ingroup domi
  140. /// @brief memory size fixed for reuse. get memory range
  141. /// @param [out] ranges return memory range
  142. /// @return Status result
  143. ///
  144. virtual Status GetMemoryRanges(std::vector<int64_t> &ranges) = 0;
  145. ///
  146. /// @ingroup domi
  147. /// @brief traverse all nodes' outputs and needed workspace mem, apply memory, consider reuse memory
  148. /// @param [in] ranges memory range provided
  149. /// @author
  150. ///
  151. void AssignMemoryWithReuse(std::vector<int64_t> &ranges);
  152. void SetOpMemOffset(bool is_zero_copy);
  153. protected:
  154. ///
  155. /// @ingroup domi
  156. /// @brief traverse all memory size, resize, and calculate offset
  157. /// @param [in&out] memory_blocks memory size, resize and calculate memory address after offset
  158. ///
  159. void ResizeMemoryBlocks();
  160. void GetOutAndWorkSpaceMem(std::vector<int64_t> &all_memory_size);
  161. void GetNodeWorkSpaceSize(const ge::NodePtr &node, std::vector<int64_t> &workspace_memory);
  162. ///
  163. /// @ingroup GE
  164. /// @brief Determine whether it is the type of zero memory node.
  165. /// @param [in] node type.
  166. /// @return bool true: is zero memory node; false: is not zero memory node
  167. /// @author
  168. ///
  169. bool CheckIsZeroMemNodeType(const std::string &node_type) const;
  170. ///
  171. /// @ingroup GE
  172. /// @brief Check pre_reuse flag & post_reuse glag for each symbol
  173. /// @return void
  174. ///
  175. void InitReuseFlag();
  176. ///
  177. /// @ingroup GE
  178. /// @brief get pre_reuse flag
  179. /// @param [in] node
  180. /// @param [in] out_index
  181. /// @return bool
  182. ///
  183. bool IsPreReuse(const NodePtr &node, uint32_t out_index) const;
  184. ///
  185. /// @ingroup GE
  186. /// @brief get post_reuse flag
  187. /// @param [in] mem_block
  188. /// @return bool
  189. ///
  190. bool IsPostReuse(const MemoryBlock *mem_block) const;
  191. ///
  192. /// @ingroup GE
  193. /// @brief check if symbol of cur node_index_io has block
  194. /// @param [in] node_index_io
  195. /// @param [out] symbol
  196. /// @return bool
  197. ///
  198. bool IsSymbolExist(const NodeIndexIO &node_index_io, std::string &symbol);
  199. ///
  200. /// @ingroup GE
  201. /// @brief Print symbol
  202. /// @return void
  203. ///
  204. void PrintSymbolMap();
  205. size_t mem_offset_;
  206. ge::ComputeGraphPtr compute_graph_;
  207. std::vector<MemoryBlock *> memory_blocks_;
  208. std::vector<MemoryBlock *> blocks_store_;
  209. std::vector<NodeTypeIndex> zero_memory_list_;
  210. // ref mapping
  211. const std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors_;
  212. const std::map<std::string, std::string> &anchor_to_symbol_;
  213. std::map<std::string, bool> pre_reuse_flag_;
  214. std::map<std::string, bool> post_reuse_flag_;
  215. std::map<std::string, size_t> symbol_size_;
  216. private:
  217. ///
  218. /// @ingroup GE
  219. /// @brief Traversing the compute_graph_ to apply for output memory while considering reuse
  220. /// @param [in] n node in compute_graph_
  221. /// @param [in] index output node index
  222. /// @param [in] ranges available memory specifications
  223. /// @return MemoryBlock*
  224. /// @author
  225. ///
  226. MemoryBlock *ApplyOutMemory(const ge::NodePtr &n, uint32_t index, const std::vector<int64_t> &ranges,
  227. const bool is_op_reuse_mem, const bool continuous);
  228. Status AssignOutputMemoryWithReuse(const NodePtr &node, vector<int64_t> &ranges);
  229. ///
  230. /// @ingroup GE
  231. /// @brief Traversing the compute_graph_ to apply for memory while considering reuse
  232. /// @param [in] block_size applied memory block size
  233. /// @param [in] real_size actual memory size required
  234. /// @param [in] type output or workspace
  235. /// @param [in] n node in compute_graph_
  236. /// @param [in] out_index output node index
  237. /// @param [in] workspace_reuse_flag reuse flag for workspace
  238. /// @return MemoryBlock*
  239. /// @author
  240. ///
  241. MemoryBlock *ApplyMemory(size_t block_size, size_t real_size, size_t no_align_size, MemoryType mem_type,
  242. const ge::NodePtr &n, uint32_t out_index, const std::vector<bool> &workspace_reuse_flag,
  243. const bool is_op_reuse_mem, const bool continuous);
  244. ///
  245. /// @ingroup GE
  246. /// @brief check workspace_reuse_flag to judge if add workspace block wait reuse
  247. /// @param [in] workspace_reuse_flag mark out index if support resue
  248. /// @param [in] index out index
  249. /// @param [in] stream_id which stream op in
  250. /// @param [in] mem_block node workspace mem_block
  251. /// @return void
  252. /// @author
  253. ///
  254. void CheckWorkspaceReuse(const vector<bool> &workspace_reuse_flag, uint32_t index, int64_t stream_id,
  255. MemoryBlock *mem_block);
  256. ///
  257. /// @ingroup GE
  258. /// @brief Release memory block to reusable list
  259. /// @param [in] to_release memory block to be released
  260. /// @param [in] reusable_memory reusable list
  261. /// @return void
  262. /// @author
  263. ///
  264. void ReleaseMemory(MemoryBlock *to_release, vector<MemoryBlock *> &reusable_memory);
  265. ///
  266. /// @ingroup GE
  267. /// @brief Release memory blocks to reusable list
  268. /// @param [in] to_releases memory blocks to be released
  269. /// @param [in] reusable_memory reusable list
  270. /// @return void
  271. /// @author
  272. ///
  273. void ReleaseMemorys(const vector<MemoryBlock *> &to_releases, vector<MemoryBlock *> &reusable_memory);
  274. ///
  275. /// @ingroup GE
  276. /// @brief Release memory block to reusable list
  277. /// @param [in] n node in compute_graph_
  278. /// @param [in] node_out_blocks output memory blocks for ops
  279. /// @param [in] reusable_memory reusable list
  280. /// @return void
  281. /// @author
  282. ///
  283. void ReleaseInputNodeOutMemory(const std::unordered_map<string, vector<MemoryBlock *>> &node_out_blocks,
  284. vector<MemoryBlock *> &reusable_memory, ge::NodePtr &n);
  285. ///
  286. /// @ingroup GE
  287. /// @brief Merge memory blocks between different batchs
  288. /// @return merge or not
  289. /// @author
  290. ///
  291. bool MergeDynamicBatchBlocks();
  292. void AssignContinuousBlocks();
  293. bool IsZeroCopyBlock(const NodePtr &node, bool continuous);
  294. bool IsOutNodeSetContinuousInput(const NodePtr &n, uint32_t out_index, std::string &peer_name,
  295. uint32_t &peer_input_index, bool &no_need_assign_memory);
  296. ///
  297. /// @ingroup GE
  298. /// @|+++++++++block1++++++++| |+++++++++block1++++++++|
  299. /// @|+++++++++block1++++++++||++block2++| |+++++++++block1++++++++||++block2++|
  300. /// @ |++block2++||++block3++| ==> |++block3++| |++block2++|
  301. /// @ |++block3++| |++block3++|
  302. /// @return void
  303. /// @author
  304. ///
  305. void ReuseBlocksByLifeTime(size_t range_size);
  306. bool IsContinuousOutput(const NodePtr &n);
  307. MemoryBlock *ApplyContinuousMemory(const NodePtr &n, const vector<int64_t> &ranges, const bool is_op_reuse_mem);
  308. std::unordered_map<int64_t, std::vector<MemoryBlock *>> reusable_blocks_;
  309. std::map<std::string, uint64_t> reusable_block_counts_;
  310. std::unordered_map<int64_t, std::vector<MemoryBlock *>> stream_workspace_blocks_;
  311. std::unordered_map<std::string, std::vector<MemoryBlock *>> node_out_blocks_;
  312. std::unordered_map<std::string, MemoryBlock *> symbol_blocks_;
  313. std::unordered_map<std::string, std::unordered_map<uint32_t, MemoryBlock *>> node_continuous_input_blocks_;
  314. std::unordered_map<std::string, uint32_t> node_continuous_input_counts_;
  315. // reuse memory
  316. vector<string> op_no_reuse_mem_vec_;
  317. bool op_reuse_env_valid_ = false;
  318. std::string ge_disable_reuse_mem_env_ = "0";
  319. bool is_op_reuse_mem_ = true;
  320. size_t life_time_;
  321. int64_t atomic_addr_clean_id_ = 0;
  322. DependStreamLife total_node_depend_stream_life_;
  323. };
  324. } // namespace ge
  325. #endif // GE_GRAPH_BUILD_MEMORY_BLOCK_MEM_ASSIGNER_H_

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