First upload version 0.0.1

This commit is contained in:
Neyra
2026-02-05 15:27:49 +08:00
commit 8e9b7201ed
4182 changed files with 593136 additions and 0 deletions

View File

@@ -0,0 +1,143 @@
#include <sstream>
#include "addonLog.h"
AddonThreadSafeLogCallbackFunction addonThreadSafeLoggerCallback;
bool addonJsLoggerCallbackSet = false;
int addonLoggerLogLevel = 5;
int addonLastLoggerLogLevel = 6;
static int addonGetGgmlLogLevelNumber(ggml_log_level level) {
switch (level) {
case GGML_LOG_LEVEL_ERROR: return 2;
case GGML_LOG_LEVEL_WARN: return 3;
case GGML_LOG_LEVEL_INFO: return 4;
case GGML_LOG_LEVEL_NONE: return 5;
case GGML_LOG_LEVEL_DEBUG: return 6;
case GGML_LOG_LEVEL_CONT: return addonLastLoggerLogLevel;
}
return 1;
}
void addonCallJsLogCallback(
Napi::Env env, Napi::Function callback, AddonThreadSafeLogCallbackFunctionContext* context, addon_logger_log* data
) {
bool called = false;
if (env != nullptr && callback != nullptr && addonJsLoggerCallbackSet) {
try {
callback.Call({
Napi::Number::New(env, data->logLevelNumber),
Napi::String::New(env, data->stringStream->str()),
});
called = true;
} catch (const Napi::Error& e) {
called = false;
}
}
if (!called && data != nullptr) {
if (data->logLevelNumber == 2) {
fputs(data->stringStream->str().c_str(), stderr);
fflush(stderr);
} else {
fputs(data->stringStream->str().c_str(), stdout);
fflush(stdout);
}
}
if (data != nullptr) {
delete data->stringStream;
delete data;
}
}
void addonLlamaCppLogCallback(ggml_log_level level, const char* text, void* user_data) {
int logLevelNumber = addonGetGgmlLogLevelNumber(level);
addonLastLoggerLogLevel = logLevelNumber;
if (logLevelNumber > addonLoggerLogLevel) {
return;
}
if (addonJsLoggerCallbackSet) {
std::stringstream* stringStream = new std::stringstream();
if (text != nullptr) {
*stringStream << text;
}
addon_logger_log* data = new addon_logger_log {
logLevelNumber,
stringStream,
};
auto status = addonThreadSafeLoggerCallback.NonBlockingCall(data);
if (status == napi_ok) {
return;
} else {
delete stringStream;
delete data;
}
}
if (text != nullptr) {
if (level == 2) {
fputs(text, stderr);
fflush(stderr);
} else {
fputs(text, stdout);
fflush(stdout);
}
}
}
Napi::Value setLogger(const Napi::CallbackInfo& info) {
if (addonJsLoggerCallbackSet) {
addonJsLoggerCallbackSet = false;
addonThreadSafeLoggerCallback.Release();
}
if (info.Length() < 1 || !info[0].IsFunction()) {
return info.Env().Undefined();
}
auto addonLoggerJSCallback = info[0].As<Napi::Function>();
AddonThreadSafeLogCallbackFunctionContext* context = new Napi::Reference<Napi::Value>(Napi::Persistent(info.This()));
addonThreadSafeLoggerCallback = AddonThreadSafeLogCallbackFunction::New(
info.Env(),
addonLoggerJSCallback,
"loggerCallback",
0,
1,
context,
[](Napi::Env, void*, AddonThreadSafeLogCallbackFunctionContext* ctx) {
addonJsLoggerCallbackSet = false;
delete ctx;
}
);
addonJsLoggerCallbackSet = true;
// prevent blocking the main node process from exiting due to active resources
addonThreadSafeLoggerCallback.Unref(info.Env());
return info.Env().Undefined();
}
Napi::Value setLoggerLogLevel(const Napi::CallbackInfo& info) {
if (info.Length() < 1 || !info[0].IsNumber()) {
addonLoggerLogLevel = 5;
return info.Env().Undefined();
}
addonLoggerLogLevel = info[0].As<Napi::Number>().Int32Value();
return info.Env().Undefined();
}
void addonLog(ggml_log_level level, const std::string text) {
addonLlamaCppLogCallback(level, std::string("[addon] " + text + "\n").c_str(), nullptr);
}

View File

