Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions llama/addon/addon.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <cuda.h>
#include <dlfcn.h>
#include "addonGlobals.h"
#include "AddonModel.h"
#include "AddonModelLora.h"
Expand Down Expand Up @@ -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),
Expand Down
25 changes: 20 additions & 5 deletions src/bindings/utils/getPlatformInfo.ts
Original file line number Diff line number Diff line change
@@ -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
};
}

Expand Down
8 changes: 7 additions & 1 deletion src/cli/commands/source/commands/BuildCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down