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.

npu_memory_allocator.cc 5.1 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "npu_memory_allocator.h"
  17. #include <mutex>
  18. #include "framework/common/debug/log.h"
  19. #include "graph/manager/graph_caching_allocator.h"
  20. #include "graph/manager/graph_mem_allocator.h"
  21. #include "graph/manager/rdma_pool_allocator.h"
  22. #ifndef ONLY_COMPILE_OPEN_SRC
  23. #include "graph/manager/host_mem_allocator.h"
  24. #endif
  25. namespace ge {
  26. namespace hybrid {
  27. const size_t kPaddingUnit = 2;
  28. size_t kMaxHbmMemorySize = 1024UL * 1024UL * 1024UL * 1024UL; // 1024G
  29. std::map<uint32_t, std::unique_ptr<NpuMemoryAllocator>> NpuMemoryAllocator::allocators_;
  30. std::mutex NpuMemoryAllocator::mu_;
  31. AllocationAttr::AllocationAttr(int padding, void *try_reuse_addr, MemStorageType mem_type)
  32. : padding_(padding), try_reuse_addr_(try_reuse_addr), mem_type_(mem_type) {}
  33. AllocationAttr::AllocationAttr(int padding) : AllocationAttr(padding, nullptr) {}
  34. AllocationAttr::AllocationAttr(void *try_reuse_addr) : AllocationAttr(0, try_reuse_addr) {}
  35. NpuMemoryAllocator *NpuMemoryAllocator::GetAllocator() {
  36. int32_t device_id = 0;
  37. if (rtGetDevice(&device_id) != RT_ERROR_NONE) {
  38. GELOGE(RT_FAILED, "Failed to get device id");
  39. return nullptr;
  40. }
  41. GELOGD("Got device id = %d from context", device_id);
  42. return GetAllocator(static_cast<uint32_t>(device_id));
  43. }
  44. NpuMemoryAllocator::NpuMemoryAllocator(uint32_t device_id) : device_id_(device_id) {}
  45. void *NpuMemoryAllocator::Allocate(std::size_t size, AllocationAttr *attr) {
  46. size_t allocate_size = size;
  47. MemStorageType mem_type = HBM;
  48. if (attr != nullptr) {
  49. mem_type = attr->mem_type_;
  50. }
  51. if (allocate_size == 0) {
  52. GELOGE(MEMALLOC_FAILED, "Memory size is 0, device_id = %u, size = %zu", device_id_, allocate_size);
  53. return nullptr;
  54. }
  55. void *buffer = nullptr;
  56. if (mem_type == RDMA_HBM) {
  57. buffer = MemManager::Instance().RdmaPoolInstance(RT_MEMORY_HBM).Malloc(allocate_size, device_id_);
  58. } else if (mem_type == HOST_DDR) {
  59. #ifndef ONLY_COMPILE_OPEN_SRC
  60. buffer = MemManager::Instance().HostMemInstance(RT_MEMORY_HBM).Malloc(allocate_size);
  61. #else
  62. buffer = malloc(allocate_size);
  63. #endif
  64. } else {
  65. if (allocate_size > kMaxHbmMemorySize) {
  66. GELOGE(PARAM_INVALID, "Invalid HBM memory size: %zu", allocate_size);
  67. return nullptr;
  68. }
  69. void *try_reuse_addr = nullptr;
  70. int padding = kDefaultPadding;
  71. if (attr != nullptr) {
  72. try_reuse_addr = attr->try_reuse_addr_;
  73. if (attr->padding_ > 0) {
  74. padding = attr->padding_;
  75. }
  76. }
  77. // padding up to multiple of padding, and add extra padding
  78. allocate_size = (size + kPaddingUnit * padding - 1) / padding * padding;
  79. GELOGD("Padding size %ld by %d. final size = %zu.", size, padding, allocate_size);
  80. buffer = MemManager::Instance()
  81. .CachingInstance(RT_MEMORY_HBM)
  82. .Malloc(allocate_size, reinterpret_cast<uint8_t *>(try_reuse_addr), device_id_);
  83. }
  84. if (buffer == nullptr) {
  85. GELOGE(MEMALLOC_FAILED, "Failed to malloc memory, device_id = %u, size = %zu", device_id_, allocate_size);
  86. return nullptr;
  87. }
  88. GELOGI("Allocating buffer of size %zu successfully. device_id = %u, address = %p", allocate_size, device_id_, buffer);
  89. return buffer;
  90. }
  91. void NpuMemoryAllocator::Deallocate(void *data, MemStorageType mem_type) {
  92. GELOGI("To deallocating buffer, addr = %p", data);
  93. if (data != nullptr) {
  94. GELOGI("Deallocating buffer successfully. addr = %p", data);
  95. if (mem_type == RDMA_HBM) {
  96. MemManager::Instance().RdmaPoolInstance(RT_MEMORY_HBM).Free(reinterpret_cast<uint8_t *>(data), device_id_);
  97. } else if (mem_type == HOST_DDR) {
  98. #ifndef ONLY_COMPILE_OPEN_SRC
  99. MemManager::Instance().HostMemInstance(RT_MEMORY_HBM).Free(data);
  100. #else
  101. free(data);
  102. #endif
  103. } else {
  104. MemManager::Instance().CachingInstance(RT_MEMORY_HBM).Free(reinterpret_cast<uint8_t *>(data), device_id_);
  105. }
  106. }
  107. }
  108. NpuMemoryAllocator *NpuMemoryAllocator::GetAllocator(uint32_t device_id) {
  109. std::lock_guard<std::mutex> lk(mu_);
  110. auto it = allocators_.find(device_id);
  111. if (it == allocators_.end()) {
  112. auto allocator = std::unique_ptr<NpuMemoryAllocator>(new (std::nothrow) NpuMemoryAllocator(device_id));
  113. if (allocator == nullptr) {
  114. return nullptr;
  115. }
  116. allocators_.emplace(device_id, std::move(allocator));
  117. }
  118. return allocators_[device_id].get();
  119. }
  120. void NpuMemoryAllocator::DestroyAllocator() {
  121. std::lock_guard<std::mutex> lk(mu_);
  122. int device_id = 0;
  123. allocators_.erase(device_id);
  124. }
  125. } // namespace hybrid
  126. } // namespace ge

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