@@ -0,0 +1,24 @@
#pragma once
#include "llama.h"
#include "napi.h"
struct addon_logger_log {
public:
const int logLevelNumber;
const std::stringstream* stringStream;
};
void addonLlamaCppLogCallback(ggml_log_level level, const char* text, void* user_data);
using AddonThreadSafeLogCallbackFunctionContext = Napi::Reference<Napi::Value>;
void addonCallJsLogCallback(
Napi::Env env, Napi::Function callback, AddonThreadSafeLogCallbackFunctionContext* context, addon_logger_log* data
);
using AddonThreadSafeLogCallbackFunction =
Napi::TypedThreadSafeFunction<AddonThreadSafeLogCallbackFunctionContext, addon_logger_log, addonCallJsLogCallback>;
Napi::Value setLogger(const Napi::CallbackInfo& info);
Napi::Value setLoggerLogLevel(const Napi::CallbackInfo& info);
void addonLog(ggml_log_level level, const std::string text);

View File

@@ -0,0 +1,15 @@
#include "addonProgress.h"
void addonCallJsProgressCallback(
Napi::Env env, Napi::Function callback, AddonThreadSafeProgressCallbackFunctionContext* context, addon_progress_event* data
) {
if (env != nullptr && callback != nullptr) {
try {
callback.Call({Napi::Number::New(env, data->progress)});
} catch (const Napi::Error& e) {}
}
if (data != nullptr) {
delete data;
}
}

View File

@@ -0,0 +1,15 @@
#pragma once
#include "napi.h"
struct addon_progress_event {
public:
const float progress;
};
using AddonThreadSafeProgressCallbackFunctionContext = Napi::Reference<Napi::Value>;
void addonCallJsProgressCallback(
Napi::Env env, Napi::Function callback, AddonThreadSafeProgressCallbackFunctionContext* context, addon_progress_event* data
);
using AddonThreadSafeProgressEventCallbackFunction =
Napi::TypedThreadSafeFunction<AddonThreadSafeProgressCallbackFunctionContext, addon_progress_event, addonCallJsProgressCallback>;

View File

