Skip to content

Commit be2d7e0

Browse files
authored
Merge pull request #882 from AsbjornOlling/use-cargo-cpu-features-configuration
Use cargo's target-cpu configuration
2 parents 8f6aa1f + b078bc9 commit be2d7e0

File tree

7 files changed

+81
-8
lines changed

7 files changed

+81
-8
lines changed

examples/embeddings/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ anyhow = { workspace = true }
1313
[features]
1414
cuda = ["llama-cpp-2/cuda"]
1515
metal = ["llama-cpp-2/metal"]
16-
native = ["llama-cpp-2/native"]
1716
vulkan = ["llama-cpp-2/vulkan"]
1817

1918
[lints]

examples/mtmd/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ clap = { workspace = true, features = ["derive"] }
1111
[features]
1212
cuda = ["llama-cpp-2/cuda"]
1313
metal = ["llama-cpp-2/metal"]
14-
native = ["llama-cpp-2/native"]
1514
vulkan = ["llama-cpp-2/vulkan"]
1615

1716
[lints]

examples/reranker/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ encoding_rs = { workspace = true }
1414
[features]
1515
cuda = ["llama-cpp-2/cuda"]
1616
metal = ["llama-cpp-2/metal"]
17-
native = ["llama-cpp-2/native"]
1817
vulkan = ["llama-cpp-2/vulkan"]
1918

2019
[lints]

examples/simple/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ tracing-subscriber = { workspace = true }
1717
[features]
1818
cuda = ["llama-cpp-2/cuda"]
1919
metal = ["llama-cpp-2/metal"]
20-
native = ["llama-cpp-2/native"]
2120
vulkan = ["llama-cpp-2/vulkan"]
2221

2322
[lints]

llama-cpp-2/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ cuda-no-vmm = ["cuda", "llama-cpp-sys-2/cuda-no-vmm"]
2626
metal = ["llama-cpp-sys-2/metal"]
2727
dynamic-link = ["llama-cpp-sys-2/dynamic-link"]
2828
vulkan = ["llama-cpp-sys-2/vulkan"]
29-
native = ["llama-cpp-sys-2/native"]
3029
openmp = ["llama-cpp-sys-2/openmp"]
3130
sampler = []
3231
# Only has an impact on Android.

llama-cpp-sys-2/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ cuda-no-vmm = ["cuda"]
7878
metal = []
7979
dynamic-link = []
8080
vulkan = []
81-
native = []
8281
openmp = []
8382
# Only has an impact on Android.
8483
shared-stdcxx = []

llama-cpp-sys-2/build.rs

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,85 @@ fn main() {
508508
}
509509
}
510510

511+
// extract the target-cpu config value, if specified
512+
let target_cpu = std::env::var("CARGO_ENCODED_RUSTFLAGS")
513+
.ok()
514+
.and_then(|rustflags| {
515+
rustflags
516+
.split('\x1f')
517+
.find(|f| f.contains("target-cpu="))
518+
.and_then(|f| f.split("target-cpu=").nth(1))
519+
.map(|s| s.to_string())
520+
});
521+
522+
if target_cpu == Some("native".into()) {
523+
debug_log!("Detected target-cpu=native, compiling with GGML_NATIVE");
524+
config.define("GGML_NATIVE", "ON");
525+
}
526+
// if native isn't specified, enable specific features for ggml instead
527+
else {
528+
// rust code isn't using `target-cpu=native`, so llama.cpp shouldn't use GGML_NATIVE either
529+
config.define("GGML_NATIVE", "OFF");
530+
531+
// if `target-cpu` is set set, also set -march for llama.cpp to the same value
532+
if let Some(ref cpu) = target_cpu {
533+
debug_log!("Setting baseline architecture: -march={}", cpu);
534+
config.cflag(&format!("-march={}", cpu));
535+
config.cxxflag(&format!("-march={}", cpu));
536+
}
537+
538+
// I expect this env var to always be present
539+
let features = std::env::var("CARGO_CFG_TARGET_FEATURE")
540+
.expect("Env var CARGO_CFG_TARGET_FEATURE not found.");
541+
debug_log!("Compiling with target features: {}", features);
542+
543+
// list of rust target_features here:
544+
// https://doc.rust-lang.org/reference/attributes/codegen.html#the-target_feature-attribute
545+
// GGML config flags have been found by looking at:
546+
// llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt
547+
for feature in features.split(',') {
548+
match feature {
549+
"avx" => {
550+
config.define("GGML_AVX", "ON");
551+
}
552+
"avx2" => {
553+
config.define("GGML_AVX2", "ON");
554+
}
555+
"avx512bf16" => {
556+
config.define("GGML_AVX512_BF16", "ON");
557+
}
558+
"avx512vbmi" => {
559+
config.define("GGML_AVX512_VBMI", "ON");
560+
}
561+
"avx512vnni" => {
562+
config.define("GGML_AVX512_VNNI", "ON");
563+
}
564+
"avxvnni" => {
565+
config.define("GGML_AVX_VNNI", "ON");
566+
}
567+
"bmi2" => {
568+
config.define("GGML_BMI2", "ON");
569+
}
570+
"f16c" => {
571+
config.define("GGML_F16C", "ON");
572+
}
573+
"fma" => {
574+
config.define("GGML_FMA", "ON");
575+
}
576+
"sse4.2" => {
577+
config.define("GGML_SSE42", "ON");
578+
}
579+
_ => {
580+
debug_log!(
581+
"Unrecognized cpu feature: '{}' - skipping GGML config for it.",
582+
feature
583+
);
584+
continue;
585+
}
586+
};
587+
}
588+
}
589+
511590
config.define(
512591
"BUILD_SHARED_LIBS",
513592
if build_shared_libs { "ON" } else { "OFF" },
@@ -627,9 +706,9 @@ fn main() {
627706

628707
if matches!(target_os, TargetOs::Linux)
629708
&& target_triple.contains("aarch64")
630-
&& env::var(format!("CARGO_FEATURE_{}", "native".to_uppercase())).is_err()
709+
&& target_cpu != Some("native".into())
631710
{
632-
// If the native feature is not enabled, we take off the native ARM64 support.
711+
// If the target-cpu is not specified as native, we take off the native ARM64 support.
633712
// It is useful in docker environments where the native feature is not enabled.
634713
config.define("GGML_NATIVE", "OFF");
635714
config.define("GGML_CPU_ARM_ARCH", "armv8-a");

0 commit comments

Comments
 (0)