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