ggml-cuda: flush legacy pool on OOM and retry (#22155)
* ggml-cuda: flush legacy pool on OOM and retry Signed-off-by: 梁厚宏 <2695316095@qq.com> * Address review comments: add explicit sync, update destructor, clean up MUSA macros Signed-off-by: 梁厚宏 <2695316095@qq.com> --------- Signed-off-by: 梁厚宏 <2695316095@qq.com>
This commit is contained in:
@@ -368,15 +368,21 @@ struct ggml_cuda_pool_leg : public ggml_cuda_pool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
~ggml_cuda_pool_leg() {
|
~ggml_cuda_pool_leg() {
|
||||||
|
clear_pool();
|
||||||
|
GGML_ASSERT(pool_size == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear_pool() {
|
||||||
ggml_cuda_set_device(device);
|
ggml_cuda_set_device(device);
|
||||||
for (int i = 0; i < MAX_BUFFERS; ++i) {
|
for (int i = 0; i < MAX_BUFFERS; ++i) {
|
||||||
ggml_cuda_buffer & b = buffer_pool[i];
|
ggml_cuda_buffer & b = buffer_pool[i];
|
||||||
if (b.ptr != nullptr) {
|
if (b.ptr != nullptr) {
|
||||||
CUDA_CHECK(cudaFree(b.ptr));
|
CUDA_CHECK(cudaFree(b.ptr));
|
||||||
pool_size -= b.size;
|
pool_size -= b.size;
|
||||||
|
b.ptr = nullptr;
|
||||||
|
b.size = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
GGML_ASSERT(pool_size == 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void * alloc(size_t size, size_t * actual_size) override {
|
void * alloc(size_t size, size_t * actual_size) override {
|
||||||
@@ -421,7 +427,20 @@ struct ggml_cuda_pool_leg : public ggml_cuda_pool {
|
|||||||
size_t look_ahead_size = (size_t) (1.05 * size);
|
size_t look_ahead_size = (size_t) (1.05 * size);
|
||||||
look_ahead_size = 256 * ((look_ahead_size + 255)/256);
|
look_ahead_size = 256 * ((look_ahead_size + 255)/256);
|
||||||
ggml_cuda_set_device(device);
|
ggml_cuda_set_device(device);
|
||||||
CUDA_CHECK(ggml_cuda_device_malloc(&ptr, look_ahead_size, device));
|
cudaError_t err = ggml_cuda_device_malloc(&ptr, look_ahead_size, device);
|
||||||
|
if (err == cudaErrorMemoryAllocation) {
|
||||||
|
(void)cudaGetLastError();
|
||||||
|
const size_t cached_bytes = pool_size;
|
||||||
|
GGML_LOG_DEBUG(GGML_CUDA_NAME " pool[%d]: alloc of %.2f MiB failed, flushing %.2f MiB of cached buffers and retrying\n",
|
||||||
|
device, look_ahead_size/1024.0/1024.0, cached_bytes/1024.0/1024.0);
|
||||||
|
CUDA_CHECK(cudaDeviceSynchronize());
|
||||||
|
clear_pool();
|
||||||
|
err = ggml_cuda_device_malloc(&ptr, look_ahead_size, device);
|
||||||
|
if (err == cudaSuccess) {
|
||||||
|
GGML_LOG_DEBUG(GGML_CUDA_NAME " pool[%d]: retry succeeded\n", device);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CUDA_CHECK(err);
|
||||||
*actual_size = look_ahead_size;
|
*actual_size = look_ahead_size;
|
||||||
pool_size += look_ahead_size;
|
pool_size += look_ahead_size;
|
||||||
#ifdef DEBUG_CUDA_MALLOC
|
#ifdef DEBUG_CUDA_MALLOC
|
||||||
|
|||||||
Vendored
+1
@@ -58,6 +58,7 @@
|
|||||||
#define cudaDeviceProp hipDeviceProp_t
|
#define cudaDeviceProp hipDeviceProp_t
|
||||||
#define cudaDeviceSynchronize hipDeviceSynchronize
|
#define cudaDeviceSynchronize hipDeviceSynchronize
|
||||||
#define cudaError_t hipError_t
|
#define cudaError_t hipError_t
|
||||||
|
#define cudaErrorMemoryAllocation hipErrorOutOfMemory
|
||||||
#define cudaErrorPeerAccessAlreadyEnabled hipErrorPeerAccessAlreadyEnabled
|
#define cudaErrorPeerAccessAlreadyEnabled hipErrorPeerAccessAlreadyEnabled
|
||||||
#define cudaErrorPeerAccessNotEnabled hipErrorPeerAccessNotEnabled
|
#define cudaErrorPeerAccessNotEnabled hipErrorPeerAccessNotEnabled
|
||||||
#define cudaEventCreateWithFlags hipEventCreateWithFlags
|
#define cudaEventCreateWithFlags hipEventCreateWithFlags
|
||||||
|
|||||||
Vendored
+1
@@ -42,6 +42,7 @@
|
|||||||
#define cudaDeviceProp musaDeviceProp
|
#define cudaDeviceProp musaDeviceProp
|
||||||
#define cudaDeviceSynchronize musaDeviceSynchronize
|
#define cudaDeviceSynchronize musaDeviceSynchronize
|
||||||
#define cudaError_t musaError_t
|
#define cudaError_t musaError_t
|
||||||
|
#define cudaErrorMemoryAllocation musaErrorMemoryAllocation
|
||||||
#define cudaErrorPeerAccessAlreadyEnabled musaErrorPeerAccessAlreadyEnabled
|
#define cudaErrorPeerAccessAlreadyEnabled musaErrorPeerAccessAlreadyEnabled
|
||||||
#define cudaErrorPeerAccessNotEnabled musaErrorPeerAccessNotEnabled
|
#define cudaErrorPeerAccessNotEnabled musaErrorPeerAccessNotEnabled
|
||||||
#define cudaEventCreateWithFlags musaEventCreateWithFlags
|
#define cudaEventCreateWithFlags musaEventCreateWithFlags
|
||||||
|
|||||||
Reference in New Issue
Block a user