@@ -0,0 +1,146 @@
#include "getGpuInfo.h"
#include "addonLog.h"
#ifdef __APPLE__
#include <TargetConditionals.h>
#endif
#ifdef GPU_INFO_USE_VULKAN
# include "../../gpuInfo/vulkan-gpu-info.h"
#endif
#ifdef GPU_INFO_USE_VULKAN
void logVulkanWarning(const char* message) {
addonLlamaCppLogCallback(GGML_LOG_LEVEL_WARN, (std::string("Vulkan warning: ") + std::string(message)).c_str(), nullptr);
}
#endif
Napi::Value getGpuVramInfo(const Napi::CallbackInfo& info) {
ggml_backend_dev_t device = NULL;
size_t deviceTotal = 0;
size_t deviceFree = 0;
uint64_t total = 0;
uint64_t used = 0;
uint64_t unifiedVramSize = 0;
for (size_t i = 0; i < ggml_backend_dev_count(); i++) {
device = ggml_backend_dev_get(i);
auto deviceType = ggml_backend_dev_type(device);
if (deviceType == GGML_BACKEND_DEVICE_TYPE_GPU || deviceType == GGML_BACKEND_DEVICE_TYPE_IGPU) {
deviceTotal = 0;
deviceFree = 0;
ggml_backend_dev_memory(device, &deviceFree, &deviceTotal);
total += deviceTotal;
used += deviceTotal - deviceFree;
#if defined(__arm64__) || defined(__aarch64__)
if (std::string(ggml_backend_dev_name(device)) == "Metal") {
unifiedVramSize += deviceTotal;
}
#endif
}
}
#ifdef GPU_INFO_USE_VULKAN
uint64_t vulkanDeviceTotal = 0;
uint64_t vulkanDeviceUsed = 0;
uint64_t vulkanDeviceUnifiedVramSize = 0;
const bool vulkanDeviceSupportsMemoryBudgetExtension = gpuInfoGetTotalVulkanDevicesInfo(&vulkanDeviceTotal, &vulkanDeviceUsed, &vulkanDeviceUnifiedVramSize, logVulkanWarning);
if (vulkanDeviceSupportsMemoryBudgetExtension) {
if (vulkanDeviceUnifiedVramSize > total) {
// this means that we counted memory from devices that aren't used by llama.cpp
vulkanDeviceUnifiedVramSize = 0;
}
unifiedVramSize += vulkanDeviceUnifiedVramSize;
}
if (used == 0 && vulkanDeviceUsed != 0) {
used = vulkanDeviceUsed;
}
#endif
Napi::Object result = Napi::Object::New(info.Env());
result.Set("total", Napi::Number::From(info.Env(), total));
result.Set("used", Napi::Number::From(info.Env(), used));
result.Set("unifiedSize", Napi::Number::From(info.Env(), unifiedVramSize));
return result;
}
Napi::Value getGpuDeviceInfo(const Napi::CallbackInfo& info) {
std::vector<std::string> deviceNames;
for (size_t i = 0; i < ggml_backend_dev_count(); i++) {
ggml_backend_dev_t device = ggml_backend_dev_get(i);
auto deviceType = ggml_backend_dev_type(device);
if (deviceType == GGML_BACKEND_DEVICE_TYPE_GPU || deviceType == GGML_BACKEND_DEVICE_TYPE_IGPU) {
deviceNames.push_back(std::string(ggml_backend_dev_description(device)));
}
}
Napi::Object result = Napi::Object::New(info.Env());
Napi::Array deviceNamesNapiArray = Napi::Array::New(info.Env(), deviceNames.size());
for (size_t i = 0; i < deviceNames.size(); ++i) {
deviceNamesNapiArray[i] = Napi::String::New(info.Env(), deviceNames[i]);
}
result.Set("deviceNames", deviceNamesNapiArray);
return result;
}
std::pair<ggml_backend_dev_t, std::string> getGpuDevice() {
for (size_t i = 0; i < ggml_backend_dev_count(); i++) {
ggml_backend_dev_t device = ggml_backend_dev_get(i);
const auto deviceName = std::string(ggml_backend_dev_name(device));
if (deviceName == "Metal") {
return std::pair<ggml_backend_dev_t, std::string>(device, "metal");
} else if (std::string(deviceName).find("Vulkan") == 0) {
return std::pair<ggml_backend_dev_t, std::string>(device, "vulkan");
} else if (std::string(deviceName).find("CUDA") == 0 || std::string(deviceName).find("ROCm") == 0 || std::string(deviceName).find("MUSA") == 0) {
return std::pair<ggml_backend_dev_t, std::string>(device, "cuda");
}
}
for (size_t i = 0; i < ggml_backend_dev_count(); i++) {
ggml_backend_dev_t device = ggml_backend_dev_get(i);
const auto deviceName = std::string(ggml_backend_dev_name(device));
if (deviceName == "CPU") {
return std::pair<ggml_backend_dev_t, std::string>(device, "cpu");
}
}
return std::pair<ggml_backend_dev_t, std::string>(nullptr, "");
}
Napi::Value getGpuType(const Napi::CallbackInfo& info) {
const auto gpuDeviceRes = getGpuDevice();
const auto device = gpuDeviceRes.first;
const auto deviceType = gpuDeviceRes.second;
if (deviceType == "cpu") {
return Napi::Boolean::New(info.Env(), false);
} else if (device != nullptr && deviceType != "") {
return Napi::String::New(info.Env(), deviceType);
}
return info.Env().Undefined();
}
Napi::Value ensureGpuDeviceIsSupported(const Napi::CallbackInfo& info) {
#ifdef GPU_INFO_USE_VULKAN
if (!checkIsVulkanEnvSupported(logVulkanWarning)) {
Napi::Error::New(info.Env(), "Vulkan device is not supported").ThrowAsJavaScriptException();
return info.Env().Undefined();
}
#endif
return info.Env().Undefined();
}

View File

@@ -0,0 +1,11 @@
#pragma once
#include <utility>
#include <string>
#include "napi.h"
#include "llama.h"
Napi::Value getGpuVramInfo(const Napi::CallbackInfo& info);
Napi::Value getGpuDeviceInfo(const Napi::CallbackInfo& info);
std::pair<ggml_backend_dev_t, std::string> getGpuDevice();
Napi::Value getGpuType(const Napi::CallbackInfo& info);
Napi::Value ensureGpuDeviceIsSupported(const Napi::CallbackInfo& info);

View File

