1212#ifdef GPU_INFO_USE_CUBLAS
1313# include " gpuInfo/cuda-gpu-info.h"
1414#endif
15+ #ifdef GPU_INFO_USE_VULKAN
16+ # include " gpuInfo/vulkan-gpu-info.h"
17+ #endif
1518#ifdef GPU_INFO_USE_METAL
1619# include " gpuInfo/metal-gpu-info.h"
1720#endif
@@ -35,6 +38,7 @@ using AddonThreadSafeLogCallbackFunction =
3538AddonThreadSafeLogCallbackFunction addonThreadSafeLoggerCallback;
3639bool addonJsLoggerCallbackSet = false ;
3740int addonLoggerLogLevel = 5 ;
41+ bool backendInitialized = false ;
3842
3943std::string addon_model_token_to_piece (const struct llama_model * model, llama_token token) {
4044 std::vector<char > result (8 , 0 );
@@ -51,10 +55,15 @@ std::string addon_model_token_to_piece(const struct llama_model* model, llama_to
5155}
5256
5357#ifdef GPU_INFO_USE_CUBLAS
54- void lodCudaError (const char * message) {
58+ void logCudaError (const char * message) {
5559 addonLlamaCppLogCallback (GGML_LOG_LEVEL_ERROR, (std::string (" CUDA error: " ) + std::string (message)).c_str (), nullptr );
5660}
5761#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
5867
5968Napi::Value getGpuVramInfo (const Napi::CallbackInfo& info) {
6069 uint64_t total = 0 ;
@@ -63,14 +72,25 @@ Napi::Value getGpuVramInfo(const Napi::CallbackInfo& info) {
6372#ifdef GPU_INFO_USE_CUBLAS
6473 size_t cudaDeviceTotal = 0 ;
6574 size_t cudaDeviceUsed = 0 ;
66- bool cudeGetInfoSuccess = gpuInfoGetTotalCudaDevicesInfo (&cudaDeviceTotal, &cudaDeviceUsed, lodCudaError );
75+ bool cudeGetInfoSuccess = gpuInfoGetTotalCudaDevicesInfo (&cudaDeviceTotal, &cudaDeviceUsed, logCudaError );
6776
6877 if (cudeGetInfoSuccess) {
6978 total += cudaDeviceTotal;
7079 used += cudaDeviceUsed;
7180 }
7281#endif
7382
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+
7494#ifdef GPU_INFO_USE_METAL
7595 uint64_t metalDeviceTotal = 0 ;
7696 uint64_t metalDeviceUsed = 0 ;
@@ -950,7 +970,7 @@ void addonCallJsLogCallback(
950970 called = false ;
951971 }
952972 }
953-
973+
954974 if (!called && data != nullptr ) {
955975 if (data->logLevelNumber == 2 ) {
956976 fputs (data->stringStream ->str ().c_str (), stderr);
@@ -1046,8 +1066,17 @@ Napi::Value setLoggerLogLevel(const Napi::CallbackInfo& info) {
10461066 return info.Env ().Undefined ();
10471067}
10481068
1069+ static void addonFreeLlamaBackend (Napi::Env env, int * data) {
1070+ if (backendInitialized) {
1071+ llama_backend_free ();
1072+ backendInitialized = false ;
1073+ }
1074+ }
1075+
10491076Napi::Object registerCallback (Napi::Env env, Napi::Object exports) {
10501077 llama_backend_init ();
1078+ backendInitialized = true ;
1079+
10511080 exports.DefineProperties ({
10521081 Napi::PropertyDescriptor::Function (" systemInfo" , systemInfo),
10531082 Napi::PropertyDescriptor::Function (" setLogger" , setLogger),
@@ -1061,6 +1090,8 @@ Napi::Object registerCallback(Napi::Env env, Napi::Object exports) {
10611090
10621091 llama_log_set (addonLlamaCppLogCallback, nullptr );
10631092
1093+ exports.AddFinalizer (addonFreeLlamaBackend, static_cast <int *>(nullptr ));
1094+
10641095 return exports;
10651096}
10661097
0 commit comments