02-cudaleaks.diff 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. diff --git a/examples/server/server.cpp b/examples/server/server.cpp
  2. index 2b2f4a0f..25857bdd 100644
  3. --- a/examples/server/server.cpp
  4. +++ b/examples/server/server.cpp
  5. @@ -31,6 +31,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. @@ -363,6 +367,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. @@ -3494,6 +3501,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 0c6501e9..75c12723 100644
  33. --- a/ggml-cuda.cu
  34. +++ b/ggml-cuda.cu
  35. @@ -43,6 +43,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. @@ -8694,10 +8695,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. @@ -8707,7 +8708,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. @@ -8778,7 +8779,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. @@ -12345,3 +12346,22 @@ GGML_CALL int ggml_backend_cuda_reg_devices() {
  71. }
  72. return device_count;
  73. }
  74. +
  75. +extern "C" GGML_CALL void ggml_free_cublas(void);
  76. +GGML_CALL void ggml_free_cublas(void) {
  77. + for (int id = 0; id < g_device_count; ++id) {
  78. +#if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  79. + if (g_device_caps[id].vmm) {
  80. + CU_CHECK(cuMemUnmap(g_cuda_pool_addr[id], g_cuda_pool_size[id]));
  81. + g_cuda_pool_size[id] = 0;
  82. + g_cuda_pool_addr[id] = 0;
  83. + }
  84. +#endif
  85. + // TODO: free legacy non-vmm memory
  86. + // destroy cublas handle
  87. + CUBLAS_CHECK(cublasDestroy(g_cublas_handles[id]));
  88. + g_cublas_handles[id] = nullptr;
  89. + }
  90. +
  91. + g_cublas_initialized = false;
  92. +}
  93. diff --git a/ggml-cuda.h b/ggml-cuda.h
  94. index b1ebd61d..6dd58ddf 100644
  95. --- a/ggml-cuda.h
  96. +++ b/ggml-cuda.h
  97. @@ -23,6 +23,9 @@ GGML_API GGML_CALL void ggml_init_cublas(void);
  98. // Returns `true` if there are available CUDA devices and cublas loads successfully; otherwise, it returns `false`.
  99. GGML_API GGML_CALL bool ggml_cublas_loaded(void);
  100. +// Release CUDA resources
  101. +GGML_API GGML_CALL void ggml_free_cublas(void);
  102. +
  103. GGML_API GGML_CALL void * ggml_cuda_host_malloc(size_t size);
  104. GGML_API GGML_CALL void ggml_cuda_host_free(void * ptr);