Skip to content

Commit 100f242

Browse files
authored
Merge pull request #671 from vlovich/cuda-static-build
Don't imply dynamic llama.cpp just because CUDA is on
2 parents 8301d63 + cfa76bd commit 100f242

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

llama-cpp-2/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ encoding_rs = { workspace = true }
2121
[features]
2222
default = ["openmp", "android-shared-stdcxx"]
2323
cuda = ["llama-cpp-sys-2/cuda"]
24+
cuda-no-vmm = ["cuda", "llama-cpp-sys-2/cuda-no-vmm"]
2425
metal = ["llama-cpp-sys-2/metal"]
2526
dynamic-link = ["llama-cpp-sys-2/dynamic-link"]
2627
vulkan = ["llama-cpp-sys-2/vulkan"]

llama-cpp-sys-2/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,18 @@ include = [
7474
bindgen = { workspace = true }
7575
cc = { workspace = true, features = ["parallel"] }
7676
cmake = "0.1"
77+
find_cuda_helper = "0.2.0"
7778
glob = "0.3.2"
7879
walkdir = "2"
7980

8081
[features]
8182
cuda = []
83+
# Disables the need to dynamically link against libcuda.so / cuda.dll
84+
cuda-no-vmm = ["cuda"]
8285
metal = []
8386
dynamic-link = []
8487
vulkan = []
8588
native = []
8689
openmp = []
8790
# Only has an impact on Android.
88-
shared-stdcxx = []
91+
shared-stdcxx = []

llama-cpp-sys-2/build.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ fn main() {
179179
let target_dir = get_cargo_target_dir().unwrap();
180180
let manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("Failed to get CARGO_MANIFEST_DIR");
181181
let llama_src = Path::new(&manifest_dir).join("llama.cpp");
182-
let build_shared_libs = cfg!(feature = "cuda") || cfg!(feature = "dynamic-link");
182+
let build_shared_libs = cfg!(feature = "dynamic-link");
183183

184184
let build_shared_libs = std::env::var("LLAMA_BUILD_SHARED_LIBS")
185185
.map(|v| v == "1")
@@ -352,6 +352,10 @@ fn main() {
352352

353353
if cfg!(feature = "cuda") {
354354
config.define("GGML_CUDA", "ON");
355+
356+
if cfg!(feature = "cuda-no-vmm") {
357+
config.define("GGML_CUDA_NO_VMM", "ON");
358+
}
355359
}
356360

357361
// Android doesn't have OpenMP support AFAICT and openmp is a default feature. Do this here
@@ -391,6 +395,31 @@ fn main() {
391395
);
392396
println!("cargo:rustc-link-search={}", build_dir.display());
393397

398+
if cfg!(feature = "cuda") && !build_shared_libs {
399+
println!("cargo:rerun-if-env-changed=CUDA_PATH");
400+
401+
for lib_dir in find_cuda_helper::find_cuda_lib_dirs() {
402+
println!("cargo:rustc-link-search=native={}", lib_dir.display());
403+
}
404+
405+
// Logic from ggml-cuda/CMakeLists.txt
406+
println!("cargo:rustc-link-lib=static=cudart_static");
407+
if matches!(target_os, TargetOs::Windows(_)) {
408+
println!("cargo:rustc-link-lib=static=cublas");
409+
println!("cargo:rustc-link-lib=static=cublasLt");
410+
} else {
411+
println!("cargo:rustc-link-lib=static=cublas_static");
412+
println!("cargo:rustc-link-lib=static=cublasLt_static");
413+
}
414+
415+
// Need to link against libcuda.so unless GGML_CUDA_NO_VMM is defined.
416+
if !cfg!(feature = "cuda-no-vmm") {
417+
println!("cargo:rustc-link-lib=cuda");
418+
}
419+
420+
println!("cargo:rustc-link-lib=static=culibos");
421+
}
422+
394423
// Link libraries
395424
let llama_libs_kind = if build_shared_libs { "dylib" } else { "static" };
396425
let llama_libs = extract_lib_names(&out_dir, build_shared_libs);

0 commit comments

Comments
 (0)