@llama-node/llama-cpp
Advanced tools
| #![allow(clippy::uninlined_format_args)] | ||
| // Based on llm-chain implementation | ||
| // https://github.com/sobelio/llm-chain/blob/main/llm-chain-llama/sys/build.rs | ||
| extern crate bindgen; | ||
| use std::env; | ||
| use std::path::PathBuf; | ||
| fn main() { | ||
| let (_host, target_arch, target_os) = get_build_target(); | ||
| let target = env::var("TARGET").unwrap(); | ||
| // Link C++ standard library | ||
| if let Some(cpp_stdlib) = get_cpp_link_stdlib(&target) { | ||
| println!("cargo:rustc-link-lib=dylib={}", cpp_stdlib); | ||
| println!("cargo:rustc-link-arg=-l{}", cpp_stdlib); | ||
| } | ||
| // Link macOS Accelerate framework for matrix calculations | ||
| if target.contains("apple") { | ||
| println!("cargo:rustc-link-lib=framework=Accelerate"); | ||
| } | ||
| println!("cargo:rustc-link-search={}", env::var("OUT_DIR").unwrap()); | ||
| println!("cargo:rustc-link-lib=static=llama"); | ||
| println!("cargo:rerun-if-changed=wrapper.h"); | ||
| env::set_var("CXXFLAGS", "-fPIC"); | ||
| env::set_var("CFLAGS", "-fPIC"); | ||
| if env::var("LLAMA_DONT_GENERATE_BINDINGS").is_ok() { | ||
| let _: u64 = std::fs::copy( | ||
| "src/bindings.rs", | ||
| env::var("OUT_DIR").unwrap() + "/bindings.rs", | ||
| ) | ||
| .expect("Failed to copy bindings.rs"); | ||
| } else { | ||
| let bindings = bindgen::Builder::default() | ||
| .header("wrapper.h") | ||
| .clang_arg("-I./llama.cpp") | ||
| .parse_callbacks(Box::new(bindgen::CargoCallbacks)) | ||
| .generate(); | ||
| match bindings { | ||
| Ok(b) => { | ||
| let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); | ||
| b.write_to_file(out_path.join("bindings.rs")) | ||
| .expect("Couldn't write bindings!"); | ||
| } | ||
| Err(e) => { | ||
| println!("cargo:warning=Unable to generate bindings: {}", e); | ||
| println!("cargo:warning=Using bundled bindings.rs, which may be out of date"); | ||
| // copy src/bindings.rs to OUT_DIR | ||
| std::fs::copy( | ||
| "src/bindings.rs", | ||
| env::var("OUT_DIR").unwrap() + "/bindings.rs", | ||
| ) | ||
| .expect("Unable to copy bindings.rs"); | ||
| } | ||
| } | ||
| }; | ||
| // stop if we're on docs.rs | ||
| if env::var("DOCS_RS").is_ok() { | ||
| return; | ||
| } | ||
| // build lib | ||
| env::set_current_dir("llama.cpp").expect("Unable to change directory to llama.cpp"); | ||
| _ = std::fs::remove_dir_all("build"); | ||
| _ = std::fs::create_dir("build"); | ||
| env::set_current_dir("build").expect("Unable to change directory to llama.cpp build"); | ||
| let mut command = std::process::Command::new("cmake"); | ||
| let command = command | ||
| .arg("..") | ||
| .arg("-DCMAKE_BUILD_TYPE=Release") | ||
| .arg("-DLLAMA_ALL_WARNINGS=OFF") | ||
| .arg("-DLLAMA_ALL_WARNINGS_3RD_PARTY=OFF") | ||
| .arg("-DLLAMA_BUILD_TESTS=OFF") | ||
| .arg("-DLLAMA_BUILD_EXAMPLES=OFF"); | ||
| if target_os.contains("darwin") && target_arch.contains("aarch64") { | ||
| command | ||
| .arg("-DCMAKE_SYSTEM_NAME=Darwin") | ||
| .arg("-DCMAKE_SYSTEM_PROCESSOR=apple-m1") | ||
| .arg("-DCMAKE_OSX_ARCHITECTURES=arm64") | ||
| .arg("-DLLAMA_NATIVE=OFF"); | ||
| println!("command: {:?}", command.get_args()); | ||
| } | ||
| let code = command.status().expect("Failed to generate build script"); | ||
| if code.code() != Some(0) { | ||
| panic!("Failed to generate build script"); | ||
| } | ||
| let code = std::process::Command::new("cmake") | ||
| .arg("--build") | ||
| .arg(".") | ||
| .arg("--config Release") | ||
| .status() | ||
| .expect("Failed to build lib"); | ||
| if code.code() != Some(0) { | ||
| panic!("Failed to build lib"); | ||
| } | ||
| // move libllama.a to where Cargo expects it (OUT_DIR) | ||
| #[cfg(target_os = "windows")] | ||
| { | ||
| std::fs::copy( | ||
| "Release/llama.lib", | ||
| format!("{}/llama.lib", env::var("OUT_DIR").unwrap()), | ||
| ) | ||
| .expect("Failed to copy lib"); | ||
| } | ||
| #[cfg(not(target_os = "windows"))] | ||
| { | ||
| std::fs::copy( | ||
| "libllama.a", | ||
| format!("{}/libllama.a", env::var("OUT_DIR").unwrap()), | ||
| ) | ||
| .expect("Failed to copy lib"); | ||
| } | ||
| // clean the llama build directory to prevent Cargo from complaining during crate publish | ||
| _ = std::fs::remove_dir_all("build"); | ||
| } | ||
| // From https://github.com/alexcrichton/cc-rs/blob/fba7feded71ee4f63cfe885673ead6d7b4f2f454/src/lib.rs#L2462 | ||
| fn get_cpp_link_stdlib(target: &str) -> Option<&'static str> { | ||
| if target.contains("msvc") { | ||
| None | ||
| } else if target.contains("apple") || target.contains("freebsd") || target.contains("openbsd") { | ||
| Some("c++") | ||
| } else if target.contains("android") { | ||
| Some("c++_shared") | ||
| } else { | ||
| Some("stdc++") | ||
| } | ||
| } | ||
| fn get_build_target() -> (String, String, String) { | ||
| let target = env::var("TARGET").unwrap(); | ||
| let target_triple = target.split('-').collect::<Vec<&str>>(); | ||
| let target_arch = target_triple[0]; | ||
| let target_os = target_triple[2]; | ||
| let host = env::var("HOST").unwrap(); | ||
| println!("target_arch: {}", target_arch); | ||
| println!("target_os: {}", target_os); | ||
| println!("host: {}", host); | ||
| (host, target_arch.to_string(), target_os.to_string()) | ||
| } |
| [package] | ||
| name = "llama-sys" | ||
| version = "0.0.1" | ||
| edition = "2021" | ||
| license = "MIT" | ||
| [dependencies] | ||
| [build-dependencies] | ||
| bindgen = "0.64" |
| /* automatically generated by rust-bindgen 0.65.1 */ | ||
| pub const _STDINT_H: u32 = 1; | ||
| pub const _FEATURES_H: u32 = 1; | ||
| pub const _DEFAULT_SOURCE: u32 = 1; | ||
| pub const __GLIBC_USE_ISOC2X: u32 = 0; | ||
| pub const __USE_ISOC11: u32 = 1; | ||
| pub const __USE_ISOC99: u32 = 1; | ||
| pub const __USE_ISOC95: u32 = 1; | ||
| pub const __USE_POSIX_IMPLICITLY: u32 = 1; | ||
| pub const _POSIX_SOURCE: u32 = 1; | ||
| pub const _POSIX_C_SOURCE: u32 = 200809; | ||
| pub const __USE_POSIX: u32 = 1; | ||
| pub const __USE_POSIX2: u32 = 1; | ||
| pub const __USE_POSIX199309: u32 = 1; | ||
| pub const __USE_POSIX199506: u32 = 1; | ||
| pub const __USE_XOPEN2K: u32 = 1; | ||
| pub const __USE_XOPEN2K8: u32 = 1; | ||
| pub const _ATFILE_SOURCE: u32 = 1; | ||
| pub const __WORDSIZE: u32 = 64; | ||
| pub const __WORDSIZE_TIME64_COMPAT32: u32 = 1; | ||
| pub const __SYSCALL_WORDSIZE: u32 = 64; | ||
| pub const __TIMESIZE: u32 = 64; | ||
| pub const __USE_MISC: u32 = 1; | ||
| pub const __USE_ATFILE: u32 = 1; | ||
| pub const __USE_FORTIFY_LEVEL: u32 = 0; | ||
| pub const __GLIBC_USE_DEPRECATED_GETS: u32 = 0; | ||
| pub const __GLIBC_USE_DEPRECATED_SCANF: u32 = 0; | ||
| pub const _STDC_PREDEF_H: u32 = 1; | ||
| pub const __STDC_IEC_559__: u32 = 1; | ||
| pub const __STDC_IEC_60559_BFP__: u32 = 201404; | ||
| pub const __STDC_IEC_559_COMPLEX__: u32 = 1; | ||
| pub const __STDC_IEC_60559_COMPLEX__: u32 = 201404; | ||
| pub const __STDC_ISO_10646__: u32 = 201706; | ||
| pub const __GNU_LIBRARY__: u32 = 6; | ||
| pub const __GLIBC__: u32 = 2; | ||
| pub const __GLIBC_MINOR__: u32 = 35; | ||
| pub const _SYS_CDEFS_H: u32 = 1; | ||
| pub const __glibc_c99_flexarr_available: u32 = 1; | ||
| pub const __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI: u32 = 0; | ||
| pub const __HAVE_GENERIC_SELECTION: u32 = 1; | ||
| pub const __GLIBC_USE_LIB_EXT2: u32 = 0; | ||
| pub const __GLIBC_USE_IEC_60559_BFP_EXT: u32 = 0; | ||
| pub const __GLIBC_USE_IEC_60559_BFP_EXT_C2X: u32 = 0; | ||
| pub const __GLIBC_USE_IEC_60559_EXT: u32 = 0; | ||
| pub const __GLIBC_USE_IEC_60559_FUNCS_EXT: u32 = 0; | ||
| pub const __GLIBC_USE_IEC_60559_FUNCS_EXT_C2X: u32 = 0; | ||
| pub const __GLIBC_USE_IEC_60559_TYPES_EXT: u32 = 0; | ||
| pub const _BITS_TYPES_H: u32 = 1; | ||
| pub const _BITS_TYPESIZES_H: u32 = 1; | ||
| pub const __OFF_T_MATCHES_OFF64_T: u32 = 1; | ||
| pub const __INO_T_MATCHES_INO64_T: u32 = 1; | ||
| pub const __RLIM_T_MATCHES_RLIM64_T: u32 = 1; | ||
| pub const __STATFS_MATCHES_STATFS64: u32 = 1; | ||
| pub const __KERNEL_OLD_TIMEVAL_MATCHES_TIMEVAL64: u32 = 1; | ||
| pub const __FD_SETSIZE: u32 = 1024; | ||
| pub const _BITS_TIME64_H: u32 = 1; | ||
| pub const _BITS_WCHAR_H: u32 = 1; | ||
| pub const _BITS_STDINT_INTN_H: u32 = 1; | ||
| pub const _BITS_STDINT_UINTN_H: u32 = 1; | ||
| pub const INT8_MIN: i32 = -128; | ||
| pub const INT16_MIN: i32 = -32768; | ||
| pub const INT32_MIN: i32 = -2147483648; | ||
| pub const INT8_MAX: u32 = 127; | ||
| pub const INT16_MAX: u32 = 32767; | ||
| pub const INT32_MAX: u32 = 2147483647; | ||
| pub const UINT8_MAX: u32 = 255; | ||
| pub const UINT16_MAX: u32 = 65535; | ||
| pub const UINT32_MAX: u32 = 4294967295; | ||
| pub const INT_LEAST8_MIN: i32 = -128; | ||
| pub const INT_LEAST16_MIN: i32 = -32768; | ||
| pub const INT_LEAST32_MIN: i32 = -2147483648; | ||
| pub const INT_LEAST8_MAX: u32 = 127; | ||
| pub const INT_LEAST16_MAX: u32 = 32767; | ||
| pub const INT_LEAST32_MAX: u32 = 2147483647; | ||
| pub const UINT_LEAST8_MAX: u32 = 255; | ||
| pub const UINT_LEAST16_MAX: u32 = 65535; | ||
| pub const UINT_LEAST32_MAX: u32 = 4294967295; | ||
| pub const INT_FAST8_MIN: i32 = -128; | ||
| pub const INT_FAST16_MIN: i64 = -9223372036854775808; | ||
| pub const INT_FAST32_MIN: i64 = -9223372036854775808; | ||
| pub const INT_FAST8_MAX: u32 = 127; | ||
| pub const INT_FAST16_MAX: u64 = 9223372036854775807; | ||
| pub const INT_FAST32_MAX: u64 = 9223372036854775807; | ||
| pub const UINT_FAST8_MAX: u32 = 255; | ||
| pub const UINT_FAST16_MAX: i32 = -1; | ||
| pub const UINT_FAST32_MAX: i32 = -1; | ||
| pub const INTPTR_MIN: i64 = -9223372036854775808; | ||
| pub const INTPTR_MAX: u64 = 9223372036854775807; | ||
| pub const UINTPTR_MAX: i32 = -1; | ||
| pub const PTRDIFF_MIN: i64 = -9223372036854775808; | ||
| pub const PTRDIFF_MAX: u64 = 9223372036854775807; | ||
| pub const SIG_ATOMIC_MIN: i32 = -2147483648; | ||
| pub const SIG_ATOMIC_MAX: u32 = 2147483647; | ||
| pub const SIZE_MAX: i32 = -1; | ||
| pub const WINT_MIN: u32 = 0; | ||
| pub const WINT_MAX: u32 = 4294967295; | ||
| pub const true_: u32 = 1; | ||
| pub const false_: u32 = 0; | ||
| pub const __bool_true_false_are_defined: u32 = 1; | ||
| pub const LLAMA_FILE_VERSION: u32 = 1; | ||
| pub const LLAMA_FILE_MAGIC: u32 = 1734830708; | ||
| pub const LLAMA_FILE_MAGIC_UNVERSIONED: u32 = 1734831468; | ||
| pub type wchar_t = ::std::os::raw::c_int; | ||
| #[repr(C)] | ||
| #[repr(align(16))] | ||
| #[derive(Debug, Copy, Clone)] | ||
| pub struct max_align_t { | ||
| pub __clang_max_align_nonce1: ::std::os::raw::c_longlong, | ||
| pub __bindgen_padding_0: u64, | ||
| pub __clang_max_align_nonce2: u128, | ||
| } | ||
| #[test] | ||
| fn bindgen_test_layout_max_align_t() { | ||
| const UNINIT: ::std::mem::MaybeUninit<max_align_t> = ::std::mem::MaybeUninit::uninit(); | ||
| let ptr = UNINIT.as_ptr(); | ||
| assert_eq!( | ||
| ::std::mem::size_of::<max_align_t>(), | ||
| 32usize, | ||
| concat!("Size of: ", stringify!(max_align_t)) | ||
| ); | ||
| assert_eq!( | ||
| ::std::mem::align_of::<max_align_t>(), | ||
| 16usize, | ||
| concat!("Alignment of ", stringify!(max_align_t)) | ||
| ); | ||
| assert_eq!( | ||
| unsafe { ::std::ptr::addr_of!((*ptr).__clang_max_align_nonce1) as usize - ptr as usize }, | ||
| 0usize, | ||
| concat!( | ||
| "Offset of field: ", | ||
| stringify!(max_align_t), | ||
| "::", | ||
| stringify!(__clang_max_align_nonce1) | ||
| ) | ||
| ); | ||
| assert_eq!( | ||
| unsafe { ::std::ptr::addr_of!((*ptr).__clang_max_align_nonce2) as usize - ptr as usize }, | ||
| 16usize, | ||
| concat!( | ||
| "Offset of field: ", | ||
| stringify!(max_align_t), | ||
| "::", | ||
| stringify!(__clang_max_align_nonce2) | ||
| ) | ||
| ); | ||
| } | ||
| pub type __u_char = ::std::os::raw::c_uchar; | ||
| pub type __u_short = ::std::os::raw::c_ushort; | ||
| pub type __u_int = ::std::os::raw::c_uint; | ||
| pub type __u_long = ::std::os::raw::c_ulong; | ||
| pub type __int8_t = ::std::os::raw::c_schar; | ||
| pub type __uint8_t = ::std::os::raw::c_uchar; | ||
| pub type __int16_t = ::std::os::raw::c_short; | ||
| pub type __uint16_t = ::std::os::raw::c_ushort; | ||
| pub type __int32_t = ::std::os::raw::c_int; | ||
| pub type __uint32_t = ::std::os::raw::c_uint; | ||
| pub type __int64_t = ::std::os::raw::c_long; | ||
| pub type __uint64_t = ::std::os::raw::c_ulong; | ||
| pub type __int_least8_t = __int8_t; | ||
| pub type __uint_least8_t = __uint8_t; | ||
| pub type __int_least16_t = __int16_t; | ||
| pub type __uint_least16_t = __uint16_t; | ||
| pub type __int_least32_t = __int32_t; | ||
| pub type __uint_least32_t = __uint32_t; | ||
| pub type __int_least64_t = __int64_t; | ||
| pub type __uint_least64_t = __uint64_t; | ||
| pub type __quad_t = ::std::os::raw::c_long; | ||
| pub type __u_quad_t = ::std::os::raw::c_ulong; | ||
| pub type __intmax_t = ::std::os::raw::c_long; | ||
| pub type __uintmax_t = ::std::os::raw::c_ulong; | ||
| pub type __dev_t = ::std::os::raw::c_ulong; | ||
| pub type __uid_t = ::std::os::raw::c_uint; | ||
| pub type __gid_t = ::std::os::raw::c_uint; | ||
| pub type __ino_t = ::std::os::raw::c_ulong; | ||
| pub type __ino64_t = ::std::os::raw::c_ulong; | ||
| pub type __mode_t = ::std::os::raw::c_uint; | ||
| pub type __nlink_t = ::std::os::raw::c_ulong; | ||
| pub type __off_t = ::std::os::raw::c_long; | ||
| pub type __off64_t = ::std::os::raw::c_long; | ||
| pub type __pid_t = ::std::os::raw::c_int; | ||
| #[repr(C)] | ||
| #[derive(Debug, Copy, Clone)] | ||
| pub struct __fsid_t { | ||
| pub __val: [::std::os::raw::c_int; 2usize], | ||
| } | ||
| #[test] | ||
| fn bindgen_test_layout___fsid_t() { | ||
| const UNINIT: ::std::mem::MaybeUninit<__fsid_t> = ::std::mem::MaybeUninit::uninit(); | ||
| let ptr = UNINIT.as_ptr(); | ||
| assert_eq!( | ||
| ::std::mem::size_of::<__fsid_t>(), | ||
| 8usize, | ||
| concat!("Size of: ", stringify!(__fsid_t)) | ||
| ); | ||
| assert_eq!( | ||
| ::std::mem::align_of::<__fsid_t>(), | ||
| 4usize, | ||
| concat!("Alignment of ", stringify!(__fsid_t)) | ||
| ); | ||
| assert_eq!( | ||
| unsafe { ::std::ptr::addr_of!((*ptr).__val) as usize - ptr as usize }, | ||
| 0usize, | ||
| concat!( | ||
| "Offset of field: ", | ||
| stringify!(__fsid_t), | ||
| "::", | ||
| stringify!(__val) | ||
| ) | ||
| ); | ||
| } | ||
| pub type __clock_t = ::std::os::raw::c_long; | ||
| pub type __rlim_t = ::std::os::raw::c_ulong; | ||
| pub type __rlim64_t = ::std::os::raw::c_ulong; | ||
| pub type __id_t = ::std::os::raw::c_uint; | ||
| pub type __time_t = ::std::os::raw::c_long; | ||
| pub type __useconds_t = ::std::os::raw::c_uint; | ||
| pub type __suseconds_t = ::std::os::raw::c_long; | ||
| pub type __suseconds64_t = ::std::os::raw::c_long; | ||
| pub type __daddr_t = ::std::os::raw::c_int; | ||
| pub type __key_t = ::std::os::raw::c_int; | ||
| pub type __clockid_t = ::std::os::raw::c_int; | ||
| pub type __timer_t = *mut ::std::os::raw::c_void; | ||
| pub type __blksize_t = ::std::os::raw::c_long; | ||
| pub type __blkcnt_t = ::std::os::raw::c_long; | ||
| pub type __blkcnt64_t = ::std::os::raw::c_long; | ||
| pub type __fsblkcnt_t = ::std::os::raw::c_ulong; | ||
| pub type __fsblkcnt64_t = ::std::os::raw::c_ulong; | ||
| pub type __fsfilcnt_t = ::std::os::raw::c_ulong; | ||
| pub type __fsfilcnt64_t = ::std::os::raw::c_ulong; | ||
| pub type __fsword_t = ::std::os::raw::c_long; | ||
| pub type __ssize_t = ::std::os::raw::c_long; | ||
| pub type __syscall_slong_t = ::std::os::raw::c_long; | ||
| pub type __syscall_ulong_t = ::std::os::raw::c_ulong; | ||
| pub type __loff_t = __off64_t; | ||
| pub type __caddr_t = *mut ::std::os::raw::c_char; | ||
| pub type __intptr_t = ::std::os::raw::c_long; | ||
| pub type __socklen_t = ::std::os::raw::c_uint; | ||
| pub type __sig_atomic_t = ::std::os::raw::c_int; | ||
| pub type int_least8_t = __int_least8_t; | ||
| pub type int_least16_t = __int_least16_t; | ||
| pub type int_least32_t = __int_least32_t; | ||
| pub type int_least64_t = __int_least64_t; | ||
| pub type uint_least8_t = __uint_least8_t; | ||
| pub type uint_least16_t = __uint_least16_t; | ||
| pub type uint_least32_t = __uint_least32_t; | ||
| pub type uint_least64_t = __uint_least64_t; | ||
| pub type int_fast8_t = ::std::os::raw::c_schar; | ||
| pub type int_fast16_t = ::std::os::raw::c_long; | ||
| pub type int_fast32_t = ::std::os::raw::c_long; | ||
| pub type int_fast64_t = ::std::os::raw::c_long; | ||
| pub type uint_fast8_t = ::std::os::raw::c_uchar; | ||
| pub type uint_fast16_t = ::std::os::raw::c_ulong; | ||
| pub type uint_fast32_t = ::std::os::raw::c_ulong; | ||
| pub type uint_fast64_t = ::std::os::raw::c_ulong; | ||
| pub type intmax_t = __intmax_t; | ||
| pub type uintmax_t = __uintmax_t; | ||
| #[repr(C)] | ||
| #[derive(Debug, Copy, Clone)] | ||
| pub struct llama_context { | ||
| _unused: [u8; 0], | ||
| } | ||
| pub type llama_token = ::std::os::raw::c_int; | ||
| #[repr(C)] | ||
| #[derive(Debug, Copy, Clone)] | ||
| pub struct llama_token_data { | ||
| pub id: llama_token, | ||
| pub p: f32, | ||
| pub plog: f32, | ||
| } | ||
| #[test] | ||
| fn bindgen_test_layout_llama_token_data() { | ||
| const UNINIT: ::std::mem::MaybeUninit<llama_token_data> = ::std::mem::MaybeUninit::uninit(); | ||
| let ptr = UNINIT.as_ptr(); | ||
| assert_eq!( | ||
| ::std::mem::size_of::<llama_token_data>(), | ||
| 12usize, | ||
| concat!("Size of: ", stringify!(llama_token_data)) | ||
| ); | ||
| assert_eq!( | ||
| ::std::mem::align_of::<llama_token_data>(), | ||
| 4usize, | ||
| concat!("Alignment of ", stringify!(llama_token_data)) | ||
| ); | ||
| assert_eq!( | ||
| unsafe { ::std::ptr::addr_of!((*ptr).id) as usize - ptr as usize }, | ||
| 0usize, | ||
| concat!( | ||
| "Offset of field: ", | ||
| stringify!(llama_token_data), | ||
| "::", | ||
| stringify!(id) | ||
| ) | ||
| ); | ||
| assert_eq!( | ||
| unsafe { ::std::ptr::addr_of!((*ptr).p) as usize - ptr as usize }, | ||
| 4usize, | ||
| concat!( | ||
| "Offset of field: ", | ||
| stringify!(llama_token_data), | ||
| "::", | ||
| stringify!(p) | ||
| ) | ||
| ); | ||
| assert_eq!( | ||
| unsafe { ::std::ptr::addr_of!((*ptr).plog) as usize - ptr as usize }, | ||
| 8usize, | ||
| concat!( | ||
| "Offset of field: ", | ||
| stringify!(llama_token_data), | ||
| "::", | ||
| stringify!(plog) | ||
| ) | ||
| ); | ||
| } | ||
| pub type llama_progress_callback = | ||
| ::std::option::Option<unsafe extern "C" fn(progress: f32, ctx: *mut ::std::os::raw::c_void)>; | ||
| #[repr(C)] | ||
| #[derive(Debug, Copy, Clone)] | ||
| pub struct llama_context_params { | ||
| pub n_ctx: ::std::os::raw::c_int, | ||
| pub n_parts: ::std::os::raw::c_int, | ||
| pub seed: ::std::os::raw::c_int, | ||
| pub f16_kv: bool, | ||
| pub logits_all: bool, | ||
| pub vocab_only: bool, | ||
| pub use_mmap: bool, | ||
| pub use_mlock: bool, | ||
| pub embedding: bool, | ||
| pub progress_callback: llama_progress_callback, | ||
| pub progress_callback_user_data: *mut ::std::os::raw::c_void, | ||
| } | ||
| #[test] | ||
| fn bindgen_test_layout_llama_context_params() { | ||
| const UNINIT: ::std::mem::MaybeUninit<llama_context_params> = ::std::mem::MaybeUninit::uninit(); | ||
| let ptr = UNINIT.as_ptr(); | ||
| assert_eq!( | ||
| ::std::mem::size_of::<llama_context_params>(), | ||
| 40usize, | ||
| concat!("Size of: ", stringify!(llama_context_params)) | ||
| ); | ||
| assert_eq!( | ||
| ::std::mem::align_of::<llama_context_params>(), | ||
| 8usize, | ||
| concat!("Alignment of ", stringify!(llama_context_params)) | ||
| ); | ||
| assert_eq!( | ||
| unsafe { ::std::ptr::addr_of!((*ptr).n_ctx) as usize - ptr as usize }, | ||
| 0usize, | ||
| concat!( | ||
| "Offset of field: ", | ||
| stringify!(llama_context_params), | ||
| "::", | ||
| stringify!(n_ctx) | ||
| ) | ||
| ); | ||
| assert_eq!( | ||
| unsafe { ::std::ptr::addr_of!((*ptr).n_parts) as usize - ptr as usize }, | ||
| 4usize, | ||
| concat!( | ||
| "Offset of field: ", | ||
| stringify!(llama_context_params), | ||
| "::", | ||
| stringify!(n_parts) | ||
| ) | ||
| ); | ||
| assert_eq!( | ||
| unsafe { ::std::ptr::addr_of!((*ptr).seed) as usize - ptr as usize }, | ||
| 8usize, | ||
| concat!( | ||
| "Offset of field: ", | ||
| stringify!(llama_context_params), | ||
| "::", | ||
| stringify!(seed) | ||
| ) | ||
| ); | ||
| assert_eq!( | ||
| unsafe { ::std::ptr::addr_of!((*ptr).f16_kv) as usize - ptr as usize }, | ||
| 12usize, | ||
| concat!( | ||
| "Offset of field: ", | ||
| stringify!(llama_context_params), | ||
| "::", | ||
| stringify!(f16_kv) | ||
| ) | ||
| ); | ||
| assert_eq!( | ||
| unsafe { ::std::ptr::addr_of!((*ptr).logits_all) as usize - ptr as usize }, | ||
| 13usize, | ||
| concat!( | ||
| "Offset of field: ", | ||
| stringify!(llama_context_params), | ||
| "::", | ||
| stringify!(logits_all) | ||
| ) | ||
| ); | ||
| assert_eq!( | ||
| unsafe { ::std::ptr::addr_of!((*ptr).vocab_only) as usize - ptr as usize }, | ||
| 14usize, | ||
| concat!( | ||
| "Offset of field: ", | ||
| stringify!(llama_context_params), | ||
| "::", | ||
| stringify!(vocab_only) | ||
| ) | ||
| ); | ||
| assert_eq!( | ||
| unsafe { ::std::ptr::addr_of!((*ptr).use_mmap) as usize - ptr as usize }, | ||
| 15usize, | ||
| concat!( | ||
| "Offset of field: ", | ||
| stringify!(llama_context_params), | ||
| "::", | ||
| stringify!(use_mmap) | ||
| ) | ||
| ); | ||
| assert_eq!( | ||
| unsafe { ::std::ptr::addr_of!((*ptr).use_mlock) as usize - ptr as usize }, | ||
| 16usize, | ||
| concat!( | ||
| "Offset of field: ", | ||
| stringify!(llama_context_params), | ||
| "::", | ||
| stringify!(use_mlock) | ||
| ) | ||
| ); | ||
| assert_eq!( | ||
| unsafe { ::std::ptr::addr_of!((*ptr).embedding) as usize - ptr as usize }, | ||
| 17usize, | ||
| concat!( | ||
| "Offset of field: ", | ||
| stringify!(llama_context_params), | ||
| "::", | ||
| stringify!(embedding) | ||
| ) | ||
| ); | ||
| assert_eq!( | ||
| unsafe { ::std::ptr::addr_of!((*ptr).progress_callback) as usize - ptr as usize }, | ||
| 24usize, | ||
| concat!( | ||
| "Offset of field: ", | ||
| stringify!(llama_context_params), | ||
| "::", | ||
| stringify!(progress_callback) | ||
| ) | ||
| ); | ||
| assert_eq!( | ||
| unsafe { ::std::ptr::addr_of!((*ptr).progress_callback_user_data) as usize - ptr as usize }, | ||
| 32usize, | ||
| concat!( | ||
| "Offset of field: ", | ||
| stringify!(llama_context_params), | ||
| "::", | ||
| stringify!(progress_callback_user_data) | ||
| ) | ||
| ); | ||
| } | ||
| pub const llama_ftype_LLAMA_FTYPE_ALL_F32: llama_ftype = 0; | ||
| pub const llama_ftype_LLAMA_FTYPE_MOSTLY_F16: llama_ftype = 1; | ||
| pub const llama_ftype_LLAMA_FTYPE_MOSTLY_Q4_0: llama_ftype = 2; | ||
| pub const llama_ftype_LLAMA_FTYPE_MOSTLY_Q4_1: llama_ftype = 3; | ||
| pub const llama_ftype_LLAMA_FTYPE_MOSTLY_Q4_1_SOME_F16: llama_ftype = 4; | ||
| pub type llama_ftype = ::std::os::raw::c_uint; | ||
| extern "C" { | ||
| pub fn llama_context_default_params() -> llama_context_params; | ||
| } | ||
| extern "C" { | ||
| pub fn llama_mmap_supported() -> bool; | ||
| } | ||
| extern "C" { | ||
| pub fn llama_mlock_supported() -> bool; | ||
| } | ||
| extern "C" { | ||
| pub fn llama_init_from_file( | ||
| path_model: *const ::std::os::raw::c_char, | ||
| params: llama_context_params, | ||
| ) -> *mut llama_context; | ||
| } | ||
| extern "C" { | ||
| pub fn llama_free(ctx: *mut llama_context); | ||
| } | ||
| extern "C" { | ||
| pub fn llama_model_quantize( | ||
| fname_inp: *const ::std::os::raw::c_char, | ||
| fname_out: *const ::std::os::raw::c_char, | ||
| ftype: llama_ftype, | ||
| ) -> ::std::os::raw::c_int; | ||
| } | ||
| extern "C" { | ||
| pub fn llama_get_kv_cache(ctx: *mut llama_context) -> *const u8; | ||
| } | ||
| extern "C" { | ||
| pub fn llama_get_kv_cache_size(ctx: *mut llama_context) -> usize; | ||
| } | ||
| extern "C" { | ||
| pub fn llama_get_kv_cache_token_count(ctx: *mut llama_context) -> ::std::os::raw::c_int; | ||
| } | ||
| extern "C" { | ||
| pub fn llama_set_kv_cache( | ||
| ctx: *mut llama_context, | ||
| kv_cache: *const u8, | ||
| n_size: usize, | ||
| n_token_count: ::std::os::raw::c_int, | ||
| ); | ||
| } | ||
| extern "C" { | ||
| pub fn llama_eval( | ||
| ctx: *mut llama_context, | ||
| tokens: *const llama_token, | ||
| n_tokens: ::std::os::raw::c_int, | ||
| n_past: ::std::os::raw::c_int, | ||
| n_threads: ::std::os::raw::c_int, | ||
| ) -> ::std::os::raw::c_int; | ||
| } | ||
| extern "C" { | ||
| pub fn llama_tokenize( | ||
| ctx: *mut llama_context, | ||
| text: *const ::std::os::raw::c_char, | ||
| tokens: *mut llama_token, | ||
| n_max_tokens: ::std::os::raw::c_int, | ||
| add_bos: bool, | ||
| ) -> ::std::os::raw::c_int; | ||
| } | ||
| extern "C" { | ||
| pub fn llama_n_vocab(ctx: *mut llama_context) -> ::std::os::raw::c_int; | ||
| } | ||
| extern "C" { | ||
| pub fn llama_n_ctx(ctx: *mut llama_context) -> ::std::os::raw::c_int; | ||
| } | ||
| extern "C" { | ||
| pub fn llama_n_embd(ctx: *mut llama_context) -> ::std::os::raw::c_int; | ||
| } | ||
| extern "C" { | ||
| pub fn llama_get_logits(ctx: *mut llama_context) -> *mut f32; | ||
| } | ||
| extern "C" { | ||
| pub fn llama_get_embeddings(ctx: *mut llama_context) -> *mut f32; | ||
| } | ||
| extern "C" { | ||
| pub fn llama_token_to_str( | ||
| ctx: *mut llama_context, | ||
| token: llama_token, | ||
| ) -> *const ::std::os::raw::c_char; | ||
| } | ||
| extern "C" { | ||
| pub fn llama_token_bos() -> llama_token; | ||
| } | ||
| extern "C" { | ||
| pub fn llama_token_eos() -> llama_token; | ||
| } | ||
| extern "C" { | ||
| pub fn llama_sample_top_p_top_k( | ||
| ctx: *mut llama_context, | ||
| last_n_tokens_data: *const llama_token, | ||
| last_n_tokens_size: ::std::os::raw::c_int, | ||
| top_k: ::std::os::raw::c_int, | ||
| top_p: f32, | ||
| temp: f32, | ||
| repeat_penalty: f32, | ||
| ) -> llama_token; | ||
| } | ||
| extern "C" { | ||
| pub fn llama_print_timings(ctx: *mut llama_context); | ||
| } | ||
| extern "C" { | ||
| pub fn llama_reset_timings(ctx: *mut llama_context); | ||
| } | ||
| extern "C" { | ||
| pub fn llama_print_system_info() -> *const ::std::os::raw::c_char; | ||
| } |
| #![allow(non_upper_case_globals)] | ||
| #![allow(non_camel_case_types)] | ||
| #![allow(non_snake_case)] | ||
| include!(concat!(env!("OUT_DIR"), "/bindings.rs")); |
| #include <llama.h> |
+1
-1
@@ -19,3 +19,3 @@ [package] | ||
| rand = "0.8.5" | ||
| llm-chain-llama-sys = { git = "https://github.com/hlhr202/llm-chain.git", branch = "feature/fix-cross-compile" } | ||
| llama-sys = { path = "./llama-sys" } | ||
| # llm-chain-llama-sys = { path = "../../../llm-chain/llm-chain-llama/sys" } | ||
@@ -22,0 +22,0 @@ napi = { version = "2.12.2", default-features = false, features = ["napi6", "async"] } |
@@ -15,2 +15,3 @@ import { LLama, LlamaContextParams, LlamaInvocation } from "../index"; | ||
| embedding: true, | ||
| useMmap: true, | ||
| }, | ||
@@ -17,0 +18,0 @@ false |
+1
-0
@@ -25,2 +25,3 @@ /* tslint:disable */ | ||
| embedding: boolean | ||
| useMmap: boolean | ||
| } | ||
@@ -27,0 +28,0 @@ export const enum TokenizeResultType { |
+1
-1
| { | ||
| "name": "@llama-node/llama-cpp", | ||
| "version": "0.0.26", | ||
| "version": "0.0.27", | ||
| "main": "index.js", | ||
@@ -5,0 +5,0 @@ "types": "index.d.ts", |
+3
-3
| use std::{ffi::CStr, ptr::null_mut, slice}; | ||
| use anyhow::Result; | ||
| use llm_chain_llama_sys::{ | ||
| use llama_sys::{ | ||
| llama_context, llama_context_default_params, llama_context_params, llama_eval, llama_free, | ||
@@ -35,3 +35,3 @@ llama_get_embeddings, llama_init_from_file, llama_n_embd, llama_print_system_info, | ||
| pub embedding: bool, | ||
| // pub use_mmap: bool, | ||
| pub use_mmap: bool, | ||
| } | ||
@@ -62,3 +62,3 @@ | ||
| progress_callback_user_data: null_mut(), | ||
| // use_mmap: params.use_mmap, | ||
| use_mmap: params.use_mmap, | ||
| } | ||
@@ -65,0 +65,0 @@ } |
+1
-1
@@ -6,3 +6,3 @@ use crate::output::Output; | ||
| use llm_chain_llama_sys::{ | ||
| use llama_sys::{ | ||
| llama_token, llama_token_eos as inner_eos, llama_token_to_str, llama_tokenize, | ||
@@ -9,0 +9,0 @@ }; |
+2
-1
@@ -108,4 +108,5 @@ { | ||
| "skipLibCheck": true /* Skip type checking all .d.ts files. */ | ||
| } | ||
| }, | ||
| "exclude": ["llama-sys"] | ||
| } | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
26
23.81%505
0.6%14802805
-1.14%