Skip to content

Commit 25b0980

Browse files
authored
CustomDevice memory support (#73033)
1 parent fbdc338 commit 25b0980

File tree

6 files changed

+63
-3
lines changed

6 files changed

+63
-3
lines changed

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,17 @@ class AllocatorFacadePrivate {
791791
}
792792
}
793793

794+
void EraseStream(std::shared_ptr<phi::Allocation> allocation,
795+
phi::stream::stream_t stream) {
796+
if (auto stream_safe_cuda_allocation =
797+
std::dynamic_pointer_cast<StreamSafeCustomDeviceAllocation>(
798+
allocation)) {
799+
stream_safe_cuda_allocation->EraseStream(stream);
800+
} else {
801+
VLOG(6) << "EraseStream for a non-StreamSafeCUDAAllocation";
802+
}
803+
}
804+
794805
phi::stream::stream_t GetStream(
795806
const std::shared_ptr<phi::Allocation>& allocation) const {
796807
const std::shared_ptr<StreamSafeCustomDeviceAllocation>
@@ -1787,11 +1798,17 @@ AllocationPtr AllocatorFacade::Alloc(const phi::Place& place,
17871798
bool AllocatorFacade::InSameStream(
17881799
const std::shared_ptr<phi::Allocation>& allocation,
17891800
const phi::Stream& stream) {
1790-
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
1801+
#if (defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)) && \
1802+
!defined(PADDLE_WITH_CUSTOM_DEVICE)
17911803
gpuStream_t s = reinterpret_cast<gpuStream_t>(stream.id()); // NOLINT
17921804
return s == GetStream(allocation);
1805+
#elif defined(PADDLE_WITH_CUSTOM_DEVICE)
1806+
phi::stream::stream_t s =
1807+
reinterpret_cast<phi::stream::stream_t>(stream.id()); // NOLINT
1808+
return s == GetStream(allocation);
17931809
#else
1794-
PADDLE_THROW(common::errors::PreconditionNotMet("Not compiled with GPU."));
1810+
PADDLE_THROW(common::errors::PreconditionNotMet(
1811+
"Not compiled with GPU or CUDA backend."));
17951812
#endif
17961813
}
17971814

@@ -1946,6 +1963,11 @@ bool AllocatorFacade::RecordStream(std::shared_ptr<phi::Allocation> allocation,
19461963
return GetPrivate()->RecordStream(allocation, stream);
19471964
}
19481965

1966+
void AllocatorFacade::EraseStream(std::shared_ptr<phi::Allocation> allocation,
1967+
phi::stream::stream_t stream) {
1968+
GetPrivate()->EraseStream(allocation, stream);
1969+
}
1970+
19491971
const std::shared_ptr<Allocator>& AllocatorFacade::GetAllocator(
19501972
const phi::Place& place, phi::stream::stream_t stream) {
19511973
AllocatorFacadePrivate* m = GetPrivate();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ class AllocatorFacade {
109109
uint64_t Release(const phi::CustomPlace& place, phi::stream::stream_t stream);
110110
bool RecordStream(std::shared_ptr<Allocation> allocation,
111111
phi::stream::stream_t stream);
112+
void EraseStream(std::shared_ptr<Allocation> allocation,
113+
phi::stream::stream_t stream);
112114
TEST_API const std::shared_ptr<Allocator>& GetAllocator(
113115
const phi::Place& place, phi::stream::stream_t stream);
114116
phi::stream::stream_t GetStream(

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ bool StreamSafeCustomDeviceAllocation::RecordStream(
5858
return true;
5959
}
6060

61+
void StreamSafeCustomDeviceAllocation::EraseStream(
62+
phi::stream::stream_t stream) {
63+
VLOG(8) << "Try remove stream " << stream << " for address " << ptr();
64+
std::lock_guard<SpinLock> lock_guard(outstanding_event_map_lock_);
65+
auto it = outstanding_event_map_.find(stream);
66+
if (it == outstanding_event_map_.end()) {
67+
return;
68+
}
69+
it->second->Destroy();
70+
outstanding_event_map_.erase(it);
71+
}
72+
6173
bool StreamSafeCustomDeviceAllocation::CanBeFreed() {
6274
std::lock_guard<SpinLock> lock_guard(outstanding_event_map_lock_);
6375
if (!phi::DeviceManager::HasDeviceType(place_.GetDeviceType())) {
@@ -191,7 +203,8 @@ uint64_t StreamSafeCustomDeviceAllocator::ReleaseImpl(const phi::Place& place) {
191203

192204
void StreamSafeCustomDeviceAllocator::ProcessUnfreedAllocations() {
193205
// NOTE(Ruibiao): This condition is to reduce lock completion. It does not
194-
// need to be thread-safe since here occasional misjudgments are permissible.
206+
// need to be thread-safe since here occasional misjudgments are
207+
// permissible.
195208
if (unfreed_allocations_.empty()) {
196209
return;
197210
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class StreamSafeCustomDeviceAllocation : public Allocation {
3636
StreamSafeCustomDeviceAllocator *allocator);
3737

3838
bool RecordStream(phi::stream::stream_t stream);
39+
void EraseStream(phi::stream::stream_t stream);
3940
bool CanBeFreed();
4041
phi::stream::stream_t GetOwningStream() const;
4142
void SetOwningStream(phi::stream::stream_t s);

paddle/phi/core/memory/malloc.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,24 @@ gpuStream_t GetStream(const std::shared_ptr<Allocation>& allocation) {
7777
#endif
7878

7979
#ifdef PADDLE_WITH_CUSTOM_DEVICE
80+
uint64_t Release(const phi::CustomPlace& place, phi::stream::stream_t stream) {
81+
return allocation::AllocatorFacade::Instance().Release(place, stream);
82+
}
83+
8084
bool RecordStream(std::shared_ptr<Allocation> allocation,
8185
phi::stream::stream_t stream) {
8286
return allocation::AllocatorFacade::Instance().RecordStream(allocation,
8387
stream);
8488
}
89+
90+
void EraseStream(std::shared_ptr<Allocation> allocation,
91+
phi::stream::stream_t stream) {
92+
return allocation::AllocatorFacade::Instance().EraseStream(allocation,
93+
stream);
94+
}
95+
96+
phi::stream::stream_t GetStream(const std::shared_ptr<Allocation>& allocation) {
97+
return allocation::AllocatorFacade::Instance().GetStream(allocation);
98+
}
8599
#endif
86100
} // namespace paddle::memory

paddle/phi/core/memory/malloc.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,16 @@ void EraseStream(std::shared_ptr<Allocation> allocation, gpuStream_t stream);
5959
gpuStream_t GetStream(const std::shared_ptr<Allocation>& allocation);
6060
#endif
6161
#ifdef PADDLE_WITH_CUSTOM_DEVICE
62+
extern uint64_t Release(const phi::CustomPlace& place,
63+
phi::stream::stream_t stream);
64+
6265
bool RecordStream(std::shared_ptr<Allocation> allocation,
6366
phi::stream::stream_t stream);
67+
68+
void EraseStream(std::shared_ptr<Allocation> allocation,
69+
phi::stream::stream_t stream);
70+
71+
phi::stream::stream_t GetStream(const std::shared_ptr<Allocation>& allocation);
6472
#endif
6573

6674
template <typename StreamType>

0 commit comments

Comments
 (0)