Skip to content

Commit f303b2f

Browse files
committed
feat: don't share loaded shared libraries between backends
1 parent 7b3706a commit f303b2f

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

llama/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ if (NLC_CURRENT_PLATFORM STREQUAL "win-x64" OR NLC_CURRENT_PLATFORM STREQUAL "wi
44
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
55
endif()
66

7+
include("./cmake/addVariantSuffix.cmake")
8+
79
if (NLC_CURRENT_PLATFORM STREQUAL "win-x64")
810
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
911
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDebugDLL" CACHE STRING "" FORCE)
@@ -109,6 +111,9 @@ list(REMOVE_DUPLICATES GPU_INFO_HEADERS)
109111
list(REMOVE_DUPLICATES GPU_INFO_SOURCES)
110112
list(REMOVE_DUPLICATES GPU_INFO_EXTRA_LIBS)
111113

114+
addVariantSuffix(llama ${NLC_VARIANT})
115+
addVariantSuffix(ggml ${NLC_VARIANT})
116+
112117
file(GLOB SOURCE_FILES "addon/*.cpp" "addon/**/*.cpp" ${GPU_INFO_SOURCES})
113118

114119
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC} ${GPU_INFO_HEADERS})

llama/cmake/addVariantSuffix.cmake

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function(addVariantSuffix originalTarget variantSuffix)
2+
if (NOT TARGET ${originalTarget} OR variantSuffix STREQUAL "")
3+
return()
4+
endif()
5+
6+
set(_name "${originalTarget}.${variantSuffix}")
7+
8+
set_target_properties(${originalTarget} PROPERTIES
9+
OUTPUT_NAME "${_name}"
10+
RUNTIME_OUTPUT_NAME "${_name}" # Windows .dll
11+
LIBRARY_OUTPUT_NAME "${_name}" # Unix shared lib
12+
ARCHIVE_OUTPUT_NAME "${_name}" # static / import lib
13+
)
14+
15+
if (APPLE)
16+
set_target_properties(${originalTarget} PROPERTIES
17+
MACOSX_RPATH ON
18+
INSTALL_NAME_DIR "@rpath"
19+
)
20+
endif()
21+
endfunction()

