Skip to content

Commit cfa76bd

Browse files
committed
Don't imply dynamic llama.cpp just because CUDA is on
Link against CUDA statically as well to maintain consistency with GGML_STATIC although technically that's our discretion.
1 parent 8b11c5c commit cfa76bd

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")
@@ -355,6 +355,10 @@ fn main() {
355355

356356
if cfg!(feature = "cuda") {
357357
config.define("GGML_CUDA", "ON");
358+
359+
if cfg!(feature = "cuda-no-vmm") {
360+
config.define("GGML_CUDA_NO_VMM", "ON");
361+
}
358362
}
359363

360364
// Android doesn't have OpenMP support AFAICT and openmp is a default feature. Do this here
@@ -394,6 +398,31 @@ fn main() {
394398
);
395399
println!("cargo:rustc-link-search={}", build_dir.display());
396400

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

0 commit comments

Comments
 (0)