@@ -508,6 +508,70 @@ fn main() {
508508 }
509509 }
510510
511+ // in this next bit, we select which cpu-specific features to compile for
512+ // first check for target-cpu=native
513+ let has_native_target_cpu = std:: env:: var ( "CARGO_ENCODED_RUSTFLAGS" )
514+ . map ( |rustflags| {
515+ rustflags
516+ . split ( '\x1f' )
517+ . any ( |f| f. contains ( "target-cpu=native" ) )
518+ } )
519+ . unwrap_or ( false ) ;
520+ if has_native_target_cpu {
521+ debug_log ! ( "Detected target-cpu=native, compiling with GGML_NATIVE" ) ;
522+ config. define ( "GGML_NATIVE" , "ON" ) ;
523+ }
524+ // if native isn't specified, enable specific features for ggml
525+ // Get the target features as a comma-separated string
526+ else if let Ok ( features) = std:: env:: var ( "CARGO_CFG_TARGET_FEATURE" ) {
527+ debug_log ! ( "Compiling with target features: {}" , features) ;
528+ // list of rust target_features here:
529+ // https://doc.rust-lang.org/reference/attributes/codegen.html#the-target_feature-attribute
530+ // GGML config flags have been found by looking at:
531+ // llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt
532+ for feature in features. split ( ',' ) {
533+ match feature {
534+ "avx" => {
535+ config. define ( "GGML_AVX" , "ON" ) ;
536+ }
537+ "avx2" => {
538+ config. define ( "GGML_AVX2" , "ON" ) ;
539+ }
540+ "avx512bf16" => {
541+ config. define ( "GGML_AVX512_BF16" , "ON" ) ;
542+ }
543+ "avx512vbmi" => {
544+ config. define ( "GGML_AVX512_VBMI" , "ON" ) ;
545+ }
546+ "avx512vnni" => {
547+ config. define ( "GGML_AVX512_VNNI" , "ON" ) ;
548+ }
549+ "avxvnni" => {
550+ config. define ( "GGML_AVX_VNNI" , "ON" ) ;
551+ }
552+ "bmi2" => {
553+ config. define ( "GGML_BMI2" , "ON" ) ;
554+ }
555+ "f16c" => {
556+ config. define ( "GGML_F16C" , "ON" ) ;
557+ }
558+ "fma" => {
559+ config. define ( "GGML_FMA" , "ON" ) ;
560+ }
561+ "sse4.2" => {
562+ config. define ( "GGML_SSE42" , "ON" ) ;
563+ }
564+ _ => {
565+ debug_log ! (
566+ "Unrecognized cpu feature: '{}' - skipping GGML config for it." ,
567+ feature
568+ ) ;
569+ continue ;
570+ }
571+ } ;
572+ }
573+ }
574+
511575 config. define (
512576 "BUILD_SHARED_LIBS" ,
513577 if build_shared_libs { "ON" } else { "OFF" } ,
@@ -627,9 +691,9 @@ fn main() {
627691
628692 if matches ! ( target_os, TargetOs :: Linux )
629693 && target_triple. contains ( "aarch64" )
630- && env :: var ( format ! ( "CARGO_FEATURE_{}" , "native" . to_uppercase ( ) ) ) . is_err ( )
694+ && has_native_target_cpu
631695 {
632- // If the native feature is not enabled , we take off the native ARM64 support.
696+ // If the target-cpu is not specified as native , we take off the native ARM64 support.
633697 // It is useful in docker environments where the native feature is not enabled.
634698 config. define ( "GGML_NATIVE" , "OFF" ) ;
635699 config. define ( "GGML_CPU_ARM_ARCH" , "armv8-a" ) ;
0 commit comments