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.cc 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. #include "graph/manager/graph_caching_allocator.h"
  17. #include <set>
  18. #include <string>
  19. #include <utility>
  20. #include "framework/common/debug/ge_log.h"
  21. #include "graph/manager/graph_mem_allocator.h"
  22. namespace ge {
  23. const size_t bin_ranges[kNumBins] = {kRoundBlockSize * kKByteSize,
  24. kBinSizeUnit8 * kMByteSize,
  25. kBinSizeUnit32 * kMByteSize,
  26. kBinSizeUnit128 * kMByteSize,
  27. kBinSizeUnit256 * kMByteSize,
  28. kBinSizeUnit512 * kMByteSize,
  29. kGByteSize};
  30. static bool BlockComparator(const Block *left, const Block *right) {
  31. if (left->size != right->size) {
  32. return left->size < right->size;
  33. }
  34. return reinterpret_cast<uintptr_t>(left->ptr) < reinterpret_cast<uintptr_t>(right->ptr);
  35. }
  36. bool CanMerge(Block *block) {
  37. if (block == nullptr || block->allocated || !block->IsSplit()) {
  38. return false;
  39. }
  40. return true;
  41. }
  42. size_t GetBinIndex(size_t size) {
  43. size_t index = 0;
  44. for (auto range : bin_ranges) {
  45. if (size <= range) {
  46. break;
  47. }
  48. ++index;
  49. }
  50. if (index > kNumBins - 1) {
  51. index = kNumBins - 1;
  52. }
  53. return index;
  54. }
  55. size_t GetAllocationSize(size_t size) {
  56. size_t index = GetBinIndex(size);
  57. if (bin_ranges[index] >= size) {
  58. return bin_ranges[index];
  59. }
  60. return kGByteSize * ((size + kGByteSize - 1) / kGByteSize);
  61. }
  62. ///
  63. /// @ingroup ge_graph
  64. /// @brief block size based on alignment
  65. /// @param [in] original malloc size
  66. /// @return allocation size
  67. ///
  68. size_t GetBlockSize(size_t size) {
  69. if (size == 0) {
  70. return kRoundBlockSize;
  71. }
  72. return kRoundBlockSize * ((size + kRoundBlockSize - 1) / kRoundBlockSize);
  73. }
  74. bool ShouldSplit(const Block *block, size_t size) {
  75. return static_cast<double>(size) <= (static_cast<double>(block->size) * kSplitThreshold);
  76. }
  77. CachingAllocator::CachingAllocator(rtMemType_t memory_type) : memory_type_(memory_type), memory_allocator_(nullptr) {
  78. for (uint32_t i = 0; i < kNumBins; ++i) {
  79. free_block_bins_[i] = nullptr;
  80. }
  81. }
  82. Status CachingAllocator::Initialize(uint32_t device_id) {
  83. GELOGI("Device id %u", device_id);
  84. // when redo Initialize free old memory
  85. FreeBlocks();
  86. std::lock_guard<std::recursive_mutex> lock(mutex_);
  87. for (uint32_t i = 0; i < kNumBins; ++i) {
  88. if (free_block_bins_[i] != nullptr) {
  89. continue;
  90. }
  91. auto bin_ptr = new (std::nothrow) BlockBin(BlockComparator);
  92. if (bin_ptr == nullptr) {
  93. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "Alloc BlockBin failed.");
  94. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  95. }
  96. free_block_bins_[i] = bin_ptr;
  97. }
  98. memory_allocator_ = MemManager::Instance(memory_type_);
  99. if (memory_allocator_ == nullptr) {
  100. return ACL_ERROR_GE_INTERNAL_ERROR;
  101. }
  102. return ge::SUCCESS;
  103. }
  104. void CachingAllocator::Finalize(uint32_t device_id) {
  105. GELOGI("Device id %u", device_id);
  106. FreeBlocks();
  107. FreeBlockBins();
  108. }
  109. uint8_t *CachingAllocator::Malloc(size_t size, uint8_t *org_ptr, uint32_t device_id) {
  110. GELOGI("Start malloc pool memory, size = %zu, device id = %u", size, device_id);
  111. uint8_t *ptr = nullptr;
  112. size = GetBlockSize(size);
  113. Block *block = FindFreeBlock(size, org_ptr, device_id);
  114. if (block != nullptr) {
  115. ptr = block->ptr;
  116. } else {
  117. if (ge::SUCCESS == TryExtendCache(size, device_id)) {
  118. block = FindFreeBlock(size, org_ptr, device_id);
  119. if (block != nullptr) {
  120. ptr = block->ptr;
  121. }
  122. }
  123. }
  124. if (ptr == nullptr) {
  125. GELOGE(FAILED, "Malloc failed device id = %u, size= %zu", device_id, size);
  126. }
  127. return ptr;
  128. }
  129. Status CachingAllocator::Free(uint8_t *ptr, uint32_t device_id) {
  130. GELOGI("Free device id = %u", device_id);
  131. if (ptr == nullptr) {
  132. GELOGE(PARAM_INVALID, "Invalid memory pointer");
  133. return ge::PARAM_INVALID;
  134. }
  135. std::lock_guard<std::recursive_mutex> lock(mutex_);
  136. auto it = allocated_blocks_.find(ptr);
  137. if (it == allocated_blocks_.end()) {
  138. GELOGE(PARAM_INVALID, "Invalid memory pointer");
  139. return ge::PARAM_INVALID;
  140. }
  141. Block *block = it->second;
  142. allocated_blocks_.erase(it);
  143. FreeBlock(block);
  144. return ge::SUCCESS;
  145. }
  146. void CachingAllocator::FreeBlock(Block *block) {
  147. if (block == nullptr || !block->allocated) {
  148. return;
  149. }
  150. GELOGI("Free block size = %zu", block->size);
  151. std::lock_guard<std::recursive_mutex> lock(mutex_);
  152. block->allocated = false;
  153. auto &bin = *block->bin;
  154. Block *merge_blocks[] = {block->prev, block->next};
  155. for (Block *merge_block : merge_blocks) {
  156. MergeBlocks(block, merge_block, bin);
  157. }
  158. bin.insert(block);
  159. }
  160. void CachingAllocator::MergeBlocks(Block *dst, Block *src, BlockBin &bin) {
  161. if (!CanMerge(dst) || !CanMerge(src)) {
  162. return;
  163. }
  164. if (dst->prev == src) {
  165. dst->ptr = src->ptr;
  166. dst->prev = src->prev;
  167. if (dst->prev != nullptr) {
  168. dst->prev->next = dst;
  169. }
  170. } else {
  171. dst->next = src->next;
  172. if (dst->next != nullptr) {
  173. dst->next->prev = dst;
  174. }
  175. }
  176. dst->size += src->size;
  177. bin.erase(src);
  178. delete src;
  179. }
  180. BlockBin *CachingAllocator::GetBlockBin(size_t size) {
  181. size_t index = GetBinIndex(size);
  182. return free_block_bins_[index];
  183. }
  184. Block *CachingAllocator::FindFreeBlock(size_t size, uint8_t *org_ptr, uint32_t device_id) {
  185. // org_ptr - 1, try to find ptr same as org_ptr
  186. Block key(device_id, size, (org_ptr == nullptr ? nullptr : org_ptr - 1));
  187. BlockBin *bin = GetBlockBin(size);
  188. if (bin == nullptr) {
  189. GELOGE(ge::FAILED, "Get block bin failed size = %zu", size);
  190. return nullptr;
  191. }
  192. std::lock_guard<std::recursive_mutex> lock(mutex_);
  193. auto it = bin->lower_bound(&key);
  194. if (it != bin->end()) {
  195. Block *block = *it;
  196. bin->erase(it);
  197. if (block != nullptr) {
  198. GELOGI("Find block size = %zu", block->size);
  199. if (ShouldSplit(block, size)) {
  200. block = SplitBlock(block, size, *bin, device_id);
  201. }
  202. if (block->ptr != nullptr) {
  203. block->allocated = true;
  204. allocated_blocks_[block->ptr] = block;
  205. GELOGI("Malloc device id = %u, size= %zu", device_id, size);
  206. }
  207. }
  208. return block;
  209. }
  210. return nullptr;
  211. }
  212. Block *CachingAllocator::SplitBlock(Block *block, size_t size, BlockBin &bin, uint32_t device_id) {
  213. // block has been checked, should not be nullptr
  214. Block *remaining = block;
  215. Block *new_block = new (std::nothrow) Block(device_id, size, &bin, block->ptr);
  216. if (new_block == nullptr) {
  217. GELOGE(ge::FAILED, "Alloc block failed size = %zu", size);
  218. return block;
  219. }
  220. new_block->prev = remaining->prev;
  221. if (new_block->prev != nullptr) {
  222. new_block->prev->next = new_block;
  223. }
  224. new_block->next = remaining;
  225. remaining->prev = new_block;
  226. remaining->ptr = remaining->ptr + size;
  227. remaining->size -= size;
  228. bin.insert(remaining);
  229. return new_block;
  230. }
  231. Status CachingAllocator::TryExtendCache(size_t size, uint32_t device_id) {
  232. GELOGI("Try to extend cache. size = %zu, device id = %u", size, device_id);
  233. auto memory_size = GetAllocationSize(size);
  234. const std::string purpose = "Memory for caching.";
  235. auto memory_addr = memory_allocator_->MallocMemory(purpose, memory_size, device_id);
  236. // try to free caches and malloc again when malloc memory failed
  237. if (memory_addr == nullptr) {
  238. FreeCachedBlocks();
  239. memory_addr = memory_allocator_->MallocMemory(purpose, memory_size, device_id);
  240. if (memory_addr == nullptr) {
  241. GELOGE(ge::FAILED, "TryExtendCache failed, no enough memory for size = %zu, device_id = %u", memory_size,
  242. device_id);
  243. return ge::FAILED;
  244. }
  245. }
  246. if (AddToBlockBin(memory_addr, memory_size, device_id) != ge::SUCCESS) {
  247. (void)memory_allocator_->FreeMemory(memory_addr);
  248. return ge::FAILED;
  249. }
  250. return ge::SUCCESS;
  251. }
  252. Status CachingAllocator::AddToBlockBin(uint8_t *ptr, size_t size, uint32_t device_id) {
  253. BlockBin *bin = GetBlockBin(size);
  254. if (bin == nullptr) {
  255. GELOGE(ge::FAILED, "Get block bin failed size = %zu", size);
  256. return ge::FAILED;
  257. }
  258. Block *block = new (std::nothrow) Block(device_id, size, bin, nullptr);
  259. if (block == nullptr) {
  260. GELOGE(ge::FAILED, "Alloc block failed size = %zu", size);
  261. return ge::FAILED;
  262. }
  263. GELOGI("Block size = %zu", size);
  264. block->ptr = ptr;
  265. block->size = size;
  266. std::lock_guard<std::recursive_mutex> lock(mutex_);
  267. bin->insert(block);
  268. return ge::SUCCESS;
  269. }
  270. void CachingAllocator::FreeCachedBlocks() {
  271. GELOGI("Free cached blocks");
  272. std::lock_guard<std::recursive_mutex> lock(mutex_);
  273. for (uint32_t i = 0; i < kNumBins; ++i) {
  274. auto pool = free_block_bins_[i];
  275. if (pool == nullptr) {
  276. continue;
  277. }
  278. for (auto it = pool->begin(); it != pool->end();) {
  279. Block *block = *it;
  280. // free block memory that has not been split
  281. if ((block != nullptr) && (block->ptr != nullptr) && (block->prev == nullptr) && (block->next == nullptr) &&
  282. (memory_allocator_->FreeMemory(block->ptr) == ge::SUCCESS)) {
  283. pool->erase(it++);
  284. delete block;
  285. continue;
  286. }
  287. ++it;
  288. }
  289. }
  290. }
  291. void CachingAllocator::FreeBlocks() {
  292. GELOGI("Free blocks");
  293. std::lock_guard<std::recursive_mutex> lock(mutex_);
  294. // free allocated blocks and put to cache
  295. for (auto &it : allocated_blocks_) {
  296. FreeBlock(it.second);
  297. }
  298. allocated_blocks_.clear();
  299. FreeCachedBlocks();
  300. }
  301. void CachingAllocator::FreeBlockBins() {
  302. GELOGI("Free block bins");
  303. std::lock_guard<std::recursive_mutex> lock(mutex_);
  304. for (uint32_t i = 0; i < kNumBins; ++i) {
  305. if (free_block_bins_[i] != nullptr) {
  306. delete free_block_bins_[i];
  307. free_block_bins_[i] = nullptr;
  308. }
  309. }
  310. }
  311. } // namespace ge

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