}
~ggml_cuda_pool_leg() {
+ clear_pool();
+ GGML_ASSERT(pool_size == 0);
+ }
+
+ void clear_pool() {
ggml_cuda_set_device(device);
for (int i = 0; i < MAX_BUFFERS; ++i) {
ggml_cuda_buffer & b = buffer_pool[i];
if (b.ptr != nullptr) {
CUDA_CHECK(cudaFree(b.ptr));
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 {
size_t look_ahead_size = (size_t) (1.05 * size);
look_ahead_size = 256 * ((look_ahead_size + 255)/256);
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;
pool_size += look_ahead_size;
#ifdef DEBUG_CUDA_MALLOC
#define cudaDeviceProp hipDeviceProp_t
#define cudaDeviceSynchronize hipDeviceSynchronize
#define cudaError_t hipError_t
+#define cudaErrorMemoryAllocation hipErrorOutOfMemory
#define cudaErrorPeerAccessAlreadyEnabled hipErrorPeerAccessAlreadyEnabled
#define cudaErrorPeerAccessNotEnabled hipErrorPeerAccessNotEnabled
#define cudaEventCreateWithFlags hipEventCreateWithFlags
#define cudaDeviceProp musaDeviceProp
#define cudaDeviceSynchronize musaDeviceSynchronize
#define cudaError_t musaError_t
+#define cudaErrorMemoryAllocation musaErrorMemoryAllocation
#define cudaErrorPeerAccessAlreadyEnabled musaErrorPeerAccessAlreadyEnabled
#define cudaErrorPeerAccessNotEnabled musaErrorPeerAccessNotEnabled
#define cudaEventCreateWithFlags musaEventCreateWithFlags