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

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

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