diff --git a/llama/addon/addon.cpp b/llama/addon/addon.cpp index eb1f35bf..e8cd6124 100644 --- a/llama/addon/addon.cpp +++ b/llama/addon/addon.cpp @@ -1,3 +1,5 @@ +#include +#include #include "addonGlobals.h" #include "AddonModel.h" #include "AddonModelLora.h" @@ -286,7 +288,32 @@ static void addonFreeLlamaBackendFromFinalizer(Napi::Env env, int* data) { addonFreeLlamaBackend(); } + +// WSL2 Fix: Force load CUDA driver +static void wsl_cuda_preinit() { + void* handle = dlopen("/usr/lib/wsl/lib/libcuda.so", RTLD_NOW | RTLD_GLOBAL); + if (!handle) { + fprintf(stderr, "[WSL2-CUDA] Failed to preload libcuda.so: %s +", dlerror()); + return; + } + typedef CUresult (*cuInit_fn)(unsigned int); + cuInit_fn my_cuInit = (cuInit_fn)dlsym(handle, "cuInit"); + if (my_cuInit) { + CUresult res = my_cuInit(0); + if (res == 0) { + fprintf(stderr, "[WSL2-CUDA] Pre-init cuInit result: 0 (SUCCESS) +"); + } else { + fprintf(stderr, "[WSL2-CUDA] Pre-init cuInit result: %d (FAILED) +", res); + } + } +} + Napi::Object registerCallback(Napi::Env env, Napi::Object exports) { + // WSL2 Fix + wsl_cuda_preinit(); exports.DefineProperties({ Napi::PropertyDescriptor::Function("markLoaded", markLoaded), Napi::PropertyDescriptor::Function("systemInfo", systemInfo), diff --git a/src/bindings/utils/getPlatformInfo.ts b/src/bindings/utils/getPlatformInfo.ts index 48c88f4f..637ca6d2 100644 --- a/src/bindings/utils/getPlatformInfo.ts +++ b/src/bindings/utils/getPlatformInfo.ts @@ -1,31 +1,46 @@ import os from "os"; +import fs from "fs"; import {getPlatform} from "./getPlatform.js"; import {getLinuxDistroInfo} from "./getLinuxDistroInfo.js"; -export async function getPlatformInfo(): Promise<{name: string, version: string}> { +export async function getPlatformInfo(): Promise<{name: string, version: string, wsl2: boolean}> { const currentPlatform = getPlatform(); + let isWsl2 = false; + + if (currentPlatform === "linux") { + try { + const release = fs.readFileSync("/proc/sys/kernel/osrelease", "utf8").toLowerCase(); + isWsl2 = release.includes("microsoft") || release.includes("wsl"); + } catch { + // ignore + } + } if (currentPlatform === "mac") return { name: "macOS", - version: os.release() + version: os.release(), + wsl2: false }; else if (currentPlatform === "linux") { const linuxDistroInfo = await getLinuxDistroInfo(); return { name: linuxDistroInfo.name, - version: linuxDistroInfo.version + version: linuxDistroInfo.version, + wsl2: isWsl2 }; } else if (currentPlatform === "win") return { name: "Windows", - version: os.release() + version: os.release(), + wsl2: false }; return { name: "Unknown", - version: os.release() + version: os.release(), + wsl2: false }; } diff --git a/src/cli/commands/source/commands/BuildCommand.ts b/src/cli/commands/source/commands/BuildCommand.ts index e592fa00..d39c93ed 100644 --- a/src/cli/commands/source/commands/BuildCommand.ts +++ b/src/cli/commands/source/commands/BuildCommand.ts @@ -124,8 +124,14 @@ export async function BuildLlamaCppCommand({ downloadedCmake = true; } + // WSL2 fix: Disable VMM to prevent cuMemAddressReserve crash (Issue #580) + const effectiveCmakeOptions = new Map(customCmakeOptions); + if (gpuToTry === "cuda" && platformInfo.wsl2) { + effectiveCmakeOptions.set("GGML_CUDA_NO_VMM", "ON"); + } + const buildOptions: BuildOptions = { - customCmakeOptions, + customCmakeOptions: effectiveCmakeOptions, progressLogs: true, platform, platformInfo,