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

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

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