Skip to content

Commit 4afdaf0

Browse files
authored
Merge pull request #1014 from marek-hradil/allow-dynamic-backends
Allow dynamic backends
2 parents b568332 + 48887bf commit 4afdaf0

5 files changed

Lines changed: 51 additions & 0 deletions

File tree

llama-cpp-2/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ mtmd = ["llama-cpp-sys-2/mtmd"]
4141
system-ggml = ["llama-cpp-sys-2/system-ggml"]
4242
system-ggml-static = ["llama-cpp-sys-2/system-ggml-static"]
4343
llguidance = ["dep:llguidance", "dep:toktrie"]
44+
dynamic-backends = ["llama-cpp-sys-2/dynamic-backends"]
4445

4546

4647
[target.'cfg(all(target_os = "macos", any(target_arch = "aarch64", target_arch = "arm64")))'.dependencies]

llama-cpp-2/build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
if let Ok(dir) = std::env::var("DEP_LLAMA_BACKENDS_DIR") {
3+
println!("cargo:rustc-env=GGML_BACKENDS_DIR={}", dir);
4+
}
5+
}

llama-cpp-2/src/llama_backend.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,34 @@ impl Drop for LlamaBackend {
180180
}
181181
}
182182

183+
/// Compile-time path to the built GGML backend modules directory.
184+
/// Populated by build.rs from `DEP_LLAMA_BACKENDS_DIR` (emitted by llama-cpp-sys-2).
185+
/// None on static builds or when the feature is disabled.
186+
#[cfg(feature = "dynamic-backends")]
187+
pub const BACKENDS_DIR: Option<&str> = option_env!("GGML_BACKENDS_DIR");
188+
189+
/// Load GGML backend modules from the given directory.
190+
///
191+
/// Call this before [`LlamaBackend::init`] to enable runtime hardware selection
192+
/// (Vulkan, CPU-AVX512, CPU-AVX2, etc.) when built with the `dynamic-backends` feature.
193+
#[cfg(feature = "dynamic-backends")]
194+
pub fn load_backends_from_path(path: &std::path::Path) {
195+
let s = std::ffi::CString::new(path.to_str().expect("path must be valid UTF-8"))
196+
.expect("path must not contain null bytes");
197+
unsafe { llama_cpp_sys_2::ggml_backend_load_all_from_path(s.as_ptr()) }
198+
}
199+
200+
/// Load GGML backend modules from the compile-time default directory ([`BACKENDS_DIR`]).
201+
///
202+
/// This is a no-op when `BACKENDS_DIR` is `None` (static builds or development builds
203+
/// that have not set `GGML_BACKENDS_DIR`).
204+
#[cfg(feature = "dynamic-backends")]
205+
pub fn load_backends() {
206+
if let Some(dir) = BACKENDS_DIR {
207+
load_backends_from_path(std::path::Path::new(dir));
208+
}
209+
}
210+
183211
#[cfg(test)]
184212
mod tests {
185213
use super::*;

llama-cpp-sys-2/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,4 @@ static-stdcxx = []
9696
system-ggml = []
9797
system-ggml-static = ["system-ggml"]
9898
mtmd = []
99+
dynamic-backends = ["dynamic-link"]

llama-cpp-sys-2/build.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,18 @@ fn main() {
820820
config.define("LLAMA_USE_SYSTEM_GGML", "ON");
821821
}
822822

823+
if cfg!(feature = "dynamic-backends") {
824+
// Pre-create the backends directory so CMake can install MODULE libs there.
825+
// GGML_BACKEND_DIR causes backends to install to this known path instead of
826+
// CMAKE_INSTALL_BINDIR, making them easy to locate in downstream build scripts.
827+
let backends_dir = out_dir.join("backends");
828+
std::fs::create_dir_all(&backends_dir).unwrap();
829+
config.define("GGML_BACKEND_DL", "ON");
830+
config.define("GGML_CPU_ALL_VARIANTS", "ON");
831+
config.define("GGML_BACKEND_DIR", backends_dir.to_str().unwrap());
832+
// BUILD_SHARED_LIBS=ON is already set above via the dynamic-link feature.
833+
}
834+
823835
// General
824836
config
825837
.profile(&profile)
@@ -828,6 +840,10 @@ fn main() {
828840

829841
let build_dir = config.build();
830842

843+
if cfg!(feature = "dynamic-backends") {
844+
println!("cargo:backends_dir={}", out_dir.join("backends").display());
845+
}
846+
831847
// Build mtmd directly with cc::Build, bypassing the cmake tools build.
832848
// Using LLAMA_BUILD_TOOLS=ON would pull in all tools (batched-bench, quantize, etc.)
833849
// and their CMakeLists.txt files, which are not included in the crate package.

0 commit comments

Comments
 (0)