2024-10-04 02:42:31 -06:00
|
|
|
use cxx_build::CFG;
|
|
|
|
use std::env;
|
2024-10-24 08:42:50 -06:00
|
|
|
use std::path::{Path, PathBuf};
|
2024-10-04 02:42:31 -06:00
|
|
|
|
2024-10-21 01:14:51 -06:00
|
|
|
const CMAKE_LLAMA_CPP_DEFAULT_CUDA_ARCHS: &str = "75-real;80-real;86-real;89-real;90-real";
|
2024-10-24 08:42:50 -06:00
|
|
|
const CMAKE_LLAMA_CPP_TARGET: &str = "tgi_llamacpp_backend_impl";
|
|
|
|
const CMAKE_LLAMA_CPP_FFI_TARGET: &str = "tgi_llamacpp_backend";
|
2024-10-04 02:42:31 -06:00
|
|
|
const MPI_REQUIRED_VERSION: &str = "4.1";
|
|
|
|
|
2024-10-24 08:42:50 -06:00
|
|
|
const BACKEND_DEPS: [&str; 2] = [CMAKE_LLAMA_CPP_TARGET, CMAKE_LLAMA_CPP_FFI_TARGET];
|
|
|
|
|
2024-10-04 02:42:31 -06:00
|
|
|
macro_rules! probe {
|
|
|
|
($name: expr, $version: expr) => {
|
|
|
|
if let Err(_) = pkg_config::probe_library($name) {
|
2024-11-21 05:52:38 -07:00
|
|
|
match pkg_config::probe_library(&format!("{}-{}", $name, $version)) {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(_) => Err(()),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(())
|
2024-10-04 02:42:31 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-10-24 08:42:50 -06:00
|
|
|
fn build_backend(
|
|
|
|
is_debug: bool,
|
|
|
|
opt_level: &str,
|
|
|
|
out_dir: &Path,
|
|
|
|
install_path: &PathBuf,
|
|
|
|
) -> PathBuf {
|
2024-10-21 01:14:51 -06:00
|
|
|
let build_cuda = option_env!("LLAMA_CPP_BUILD_CUDA").unwrap_or("OFF");
|
|
|
|
let cuda_archs =
|
|
|
|
option_env!("LLAMA_CPP_TARGET_CUDA_ARCHS").unwrap_or(CMAKE_LLAMA_CPP_DEFAULT_CUDA_ARCHS);
|
|
|
|
|
2024-10-04 02:42:31 -06:00
|
|
|
let _ = cmake::Config::new(".")
|
|
|
|
.uses_cxx11()
|
|
|
|
.generator("Ninja")
|
|
|
|
.profile(match is_debug {
|
|
|
|
true => "Debug",
|
|
|
|
false => "Release",
|
|
|
|
})
|
|
|
|
.env("OPT_LEVEL", opt_level)
|
|
|
|
.define("CMAKE_INSTALL_PREFIX", &install_path)
|
2024-10-21 01:14:51 -06:00
|
|
|
.define("LLAMA_CPP_BUILD_CUDA", build_cuda)
|
|
|
|
.define("LLAMA_CPP_TARGET_CUDA_ARCHS", cuda_archs)
|
2024-10-04 02:42:31 -06:00
|
|
|
.build();
|
|
|
|
|
2024-10-24 23:27:12 -06:00
|
|
|
// On some x64 and ARM mainly the lib install destination is "lib" and not "lib64"
|
|
|
|
let lib_path = if install_path.join("lib64").exists() {
|
|
|
|
install_path.join("lib64")
|
|
|
|
} else {
|
|
|
|
install_path.join("lib")
|
|
|
|
};
|
2024-10-24 08:42:50 -06:00
|
|
|
println!("cargo:rustc-link-search=native={}", lib_path.display());
|
2024-10-04 02:42:31 -06:00
|
|
|
|
|
|
|
let deps_folder = out_dir.join("build").join("_deps");
|
|
|
|
deps_folder
|
|
|
|
}
|
|
|
|
|
2024-11-21 05:52:38 -07:00
|
|
|
fn build_ffi_layer(is_debug: bool, install_prefix: &Path) {
|
2024-10-04 02:42:31 -06:00
|
|
|
CFG.include_prefix = "backends/llamacpp";
|
2024-11-21 05:52:38 -07:00
|
|
|
|
|
|
|
let mut bridge = cxx_build::bridge("src/lib.rs");
|
|
|
|
|
|
|
|
bridge
|
2024-10-04 02:42:31 -06:00
|
|
|
.static_flag(true)
|
2024-10-24 01:56:40 -06:00
|
|
|
.std("c++23")
|
2024-10-24 08:42:50 -06:00
|
|
|
.include(install_prefix.join("include"))
|
|
|
|
.include("csrc")
|
2024-11-21 05:52:38 -07:00
|
|
|
.file("csrc/ffi.hpp");
|
|
|
|
|
|
|
|
if is_debug {
|
|
|
|
bridge.define("TGI_LLAMACPP_BACKEND_DEBUG", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
if probe!("numa", "2.0").is_ok() {
|
|
|
|
bridge.define("NUMA_AVAILABLE", "");
|
|
|
|
};
|
|
|
|
|
|
|
|
bridge.compile(CMAKE_LLAMA_CPP_FFI_TARGET); // Make sure this target is not the same as cmake above
|
2024-10-04 02:42:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// Misc variables
|
|
|
|
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
|
|
|
|
let build_profile = env::var("PROFILE").unwrap();
|
|
|
|
let (is_debug, opt_level) = match build_profile.as_ref() {
|
|
|
|
"debug" => (true, "0"),
|
|
|
|
_ => (false, "3"),
|
|
|
|
};
|
|
|
|
|
2024-10-24 08:42:50 -06:00
|
|
|
let install_path = env::var("CMAKE_INSTALL_PREFIX")
|
|
|
|
.map(|val| PathBuf::from(val))
|
|
|
|
.unwrap_or(out_dir.join("dist"));
|
|
|
|
|
2024-10-04 02:42:31 -06:00
|
|
|
// Build the backend
|
2024-11-28 06:53:46 -07:00
|
|
|
let _ = build_backend(is_debug, opt_level, out_dir.as_path(), &install_path);
|
2024-10-04 02:42:31 -06:00
|
|
|
|
|
|
|
// Build the FFI layer calling the backend above
|
2024-11-21 05:52:38 -07:00
|
|
|
build_ffi_layer(is_debug, &install_path);
|
2024-10-04 02:42:31 -06:00
|
|
|
|
|
|
|
// Emit linkage search path
|
2024-11-21 05:52:38 -07:00
|
|
|
if probe!("ompi", MPI_REQUIRED_VERSION).is_err() {
|
|
|
|
panic!("An implement of MPI is required");
|
|
|
|
}
|
2024-10-04 02:42:31 -06:00
|
|
|
|
|
|
|
// Backend
|
2024-10-24 08:42:50 -06:00
|
|
|
BACKEND_DEPS.iter().for_each(|name| {
|
|
|
|
println!("cargo:rustc-link-lib=static={}", name);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Linkage info
|
|
|
|
println!("cargo:rustc-link-search=native={}", out_dir.display());
|
2024-10-24 23:27:12 -06:00
|
|
|
|
2024-11-14 01:38:13 -07:00
|
|
|
let spdlog_linkage_target = if is_debug { "spdlogd" } else { "spdlog" };
|
2024-11-20 16:03:05 -07:00
|
|
|
println!("cargo:rustc-link-lib=dylib={spdlog_linkage_target}");
|
2024-10-24 08:42:50 -06:00
|
|
|
println!("cargo:rustc-link-lib=dylib=ggml");
|
|
|
|
println!("cargo:rustc-link-lib=dylib=llama");
|
|
|
|
|
|
|
|
// Rerun if one of these file change
|
|
|
|
println!("cargo:rerun-if-changed=CMakeLists.txt");
|
|
|
|
println!("cargo:rerun-if-changed=csrc/backend.hpp");
|
|
|
|
println!("cargo:rerun-if-changed=csrc/backend.cpp");
|
|
|
|
println!("cargo:rerun-if-changed=csrc/ffi.hpp");
|
2024-10-04 02:42:31 -06:00
|
|
|
}
|