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.2 kB

4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/debug/ge_log.h"
  28. #include "framework/common/ge_inner_error_codes.h"
  29. #include "graph/node.h"
  30. #include "graph/manager/block_memory.h"
  31. #include "runtime/mem.h"
  32. namespace ge {
  33. constexpr size_t kRoundBlockSize = 512; // all block sizes are rounded to at least 512 bytes
  34. constexpr size_t kBinSizeUnit4 = 4;
  35. constexpr size_t kBinSizeUnit8 = 8;
  36. constexpr size_t kBinSizeUnit32 = 32;
  37. constexpr size_t kBinSizeUnit128 = 128;
  38. constexpr size_t kBinSizeUnit256 = 256;
  39. constexpr size_t kBinSizeUnit512 = 512;
  40. constexpr double kSplitThreshold = 0.5; // split when malloc size <= small block size * kSpliThreshold
  41. constexpr size_t kKByteSize = 1024;
  42. constexpr size_t kMByteSize = 1048576; // 1024 * 1024
  43. constexpr size_t kGByteSize = 1073741824; // 1024 * 1024 * 1024
  44. static const uint32_t kNumBins = 7;
  45. class MemoryAllocator;
  46. class CachingAllocator {
  47. public:
  48. explicit CachingAllocator(rtMemType_t memory_type);
  49. CachingAllocator(const CachingAllocator &) = delete;
  50. CachingAllocator &operator=(const CachingAllocator &) = delete;
  51. virtual ~CachingAllocator() = default;
  52. ///
  53. /// @ingroup ge_graph
  54. /// @brief caching allocator init
  55. /// @param [in] device id
  56. /// @return Status of init
  57. ///
  58. Status Initialize(uint32_t device_id = 0);
  59. ///
  60. /// @ingroup ge_graph
  61. /// @brief memory allocator finalize, release cached memory
  62. /// @return void
  63. ///
  64. void Finalize(uint32_t device_id = 0);
  65. ///
  66. /// @ingroup ge_graph
  67. /// @brief malloc memory
  68. /// @param [in] size memory size
  69. /// @param [in] try to reuse the same memory
  70. /// @param [in] device id
  71. /// @return memory address
  72. ///
  73. uint8_t *Malloc(size_t size, uint8_t *org_ptr = nullptr, uint32_t device_id = 0);
  74. ///
  75. /// @ingroup ge_graph
  76. /// @brief free memory
  77. /// @param [in] memory_ptr memory address ptr
  78. /// @param [in] device_id device id
  79. /// @return Status result of function
  80. ///
  81. Status Free(uint8_t *memory_addr, uint32_t device_id = 0);
  82. ///
  83. /// @ingroup ge_graph
  84. /// @brief try to free memory when no memory is referenced
  85. /// @return void
  86. ///
  87. void TryFreeBlocks();
  88. private:
  89. ///
  90. /// @ingroup ge_graph
  91. /// @brief extend cache by size
  92. /// @param [in] memory size
  93. /// @param [in] device id
  94. /// @return Status result of function
  95. ///
  96. Status TryExtendCache(size_t size, uint32_t device_id);
  97. ///
  98. /// @ingroup ge_graph
  99. /// @brief find free block by size
  100. /// @param [in] memory size
  101. /// @param [in] device_id device id
  102. /// @return block ptr
  103. ///
  104. Block *FindFreeBlock(size_t size, uint8_t *org_ptr, uint32_t device_id);
  105. ///
  106. /// @ingroup ge_graph
  107. /// @brief get the right bin based on size
  108. /// @param [in] original malloc size
  109. /// @return block bin
  110. ///
  111. BlockBin *GetBlockBin(size_t size);
  112. ///
  113. /// @ingroup ge_graph
  114. /// @brief add memory to right bin based on size
  115. /// @param [in] memory ptr
  116. /// @param [in] memory size
  117. /// @param [in] device_id device id
  118. /// @return Status result of function
  119. ///
  120. Status AddToBlockBin(uint8_t *ptr, size_t size, uint32_t device_id);
  121. ///
  122. /// @ingroup ge_graph
  123. /// @brief free block to right bin
  124. /// @param [in] block ptr
  125. /// @return void
  126. ///
  127. void FreeBlock(Block* block);
  128. ///
  129. /// @ingroup ge_graph
  130. /// @brief free all cached blocks to right bin and release the memory when memory is not enough
  131. /// @return free cached memory size
  132. ///
  133. size_t FreeCachedBlocks();
  134. ///
  135. /// @ingroup ge_graph
  136. /// @brief free allocated and cached blocks and release the memory when process exit
  137. /// @return void
  138. ///
  139. void FreeBlocks();
  140. ///
  141. /// @ingroup ge_graph
  142. /// @brief free block bins when process exit
  143. /// @return void
  144. ///
  145. void FreeBlockBins();
  146. ///
  147. /// @ingroup ge_graph
  148. /// @brief If a split block is freed, try merging with the original block
  149. /// @param [inout] dest block ptr
  150. /// @param [in] src block ptr
  151. /// @param [out] block bin
  152. /// @return void
  153. ///
  154. void MergeBlocks(Block *dst, Block *src, BlockBin &bin);
  155. ///
  156. /// @ingroup ge_graph
  157. /// @brief If the allocated memory size is too much smaller than the memory block, try to split the memory block
  158. /// @param [in] original block ptr
  159. /// @param [in] allocated memory size
  160. /// @param [in] block bin
  161. /// @param [in] device id
  162. /// @return splited block ptr
  163. ///
  164. Block *SplitBlock(Block *block, size_t size, BlockBin &bin, uint32_t device_id);
  165. ///
  166. /// @ingroup ge_graph
  167. /// @brief print the memory info in pool
  168. /// @param [in] log level
  169. /// @return void
  170. ///
  171. void PrintStatics(int32_t level = DLOG_INFO);
  172. private:
  173. rtMemType_t memory_type_;
  174. // device memory allocator
  175. MemoryAllocator *memory_allocator_;
  176. // lock around all operations
  177. mutable std::recursive_mutex mutex_;
  178. // allocated blocks by memory pointer
  179. std::unordered_map<uint8_t *, Block *> allocated_blocks_;
  180. // block bins by different block size
  181. BlockBin *free_block_bins_[kNumBins];
  182. // malloced memorys from device
  183. std::map<size_t, size_t> malloced_memory_;
  184. //user call Malloc total counts
  185. std::atomic<size_t> called_malloc_counts_;
  186. //user call Free total counts
  187. std::atomic<size_t> called_free_counts_;
  188. };
  189. } // namespace ge
  190. #endif // GE_GRAPH_MANAGER_GRAPH_CACHING_ALLOCATOR_H_

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