02-cudaleaks.diff 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. diff --git a/examples/server/server.cpp b/examples/server/server.cpp
  2. index 7800c6e7..be30db23 100644
  3. --- a/examples/server/server.cpp
  4. +++ b/examples/server/server.cpp
  5. @@ -30,6 +30,10 @@
  6. #include <atomic>
  7. #include <signal.h>
  8. +#ifdef GGML_USE_CUBLAS
  9. +extern "C" GGML_CALL void ggml_free_cublas(void);
  10. +#endif
  11. +
  12. using json = nlohmann::json;
  13. struct server_params
  14. @@ -353,6 +357,9 @@ struct llama_server_context
  15. llama_free_model(model);
  16. model = nullptr;
  17. }
  18. +#ifdef GGML_USE_CUBLAS
  19. + ggml_free_cublas();
  20. +#endif
  21. }
  22. bool load_model(const gpt_params &params_)
  23. @@ -3143,6 +3150,7 @@ int main(int argc, char **argv)
  24. sigemptyset (&sigint_action.sa_mask);
  25. sigint_action.sa_flags = 0;
  26. sigaction(SIGINT, &sigint_action, NULL);
  27. + sigaction(SIGUSR1, &sigint_action, NULL);
  28. #elif defined (_WIN32)
  29. auto console_ctrl_handler = +[](DWORD ctrl_type) -> BOOL {
  30. return (ctrl_type == CTRL_C_EVENT) ? (signal_handler(SIGINT), true) : false;
  31. diff --git a/ggml-cuda.cu b/ggml-cuda.cu
  32. index 933ebbc4..88a4f664 100644
  33. --- a/ggml-cuda.cu
  34. +++ b/ggml-cuda.cu
  35. @@ -39,6 +39,7 @@
  36. #define __shfl_xor_sync(mask, var, laneMask, width) __shfl_xor(var, laneMask, width)
  37. #define cublasComputeType_t hipblasDatatype_t //deprecated, new hipblasComputeType_t not in 5.6
  38. #define cublasCreate hipblasCreate
  39. +#define cublasDestroy hipblasDestroy
  40. #define cublasGemmEx hipblasGemmEx
  41. #define cublasGemmBatchedEx hipblasGemmBatchedEx
  42. #define cublasGemmStridedBatchedEx hipblasGemmStridedBatchedEx
  43. @@ -7991,10 +7992,10 @@ GGML_CALL bool ggml_cublas_loaded(void) {
  44. return g_cublas_loaded;
  45. }
  46. -GGML_CALL void ggml_init_cublas() {
  47. - static bool initialized = false;
  48. +static bool g_cublas_initialized = false;
  49. - if (!initialized) {
  50. +GGML_CALL void ggml_init_cublas() {
  51. + if (!g_cublas_initialized) {
  52. #ifdef __HIP_PLATFORM_AMD__
  53. // Workaround for a rocBLAS bug when using multiple graphics cards:
  54. @@ -8004,7 +8005,7 @@ GGML_CALL void ggml_init_cublas() {
  55. #endif
  56. if (cudaGetDeviceCount(&g_device_count) != cudaSuccess) {
  57. - initialized = true;
  58. + g_cublas_initialized = true;
  59. g_cublas_loaded = false;
  60. fprintf(stderr, "%s: no " GGML_CUDA_NAME " devices found, " GGML_CUDA_NAME " will be disabled\n", __func__);
  61. return;
  62. @@ -8075,7 +8076,7 @@ GGML_CALL void ggml_init_cublas() {
  63. // configure logging to stdout
  64. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  65. - initialized = true;
  66. + g_cublas_initialized = true;
  67. g_cublas_loaded = true;
  68. }
  69. }
  70. @@ -11604,3 +11605,23 @@ GGML_CALL int ggml_backend_cuda_reg_devices() {
  71. }
  72. return device_count;
  73. }
  74. +
  75. +
  76. +extern "C" GGML_CALL void ggml_free_cublas(void);
  77. +GGML_CALL void ggml_free_cublas(void) {
  78. + for (int id = 0; id < g_device_count; ++id) {
  79. +#if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  80. + if (g_device_caps[id].vmm) {
  81. + CU_CHECK(cuMemUnmap(g_cuda_pool_addr[id], g_cuda_pool_size[id]));
  82. + g_cuda_pool_size[id] = 0;
  83. + g_cuda_pool_addr[id] = 0;
  84. + }
  85. +#endif
  86. + // TODO: free legacy non-vmm memory
  87. + // destroy cublas handle
  88. + CUBLAS_CHECK(cublasDestroy(g_cublas_handles[id]));
  89. + g_cublas_handles[id] = nullptr;
  90. + }
  91. +
  92. + g_cublas_initialized = false;
  93. +}
  94. diff --git a/ggml-cuda.h b/ggml-cuda.h
  95. index b1ebd61d..b4c80c2c 100644
  96. --- a/ggml-cuda.h
  97. +++ b/ggml-cuda.h
  98. @@ -20,6 +20,9 @@ extern "C" {
  99. // Always success. To check if CUDA is actually loaded, use `ggml_cublas_loaded`.
  100. GGML_API GGML_CALL void ggml_init_cublas(void);
  101. +// Release CUDA resources
  102. +GGML_API GGML_CALL void ggml_free_cublas(void);
  103. +
  104. // Returns `true` if there are available CUDA devices and cublas loads successfully; otherwise, it returns `false`.
  105. GGML_API GGML_CALL bool ggml_cublas_loaded(void);