12
12
#ifdef GPU_INFO_USE_CUBLAS
13
13
# include " gpuInfo/cuda-gpu-info.h"
14
14
#endif
15
+ #ifdef GPU_INFO_USE_VULKAN
16
+ # include " gpuInfo/vulkan-gpu-info.h"
17
+ #endif
15
18
#ifdef GPU_INFO_USE_METAL
16
19
# include " gpuInfo/metal-gpu-info.h"
17
20
#endif
@@ -35,6 +38,7 @@ using AddonThreadSafeLogCallbackFunction =
35
38
AddonThreadSafeLogCallbackFunction addonThreadSafeLoggerCallback;
36
39
bool addonJsLoggerCallbackSet = false ;
37
40
int addonLoggerLogLevel = 5 ;
41
+ bool backendInitialized = false ;
38
42
39
43
std::string addon_model_token_to_piece (const struct llama_model * model, llama_token token) {
40
44
std::vector<char > result (8 , 0 );
@@ -51,10 +55,15 @@ std::string addon_model_token_to_piece(const struct llama_model* model, llama_to
51
55
}
52
56
53
57
#ifdef GPU_INFO_USE_CUBLAS
54
- void lodCudaError (const char * message) {
58
+ void logCudaError (const char * message) {
55
59
addonLlamaCppLogCallback (GGML_LOG_LEVEL_ERROR, (std::string (" CUDA error: " ) + std::string (message)).c_str (), nullptr );
56
60
}
57
61
#endif
62
+ #ifdef GPU_INFO_USE_VULKAN
63
+ void logVulkanWarning (const char * message) {
64
+ addonLlamaCppLogCallback (GGML_LOG_LEVEL_WARN, (std::string (" Vulkan warning: " ) + std::string (message)).c_str (), nullptr );
65
+ }
66
+ #endif
58
67
59
68
Napi::Value getGpuVramInfo (const Napi::CallbackInfo& info) {
60
69
uint64_t total = 0 ;
@@ -63,14 +72,25 @@ Napi::Value getGpuVramInfo(const Napi::CallbackInfo& info) {
63
72
#ifdef GPU_INFO_USE_CUBLAS
64
73
size_t cudaDeviceTotal = 0 ;
65
74
size_t cudaDeviceUsed = 0 ;
66
- bool cudeGetInfoSuccess = gpuInfoGetTotalCudaDevicesInfo (&cudaDeviceTotal, &cudaDeviceUsed, lodCudaError );
75
+ bool cudeGetInfoSuccess = gpuInfoGetTotalCudaDevicesInfo (&cudaDeviceTotal, &cudaDeviceUsed, logCudaError );
67
76
68
77
if (cudeGetInfoSuccess) {
69
78
total += cudaDeviceTotal;
70
79
used += cudaDeviceUsed;
71
80
}
72
81
#endif
73
82
83
+ #ifdef GPU_INFO_USE_VULKAN
84
+ uint64_t vulkanDeviceTotal = 0 ;
85
+ uint64_t vulkanDeviceUsed = 0 ;
86
+ const bool vulkanDeviceSupportsMemoryBudgetExtension = gpuInfoGetTotalVulkanDevicesInfo (&vulkanDeviceTotal, &vulkanDeviceUsed, logVulkanWarning);
87
+
88
+ if (vulkanDeviceSupportsMemoryBudgetExtension) {
89
+ total += vulkanDeviceTotal;
90
+ used += vulkanDeviceUsed;
91
+ }
92
+ #endif
93
+
74
94
#ifdef GPU_INFO_USE_METAL
75
95
uint64_t metalDeviceTotal = 0 ;
76
96
uint64_t metalDeviceUsed = 0 ;
@@ -950,7 +970,7 @@ void addonCallJsLogCallback(
950
970
called = false ;
951
971
}
952
972
}
953
-
973
+
954
974
if (!called && data != nullptr ) {
955
975
if (data->logLevelNumber == 2 ) {
956
976
fputs (data->stringStream ->str ().c_str (), stderr);
@@ -1046,8 +1066,17 @@ Napi::Value setLoggerLogLevel(const Napi::CallbackInfo& info) {
1046
1066
return info.Env ().Undefined ();
1047
1067
}
1048
1068
1069
+ static void addonFreeLlamaBackend (Napi::Env env, int * data) {
1070
+ if (backendInitialized) {
1071
+ llama_backend_free ();
1072
+ backendInitialized = false ;
1073
+ }
1074
+ }
1075
+
1049
1076
Napi::Object registerCallback (Napi::Env env, Napi::Object exports) {
1050
1077
llama_backend_init ();
1078
+ backendInitialized = true ;
1079
+
1051
1080
exports.DefineProperties ({
1052
1081
Napi::PropertyDescriptor::Function (" systemInfo" , systemInfo),
1053
1082
Napi::PropertyDescriptor::Function (" setLogger" , setLogger),
@@ -1061,6 +1090,8 @@ Napi::Object registerCallback(Napi::Env env, Napi::Object exports) {
1061
1090
1062
1091
llama_log_set (addonLlamaCppLogCallback, nullptr );
1063
1092
1093
+ exports.AddFinalizer (addonFreeLlamaBackend, static_cast <int *>(nullptr ));
1094
+
1064
1095
return exports;
1065
1096
}
1066
1097
0 commit comments