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

4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. private:
  82. ///
  83. /// @ingroup ge_graph
  84. /// @brief extend cache by size
  85. /// @param [in] memory size
  86. /// @param [in] device id
  87. /// @return Status result of function
  88. ///
  89. Status TryExtendCache(size_t size, uint32_t device_id);
  90. ///
  91. /// @ingroup ge_graph
  92. /// @brief find free block by size
  93. /// @param [in] memory size
  94. /// @param [in] device_id device id
  95. /// @return block ptr
  96. ///
  97. Block *FindFreeBlock(size_t size, uint8_t *org_ptr, uint32_t device_id);
  98. ///
  99. /// @ingroup ge_graph
  100. /// @brief get the right bin based on size
  101. /// @param [in] original malloc size
  102. /// @return block bin
  103. ///
  104. BlockBin *GetBlockBin(size_t size);
  105. ///
  106. /// @ingroup ge_graph
  107. /// @brief add memory to right bin based on size
  108. /// @param [in] memory ptr
  109. /// @param [in] memory size
  110. /// @param [in] device_id device id
  111. /// @return Status result of function
  112. ///
  113. Status AddToBlockBin(uint8_t *ptr, size_t size, uint32_t device_id);
  114. ///
  115. /// @ingroup ge_graph
  116. /// @brief free block to right bin
  117. /// @param [in] block ptr
  118. /// @return void
  119. ///
  120. void FreeBlock(Block* block);
  121. ///
  122. /// @ingroup ge_graph
  123. /// @brief free all cached blocks to right bin and release the memory when memory is not enough
  124. /// @return free cached memory size
  125. ///
  126. size_t FreeCachedBlocks();
  127. ///
  128. /// @ingroup ge_graph
  129. /// @brief free allocated and cached blocks and release the memory when process exit
  130. /// @return void
  131. ///
  132. void FreeBlocks();
  133. ///
  134. /// @ingroup ge_graph
  135. /// @brief free block bins when process exit
  136. /// @return void
  137. ///
  138. void FreeBlockBins();
  139. ///
  140. /// @ingroup ge_graph
  141. /// @brief If a split block is freed, try merging with the original block
  142. /// @param [inout] dest block ptr
  143. /// @param [in] src block ptr
  144. /// @param [out] block bin
  145. /// @return void
  146. ///
  147. void MergeBlocks(Block *dst, Block *src, BlockBin &bin);
  148. ///
  149. /// @ingroup ge_graph
  150. /// @brief If the allocated memory size is too much smaller than the memory block, try to split the memory block
  151. /// @param [in] original block ptr
  152. /// @param [in] allocated memory size
  153. /// @param [in] block bin
  154. /// @param [in] device id
  155. /// @return splited block ptr
  156. ///
  157. Block *SplitBlock(Block *block, size_t size, BlockBin &bin, uint32_t device_id);
  158. ///
  159. /// @ingroup ge_graph
  160. /// @brief print the memory info in pool
  161. /// @return void
  162. ///
  163. void PrintStatics();
  164. private:
  165. rtMemType_t memory_type_;
  166. // device memory allocator
  167. MemoryAllocator *memory_allocator_;
  168. // lock around all operations
  169. mutable std::recursive_mutex mutex_;
  170. // allocated blocks by memory pointer
  171. std::unordered_map<uint8_t *, Block *> allocated_blocks_;
  172. // block bins by different block size
  173. BlockBin *free_block_bins_[kNumBins];
  174. // malloced memorys from device
  175. std::map<size_t, size_t> malloced_memory_;
  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两部分组成,详细的架构图如下所示