Skip to content

Commit 0079c0e

Browse files
committed
add compact func
1 parent 658be31 commit 0079c0e

File tree

13 files changed

+125
-0
lines changed

13 files changed

+125
-0
lines changed

paddle/fluid/pybind/pybind.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3587,6 +3587,7 @@ All parameter, weight, gradient are variables in Paddle.
35873587
}
35883588
platform::EmptyCache();
35893589
});
3590+
m.def("vmm_compact", [] { platform::VmmCompact(); });
35903591
m.def(
35913592
"get_device_properties",
35923593
[](int id) -> const gpuDeviceProp & {

paddle/phi/core/memory/allocation/allocator.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,16 @@ class PADDLE_API Allocator : public phi::Allocator {
200200
}
201201

202202
uint64_t Release(const phi::Place& place) { return ReleaseImpl(place); }
203+
size_t Compact(const phi::Place& place) { return CompactImpl(place); }
203204

204205
protected:
205206
virtual phi::Allocation* AllocateImpl(size_t size) = 0;
206207
virtual void FreeImpl(phi::Allocation* allocation);
207208
virtual uint64_t ReleaseImpl(const phi::Place& place UNUSED) { return 0; }
209+
virtual size_t CompactImpl(const phi::Place& place UNUSED) {
210+
LOG(INFO) << "Compact is not supported";
211+
return 0;
212+
}
208213
};
209214

210215
inline size_t AlignedSize(size_t size, size_t alignment) {

paddle/phi/core/memory/allocation/allocator_facade.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,12 @@ uint64_t AllocatorFacade::Release(const phi::Place& place) {
16631663
->Release(place);
16641664
}
16651665

1666+
size_t AllocatorFacade::Compact(const phi::Place& place) {
1667+
return GetPrivate()
1668+
->GetAllocator(place, /* A non-zero num to choose allocator_ */ 1)
1669+
->Compact(place);
1670+
}
1671+
16661672
std::shared_ptr<phi::Allocation> AllocatorFacade::AllocShared(
16671673
const phi::Place& place, size_t size, const phi::Stream& stream) {
16681674
return std::shared_ptr<phi::Allocation>(Alloc(place, size, stream));

paddle/phi/core/memory/allocation/allocator_facade.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ class AllocatorFacade {
7070
PADDLE_API AllocationPtr Alloc(const phi::Place& place, size_t size);
7171
// Release unused memory pool.
7272
uint64_t Release(const phi::Place& place);
73+
// Compact memory of free blocks held by the VmmAllocator.
74+
size_t Compact(const phi::Place& place);
7375

7476
std::shared_ptr<Allocation> AllocShared(const phi::Place& place,
7577
size_t size,

paddle/phi/core/memory/allocation/retry_allocator.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class PADDLE_API RetryAllocator : public Allocator {
5858
uint64_t ReleaseImpl(const phi::Place& place) override {
5959
return underlying_allocator_->Release(place);
6060
}
61+
size_t CompactImpl(const phi::Place& place) override {
62+
return underlying_allocator_->Compact(place);
63+
}
6164

6265
private:
6366
std::shared_ptr<Allocator> underlying_allocator_;

paddle/phi/core/memory/allocation/stat_allocator.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ class StatAllocator : public Allocator {
6969
return underlying_allocator_->Release(place);
7070
}
7171

72+
size_t CompactImpl(const phi::Place& place) override {
73+
return underlying_allocator_->Compact(place);
74+
}
75+
7276
private:
7377
std::shared_ptr<Allocator> underlying_allocator_;
7478
};

paddle/phi/core/memory/allocation/stream_safe_cuda_allocator.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,17 @@ uint64_t StreamSafeCUDAAllocator::ReleaseImpl(const phi::Place& place) {
259259
return released_size;
260260
}
261261

262+
size_t StreamSafeCUDAAllocator::CompactImpl(const phi::Place& place) {
263+
std::lock_guard<SpinLock> lock_guard(allocator_map_lock_);
264+
VLOG(4) << "enter StreamSafeCUDAAllocator compact!!";
265+
std::vector<StreamSafeCUDAAllocator*>& allocators = allocator_map_[place];
266+
size_t compact_free_size = 0;
267+
for (StreamSafeCUDAAllocator* allocator : allocators) {
268+
compact_free_size += allocator->underlying_allocator_->Compact(place_);
269+
}
270+
return compact_free_size;
271+
}
272+
262273
void StreamSafeCUDAAllocator::ProcessUnfreedAllocations() {
263274
// NOTE(Ruibiao): This condition is to reduce lock completion. It does not
264275
// need to be thread-safe since here occasional misjudgments are permissible.

paddle/phi/core/memory/allocation/stream_safe_cuda_allocator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class StreamSafeCUDAAllocator
7777
phi::Allocation *AllocateImpl(size_t size) override;
7878
void FreeImpl(phi::Allocation *allocation) override;
7979
uint64_t ReleaseImpl(const phi::Place &place) override;
80+
size_t CompactImpl(const phi::Place &place) override;
8081

8182
private:
8283
void ProcessUnfreedAllocations();

paddle/phi/core/memory/malloc.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ uint64_t Release(const phi::Place& place) {
3232
return allocation::AllocatorFacade::Instance().Release(place);
3333
}
3434

35+
size_t Compact(const phi::GPUPlace& place) {
36+
return allocation::AllocatorFacade::Instance().Compact(place);
37+
}
38+
3539
std::shared_ptr<Allocation> AllocShared(const phi::Place& place,
3640
size_t size,
3741
const phi::Stream& stream) {

paddle/phi/core/memory/malloc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ PADDLE_API extern AllocationPtr Alloc(const phi::Place& place, size_t size);
4141

4242
PADDLE_API extern uint64_t Release(const phi::Place& place);
4343

44+
// Compact memory of free blocks held by the VmmAllocator.
45+
PADDLE_API extern size_t Compact(const phi::GPUPlace& place);
46+
4447
PADDLE_API extern std::shared_ptr<Allocation> AllocShared(
4548
const phi::Place& place, size_t size, const phi::Stream& stream);
4649

0 commit comments

Comments
 (0)