Skip to content

Commit 1a4493d

Browse files
authored
Merge pull request #121 from Hirtol/main
Add CPU Feature Support
2 parents 2849fcd + 90e088e commit 1a4493d

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

llama-cpp-sys-2/build.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,52 @@ fn main() {
6868
llama_cpp.define("GGML_USE_CUBLAS", None);
6969
}
7070

71+
for build in [&mut ggml, &mut llama_cpp] {
72+
let compiler = build.get_compiler();
73+
74+
if cfg!(target_arch = "i686") || cfg!(target_arch = "x86_64") {
75+
let features = x86::Features::get_target();
76+
if compiler.is_like_clang() || compiler.is_like_gnu() {
77+
build.flag("-pthread");
78+
79+
if features.avx {
80+
build.flag("-mavx");
81+
}
82+
if features.avx2 {
83+
build.flag("-mavx2");
84+
}
85+
if features.fma {
86+
build.flag("-mfma");
87+
}
88+
if features.f16c {
89+
build.flag("-mf16c");
90+
}
91+
if features.sse3 {
92+
build.flag("-msse3");
93+
}
94+
} else if compiler.is_like_msvc() {
95+
match (features.avx2, features.avx) {
96+
(true, _) => {
97+
build.flag("/arch:AVX2");
98+
}
99+
(_, true) => {
100+
build.flag("/arch:AVX");
101+
}
102+
_ => {}
103+
}
104+
}
105+
} else if cfg!(target_arch = "aarch64") {
106+
if compiler.is_like_clang() || compiler.is_like_gnu() {
107+
if cfg!(target_os = "macos") {
108+
build.flag("-mcpu=apple-m1");
109+
} else if std::env::var("HOST") == std::env::var("TARGET") {
110+
build.flag("-mcpu=native");
111+
}
112+
build.flag("-pthread");
113+
}
114+
}
115+
}
116+
71117
// https://github.com/ggerganov/llama.cpp/blob/191221178f51b6e81122c5bda0fd79620e547d07/Makefile#L133-L141
72118
if cfg!(target_os = "macos") {
73119
assert!(!cublas_enabled, "CUBLAS is not supported on macOS");
@@ -112,6 +158,16 @@ fn main() {
112158
.std("c++11")
113159
.file("llama.cpp/llama.cpp");
114160

161+
// Remove debug log output from `llama.cpp`
162+
let is_release = env::var("PROFILE").unwrap() == "release";
163+
if is_release {
164+
ggml.define("NDEBUG", None);
165+
llama_cpp.define("NDEBUG", None);
166+
if let Some(cuda) = ggml_cuda.as_mut() {
167+
cuda.define("NDEBUG", None);
168+
}
169+
}
170+
115171
if let Some(ggml_cuda) = ggml_cuda {
116172
println!("compiling ggml-cuda");
117173
ggml_cuda.compile("ggml-cuda");
@@ -183,3 +239,37 @@ fn metal_hack(build: &mut cc::Build) {
183239

184240
build.file(ggml_metal_path);
185241
}
242+
243+
// Courtesy of https://github.com/rustformers/llm
244+
fn get_supported_target_features() -> std::collections::HashSet<String> {
245+
env::var("CARGO_CFG_TARGET_FEATURE")
246+
.unwrap()
247+
.split(',')
248+
.map(ToString::to_string)
249+
.collect()
250+
}
251+
252+
mod x86 {
253+
#[allow(clippy::struct_excessive_bools)]
254+
#[derive(Clone, Debug, PartialEq, Eq)]
255+
pub struct Features {
256+
pub fma: bool,
257+
pub avx: bool,
258+
pub avx2: bool,
259+
pub f16c: bool,
260+
pub sse3: bool,
261+
}
262+
impl Features {
263+
264+
pub fn get_target() -> Self {
265+
let features = crate::get_supported_target_features();
266+
Self {
267+
fma: features.contains("fma"),
268+
avx: features.contains("avx"),
269+
avx2: features.contains("avx2"),
270+
f16c: features.contains("f16c"),
271+
sse3: features.contains("sse3"),
272+
}
273+
}
274+
}
275+
}

0 commit comments

Comments
 (0)