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.

graph_caching_allocator.h 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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_MANAGER_GRAPH_CACHING_ALLOCATOR_H_
  17. #define GE_GRAPH_MANAGER_GRAPH_CACHING_ALLOCATOR_H_
  18. #include <iostream>
  19. #include <map>
  20. #include <memory>
  21. #include <mutex>
  22. #include <string>
  23. #include <vector>
  24. #include <set>
  25. #include <unordered_map>
  26. #include <unordered_set>
  27. #include "framework/common/ge_inner_error_codes.h"
  28. #include "graph/node.h"
  29. #include "runtime/mem.h"
  30. namespace ge {
  31. constexpr size_t kRoundBlockSize = 512; // all block sizes are rounded to at least 512 bytes
  32. constexpr double kSplitThreshold = 0.75; // split when malloc size <= small block size * kSpliThreshold
  33. constexpr size_t kKByteSize = 1024;
  34. constexpr size_t kMByteSize = 1024 * 1024;
  35. constexpr size_t kGByteSize = 1024 * 1024 * 1024;
  36. struct Block;
  37. typedef bool (*Comparison)(const Block *, const Block *);
  38. using BlockBin = std::set<Block *, Comparison>;
  39. static const uint32_t kNumBins = 8;
  40. struct Block {
  41. uint32_t device_id; // npu device id
  42. size_t size; // block size in bytes
  43. BlockBin *bin; // owning block bin
  44. uint8_t *ptr; // memory address
  45. bool allocated; // in-use flag
  46. Block *prev; // prev block if split from a larger allocation
  47. Block *next; // next block if split from a larger allocation
  48. Block(uint32_t device, size_t size, BlockBin *bin, uint8_t *ptr)
  49. : device_id(device), size(size), bin(bin), ptr(ptr), allocated(0), prev(nullptr), next(nullptr) {}
  50. // constructor for search key
  51. Block(uint32_t device, size_t size, uint8_t *ptr)
  52. : device_id(device), size(size), bin(nullptr), ptr(ptr), allocated(0), prev(nullptr), next(nullptr) {}
  53. bool IsSplit() const { return (prev != nullptr) || (next != nullptr); }
  54. };
  55. class MemoryAllocator;
  56. class CachingAllocator {
  57. public:
  58. explicit CachingAllocator(rtMemType_t memory_type);
  59. virtual ~CachingAllocator() = default;
  60. ///
  61. /// @ingroup ge_graph
  62. /// @brief caching allocator init
  63. /// @param [in] device id
  64. /// @return Status of init
  65. ///
  66. Status Initialize(uint32_t device_id = 0);
  67. ///
  68. /// @ingroup ge_graph
  69. /// @brief memory allocator finalize, release cached memory
  70. /// @return void
  71. ///
  72. void Finalize(uint32_t device_id = 0);
  73. ///
  74. /// @ingroup ge_graph
  75. /// @brief malloc memory
  76. /// @param [in] size memory size
  77. /// @param [in] try to reuse the same memory
  78. /// @param [in] device id
  79. /// @return memory address
  80. ///
  81. uint8_t *Malloc(size_t size, uint8_t *org_ptr = nullptr, uint32_t device_id = 0);
  82. ///
  83. /// @ingroup ge_graph
  84. /// @brief free memory
  85. /// @param [in] device_id device id
  86. /// @param [out] memory_ptr memory address ptr
  87. /// @return Status result of function
  88. ///
  89. Status Free(uint8_t *memory_addr, uint32_t device_id = 0);
  90. private:
  91. ///
  92. /// @ingroup ge_graph
  93. /// @brief extend cache by size
  94. /// @param [in] memory size
  95. /// @param [in] device id
  96. /// @return Status result of function
  97. ///
  98. Status TryExtendCache(size_t size, uint32_t device_id);
  99. ///
  100. /// @ingroup ge_graph
  101. /// @brief find free block by size
  102. /// @param [in] memory size
  103. /// @param [in] device_id device id
  104. /// @return block ptr
  105. ///
  106. Block *FindFreeBlock(size_t size, uint8_t *org_ptr, uint32_t device_id);
  107. ///
  108. /// @ingroup ge_graph
  109. /// @brief get the right bin based on size
  110. /// @param [in] original malloc size
  111. /// @return block bin
  112. ///
  113. BlockBin *GetBlockBin(size_t size);
  114. ///
  115. /// @ingroup ge_graph
  116. /// @brief add memory to right bin based on size
  117. /// @param [in] memory ptr
  118. /// @param [in] memory size
  119. /// @return Status result of function
  120. ///
  121. Status AddToBlockBin(uint8_t *ptr, size_t size);
  122. ///
  123. /// @ingroup ge_graph
  124. /// @brief free block to right bin
  125. /// @param [in] block ptr
  126. /// @return void
  127. ///
  128. void FreeBlock(Block *block);
  129. ///
  130. /// @ingroup ge_graph
  131. /// @brief free all cached blocks to right bin and release the memory when memory is not enough
  132. /// @return void
  133. ///
  134. void FreeCachedBlocks();
  135. ///
  136. /// @ingroup ge_graph
  137. /// @brief free allocated and cached blocks and release the memory when process exit
  138. /// @return void
  139. ///
  140. void FreeBlocks();
  141. ///
  142. /// @ingroup ge_graph
  143. /// @brief free block bins when process exit
  144. /// @return void
  145. ///
  146. void FreeBlockBins();
  147. ///
  148. /// @ingroup ge_graph
  149. /// @brief If a split block is freed, try merging with the original block
  150. /// @param [inout] dest block ptr
  151. /// @param [in] src block ptr
  152. /// @param [out] block bin
  153. /// @return void
  154. ///
  155. void MergeBlocks(Block *dst, Block *src, BlockBin &bin);
  156. ///
  157. /// @ingroup ge_graph
  158. /// @brief If the allocated memory size is too much smaller than the memory block, try to split the memory block
  159. /// @param [in] original block ptr
  160. /// @param [in] allocated memory size
  161. /// @param [in] block bin
  162. /// @param [in] device id
  163. /// @return splited block ptr
  164. ///
  165. Block *SplitBlock(Block *block, size_t size, BlockBin &bin, uint32_t device_id);
  166. private:
  167. rtMemType_t memory_type_;
  168. // device memory allocator
  169. MemoryAllocator *memory_allocator_;
  170. // lock around all operations
  171. mutable std::recursive_mutex mutex_;
  172. // allocated blocks by memory pointer
  173. std::unordered_map<uint8_t *, Block *> allocated_blocks_;
  174. // block bins by different block size
  175. BlockBin *free_block_bins_[kNumBins];
  176. };
  177. }; // namespace ge
  178. #endif // GE_GRAPH_MANAGER_GRAPH_CACHING_ALLOCATOR_H_

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