@@ -0,0 +1,63 @@
#include "getMemoryInfo.h"
#include "addonLog.h"
#ifdef __APPLE__
#include <iostream>
#include <mach/mach.h>
#include <sys/sysctl.h>
#elif __linux__
#include <fstream>
#include <sstream>
#include <string>
#elif _WIN32
#include <iostream>
#include <windows.h>
#include <psapi.h>
#endif
Napi::Value getMemoryInfo(const Napi::CallbackInfo& info) {
uint64_t totalMemoryUsage = 0;
#ifdef __APPLE__
struct mach_task_basic_info taskInfo;
mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&taskInfo, &infoCount) == KERN_SUCCESS) {
totalMemoryUsage = taskInfo.virtual_size;
} else {
addonLlamaCppLogCallback(GGML_LOG_LEVEL_ERROR, std::string("Failed to get memory usage info").c_str(), nullptr);
}
#elif __linux__
std::ifstream procStatus("/proc/self/status");
std::string line;
bool foundMemoryUsage = false;
while (std::getline(procStatus, line)) {
if (line.rfind("VmSize:", 0) == 0) { // Resident Set Size (current memory usage)
std::istringstream iss(line);
std::string key, unit;
size_t value;
if (iss >> key >> value >> unit) {
totalMemoryUsage = value * 1024; // Convert from kB to bytes
foundMemoryUsage = true;
}
break;
}
}
if (!foundMemoryUsage) {
addonLlamaCppLogCallback(GGML_LOG_LEVEL_ERROR, std::string("Failed to get memory usage info").c_str(), nullptr);
}
#elif _WIN32
PROCESS_MEMORY_COUNTERS_EX memCounters;
if (GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&memCounters, sizeof(memCounters))) {
totalMemoryUsage = memCounters.PrivateUsage;
} else {
addonLlamaCppLogCallback(GGML_LOG_LEVEL_ERROR, std::string("Failed to get memory usage info").c_str(), nullptr);
}
#endif
Napi::Object obj = Napi::Object::New(info.Env());
obj.Set("total", Napi::Number::New(info.Env(), totalMemoryUsage));
return obj;
}

View File

@@ -0,0 +1,4 @@
#pragma once
#include "napi.h"
Napi::Value getMemoryInfo(const Napi::CallbackInfo& info);

View File

@@ -0,0 +1,69 @@
#include "getSwapInfo.h"
#include "addonLog.h"
#ifdef __APPLE__
#include <iostream>
#include <mach/mach.h>
#include <sys/sysctl.h>
#elif __linux__
#include <iostream>
#include <sys/sysinfo.h>
#elif _WIN32
#include <iostream>
#include <windows.h>
#include <psapi.h>
#endif
Napi::Value getSwapInfo(const Napi::CallbackInfo& info) {
uint64_t totalSwap = 0;
uint64_t freeSwap = 0;
uint64_t maxSize = 0;
bool maxSizeSet = true;
#ifdef __APPLE__
struct xsw_usage swapInfo;
size_t size = sizeof(swapInfo);
if (sysctlbyname("vm.swapusage", &swapInfo, &size, NULL, 0) == 0) {
totalSwap = swapInfo.xsu_total;
freeSwap = swapInfo.xsu_avail;
maxSizeSet = false;
} else {
addonLlamaCppLogCallback(GGML_LOG_LEVEL_ERROR, std::string("Failed to get swap info").c_str(), nullptr);
}
#elif __linux__
struct sysinfo sysInfo;
if (sysinfo(&sysInfo) == 0) {
totalSwap = sysInfo.totalswap;
freeSwap = sysInfo.freeswap;
maxSize = sysInfo.totalswap;
} else {
addonLlamaCppLogCallback(GGML_LOG_LEVEL_ERROR, std::string("Failed to get swap info").c_str(), nullptr);
}
#elif _WIN32
MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX);
if (GlobalMemoryStatusEx(&memInfo)) {
PERFORMANCE_INFORMATION perfInfo;
perfInfo.cb = sizeof(PERFORMANCE_INFORMATION);
if (GetPerformanceInfo(&perfInfo, sizeof(perfInfo))) {
totalSwap = memInfo.ullTotalPageFile;
freeSwap = memInfo.ullAvailPageFile;
maxSize = perfInfo.CommitLimit * perfInfo.PageSize;
} else {
addonLlamaCppLogCallback(GGML_LOG_LEVEL_ERROR, std::string("Failed to get max pagefile size").c_str(), nullptr);
}
} else {
addonLlamaCppLogCallback(GGML_LOG_LEVEL_ERROR, std::string("Failed to get pagefile info").c_str(), nullptr);
}
#endif
Napi::Object obj = Napi::Object::New(info.Env());
obj.Set("total", Napi::Number::New(info.Env(), totalSwap));
obj.Set("free", Napi::Number::New(info.Env(), freeSwap));
obj.Set("maxSize", maxSizeSet ? Napi::Number::New(info.Env(), maxSize) : Napi::Number::New(info.Env(), -1));
return obj;
}

View File

@@ -0,0 +1,4 @@
#pragma once
#include "napi.h"
Napi::Value getSwapInfo(const Napi::CallbackInfo& info);