9
9
#include " llama.h"
10
10
#include " napi.h"
11
11
12
+ #ifdef GPU_INFO_USE_CUBLAS
13
+ # include " gpuInfo/cuda-gpu-info.h"
14
+ #endif
15
+ #ifdef GPU_INFO_USE_METAL
16
+ # include " gpuInfo/metal-gpu-info.h"
17
+ #endif
18
+
19
+
12
20
struct addon_logger_log {
13
21
public:
14
22
const int logLevelNumber;
15
23
const std::stringstream* stringStream;
16
24
};
17
25
26
+ static void addonLlamaCppLogCallback (ggml_log_level level, const char * text, void * user_data);
27
+
18
28
using AddonThreadSafeLogCallbackFunctionContext = Napi::Reference<Napi::Value>;
19
29
void addonCallJsLogCallback (
20
30
Napi::Env env, Napi::Function callback, AddonThreadSafeLogCallbackFunctionContext* context, addon_logger_log* data
@@ -40,6 +50,43 @@ std::string addon_model_token_to_piece(const struct llama_model* model, llama_to
40
50
return std::string (result.data (), result.size ());
41
51
}
42
52
53
+ #ifdef GPU_INFO_USE_CUBLAS
54
+ void lodCudaError (const char * message) {
55
+ addonLlamaCppLogCallback (GGML_LOG_LEVEL_ERROR, (std::string (" CUDA error: " ) + std::string (message)).c_str (), nullptr );
56
+ }
57
+ #endif
58
+
59
+ Napi::Value getGpuVramInfo (const Napi::CallbackInfo& info) {
60
+ uint64_t total = 0 ;
61
+ uint64_t used = 0 ;
62
+
63
+ #ifdef GPU_INFO_USE_CUBLAS
64
+ size_t cudaDeviceTotal = 0 ;
65
+ size_t cudaDeviceUsed = 0 ;
66
+ bool cudeGetInfoSuccess = gpuInfoGetTotalCudaDevicesInfo (&cudaDeviceTotal, &cudaDeviceUsed, lodCudaError);
67
+
68
+ if (cudeGetInfoSuccess) {
69
+ total += cudaDeviceTotal;
70
+ used += cudaDeviceUsed;
71
+ }
72
+ #endif
73
+
74
+ #ifdef GPU_INFO_USE_METAL
75
+ uint64_t metalDeviceTotal = 0 ;
76
+ uint64_t metalDeviceUsed = 0 ;
77
+ get_metal_gpu_info (&metalDeviceTotal, &metalDeviceUsed);
78
+
79
+ total += metalDeviceTotal;
80
+ used += metalDeviceUsed;
81
+ #endif
82
+
83
+ Napi::Object result = Napi::Object::New (info.Env ());
84
+ result.Set (" total" , Napi::Number::From (info.Env (), total));
85
+ result.Set (" used" , Napi::Number::From (info.Env (), used));
86
+
87
+ return result;
88
+ }
89
+
43
90
class AddonModel : public Napi ::ObjectWrap<AddonModel> {
44
91
public:
45
92
llama_model_params model_params;
@@ -830,12 +877,21 @@ int addonGetGgmlLogLevelNumber(ggml_log_level level) {
830
877
void addonCallJsLogCallback (
831
878
Napi::Env env, Napi::Function callback, AddonThreadSafeLogCallbackFunctionContext* context, addon_logger_log* data
832
879
) {
880
+ bool called = false ;
881
+
833
882
if (env != nullptr && callback != nullptr ) {
834
- callback.Call ({
835
- Napi::Number::New (env, data->logLevelNumber ),
836
- Napi::String::New (env, data->stringStream ->str ()),
837
- });
838
- } else if (data != nullptr ) {
883
+ try {
884
+ callback.Call ({
885
+ Napi::Number::New (env, data->logLevelNumber ),
886
+ Napi::String::New (env, data->stringStream ->str ()),
887
+ });
888
+ called = true ;
889
+ } catch (const Napi::Error& e) {
890
+ called = false ;
891
+ }
892
+ }
893
+
894
+ if (!called && data != nullptr ) {
839
895
if (data->logLevelNumber == 2 ) {
840
896
fputs (data->stringStream ->str ().c_str (), stderr);
841
897
fflush (stderr);
@@ -936,6 +992,7 @@ Napi::Object registerCallback(Napi::Env env, Napi::Object exports) {
936
992
Napi::PropertyDescriptor::Function (" systemInfo" , systemInfo),
937
993
Napi::PropertyDescriptor::Function (" setLogger" , setLogger),
938
994
Napi::PropertyDescriptor::Function (" setLoggerLogLevel" , setLoggerLogLevel),
995
+ Napi::PropertyDescriptor::Function (" getGpuVramInfo" , getGpuVramInfo),
939
996
});
940
997
AddonModel::init (exports);
941
998
AddonGrammar::init (exports);
0 commit comments