@@ -62,7 +62,7 @@ static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
62
62
[[noreturn]]
63
63
void ggml_cuda_error (const char * stmt, const char * func, const char * file, int line, const char * msg) {
64
64
int id = -1 ; // in case cudaGetDevice fails
65
- cudaGetDevice (&id);
65
+ ( void ) cudaGetDevice (&id);
66
66
67
67
GGML_LOG_ERROR (GGML_CUDA_NAME " error: %s\n " , msg);
68
68
GGML_LOG_ERROR (" current device: %d, in function %s at %s:%d\n " , id, func, file, line);
@@ -152,7 +152,7 @@ static ggml_cuda_device_info ggml_cuda_init() {
152
152
for (int id = 0 ; id < info.device_count ; ++id) {
153
153
int device_vmm = 0 ;
154
154
155
- #if !defined(GGML_USE_HIP) && !defined( GGML_CUDA_NO_VMM)
155
+ #if !defined(GGML_CUDA_NO_VMM)
156
156
CUdevice device;
157
157
CU_CHECK (cuDeviceGet (&device, id));
158
158
CU_CHECK (cuDeviceGetAttribute (&device_vmm, CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED, device));
@@ -164,7 +164,7 @@ static ggml_cuda_device_info ggml_cuda_init() {
164
164
alloc_prop.location .id = id;
165
165
CU_CHECK (cuMemGetAllocationGranularity (&info.devices [id].vmm_granularity , &alloc_prop, CU_MEM_ALLOC_GRANULARITY_RECOMMENDED));
166
166
}
167
- #endif // !defined(GGML_USE_HIP) && !defined( GGML_CUDA_NO_VMM)
167
+ #endif // !defined(GGML_CUDA_NO_VMM)
168
168
info.devices [id].vmm = !!device_vmm;
169
169
170
170
cudaDeviceProp prop;
@@ -300,7 +300,7 @@ struct ggml_cuda_pool_leg : public ggml_cuda_pool {
300
300
};
301
301
302
302
// pool with virtual memory
303
- #if !defined(GGML_USE_HIP) && !defined( GGML_CUDA_NO_VMM)
303
+ #if !defined(GGML_CUDA_NO_VMM)
304
304
struct ggml_cuda_pool_vmm : public ggml_cuda_pool {
305
305
static const size_t CUDA_POOL_VMM_MAX_SIZE = 1ull << 35 ; // 32 GB
306
306
@@ -309,6 +309,7 @@ struct ggml_cuda_pool_vmm : public ggml_cuda_pool {
309
309
size_t pool_used = 0 ;
310
310
size_t pool_size = 0 ;
311
311
size_t granularity;
312
+ std::vector<std::pair<void *, size_t >> mappings;
312
313
313
314
explicit ggml_cuda_pool_vmm (int device) :
314
315
device(device),
@@ -317,7 +318,9 @@ struct ggml_cuda_pool_vmm : public ggml_cuda_pool {
317
318
318
319
~ggml_cuda_pool_vmm () {
319
320
if (pool_addr != 0 ) {
320
- CU_CHECK (cuMemUnmap (pool_addr, pool_size));
321
+ for (std::pair<void *, size_t >& mapping : mappings) {
322
+ CU_CHECK (cuMemUnmap (mapping.first , mapping.second ));
323
+ }
321
324
CU_CHECK (cuMemAddressFree (pool_addr, CUDA_POOL_VMM_MAX_SIZE));
322
325
}
323
326
}
@@ -350,7 +353,9 @@ struct ggml_cuda_pool_vmm : public ggml_cuda_pool {
350
353
}
351
354
352
355
// map at the end of the pool
353
- CU_CHECK (cuMemMap (pool_addr + pool_size, reserve_size, 0 , handle, 0 ));
356
+ void * start_ptr = reinterpret_cast <char *>(pool_addr) + pool_size;
357
+ CU_CHECK (cuMemMap (start_ptr, reserve_size, 0 , handle, 0 ));
358
+ mappings.push_back ({start_ptr, reserve_size});
354
359
355
360
// the memory allocation handle is no longer needed after mapping
356
361
CU_CHECK (cuMemRelease (handle));
@@ -360,7 +365,7 @@ struct ggml_cuda_pool_vmm : public ggml_cuda_pool {
360
365
access.location .type = CU_MEM_LOCATION_TYPE_DEVICE;
361
366
access.location .id = device;
362
367
access.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
363
- CU_CHECK (cuMemSetAccess (pool_addr + pool_size, reserve_size, &access, 1 ));
368
+ CU_CHECK (cuMemSetAccess (reinterpret_cast < char *>( pool_addr) + pool_size, reserve_size, &access, 1 ));
364
369
365
370
// add to the pool
366
371
pool_size += reserve_size;
@@ -372,7 +377,7 @@ struct ggml_cuda_pool_vmm : public ggml_cuda_pool {
372
377
373
378
GGML_ASSERT (pool_addr != 0 );
374
379
375
- void * ptr = (void *) (pool_addr + pool_used);
380
+ void * ptr = (void *) (reinterpret_cast < char *>( pool_addr) + pool_used);
376
381
*actual_size = size;
377
382
pool_used += size;
378
383
@@ -391,17 +396,17 @@ struct ggml_cuda_pool_vmm : public ggml_cuda_pool {
391
396
pool_used -= size;
392
397
393
398
// all deallocations must be in reverse order of the allocations
394
- GGML_ASSERT (ptr == (void *) (pool_addr + pool_used));
399
+ GGML_ASSERT (ptr == (void *) (static_cast < char *>( pool_addr) + pool_used));
395
400
}
396
401
};
397
- #endif // !defined(GGML_USE_HIP) && !defined( GGML_CUDA_NO_VMM)
402
+ #endif // !defined(GGML_CUDA_NO_VMM)
398
403
399
404
std::unique_ptr<ggml_cuda_pool> ggml_backend_cuda_context::new_pool_for_device (int device) {
400
- #if !defined(GGML_USE_HIP) && !defined( GGML_CUDA_NO_VMM)
405
+ #if !defined(GGML_CUDA_NO_VMM)
401
406
if (ggml_cuda_info ().devices [device].vmm ) {
402
407
return std::unique_ptr<ggml_cuda_pool>(new ggml_cuda_pool_vmm (device));
403
408
}
404
- #endif // !defined(GGML_USE_HIP) && !defined( GGML_CUDA_NO_VMM)
409
+ #endif // !defined(GGML_CUDA_NO_VMM)
405
410
return std::unique_ptr<ggml_cuda_pool>(new ggml_cuda_pool_leg (device));
406
411
}
407
412
@@ -547,7 +552,7 @@ static ggml_backend_buffer_t ggml_backend_cuda_buffer_type_alloc_buffer(ggml_bac
547
552
cudaError_t err = ggml_cuda_device_malloc (&dev_ptr, size, buft_ctx->device );
548
553
if (err != cudaSuccess) {
549
554
// clear the error
550
- cudaGetLastError ();
555
+ ( void ) cudaGetLastError ();
551
556
GGML_LOG_ERROR (" %s: allocating %.2f MiB on device %d: cudaMalloc failed: %s\n " , __func__, size / 1024.0 / 1024.0 , buft_ctx->device , cudaGetErrorString (err));
552
557
return nullptr ;
553
558
}
@@ -962,7 +967,7 @@ static void * ggml_cuda_host_malloc(size_t size) {
962
967
cudaError_t err = cudaMallocHost ((void **) &ptr, size);
963
968
if (err != cudaSuccess) {
964
969
// clear the error
965
- cudaGetLastError ();
970
+ ( void ) cudaGetLastError ();
966
971
GGML_LOG_DEBUG (" %s: failed to allocate %.2f MiB of pinned memory: %s\n " , __func__,
967
972
size / 1024.0 / 1024.0 , cudaGetErrorString (err));
968
973
return nullptr ;
@@ -1197,15 +1202,15 @@ static void ggml_cuda_set_peer_access(const int n_tokens, int main_device) {
1197
1202
CUDA_CHECK (err);
1198
1203
} else {
1199
1204
// reset the error
1200
- cudaGetLastError ();
1205
+ ( void ) cudaGetLastError ();
1201
1206
}
1202
1207
} else {
1203
1208
cudaError_t err = cudaDeviceDisablePeerAccess (id_other);
1204
1209
if (err != cudaErrorPeerAccessNotEnabled) {
1205
1210
CUDA_CHECK (err);
1206
1211
} else {
1207
1212
// reset the error
1208
- cudaGetLastError ();
1213
+ ( void ) cudaGetLastError ();
1209
1214
}
1210
1215
}
1211
1216
}
@@ -2438,7 +2443,7 @@ static void maintain_cuda_graph(ggml_backend_cuda_context * cuda_ctx, std::vecto
2438
2443
if (stat == cudaErrorInvalidDeviceFunction) {
2439
2444
// Fails due to incorrect handling by CUDA runtime of CUDA BLAS node.
2440
2445
// We don't need to update blas nodes, so clear error and move on.
2441
- cudaGetLastError ();
2446
+ ( void ) cudaGetLastError ();
2442
2447
} else {
2443
2448
GGML_ASSERT (stat == cudaSuccess);
2444
2449
}
@@ -2506,7 +2511,7 @@ static void update_cuda_graph_executable(ggml_backend_cuda_context * cuda_ctx) {
2506
2511
2507
2512
// The pre-existing graph exec cannot be updated due to violated constraints
2508
2513
// so instead clear error and re-instantiate
2509
- cudaGetLastError ();
2514
+ ( void ) cudaGetLastError ();
2510
2515
CUDA_CHECK (cudaGraphExecDestroy (cuda_ctx->cuda_graph ->instance ));
2511
2516
cuda_ctx->cuda_graph ->instance = nullptr ;
2512
2517
CUDA_CHECK (cudaGraphInstantiate (&cuda_ctx->cuda_graph ->instance , cuda_ctx->cuda_graph ->graph , NULL , NULL , 0 ));
@@ -2734,7 +2739,7 @@ bool ggml_backend_cuda_register_host_buffer(void * buffer, size_t size) {
2734
2739
cudaError_t err = cudaHostRegister (buffer, size, cudaHostRegisterPortable | cudaHostRegisterReadOnly);
2735
2740
if (err != cudaSuccess) {
2736
2741
// clear the error
2737
- cudaGetLastError ();
2742
+ ( void ) cudaGetLastError ();
2738
2743
2739
2744
GGML_LOG_DEBUG (" %s: failed to register %.2f MiB of pinned memory: %s\n " , __func__,
2740
2745
size / 1024.0 / 1024.0 , cudaGetErrorString (err));
@@ -2754,7 +2759,7 @@ void ggml_backend_cuda_unregister_host_buffer(void * buffer) {
2754
2759
cudaError_t err = cudaHostUnregister (buffer);
2755
2760
if (err != cudaSuccess) {
2756
2761
// clear the error
2757
- cudaGetLastError ();
2762
+ ( void ) cudaGetLastError ();
2758
2763
}
2759
2764
}
2760
2765
0 commit comments