From 3919924b7b694f9212fa04dcccf47e73a9aaca41 Mon Sep 17 00:00:00 2001 From: chuxing Date: Fri, 6 Nov 2020 16:11:37 +0800 Subject: [PATCH 1/2] add extra 32 bytes padding to all npu memory allocation --- ge/hybrid/common/npu_memory_allocator.cc | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/ge/hybrid/common/npu_memory_allocator.cc b/ge/hybrid/common/npu_memory_allocator.cc index bf5af73b..8e3771f1 100644 --- a/ge/hybrid/common/npu_memory_allocator.cc +++ b/ge/hybrid/common/npu_memory_allocator.cc @@ -45,16 +45,9 @@ NpuMemoryAllocator *NpuMemoryAllocator::GetAllocator() { NpuMemoryAllocator::NpuMemoryAllocator(uint32_t device_id) : device_id_(device_id) {} void *NpuMemoryAllocator::Allocate(std::size_t size, AllocationAttr *attr) { - void *try_reuse_addr = nullptr; size_t allocate_size = size; MemStorageType mem_type = HBM; if (attr != nullptr) { - try_reuse_addr = attr->try_reuse_addr_; - if (attr->padding_ != 0) { - // padding up to multiple of attr->padding, and add extra attr->padding_ - allocate_size = (size + 2 * attr->padding_ - 1) / attr->padding_ * attr->padding_; - GELOGD("Padding size %ld by %d. final size = %zu.", size, attr->padding_, allocate_size); - } mem_type = attr->mem_type_; } @@ -69,6 +62,18 @@ void *NpuMemoryAllocator::Allocate(std::size_t size, AllocationAttr *attr) { } else if (mem_type == HOST_DDR) { buffer = malloc(allocate_size); } else { + void *try_reuse_addr = nullptr; + int padding = kDefaultPadding; + if (attr != nullptr) { + try_reuse_addr = attr->try_reuse_addr_; + if (attr->padding_ > 0) { + padding = attr->padding_; + } + + // padding up to multiple of padding, and add extra padding + allocate_size = (size + 2 * padding - 1) / padding * padding; + GELOGD("Padding size %ld by %d. final size = %zu.", size, padding, allocate_size); + } buffer = MemManager::Instance() .CachingInstance(RT_MEMORY_HBM) .Malloc(allocate_size, reinterpret_cast(try_reuse_addr), device_id_); From fea763f3014cce344e299ed89f5a6b747f9759f3 Mon Sep 17 00:00:00 2001 From: chuxing Date: Fri, 6 Nov 2020 16:16:45 +0800 Subject: [PATCH 2/2] reformat --- ge/hybrid/common/npu_memory_allocator.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ge/hybrid/common/npu_memory_allocator.cc b/ge/hybrid/common/npu_memory_allocator.cc index 8e3771f1..d3ebb347 100644 --- a/ge/hybrid/common/npu_memory_allocator.cc +++ b/ge/hybrid/common/npu_memory_allocator.cc @@ -65,7 +65,7 @@ void *NpuMemoryAllocator::Allocate(std::size_t size, AllocationAttr *attr) { void *try_reuse_addr = nullptr; int padding = kDefaultPadding; if (attr != nullptr) { - try_reuse_addr = attr->try_reuse_addr_; + try_reuse_addr = attr->try_reuse_addr_; if (attr->padding_ > 0) { padding = attr->padding_; }