Skip to content

Commit

Permalink
Merge pull request #671 from vlovich/cuda-static-build
Browse files Browse the repository at this point in the history
Don't imply dynamic llama.cpp just because CUDA is on
  • Loading branch information
MarcusDunn authored Feb 25, 2025
2 parents 8301d63 + cfa76bd commit 100f242
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions llama-cpp-2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ encoding_rs = { workspace = true }
[features]
default = ["openmp", "android-shared-stdcxx"]
cuda = ["llama-cpp-sys-2/cuda"]
cuda-no-vmm = ["cuda", "llama-cpp-sys-2/cuda-no-vmm"]
metal = ["llama-cpp-sys-2/metal"]
dynamic-link = ["llama-cpp-sys-2/dynamic-link"]
vulkan = ["llama-cpp-sys-2/vulkan"]
Expand Down
5 changes: 4 additions & 1 deletion llama-cpp-sys-2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,18 @@ include = [
bindgen = { workspace = true }
cc = { workspace = true, features = ["parallel"] }
cmake = "0.1"
find_cuda_helper = "0.2.0"
glob = "0.3.2"
walkdir = "2"

[features]
cuda = []
# Disables the need to dynamically link against libcuda.so / cuda.dll
cuda-no-vmm = ["cuda"]
metal = []
dynamic-link = []
vulkan = []
native = []
openmp = []
# Only has an impact on Android.
shared-stdcxx = []
shared-stdcxx = []
31 changes: 30 additions & 1 deletion llama-cpp-sys-2/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ fn main() {
let target_dir = get_cargo_target_dir().unwrap();
let manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("Failed to get CARGO_MANIFEST_DIR");
let llama_src = Path::new(&manifest_dir).join("llama.cpp");
let build_shared_libs = cfg!(feature = "cuda") || cfg!(feature = "dynamic-link");
let build_shared_libs = cfg!(feature = "dynamic-link");

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

if cfg!(feature = "cuda") {
config.define("GGML_CUDA", "ON");

if cfg!(feature = "cuda-no-vmm") {
config.define("GGML_CUDA_NO_VMM", "ON");
}
}

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

if cfg!(feature = "cuda") && !build_shared_libs {
println!("cargo:rerun-if-env-changed=CUDA_PATH");

for lib_dir in find_cuda_helper::find_cuda_lib_dirs() {
println!("cargo:rustc-link-search=native={}", lib_dir.display());
}

// Logic from ggml-cuda/CMakeLists.txt
println!("cargo:rustc-link-lib=static=cudart_static");
if matches!(target_os, TargetOs::Windows(_)) {
println!("cargo:rustc-link-lib=static=cublas");
println!("cargo:rustc-link-lib=static=cublasLt");
} else {
println!("cargo:rustc-link-lib=static=cublas_static");
println!("cargo:rustc-link-lib=static=cublasLt_static");
}

// Need to link against libcuda.so unless GGML_CUDA_NO_VMM is defined.
if !cfg!(feature = "cuda-no-vmm") {
println!("cargo:rustc-link-lib=cuda");
}

println!("cargo:rustc-link-lib=static=culibos");
}

// Link libraries
let llama_libs_kind = if build_shared_libs { "dylib" } else { "static" };
let llama_libs = extract_lib_names(&out_dir, build_shared_libs);
Expand Down

0 comments on commit 100f242

Please sign in to comment.