src/bindings/utils/compileLLamaCpp.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export async function compileLlamaCpp(buildOptions: BuildOptions, compileOptions
6161
platform === "win" &&
6262
(
6363
buildOptions.gpu === false ||
64-
(buildOptions.gpu === "vulkan" && buildOptions.arch === "arm64") // Vulkan can't be compiled on Windows x64 with LLVM ATM
64+
buildOptions.gpu === "vulkan"
6565
) &&
6666
!ignoreWorkarounds.includes("avoidWindowsLlvm") &&
6767
!buildOptions.customCmakeOptions.has("CMAKE_TOOLCHAIN_FILE") &&
@@ -105,6 +105,7 @@ export async function compileLlamaCpp(buildOptions: BuildOptions, compileOptions
105105
cmakeCustomOptions.set("CMAKE_CONFIGURATION_TYPES", buildConfigType);
106106
cmakeCustomOptions.set("NLC_CURRENT_PLATFORM", platform + "-" + process.arch);
107107
cmakeCustomOptions.set("NLC_TARGET_PLATFORM", buildOptions.platform + "-" + buildOptions.arch);
108+
cmakeCustomOptions.set("NLC_VARIANT", buildFolderName.binVariant);
108109

109110
if (toolchainFile != null && !cmakeCustomOptions.has("CMAKE_TOOLCHAIN_FILE"))
110111
cmakeToolchainOptions.set("CMAKE_TOOLCHAIN_FILE", toolchainFile);
@@ -146,7 +147,11 @@ export async function compileLlamaCpp(buildOptions: BuildOptions, compileOptions
146147
if (!cmakeCustomOptions.has("GGML_NATIVE") || isCmakeValueOff(cmakeCustomOptions.get("GGML_NATIVE"))) {
147148
cmakeCustomOptions.set("GGML_NATIVE", "OFF");
148149

149-
if (buildOptions.arch === "x64" && !cmakeCustomOptions.has("GGML_CPU_ALL_VARIANTS")) {
150+
if (!cmakeCustomOptions.has("GGML_CPU_ALL_VARIANTS") && (
151+
buildOptions.arch === "x64" ||
152+
(buildOptions.arch === "arm64" && platform === "linux") ||
153+
(buildOptions.arch === "arm64" && platform === "mac")
154+
)) {
150155
cmakeCustomOptions.set("GGML_CPU_ALL_VARIANTS", "ON");
151156
cmakeCustomOptions.set("GGML_BACKEND_DL", "ON");
152157
} else if (!cmakeCustomOptions.has("GGML_BACKEND_DL"))
@@ -244,7 +249,8 @@ export async function compileLlamaCpp(buildOptions: BuildOptions, compileOptions
244249
else if (buildOptions.gpu === "cuda") {
245250
if (!ignoreWorkarounds.includes("cudaArchitecture") && (platform === "win" || platform === "linux") &&
246251
err instanceof SpawnError && (
247-
err.combinedStd.toLowerCase().includes("Failed to detect a default CUDA architecture".toLowerCase()) || (
252+
err.combinedStd.toLowerCase().includes("Failed to detect a default CUDA architecture".toLowerCase()) ||
253+
err.combinedStd.toLowerCase().includes("CMAKE_CUDA_COMPILER-NOTFOUND".toLowerCase()) || (
248254
err.combinedStd.toLowerCase().includes(
249255
"Tell CMake where to find the compiler by setting either the environment".toLowerCase()
250256
) &&

src/bindings/utils/getBuildFolderNameForBuildOptions.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@ import {builtinLlamaCppGitHubRepo, builtinLlamaCppRelease} from "../../config.js
44

55
export async function getBuildFolderNameForBuildOptions(buildOptions: BuildOptions) {
66
const nameParts: string[] = [buildOptions.platform, buildOptions.arch];
7+
const binParts: string[] = [];
78

8-
if (buildOptions.gpu !== false)
9+
if (buildOptions.gpu !== false) {
910
nameParts.push(makeStringSafeForPathName(buildOptions.gpu));
11+
binParts.push(makeStringSafeForPathName(buildOptions.gpu.toLowerCase()));
12+
}
1013

11-
if (buildOptions.llamaCpp.repo !== builtinLlamaCppGitHubRepo || buildOptions.llamaCpp.release !== builtinLlamaCppRelease)
12-
nameParts.push("release-" + await getFolderNamePartForRelease(buildOptions.llamaCpp.repo, buildOptions.llamaCpp.release));
14+
if (buildOptions.llamaCpp.repo !== builtinLlamaCppGitHubRepo || buildOptions.llamaCpp.release !== builtinLlamaCppRelease) {
15+
const releaseFolderNamePart = await getFolderNamePartForRelease(buildOptions.llamaCpp.repo, buildOptions.llamaCpp.release);
16+
nameParts.push("release-" + releaseFolderNamePart);
17+
binParts.push(releaseFolderNamePart.replaceAll(" ", "_"));
18+
} else if (buildOptions.llamaCpp.release !== "latest")
19+
binParts.push(buildOptions.llamaCpp.release);
1320

1421
if (buildOptions.customCmakeOptions.size === 0) {
1522
const name = nameParts.join("-");
1623
return {
1724
withoutCustomCmakeOptions: name,
18-
withCustomCmakeOptions: name
25+
withCustomCmakeOptions: name,
26+
binVariant: binParts.join(".")
1927
};
2028
}
2129

@@ -34,18 +42,21 @@ export async function getBuildFolderNameForBuildOptions(buildOptions: BuildOptio
3442
if (cmakeOptionStringsArray.length === 0) {
3543
return {
3644
withoutCustomCmakeOptions: nameWithoutCustomCmakeOptions,
37-
withCustomCmakeOptions: nameWithoutCustomCmakeOptions
45+
withCustomCmakeOptions: nameWithoutCustomCmakeOptions,
46+
binVariant: binParts.join(".")
3847
};
3948
}
4049

4150
const cmakeOptionsHash = await hashString(cmakeOptionStringsArray.join(";"));
4251

4352
nameParts.push(cmakeOptionsHash);
53+
binParts.push(cmakeOptionsHash.slice(0, 8));
4454
const nameWithCustomCmakeOptions = nameParts.join("-");
4555

4656
return {
4757
withoutCustomCmakeOptions: nameWithoutCustomCmakeOptions,
48-
withCustomCmakeOptions: nameWithCustomCmakeOptions
58+
withCustomCmakeOptions: nameWithCustomCmakeOptions,
59+
binVariant: binParts.join(".")
4960
};
5061
}
5162

@@ -60,7 +71,7 @@ async function getFolderNamePartForRelease(repo: string, release: string) {
6071
shouldHash = true;
6172
resParts.push(encodeURIComponent(String(owner)) + " " + encodeURIComponent(String(name)));
6273
} else
63-
resParts.push(owner + " " + name);
74+
resParts.push(String(owner) + " " + String(name));
6475
}
6576

6677
if (containsUnsafeCharacters(release)) {

0 commit comments

Comments
 (0)