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

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

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