From 3550c5ca2de3f07334ea97592c89639536a95943 Mon Sep 17 00:00:00 2001 From: zhaoguochun1995 Date: Thu, 11 Jul 2024 07:36:26 +0000 Subject: [PATCH 1/2] fix bs allocator bug --- .../runtime/core/allocator/DIPUBSCachingAllocator.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dipu/torch_dipu/csrc_dipu/runtime/core/allocator/DIPUBSCachingAllocator.cpp b/dipu/torch_dipu/csrc_dipu/runtime/core/allocator/DIPUBSCachingAllocator.cpp index bb923a7d8..80bcc4ebd 100644 --- a/dipu/torch_dipu/csrc_dipu/runtime/core/allocator/DIPUBSCachingAllocator.cpp +++ b/dipu/torch_dipu/csrc_dipu/runtime/core/allocator/DIPUBSCachingAllocator.cpp @@ -78,8 +78,8 @@ class BSCachingAllocator : public CacheAllocator { << size << ",allocator:" << this << ", memory-usage" << memory_allocated() << "/" << memory_reserved()); - std::lock_guard lk(mutex_); flush_mem_pool(); + std::lock_guard lk(mutex_); size_t nbytes = getAllocateSize(size); void* ptr = nullptr; auto& idel_blocks = impl->idel_blocks_[nbytes]; @@ -138,6 +138,7 @@ class BSCachingAllocator : public CacheAllocator { } void empty_resource_pool() const { + std::lock_guard lk(mutex_); DIPU_DEBUG_ALLOCATOR( 8, "BSCachingAllocator::empty_resource_pool ,allocator:" << this); while (!async_mem_pool()->empty()) { @@ -180,6 +181,7 @@ class BSCachingAllocator : public CacheAllocator { void release_all_memory() const override { release_all_memory_impl(); } void flush_mem_pool() const { + std::lock_guard lk(mutex_); DIPU_DEBUG_ALLOCATOR( 8, "BSCachingAllocator::flush_mem_pool allocator:" << this); while (async_mem_pool()->ready()) { From 540029ce93d1094c655a2cb34c1dcd32a9feb0cb Mon Sep 17 00:00:00 2001 From: zhaoguochun1995 Date: Thu, 11 Jul 2024 09:42:31 +0000 Subject: [PATCH 2/2] optimize bs allocator --- .../runtime/core/allocator/DIPUBSCachingAllocator.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/dipu/torch_dipu/csrc_dipu/runtime/core/allocator/DIPUBSCachingAllocator.cpp b/dipu/torch_dipu/csrc_dipu/runtime/core/allocator/DIPUBSCachingAllocator.cpp index 80bcc4ebd..669cfa57a 100644 --- a/dipu/torch_dipu/csrc_dipu/runtime/core/allocator/DIPUBSCachingAllocator.cpp +++ b/dipu/torch_dipu/csrc_dipu/runtime/core/allocator/DIPUBSCachingAllocator.cpp @@ -67,10 +67,12 @@ class BSCachingAllocator : public CacheAllocator { static size_t getAllocateSize(size_t nbytes) { nbytes = getMemoryAlignmentStrategy()->roundBytes(nbytes); - static bool less_fragmentation = - std::getenv("DIPU_BS_MORE_ADAPTABLE") == nullptr; - return less_fragmentation ? getAllocateSizeLessFragmentation(nbytes) - : getAllocateSizeMoreAdaptable(nbytes); + constexpr size_t kMinBlockSize = 1 << 20; // 1M + if (nbytes <= kMinBlockSize) { + return kMinBlockSize; + } + int clz = __builtin_clzll(nbytes - 1); + return (1 << (sizeof(int64_t) - clz)); } c10::DataPtr allocate(size_t size) const override {