03-cudaleaks.diff 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. diff --git a/examples/server/server.cpp b/examples/server/server.cpp
  2. index 3102762c..568ac1d0 100644
  3. --- a/examples/server/server.cpp
  4. +++ b/examples/server/server.cpp
  5. @@ -307,6 +307,10 @@ struct llama_client_slot
  6. }
  7. };
  8. +#ifdef GGML_USE_CUBLAS
  9. +extern "C" GGML_CALL void ggml_free_cublas(void);
  10. +#endif
  11. +
  12. struct llama_server_context
  13. {
  14. llama_model *model = nullptr;
  15. @@ -353,6 +357,10 @@ struct llama_server_context
  16. llama_free_model(model);
  17. model = nullptr;
  18. }
  19. +#ifdef GGML_USE_CUBLAS
  20. + ggml_free_cublas();
  21. +#endif
  22. +
  23. }
  24. bool load_model(const gpt_params &params_)
  25. @@ -3093,6 +3101,7 @@ int main(int argc, char **argv)
  26. sigemptyset (&sigint_action.sa_mask);
  27. sigint_action.sa_flags = 0;
  28. sigaction(SIGINT, &sigint_action, NULL);
  29. + sigaction(SIGUSR1, &sigint_action, NULL);
  30. #elif defined (_WIN32)
  31. auto console_ctrl_handler = +[](DWORD ctrl_type) -> BOOL {
  32. return (ctrl_type == CTRL_C_EVENT) ? (signal_handler(SIGINT), true) : false;
  33. @@ -3106,3 +3115,4 @@ int main(int argc, char **argv)
  34. llama_backend_free();
  35. return 0;
  36. }
  37. +
  38. diff --git a/ggml-cuda.cu b/ggml-cuda.cu
  39. index 96976f24..3543920e 100644
  40. --- a/ggml-cuda.cu
  41. +++ b/ggml-cuda.cu
  42. @@ -39,6 +39,7 @@
  43. #define __shfl_xor_sync(mask, var, laneMask, width) __shfl_xor(var, laneMask, width)
  44. #define cublasComputeType_t hipblasDatatype_t //deprecated, new hipblasComputeType_t not in 5.6
  45. #define cublasCreate hipblasCreate
  46. +#define cublasDestroy hipblasDestroy
  47. #define cublasGemmEx hipblasGemmEx
  48. #define cublasGemmBatchedEx hipblasGemmBatchedEx
  49. #define cublasGemmStridedBatchedEx hipblasGemmStridedBatchedEx
  50. @@ -7928,10 +7929,11 @@ GGML_CALL bool ggml_cublas_loaded(void) {
  51. return g_cublas_loaded;
  52. }
  53. +static bool g_cublas_initialized = false;
  54. +
  55. GGML_CALL void ggml_init_cublas() {
  56. - static bool initialized = false;
  57. - if (!initialized) {
  58. + if (!g_cublas_initialized) {
  59. #ifdef __HIP_PLATFORM_AMD__
  60. // Workaround for a rocBLAS bug when using multiple graphics cards:
  61. @@ -7941,7 +7943,7 @@ GGML_CALL void ggml_init_cublas() {
  62. #endif
  63. if (cudaGetDeviceCount(&g_device_count) != cudaSuccess) {
  64. - initialized = true;
  65. + g_cublas_initialized = true;
  66. g_cublas_loaded = false;
  67. return;
  68. }
  69. @@ -8011,7 +8013,7 @@ GGML_CALL void ggml_init_cublas() {
  70. // configure logging to stdout
  71. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  72. - initialized = true;
  73. + g_cublas_initialized = true;
  74. g_cublas_loaded = true;
  75. }
  76. }
  77. @@ -11528,3 +11530,17 @@ GGML_CALL int ggml_backend_cuda_reg_devices() {
  78. }
  79. return device_count;
  80. }
  81. +
  82. +extern "C" GGML_CALL void ggml_free_cublas(void);
  83. +GGML_CALL void ggml_free_cublas(void) {
  84. + for (int id = 0; id < g_device_count; ++id) {
  85. +#if !defined(GGML_USE_HIPBLAS)
  86. + CU_CHECK(cuMemUnmap(g_cuda_pool_addr[id], g_cuda_pool_size[id]));
  87. + g_cuda_pool_size[id] = 0;
  88. + g_cuda_pool_addr[id] = 0;
  89. +#endif
  90. + CUBLAS_CHECK(cublasDestroy(g_cublas_handles[id]));
  91. + g_cublas_handles[id] = nullptr;
  92. + }
  93. + g_cublas_initialized = false;
  94. +}
  95. \ No newline at end of file
  96. diff --git a/ggml-cuda.h b/ggml-cuda.h
  97. index b1ebd61d..b4c80c2c 100644
  98. --- a/ggml-cuda.h
  99. +++ b/ggml-cuda.h
  100. @@ -20,6 +20,9 @@ extern "C" {
  101. // Always success. To check if CUDA is actually loaded, use `ggml_cublas_loaded`.
  102. GGML_API GGML_CALL void ggml_init_cublas(void);
  103. +// Release CUDA resources
  104. +GGML_API GGML_CALL void ggml_free_cublas(void);
  105. +
  106. // Returns `true` if there are available CUDA devices and cublas loads successfully; otherwise, it returns `false`.
  107. GGML_API GGML_CALL bool ggml_cublas_loaded(void);