diff --git a/.gitmodules b/.gitmodules index 4a7cb543..12466c24 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "ggml/ggml"] - path = ggml/ggml + path = ggml/sys/ggml url = git@github.com:ggerganov/ggml.git diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c6e025a5..a1bae158 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -9,12 +9,13 @@ When new GGML versions are pushed to llama.cpp (or one of the other repos hosting a copy of it) and we want to update our copy, the process should be as follows: -- Update the `ggml.c` and `ggml.h` inside `ggml-sys/ggml`. -- In that same folder, update `CREDITS.txt` to indicate the llama.cpp version - these files were taken from +- Update the submodule to the latest version of GGML: + ```shell + $ git submodule update --remote + ``` - Run the bindgen script: - ```shell - $ cargo run --bin generate-ggml-bindings ggml-sys - ``` + ```shell + $ cargo run --bin generate-ggml-bindings ggml-sys + ``` - Fix any compiler errors that pop up due to the new version of the bindings and test the changes. diff --git a/Cargo.lock b/Cargo.lock index 33d0b49d..55a64934 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -104,9 +104,9 @@ dependencies = [ [[package]] name = "bindgen" -version = "0.64.0" +version = "0.65.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4243e6031260db77ede97ad86c27e501d646a27ab57b59a574f725d98ab1fb4" +checksum = "cfdf7b466f9a4903edc73f95d6d2bcd5baf8ae620638762244d3f60143643cc5" dependencies = [ "bitflags", "cexpr", @@ -115,12 +115,13 @@ dependencies = [ "lazycell", "log", "peeking_take_while", + "prettyplease", "proc-macro2", "quote", "regex", "rustc-hash", "shlex", - "syn 1.0.109", + "syn 2.0.13", "which", ] @@ -430,6 +431,13 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "generate-ggml-bindings" +version = "0.1.0" +dependencies = [ + "bindgen", +] + [[package]] name = "getrandom" version = "0.2.9" @@ -445,12 +453,18 @@ dependencies = [ name = "ggml" version = "0.1.0" dependencies = [ - "bindgen", - "cc", + "ggml-sys", "rand", "thiserror", ] +[[package]] +name = "ggml-sys" +version = "0.1.0" +dependencies = [ + "cc", +] + [[package]] name = "gimli" version = "0.27.2" @@ -809,6 +823,16 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "prettyplease" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ceca8aaf45b5c46ec7ed39fff75f57290368c1846d33d24a122ca81416ab058" +dependencies = [ + "proc-macro2", + "syn 2.0.13", +] + [[package]] name = "proc-macro2" version = "1.0.56" diff --git a/Cargo.toml b/Cargo.toml index bf235bbc..5d7d42ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,11 +3,13 @@ members = [ # Crates "bloom", "ggml", + "ggml/sys", "gpt2", "llama", "llm", "llm-base", "llm-cli", + "tools/*" ] resolver = "2" diff --git a/ggml/Cargo.toml b/ggml/Cargo.toml index 43d64758..255e3a7a 100644 --- a/ggml/Cargo.toml +++ b/ggml/Cargo.toml @@ -3,12 +3,9 @@ name = "ggml" version = { workspace = true } edition = "2021" -[build-dependencies] -bindgen = "0.64.0" -cc = "^1.0" - [dependencies] thiserror = "1.0" +ggml-sys = { path = "sys" } [dev-dependencies] -rand = "0.8" +rand = "0.8" \ No newline at end of file diff --git a/ggml/ggml b/ggml/ggml deleted file mode 160000 index 583c5a3a..00000000 --- a/ggml/ggml +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 583c5a3ad6bdb041bff5ad161a49ff4d8fa52f10 diff --git a/ggml/src/context.rs b/ggml/src/context.rs index 9aff0e38..c4ac5d68 100644 --- a/ggml/src/context.rs +++ b/ggml/src/context.rs @@ -1,26 +1,26 @@ use std::{ os::raw::{c_int, c_void}, ptr::NonNull, - sync::{Arc, Weak}, + sync::Arc, }; -use crate::{usize_to_i32, usize_to_i64, Buffer, ComputationGraph, Tensor, Type}; +use crate::{sys, usize_to_i32, usize_to_i64, Buffer, ComputationGraph, Tensor, Type}; -/// Acts as a RAII-guard over a `crate::ggml_context`, allocating via +/// Acts as a RAII-guard over a `sys::ggml_context`, allocating via /// `ggml_init` and dropping via `ggml_free`. pub struct Context { /// An `Arc` is used to model the relation between the context and the /// allocated tensors. Tensors are owned by the object, so a [`Tensor`] /// contains a `Weak` reference underneath and doesn't let you do anything /// with it if the underlying context has been deallocated. - ptr: Arc>, + ptr: Arc>, } impl Context { /// Creates a new [Context] with the specified `mem_size` as a working area. pub fn init(mem_size: usize, alloc: bool) -> Self { let raw = unsafe { - crate::ggml_init(crate::ggml_init_params { + sys::ggml_init(sys::ggml_init_params { mem_size, // Null here means we want ggml to own this memory. We don't // support passing an owned buffer from the Rust side. @@ -34,7 +34,7 @@ impl Context { } /// Wraps a raw tensor with a weak pointer to the context. - fn new_tensor_raw(&self, raw: *mut crate::ggml_tensor) -> Tensor { + fn new_tensor_raw(&self, raw: *mut sys::ggml_tensor) -> Tensor { Tensor { ptr: NonNull::new(raw).expect("Should not be null"), ctx: Arc::downgrade(&self.ptr), @@ -44,14 +44,14 @@ impl Context { /// Creates a new 1D tensor. pub fn new_tensor_1d(&self, typ: Type, ne0: usize) -> Tensor { let raw = - unsafe { crate::ggml_new_tensor_1d(self.ptr.as_ptr(), typ.into(), usize_to_i64(ne0)) }; + unsafe { sys::ggml_new_tensor_1d(self.ptr.as_ptr(), typ.into(), usize_to_i64(ne0)) }; self.new_tensor_raw(raw) } /// Creates a new 2D tensor. pub fn new_tensor_2d(&self, typ: Type, ne0: usize, ne1: usize) -> Tensor { let raw = unsafe { - crate::ggml_new_tensor_2d( + sys::ggml_new_tensor_2d( self.ptr.as_ptr(), typ.into(), usize_to_i64(ne0), @@ -64,7 +64,7 @@ impl Context { /// Creates a new 3D tensor. pub fn new_tensor_3d(&self, typ: Type, ne0: usize, ne1: usize, ne2: usize) -> Tensor { let raw = unsafe { - crate::ggml_new_tensor_3d( + sys::ggml_new_tensor_3d( self.ptr.as_ptr(), typ.into(), usize_to_i64(ne0), @@ -77,45 +77,44 @@ impl Context { /// Creates a new 1D tensor with the specified value. pub fn new_f32(&self, x: f32) -> Tensor { - let raw = unsafe { crate::ggml_new_f32(self.ptr.as_ptr(), x) }; + let raw = unsafe { sys::ggml_new_f32(self.ptr.as_ptr(), x) }; self.new_tensor_raw(raw) } /// Unknown, aside from the obvious. It's transposing something! pub fn op_transpose(&self, a: &Tensor) -> Tensor { - let tensor = unsafe { crate::ggml_transpose(self.ptr.as_ptr(), a.ptr.as_ptr()) }; + let tensor = unsafe { sys::ggml_transpose(self.ptr.as_ptr(), a.ptr.as_ptr()) }; self.new_tensor_raw(tensor) } /// Unknown. pub fn op_get_rows(&self, a: &Tensor, b: &Tensor) -> Tensor { let tensor = - unsafe { crate::ggml_get_rows(self.ptr.as_ptr(), a.ptr.as_ptr(), b.ptr.as_ptr()) }; + unsafe { sys::ggml_get_rows(self.ptr.as_ptr(), a.ptr.as_ptr(), b.ptr.as_ptr()) }; self.new_tensor_raw(tensor) } /// Creates a new tensor with the values of `a`, but normalized. pub fn op_norm(&self, a: &Tensor) -> Tensor { - let tensor = unsafe { crate::ggml_norm(self.ptr.as_ptr(), a.ptr.as_ptr()) }; + let tensor = unsafe { sys::ggml_norm(self.ptr.as_ptr(), a.ptr.as_ptr()) }; self.new_tensor_raw(tensor) } /// Creates a new tensor with the values of `a`, but normalized using RMSNorm. pub fn op_rms_norm(&self, a: &Tensor) -> Tensor { - let tensor = unsafe { crate::ggml_rms_norm(self.ptr.as_ptr(), a.ptr.as_ptr()) }; + let tensor = unsafe { sys::ggml_rms_norm(self.ptr.as_ptr(), a.ptr.as_ptr()) }; self.new_tensor_raw(tensor) } /// Creates a new tensor with the multiplication of `a` and `b`. pub fn op_mul(&self, a: &Tensor, b: &Tensor) -> Tensor { - let tensor = unsafe { crate::ggml_mul(self.ptr.as_ptr(), a.ptr.as_ptr(), b.ptr.as_ptr()) }; + let tensor = unsafe { sys::ggml_mul(self.ptr.as_ptr(), a.ptr.as_ptr(), b.ptr.as_ptr()) }; self.new_tensor_raw(tensor) } /// Unknown. pub fn op_repeat(&self, a: &Tensor, b: &Tensor) -> Tensor { - let tensor = - unsafe { crate::ggml_repeat(self.ptr.as_ptr(), a.ptr.as_ptr(), b.ptr.as_ptr()) }; + let tensor = unsafe { sys::ggml_repeat(self.ptr.as_ptr(), a.ptr.as_ptr(), b.ptr.as_ptr()) }; self.new_tensor_raw(tensor) } @@ -128,40 +127,39 @@ impl Context { /// Result is m columns, p rows pub fn op_mul_mat(&self, a: &Tensor, b: &Tensor) -> Tensor { let tensor = - unsafe { crate::ggml_mul_mat(self.ptr.as_ptr(), a.ptr.as_ptr(), b.ptr.as_ptr()) }; + unsafe { sys::ggml_mul_mat(self.ptr.as_ptr(), a.ptr.as_ptr(), b.ptr.as_ptr()) }; self.new_tensor_raw(tensor) } /// Creates a new tensor with the addition of `a` and `b`. pub fn op_add(&self, a: &Tensor, b: &Tensor) -> Tensor { - let tensor = unsafe { crate::ggml_add(self.ptr.as_ptr(), a.ptr.as_ptr(), b.ptr.as_ptr()) }; + let tensor = unsafe { sys::ggml_add(self.ptr.as_ptr(), a.ptr.as_ptr(), b.ptr.as_ptr()) }; self.new_tensor_raw(tensor) } /// Creates a new tensor with the [SiLU](https://pytorch.org/docs/stable/generated/torch.nn.SiLU.html) activation function applied to `a`. pub fn op_silu(&self, a: &Tensor) -> Tensor { - let tensor = unsafe { crate::ggml_silu(self.ptr.as_ptr(), a.ptr.as_ptr()) }; + let tensor = unsafe { sys::ggml_silu(self.ptr.as_ptr(), a.ptr.as_ptr()) }; self.new_tensor_raw(tensor) } /// In-place, scales `a` by the 1D tensor `b`. pub fn op_scale(&self, a: &Tensor, b: &Tensor) -> Tensor { - let tensor = - unsafe { crate::ggml_scale(self.ptr.as_ptr(), a.ptr.as_ptr(), b.ptr.as_ptr()) }; + let tensor = unsafe { sys::ggml_scale(self.ptr.as_ptr(), a.ptr.as_ptr(), b.ptr.as_ptr()) }; self.new_tensor_raw(tensor) } /// In-place, sets the elements above the diagonal to -INF. pub fn op_diag_mask_inf(&self, a: &Tensor, n_past: usize) -> Tensor { let tensor = unsafe { - crate::ggml_diag_mask_inf(self.ptr.as_ptr(), a.ptr.as_ptr(), usize_to_i32(n_past)) + sys::ggml_diag_mask_inf(self.ptr.as_ptr(), a.ptr.as_ptr(), usize_to_i32(n_past)) }; self.new_tensor_raw(tensor) } /// In-place, applies the [Softmax function](https://en.wikipedia.org/wiki/Softmax_function) to `a`. pub fn op_soft_max(&self, a: &Tensor) -> Tensor { - let tensor = unsafe { crate::ggml_soft_max(self.ptr.as_ptr(), a.ptr.as_ptr()) }; + let tensor = unsafe { sys::ggml_soft_max(self.ptr.as_ptr(), a.ptr.as_ptr()) }; self.new_tensor_raw(tensor) } @@ -186,7 +184,7 @@ impl Context { fun: unsafe extern "C" fn(cnt: c_int, dst: *mut f32, src: *const f32), ) -> Tensor { let tensor = - unsafe { crate::ggml_map_unary_f32(self.ptr.as_ptr(), a.ptr.as_ptr(), Some(fun)) }; + unsafe { sys::ggml_map_unary_f32(self.ptr.as_ptr(), a.ptr.as_ptr(), Some(fun)) }; self.new_tensor_raw(tensor) } @@ -212,7 +210,7 @@ impl Context { fun: unsafe extern "C" fn(cnt: c_int, dst: *mut f32, src0: *const f32, src1: *const f32), ) -> Tensor { let tensor = unsafe { - crate::ggml_map_binary_f32(self.ptr.as_ptr(), a.ptr.as_ptr(), b.ptr.as_ptr(), Some(fun)) + sys::ggml_map_binary_f32(self.ptr.as_ptr(), a.ptr.as_ptr(), b.ptr.as_ptr(), Some(fun)) }; self.new_tensor_raw(tensor) } @@ -220,7 +218,7 @@ impl Context { /// Creates a 1D view over `a`. pub fn op_view_1d(&self, a: &Tensor, ne0: usize, offset: usize) -> Tensor { let tensor = unsafe { - crate::ggml_view_1d(self.ptr.as_ptr(), a.ptr.as_ptr(), usize_to_i64(ne0), offset) + sys::ggml_view_1d(self.ptr.as_ptr(), a.ptr.as_ptr(), usize_to_i64(ne0), offset) }; self.new_tensor_raw(tensor) } @@ -229,7 +227,7 @@ impl Context { pub fn op_view_2d(&self, a: &Tensor, ne: (usize, usize), nb1: usize, offset: usize) -> Tensor { let (ne0, ne1) = ne; let tensor = unsafe { - crate::ggml_view_2d( + sys::ggml_view_2d( self.ptr.as_ptr(), a.ptr.as_ptr(), usize_to_i64(ne0), @@ -252,7 +250,7 @@ impl Context { let (ne0, ne1, ne2) = ne; let (nb1, nb2) = nb; let tensor = unsafe { - crate::ggml_view_3d( + sys::ggml_view_3d( self.ptr.as_ptr(), a.ptr.as_ptr(), usize_to_i64(ne0), @@ -268,7 +266,7 @@ impl Context { /// Copies `a` to `b` and returns `b`. pub fn op_cpy(&self, a: &Tensor, b: &Tensor) -> Tensor { - let tensor = unsafe { crate::ggml_cpy(self.ptr.as_ptr(), a.ptr.as_ptr(), b.ptr.as_ptr()) }; + let tensor = unsafe { sys::ggml_cpy(self.ptr.as_ptr(), a.ptr.as_ptr(), b.ptr.as_ptr()) }; self.new_tensor_raw(tensor) } @@ -282,7 +280,7 @@ impl Context { axis3: usize, ) -> Tensor { let tensor = unsafe { - crate::ggml_permute( + sys::ggml_permute( self.ptr.as_ptr(), a.ptr.as_ptr(), usize_to_i32(axis0), @@ -297,14 +295,14 @@ impl Context { /// In-place; reshapes `a` in accordance with the dimensions of `b` pub fn op_reshape(&self, a: &Tensor, b: &Tensor) -> Tensor { let tensor = - unsafe { crate::ggml_reshape(self.ptr.as_ptr(), a.ptr.as_ptr(), b.ptr.as_ptr()) }; + unsafe { sys::ggml_reshape(self.ptr.as_ptr(), a.ptr.as_ptr(), b.ptr.as_ptr()) }; self.new_tensor_raw(tensor) } /// In-place; reshapes `a` in accordance with the specified dimensions. pub fn op_reshape_2d(&self, a: &Tensor, ne0: usize, ne1: usize) -> Tensor { let tensor = unsafe { - crate::ggml_reshape_2d( + sys::ggml_reshape_2d( self.ptr.as_ptr(), a.ptr.as_ptr(), usize_to_i64(ne0), @@ -317,7 +315,7 @@ impl Context { /// In-place; reshapes `a` in accordance with the specified dimensions. pub fn op_reshape_3d(&self, a: &Tensor, ne0: usize, ne1: usize, ne2: usize) -> Tensor { let tensor = unsafe { - crate::ggml_reshape_3d( + sys::ggml_reshape_3d( self.ptr.as_ptr(), a.ptr.as_ptr(), usize_to_i64(ne0), @@ -331,7 +329,7 @@ impl Context { /// In-place; applies ROtary Positional Encoding. pub fn op_rope(&self, a: &Tensor, npast: usize, ndims: usize, mode: i32) -> Tensor { let tensor = unsafe { - crate::ggml_rope( + sys::ggml_rope( self.ptr.as_ptr(), a.ptr.as_ptr(), usize_to_i32(npast), @@ -345,13 +343,13 @@ impl Context { /// Computes the specified graph. Must be run in order to evaluate the graph. pub fn graph_compute(&self, graph: &mut ComputationGraph) { unsafe { - crate::ggml_graph_compute(self.ptr.as_ptr(), &mut graph.inner); + sys::ggml_graph_compute(self.ptr.as_ptr(), &mut graph.inner); } } /// Retrieves the memory used by this [Context]. pub fn used_mem(&self) -> usize { - unsafe { crate::ggml_used_mem(self.ptr.as_ptr()) } + unsafe { sys::ggml_used_mem(self.ptr.as_ptr()) } } /// Sets the scratch buffer to be used by this [Context]. @@ -365,9 +363,9 @@ impl Context { }; // SAFETY: this just passes (most likely uninitialized) memory buffer to the ggml C API unsafe { - crate::ggml_set_scratch( + sys::ggml_set_scratch( self.ptr.as_ptr(), - crate::ggml_scratch { + sys::ggml_scratch { offs: 0, size, data, @@ -379,7 +377,7 @@ impl Context { /// TODO: something something pub fn op_alibi(&self, a: &Tensor, n_past: usize, n_head: usize) -> Tensor { let tensor = unsafe { - crate::ggml_alibi( + sys::ggml_alibi( self.ptr.as_ptr(), a.ptr.as_ptr(), usize_to_i32(n_past), @@ -392,7 +390,7 @@ impl Context { /// Gaussian Error Linear Units pub fn op_gelu(&self, a: &Tensor) -> Tensor { - let tensor = unsafe { crate::ggml_gelu(self.ptr.as_ptr(), a.ptr.as_ptr()) }; + let tensor = unsafe { sys::ggml_gelu(self.ptr.as_ptr(), a.ptr.as_ptr()) }; self.new_tensor_raw(tensor) } } @@ -402,7 +400,7 @@ impl Drop for Context { // SAFETY: The only non-weak copy of ptr is no longer accessible after // this drop call. unsafe { - crate::ggml_free(self.ptr.as_ptr()); + sys::ggml_free(self.ptr.as_ptr()); } } } diff --git a/ggml/src/lib.rs b/ggml/src/lib.rs index 5ba74df9..09ae4b95 100644 --- a/ggml/src/lib.rs +++ b/ggml/src/lib.rs @@ -1,8 +1,3 @@ -#![allow(non_upper_case_globals)] -#![allow(non_camel_case_types)] -#![allow(non_snake_case)] -#![allow(unused)] - //! `ggml` is a semi-idiomatic wrapper for the `ggml` C library. //! //! It exposes a subset of operations (currently used to implement the [llama-rs](https://crates.io/crates/llama-rs) library). @@ -11,13 +6,7 @@ //! `ggml` operates on a computational graph; no values will be computed until [Context::graph_compute] is executed. //! All [Tensor]s are nodes in this computational graph, and values cannot be retrieved until computation is completed. -use std::{ - os::raw::{c_int, c_void}, - ptr::NonNull, - sync::{Arc, Weak}, -}; - -include!(concat!(env!("OUT_DIR"), "/bindings.rs")); +use std::os::raw::{c_int, c_void}; pub use tensor::Tensor; @@ -31,6 +20,8 @@ pub mod saver; pub mod context; mod tensor; +pub(crate) use ggml_sys as sys; + #[cfg(test)] mod tests; @@ -69,7 +60,7 @@ pub const FILE_MAGIC_UNVERSIONED: u32 = 0x67676d6c; pub const FORMAT_VERSION: u32 = 1; /// The size of a `ggml` object. -pub const OBJECT_SIZE: usize = crate::GGML_OBJECT_SIZE; +pub const OBJECT_SIZE: usize = sys::GGML_OBJECT_SIZE; #[derive(Debug, Copy, Clone, PartialEq, Eq, Default)] /// The type of a value in `ggml`. @@ -81,10 +72,14 @@ pub enum Type { Q4_1, /// Quantized 4-bit (type 2). Q4_2, - /// Quantized 4-bit (type 3). - Q4_3, + /// Quantized 5-bit (type 0). + Q5_0, + /// Quantized 5-bit (type 1). + Q5_1, /// Quantized 8-bit (type 0). Q8_0, + /// Quantized 8-bit (type 1). + Q8_1, /// Integer 32-bit. I32, /// Float 16-bit. @@ -92,32 +87,36 @@ pub enum Type { /// Float 32-bit. F32, } -impl From for crate::ggml_type { +impl From for sys::ggml_type { fn from(t: Type) -> Self { match t { - Type::Q4_0 => crate::ggml_type_GGML_TYPE_Q4_0, - Type::Q4_1 => crate::ggml_type_GGML_TYPE_Q4_1, - Type::Q4_2 => crate::ggml_type_GGML_TYPE_Q4_2, - Type::Q4_3 => crate::ggml_type_GGML_TYPE_Q4_3, - Type::Q8_0 => crate::ggml_type_GGML_TYPE_Q8_0, - Type::I32 => crate::ggml_type_GGML_TYPE_I32, - Type::F16 => crate::ggml_type_GGML_TYPE_F16, - Type::F32 => crate::ggml_type_GGML_TYPE_F32, + Type::Q4_0 => sys::ggml_type_GGML_TYPE_Q4_0, + Type::Q4_1 => sys::ggml_type_GGML_TYPE_Q4_1, + Type::Q4_2 => sys::ggml_type_GGML_TYPE_Q4_2, + Type::Q5_0 => sys::ggml_type_GGML_TYPE_Q5_0, + Type::Q5_1 => sys::ggml_type_GGML_TYPE_Q5_1, + Type::Q8_0 => sys::ggml_type_GGML_TYPE_Q8_0, + Type::Q8_1 => sys::ggml_type_GGML_TYPE_Q8_1, + Type::I32 => sys::ggml_type_GGML_TYPE_I32, + Type::F16 => sys::ggml_type_GGML_TYPE_F16, + Type::F32 => sys::ggml_type_GGML_TYPE_F32, } } } -impl TryFrom for Type { +impl TryFrom for Type { type Error = (); - fn try_from(t: crate::ggml_type) -> Result { + fn try_from(t: sys::ggml_type) -> Result { match t { - crate::ggml_type_GGML_TYPE_Q4_0 => Ok(Type::Q4_0), - crate::ggml_type_GGML_TYPE_Q4_1 => Ok(Type::Q4_1), - crate::ggml_type_GGML_TYPE_Q4_2 => Ok(Type::Q4_2), - crate::ggml_type_GGML_TYPE_Q4_3 => Ok(Type::Q4_3), - crate::ggml_type_GGML_TYPE_Q8_0 => Ok(Type::Q8_0), - crate::ggml_type_GGML_TYPE_I32 => Ok(Type::I32), - crate::ggml_type_GGML_TYPE_F16 => Ok(Type::F16), - crate::ggml_type_GGML_TYPE_F32 => Ok(Type::F32), + sys::ggml_type_GGML_TYPE_Q4_0 => Ok(Type::Q4_0), + sys::ggml_type_GGML_TYPE_Q4_1 => Ok(Type::Q4_1), + sys::ggml_type_GGML_TYPE_Q4_2 => Ok(Type::Q4_2), + sys::ggml_type_GGML_TYPE_Q5_0 => Ok(Type::Q5_0), + sys::ggml_type_GGML_TYPE_Q5_1 => Ok(Type::Q5_1), + sys::ggml_type_GGML_TYPE_Q8_0 => Ok(Type::Q8_0), + sys::ggml_type_GGML_TYPE_Q8_1 => Ok(Type::Q8_1), + sys::ggml_type_GGML_TYPE_I32 => Ok(Type::I32), + sys::ggml_type_GGML_TYPE_F16 => Ok(Type::F16), + sys::ggml_type_GGML_TYPE_F32 => Ok(Type::F32), _ => Err(()), } } @@ -128,8 +127,10 @@ impl std::fmt::Display for Type { Type::Q4_0 => write!(f, "q4_0"), Type::Q4_1 => write!(f, "q4_1"), Type::Q4_2 => write!(f, "q4_2"), - Type::Q4_3 => write!(f, "q4_3"), + Type::Q5_0 => write!(f, "q5_0"), + Type::Q5_1 => write!(f, "q5_1"), Type::Q8_0 => write!(f, "q8_0"), + Type::Q8_1 => write!(f, "q8_1"), Type::I32 => write!(f, "i32"), Type::F16 => write!(f, "f16"), Type::F32 => write!(f, "f32"), @@ -164,41 +165,41 @@ impl Buffer { /// A `ggml` computation graph. Keeps track of all state during computation. pub struct ComputationGraph { - inner: crate::ggml_cgraph, + inner: sys::ggml_cgraph, } impl ComputationGraph { /// Create a new [ComputationGraph] with the specified `n_threads`. pub fn new(n_threads: usize) -> Self { Self { - inner: crate::ggml_cgraph { + inner: sys::ggml_cgraph { n_threads: usize_to_i32(n_threads), // SAFETY: This should be safe to zero. The original C++ impl // just leaves it uninitialized - ..unsafe { std::mem::zeroed::() } + ..unsafe { std::mem::zeroed::() } }, } } /// Build this computational graph in the forward direction in preparation for computation. pub fn build_forward_expand(&mut self, tensor: &Tensor) { - unsafe { crate::ggml_build_forward_expand(&mut self.inner, tensor.ptr.as_ptr()) } + unsafe { sys::ggml_build_forward_expand(&mut self.inner, tensor.ptr.as_ptr()) } } } /// The size of `t` as bytes. pub fn type_size(t: Type) -> usize { - unsafe { crate::ggml_type_size(t.into()) } + unsafe { sys::ggml_type_size(t.into()) } } /// [type_size]/[blck_size] as float. pub fn type_sizef(x: Type) -> f64 { - (unsafe { crate::ggml_type_sizef(x.into()) }) as f64 + (unsafe { sys::ggml_type_sizef(x.into()) }) as f64 } /// The size of a block for `t`. Only relevant for quantized types. pub fn blck_size(t: Type) -> usize { - i32_to_usize(unsafe { crate::ggml_blck_size(t.into()) }) + i32_to_usize(unsafe { sys::ggml_blck_size(t.into()) }) } fn usize_to_i32(val: usize) -> i32 { @@ -230,7 +231,7 @@ pub struct QuantizationResult { /// You must ensure that `src.len() == n_elements`, and `n_elements_0` /// is the first dimension of `src`. pub fn quantize_q4_0(src: &[f32], n_elements: usize, n_elements_0: usize) -> QuantizationResult { - quantize_impl(src, n_elements, n_elements_0, crate::ggml_quantize_q4_0) + quantize_impl(src, n_elements, n_elements_0, sys::ggml_quantize_q4_0) } /// Quantizes `src` into `dst` using `q4_1` quantization. @@ -238,7 +239,7 @@ pub fn quantize_q4_0(src: &[f32], n_elements: usize, n_elements_0: usize) -> Qua /// You must ensure that `src.len() == n_elements`, and `n_elements_0` /// is the first dimension of `src`. pub fn quantize_q4_1(src: &[f32], n_elements: usize, n_elements_0: usize) -> QuantizationResult { - quantize_impl(src, n_elements, n_elements_0, crate::ggml_quantize_q4_1) + quantize_impl(src, n_elements, n_elements_0, sys::ggml_quantize_q4_1) } fn quantize_impl( diff --git a/ggml/src/tensor.rs b/ggml/src/tensor.rs index 6e426940..0a5edd82 100644 --- a/ggml/src/tensor.rs +++ b/ggml/src/tensor.rs @@ -1,23 +1,19 @@ -use std::{ - os::raw::{c_int, c_void}, - ptr::NonNull, - sync::{Arc, Weak}, -}; +use std::{os::raw::c_void, ptr::NonNull, sync::Weak}; -use crate::{i64_to_usize, Type}; +use crate::{i64_to_usize, sys, Type}; /// Tensors are owned by the context. A tensor is alive as long as the /// underlying context it was created with is alive. pub struct Tensor { - pub(crate) ptr: NonNull, - pub(crate) ctx: Weak>, + pub(crate) ptr: NonNull, + pub(crate) ctx: Weak>, } impl Tensor { /// Size of the `ggml_tensor` struct in bytes. /// /// Exposed for purposes of determining context size. - pub const C_TYPE_SIZE: usize = std::mem::size_of::(); + pub const C_TYPE_SIZE: usize = std::mem::size_of::(); /// Creates a shared copy of this tensor pointer. pub fn share(&self) -> Self { @@ -47,7 +43,7 @@ impl Tensor { pub fn nbytes(&self) -> usize { self.with_alive_ctx(|| { // SAFETY: The with_alive_call guarantees the context is alive - unsafe { crate::ggml_nbytes(self.ptr.as_ptr()) } + unsafe { sys::ggml_nbytes(self.ptr.as_ptr()) } }) } @@ -80,7 +76,7 @@ impl Tensor { pub fn nelements(&self) -> usize { self.with_alive_ctx(|| { // SAFETY: The with_alive_call guarantees the context is alive - i64_to_usize(unsafe { crate::ggml_nelements(self.ptr.as_ptr()) }) + i64_to_usize(unsafe { sys::ggml_nelements(self.ptr.as_ptr()) }) }) } @@ -101,7 +97,7 @@ impl Tensor { /// The size of the element type in bytes. pub fn element_size(&self) -> usize { - self.with_alive_ctx(|| unsafe { crate::ggml_element_size(self.ptr.as_ptr()) }) + self.with_alive_ctx(|| unsafe { sys::ggml_element_size(self.ptr.as_ptr()) }) } /// Writes `src` to this tensor. @@ -124,7 +120,7 @@ impl Tensor { /// /// This tensor must not be written to or read by from any other code. pub unsafe fn read_data(&self, offset: usize, dst: &mut [u8]) { - let data = unsafe { crate::ggml_get_data(self.ptr.as_ptr()).add(offset) }; + let data = unsafe { sys::ggml_get_data(self.ptr.as_ptr()).add(offset) }; std::ptr::copy_nonoverlapping(data, dst as *mut _ as _, dst.len()) } } diff --git a/ggml/sys/Cargo.toml b/ggml/sys/Cargo.toml new file mode 100644 index 00000000..793d25b3 --- /dev/null +++ b/ggml/sys/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "ggml-sys" +version = { workspace = true } +edition = "2021" + +[build-dependencies] +cc = "^1.0" diff --git a/ggml/build.rs b/ggml/sys/build.rs similarity index 90% rename from ggml/build.rs rename to ggml/sys/build.rs index 02b38258..0b99fa0f 100644 --- a/ggml/build.rs +++ b/ggml/sys/build.rs @@ -1,4 +1,4 @@ -use std::{env, path::PathBuf}; +use std::env; // By default, this crate will attempt to compile ggml with the features of your host system if // the host and target are the same. If they are not, it will turn off auto-feature-detection, @@ -89,16 +89,6 @@ fn main() { } build.warnings(false); build.compile("ggml"); - - let header_path = "./ggml/include/ggml/ggml.h"; - bindgen::Builder::default() - .header(String::from(header_path)) - .allowlist_file(header_path) - .parse_callbacks(Box::new(bindgen::CargoCallbacks)) - .generate() - .expect("Unable to generate bindings.") - .write_to_file(PathBuf::from(env::var("OUT_DIR").unwrap()).join("bindings.rs")) - .expect("Unable to write generated bindings to file."); } fn get_supported_target_features() -> std::collections::HashSet { diff --git a/ggml/sys/ggml b/ggml/sys/ggml new file mode 160000 index 00000000..43dfb439 --- /dev/null +++ b/ggml/sys/ggml @@ -0,0 +1 @@ +Subproject commit 43dfb439fbf03eaf2db34a511f9e60d8338493e7 diff --git a/ggml/sys/src/lib.rs b/ggml/sys/src/lib.rs new file mode 100644 index 00000000..be3d5d3b --- /dev/null +++ b/ggml/sys/src/lib.rs @@ -0,0 +1,1617 @@ +/* automatically generated by rust-bindgen 0.65.1 */ + +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] +#![allow(unused)] + +pub const GGML_FILE_MAGIC: u32 = 1734831468; +pub const GGML_FILE_VERSION: u32 = 1; +pub const GGML_MAX_DIMS: u32 = 4; +pub const GGML_MAX_NODES: u32 = 4096; +pub const GGML_MAX_PARAMS: u32 = 16; +pub const GGML_MAX_CONTEXTS: u32 = 64; +pub const GGML_MAX_OPT: u32 = 4; +pub const GGML_DEFAULT_N_THREADS: u32 = 4; +pub type ggml_fp16_t = u16; +extern "C" { + pub fn ggml_fp16_to_fp32(x: ggml_fp16_t) -> f32; +} +extern "C" { + pub fn ggml_fp32_to_fp16(x: f32) -> ggml_fp16_t; +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct ggml_context { + _unused: [u8; 0], +} +pub const ggml_type_GGML_TYPE_F32: ggml_type = 0; +pub const ggml_type_GGML_TYPE_F16: ggml_type = 1; +pub const ggml_type_GGML_TYPE_Q4_0: ggml_type = 2; +pub const ggml_type_GGML_TYPE_Q4_1: ggml_type = 3; +pub const ggml_type_GGML_TYPE_Q4_2: ggml_type = 4; +pub const ggml_type_GGML_TYPE_Q5_0: ggml_type = 6; +pub const ggml_type_GGML_TYPE_Q5_1: ggml_type = 7; +pub const ggml_type_GGML_TYPE_Q8_0: ggml_type = 8; +pub const ggml_type_GGML_TYPE_Q8_1: ggml_type = 9; +pub const ggml_type_GGML_TYPE_I8: ggml_type = 10; +pub const ggml_type_GGML_TYPE_I16: ggml_type = 11; +pub const ggml_type_GGML_TYPE_I32: ggml_type = 12; +pub const ggml_type_GGML_TYPE_COUNT: ggml_type = 13; +pub type ggml_type = ::std::os::raw::c_uint; +pub const ggml_ftype_GGML_FTYPE_UNKNOWN: ggml_ftype = -1; +pub const ggml_ftype_GGML_FTYPE_ALL_F32: ggml_ftype = 0; +pub const ggml_ftype_GGML_FTYPE_MOSTLY_F16: ggml_ftype = 1; +pub const ggml_ftype_GGML_FTYPE_MOSTLY_Q4_0: ggml_ftype = 2; +pub const ggml_ftype_GGML_FTYPE_MOSTLY_Q4_1: ggml_ftype = 3; +pub const ggml_ftype_GGML_FTYPE_MOSTLY_Q4_1_SOME_F16: ggml_ftype = 4; +pub const ggml_ftype_GGML_FTYPE_MOSTLY_Q4_2: ggml_ftype = 5; +pub const ggml_ftype_GGML_FTYPE_MOSTLY_Q8_0: ggml_ftype = 7; +pub const ggml_ftype_GGML_FTYPE_MOSTLY_Q5_0: ggml_ftype = 8; +pub const ggml_ftype_GGML_FTYPE_MOSTLY_Q5_1: ggml_ftype = 9; +pub type ggml_ftype = ::std::os::raw::c_int; +pub const ggml_op_GGML_OP_NONE: ggml_op = 0; +pub const ggml_op_GGML_OP_DUP: ggml_op = 1; +pub const ggml_op_GGML_OP_ADD: ggml_op = 2; +pub const ggml_op_GGML_OP_SUB: ggml_op = 3; +pub const ggml_op_GGML_OP_MUL: ggml_op = 4; +pub const ggml_op_GGML_OP_DIV: ggml_op = 5; +pub const ggml_op_GGML_OP_SQR: ggml_op = 6; +pub const ggml_op_GGML_OP_SQRT: ggml_op = 7; +pub const ggml_op_GGML_OP_SUM: ggml_op = 8; +pub const ggml_op_GGML_OP_MEAN: ggml_op = 9; +pub const ggml_op_GGML_OP_REPEAT: ggml_op = 10; +pub const ggml_op_GGML_OP_ABS: ggml_op = 11; +pub const ggml_op_GGML_OP_SGN: ggml_op = 12; +pub const ggml_op_GGML_OP_NEG: ggml_op = 13; +pub const ggml_op_GGML_OP_STEP: ggml_op = 14; +pub const ggml_op_GGML_OP_RELU: ggml_op = 15; +pub const ggml_op_GGML_OP_GELU: ggml_op = 16; +pub const ggml_op_GGML_OP_SILU: ggml_op = 17; +pub const ggml_op_GGML_OP_NORM: ggml_op = 18; +pub const ggml_op_GGML_OP_RMS_NORM: ggml_op = 19; +pub const ggml_op_GGML_OP_MUL_MAT: ggml_op = 20; +pub const ggml_op_GGML_OP_SCALE: ggml_op = 21; +pub const ggml_op_GGML_OP_CPY: ggml_op = 22; +pub const ggml_op_GGML_OP_CONT: ggml_op = 23; +pub const ggml_op_GGML_OP_RESHAPE: ggml_op = 24; +pub const ggml_op_GGML_OP_VIEW: ggml_op = 25; +pub const ggml_op_GGML_OP_PERMUTE: ggml_op = 26; +pub const ggml_op_GGML_OP_TRANSPOSE: ggml_op = 27; +pub const ggml_op_GGML_OP_GET_ROWS: ggml_op = 28; +pub const ggml_op_GGML_OP_DIAG_MASK_INF: ggml_op = 29; +pub const ggml_op_GGML_OP_SOFT_MAX: ggml_op = 30; +pub const ggml_op_GGML_OP_ROPE: ggml_op = 31; +pub const ggml_op_GGML_OP_ALIBI: ggml_op = 32; +pub const ggml_op_GGML_OP_CONV_1D_1S: ggml_op = 33; +pub const ggml_op_GGML_OP_CONV_1D_2S: ggml_op = 34; +pub const ggml_op_GGML_OP_FLASH_ATTN: ggml_op = 35; +pub const ggml_op_GGML_OP_FLASH_FF: ggml_op = 36; +pub const ggml_op_GGML_OP_MAP_UNARY: ggml_op = 37; +pub const ggml_op_GGML_OP_MAP_BINARY: ggml_op = 38; +pub const ggml_op_GGML_OP_COUNT: ggml_op = 39; +pub type ggml_op = ::std::os::raw::c_uint; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct ggml_object { + pub offs: usize, + pub size: usize, + pub next: *mut ggml_object, + pub padding: [::std::os::raw::c_char; 8usize], +} +#[test] +fn bindgen_test_layout_ggml_object() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 32usize, + concat!("Size of: ", stringify!(ggml_object)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(ggml_object)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).offs) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(ggml_object), + "::", + stringify!(offs) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).size) as usize - ptr as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(ggml_object), + "::", + stringify!(size) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).next) as usize - ptr as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(ggml_object), + "::", + stringify!(next) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).padding) as usize - ptr as usize }, + 24usize, + concat!( + "Offset of field: ", + stringify!(ggml_object), + "::", + stringify!(padding) + ) + ); +} +pub const GGML_OBJECT_SIZE: usize = 32; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct ggml_tensor { + pub type_: ggml_type, + pub n_dims: ::std::os::raw::c_int, + pub ne: [i64; 4usize], + pub nb: [usize; 4usize], + pub op: ggml_op, + pub is_param: bool, + pub grad: *mut ggml_tensor, + pub src0: *mut ggml_tensor, + pub src1: *mut ggml_tensor, + pub opt: [*mut ggml_tensor; 4usize], + pub n_tasks: ::std::os::raw::c_int, + pub perf_runs: ::std::os::raw::c_int, + pub perf_cycles: i64, + pub perf_time_us: i64, + pub data: *mut ::std::os::raw::c_void, + pub padding: [::std::os::raw::c_char; 8usize], +} +#[test] +fn bindgen_test_layout_ggml_tensor() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 176usize, + concat!("Size of: ", stringify!(ggml_tensor)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(ggml_tensor)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).type_) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(ggml_tensor), + "::", + stringify!(type_) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).n_dims) as usize - ptr as usize }, + 4usize, + concat!( + "Offset of field: ", + stringify!(ggml_tensor), + "::", + stringify!(n_dims) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).ne) as usize - ptr as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(ggml_tensor), + "::", + stringify!(ne) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).nb) as usize - ptr as usize }, + 40usize, + concat!( + "Offset of field: ", + stringify!(ggml_tensor), + "::", + stringify!(nb) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).op) as usize - ptr as usize }, + 72usize, + concat!( + "Offset of field: ", + stringify!(ggml_tensor), + "::", + stringify!(op) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).is_param) as usize - ptr as usize }, + 76usize, + concat!( + "Offset of field: ", + stringify!(ggml_tensor), + "::", + stringify!(is_param) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).grad) as usize - ptr as usize }, + 80usize, + concat!( + "Offset of field: ", + stringify!(ggml_tensor), + "::", + stringify!(grad) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).src0) as usize - ptr as usize }, + 88usize, + concat!( + "Offset of field: ", + stringify!(ggml_tensor), + "::", + stringify!(src0) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).src1) as usize - ptr as usize }, + 96usize, + concat!( + "Offset of field: ", + stringify!(ggml_tensor), + "::", + stringify!(src1) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).opt) as usize - ptr as usize }, + 104usize, + concat!( + "Offset of field: ", + stringify!(ggml_tensor), + "::", + stringify!(opt) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).n_tasks) as usize - ptr as usize }, + 136usize, + concat!( + "Offset of field: ", + stringify!(ggml_tensor), + "::", + stringify!(n_tasks) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).perf_runs) as usize - ptr as usize }, + 140usize, + concat!( + "Offset of field: ", + stringify!(ggml_tensor), + "::", + stringify!(perf_runs) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).perf_cycles) as usize - ptr as usize }, + 144usize, + concat!( + "Offset of field: ", + stringify!(ggml_tensor), + "::", + stringify!(perf_cycles) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).perf_time_us) as usize - ptr as usize }, + 152usize, + concat!( + "Offset of field: ", + stringify!(ggml_tensor), + "::", + stringify!(perf_time_us) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).data) as usize - ptr as usize }, + 160usize, + concat!( + "Offset of field: ", + stringify!(ggml_tensor), + "::", + stringify!(data) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).padding) as usize - ptr as usize }, + 168usize, + concat!( + "Offset of field: ", + stringify!(ggml_tensor), + "::", + stringify!(padding) + ) + ); +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct ggml_cgraph { + pub n_nodes: ::std::os::raw::c_int, + pub n_leafs: ::std::os::raw::c_int, + pub n_threads: ::std::os::raw::c_int, + pub work_size: usize, + pub work: *mut ggml_tensor, + pub nodes: [*mut ggml_tensor; 4096usize], + pub grads: [*mut ggml_tensor; 4096usize], + pub leafs: [*mut ggml_tensor; 4096usize], + pub perf_runs: ::std::os::raw::c_int, + pub perf_cycles: i64, + pub perf_time_us: i64, +} +#[test] +fn bindgen_test_layout_ggml_cgraph() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 98360usize, + concat!("Size of: ", stringify!(ggml_cgraph)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(ggml_cgraph)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).n_nodes) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(ggml_cgraph), + "::", + stringify!(n_nodes) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).n_leafs) as usize - ptr as usize }, + 4usize, + concat!( + "Offset of field: ", + stringify!(ggml_cgraph), + "::", + stringify!(n_leafs) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).n_threads) as usize - ptr as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(ggml_cgraph), + "::", + stringify!(n_threads) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).work_size) as usize - ptr as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(ggml_cgraph), + "::", + stringify!(work_size) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).work) as usize - ptr as usize }, + 24usize, + concat!( + "Offset of field: ", + stringify!(ggml_cgraph), + "::", + stringify!(work) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).nodes) as usize - ptr as usize }, + 32usize, + concat!( + "Offset of field: ", + stringify!(ggml_cgraph), + "::", + stringify!(nodes) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).grads) as usize - ptr as usize }, + 32800usize, + concat!( + "Offset of field: ", + stringify!(ggml_cgraph), + "::", + stringify!(grads) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).leafs) as usize - ptr as usize }, + 65568usize, + concat!( + "Offset of field: ", + stringify!(ggml_cgraph), + "::", + stringify!(leafs) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).perf_runs) as usize - ptr as usize }, + 98336usize, + concat!( + "Offset of field: ", + stringify!(ggml_cgraph), + "::", + stringify!(perf_runs) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).perf_cycles) as usize - ptr as usize }, + 98344usize, + concat!( + "Offset of field: ", + stringify!(ggml_cgraph), + "::", + stringify!(perf_cycles) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).perf_time_us) as usize - ptr as usize }, + 98352usize, + concat!( + "Offset of field: ", + stringify!(ggml_cgraph), + "::", + stringify!(perf_time_us) + ) + ); +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct ggml_scratch { + pub offs: usize, + pub size: usize, + pub data: *mut ::std::os::raw::c_void, +} +#[test] +fn bindgen_test_layout_ggml_scratch() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 24usize, + concat!("Size of: ", stringify!(ggml_scratch)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(ggml_scratch)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).offs) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(ggml_scratch), + "::", + stringify!(offs) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).size) as usize - ptr as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(ggml_scratch), + "::", + stringify!(size) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).data) as usize - ptr as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(ggml_scratch), + "::", + stringify!(data) + ) + ); +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct ggml_init_params { + pub mem_size: usize, + pub mem_buffer: *mut ::std::os::raw::c_void, + pub no_alloc: bool, +} +#[test] +fn bindgen_test_layout_ggml_init_params() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 24usize, + concat!("Size of: ", stringify!(ggml_init_params)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(ggml_init_params)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).mem_size) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(ggml_init_params), + "::", + stringify!(mem_size) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).mem_buffer) as usize - ptr as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(ggml_init_params), + "::", + stringify!(mem_buffer) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).no_alloc) as usize - ptr as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(ggml_init_params), + "::", + stringify!(no_alloc) + ) + ); +} +extern "C" { + pub fn ggml_time_init(); +} +extern "C" { + pub fn ggml_time_ms() -> i64; +} +extern "C" { + pub fn ggml_time_us() -> i64; +} +extern "C" { + pub fn ggml_cycles() -> i64; +} +extern "C" { + pub fn ggml_cycles_per_ms() -> i64; +} +extern "C" { + pub fn ggml_print_object(obj: *const ggml_object); +} +extern "C" { + pub fn ggml_print_objects(ctx: *const ggml_context); +} +extern "C" { + pub fn ggml_nelements(tensor: *const ggml_tensor) -> i64; +} +extern "C" { + pub fn ggml_nbytes(tensor: *const ggml_tensor) -> usize; +} +extern "C" { + pub fn ggml_blck_size(type_: ggml_type) -> ::std::os::raw::c_int; +} +extern "C" { + pub fn ggml_type_size(type_: ggml_type) -> usize; +} +extern "C" { + pub fn ggml_type_sizef(type_: ggml_type) -> f32; +} +extern "C" { + pub fn ggml_type_name(type_: ggml_type) -> *const ::std::os::raw::c_char; +} +extern "C" { + pub fn ggml_element_size(tensor: *const ggml_tensor) -> usize; +} +extern "C" { + pub fn ggml_is_quantized(type_: ggml_type) -> bool; +} +extern "C" { + pub fn ggml_ftype_to_ggml_type(ftype: ggml_ftype) -> ggml_type; +} +extern "C" { + pub fn ggml_init(params: ggml_init_params) -> *mut ggml_context; +} +extern "C" { + pub fn ggml_free(ctx: *mut ggml_context); +} +extern "C" { + pub fn ggml_used_mem(ctx: *const ggml_context) -> usize; +} +extern "C" { + pub fn ggml_set_scratch(ctx: *mut ggml_context, scratch: ggml_scratch) -> usize; +} +extern "C" { + pub fn ggml_new_tensor( + ctx: *mut ggml_context, + type_: ggml_type, + n_dims: ::std::os::raw::c_int, + ne: *const i64, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_new_tensor_1d( + ctx: *mut ggml_context, + type_: ggml_type, + ne0: i64, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_new_tensor_2d( + ctx: *mut ggml_context, + type_: ggml_type, + ne0: i64, + ne1: i64, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_new_tensor_3d( + ctx: *mut ggml_context, + type_: ggml_type, + ne0: i64, + ne1: i64, + ne2: i64, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_new_tensor_4d( + ctx: *mut ggml_context, + type_: ggml_type, + ne0: i64, + ne1: i64, + ne2: i64, + ne3: i64, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_new_i32(ctx: *mut ggml_context, value: i32) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_new_f32(ctx: *mut ggml_context, value: f32) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_dup_tensor(ctx: *mut ggml_context, src: *const ggml_tensor) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_view_tensor(ctx: *mut ggml_context, src: *const ggml_tensor) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_set_zero(tensor: *mut ggml_tensor) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_set_i32(tensor: *mut ggml_tensor, value: i32) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_set_f32(tensor: *mut ggml_tensor, value: f32) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_get_i32_1d(tensor: *const ggml_tensor, i: ::std::os::raw::c_int) -> i32; +} +extern "C" { + pub fn ggml_set_i32_1d(tensor: *const ggml_tensor, i: ::std::os::raw::c_int, value: i32); +} +extern "C" { + pub fn ggml_get_f32_1d(tensor: *const ggml_tensor, i: ::std::os::raw::c_int) -> f32; +} +extern "C" { + pub fn ggml_set_f32_1d(tensor: *const ggml_tensor, i: ::std::os::raw::c_int, value: f32); +} +extern "C" { + pub fn ggml_get_data(tensor: *const ggml_tensor) -> *mut ::std::os::raw::c_void; +} +extern "C" { + pub fn ggml_get_data_f32(tensor: *const ggml_tensor) -> *mut f32; +} +extern "C" { + pub fn ggml_dup(ctx: *mut ggml_context, a: *mut ggml_tensor) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_add( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + b: *mut ggml_tensor, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_add_inplace( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + b: *mut ggml_tensor, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_sub( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + b: *mut ggml_tensor, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_mul( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + b: *mut ggml_tensor, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_div( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + b: *mut ggml_tensor, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_sqr(ctx: *mut ggml_context, a: *mut ggml_tensor) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_sqrt(ctx: *mut ggml_context, a: *mut ggml_tensor) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_sum(ctx: *mut ggml_context, a: *mut ggml_tensor) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_mean(ctx: *mut ggml_context, a: *mut ggml_tensor) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_repeat( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + b: *mut ggml_tensor, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_abs(ctx: *mut ggml_context, a: *mut ggml_tensor) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_sgn(ctx: *mut ggml_context, a: *mut ggml_tensor) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_neg(ctx: *mut ggml_context, a: *mut ggml_tensor) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_step(ctx: *mut ggml_context, a: *mut ggml_tensor) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_relu(ctx: *mut ggml_context, a: *mut ggml_tensor) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_gelu(ctx: *mut ggml_context, a: *mut ggml_tensor) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_silu(ctx: *mut ggml_context, a: *mut ggml_tensor) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_norm(ctx: *mut ggml_context, a: *mut ggml_tensor) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_rms_norm(ctx: *mut ggml_context, a: *mut ggml_tensor) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_mul_mat( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + b: *mut ggml_tensor, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_scale( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + b: *mut ggml_tensor, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_cpy( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + b: *mut ggml_tensor, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_cont(ctx: *mut ggml_context, a: *mut ggml_tensor) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_reshape( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + b: *mut ggml_tensor, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_reshape_2d( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + ne0: i64, + ne1: i64, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_reshape_3d( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + ne0: i64, + ne1: i64, + ne2: i64, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_view_1d( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + ne0: i64, + offset: usize, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_view_2d( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + ne0: i64, + ne1: i64, + nb1: usize, + offset: usize, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_view_3d( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + ne0: i64, + ne1: i64, + ne2: i64, + nb1: usize, + nb2: usize, + offset: usize, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_permute( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + axis0: ::std::os::raw::c_int, + axis1: ::std::os::raw::c_int, + axis2: ::std::os::raw::c_int, + axis3: ::std::os::raw::c_int, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_transpose(ctx: *mut ggml_context, a: *mut ggml_tensor) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_get_rows( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + b: *mut ggml_tensor, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_diag_mask_inf( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + n_past: ::std::os::raw::c_int, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_soft_max(ctx: *mut ggml_context, a: *mut ggml_tensor) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_rope( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + n_past: ::std::os::raw::c_int, + n_dims: ::std::os::raw::c_int, + mode: ::std::os::raw::c_int, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_alibi( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + n_past: ::std::os::raw::c_int, + n_head: ::std::os::raw::c_int, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_conv_1d_1s( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + b: *mut ggml_tensor, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_conv_1d_2s( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + b: *mut ggml_tensor, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_flash_attn( + ctx: *mut ggml_context, + q: *mut ggml_tensor, + k: *mut ggml_tensor, + v: *mut ggml_tensor, + masked: bool, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_flash_ff( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + b0: *mut ggml_tensor, + b1: *mut ggml_tensor, + c0: *mut ggml_tensor, + c1: *mut ggml_tensor, + ) -> *mut ggml_tensor; +} +pub type ggml_unary_op_f32_t = ::std::option::Option< + unsafe extern "C" fn(arg1: ::std::os::raw::c_int, arg2: *mut f32, arg3: *const f32), +>; +pub type ggml_binary_op_f32_t = ::std::option::Option< + unsafe extern "C" fn( + arg1: ::std::os::raw::c_int, + arg2: *mut f32, + arg3: *const f32, + arg4: *const f32, + ), +>; +extern "C" { + pub fn ggml_map_unary_f32( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + fun: ggml_unary_op_f32_t, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_map_binary_f32( + ctx: *mut ggml_context, + a: *mut ggml_tensor, + b: *mut ggml_tensor, + fun: ggml_binary_op_f32_t, + ) -> *mut ggml_tensor; +} +extern "C" { + pub fn ggml_set_param(ctx: *mut ggml_context, tensor: *mut ggml_tensor); +} +extern "C" { + pub fn ggml_build_forward_expand(cgraph: *mut ggml_cgraph, tensor: *mut ggml_tensor); +} +extern "C" { + pub fn ggml_build_forward(tensor: *mut ggml_tensor) -> ggml_cgraph; +} +extern "C" { + pub fn ggml_build_backward( + ctx: *mut ggml_context, + gf: *mut ggml_cgraph, + keep: bool, + ) -> ggml_cgraph; +} +extern "C" { + pub fn ggml_graph_compute(ctx: *mut ggml_context, cgraph: *mut ggml_cgraph); +} +extern "C" { + pub fn ggml_graph_reset(cgraph: *mut ggml_cgraph); +} +extern "C" { + pub fn ggml_graph_print(cgraph: *const ggml_cgraph); +} +extern "C" { + pub fn ggml_graph_dump_dot( + gb: *const ggml_cgraph, + gf: *const ggml_cgraph, + filename: *const ::std::os::raw::c_char, + ); +} +pub const ggml_opt_type_GGML_OPT_ADAM: ggml_opt_type = 0; +pub const ggml_opt_type_GGML_OPT_LBFGS: ggml_opt_type = 1; +pub type ggml_opt_type = ::std::os::raw::c_uint; +pub const ggml_linesearch_GGML_LINESEARCH_DEFAULT: ggml_linesearch = 1; +pub const ggml_linesearch_GGML_LINESEARCH_BACKTRACKING_ARMIJO: ggml_linesearch = 0; +pub const ggml_linesearch_GGML_LINESEARCH_BACKTRACKING_WOLFE: ggml_linesearch = 1; +pub const ggml_linesearch_GGML_LINESEARCH_BACKTRACKING_STRONG_WOLFE: ggml_linesearch = 2; +pub type ggml_linesearch = ::std::os::raw::c_uint; +pub const ggml_opt_result_GGML_OPT_OK: ggml_opt_result = 0; +pub const ggml_opt_result_GGML_OPT_DID_NOT_CONVERGE: ggml_opt_result = 1; +pub const ggml_opt_result_GGML_OPT_NO_CONTEXT: ggml_opt_result = 2; +pub const ggml_opt_result_GGML_OPT_INVALID_WOLFE: ggml_opt_result = 3; +pub const ggml_opt_result_GGML_OPT_FAIL: ggml_opt_result = 4; +pub const ggml_opt_result_GGML_LINESEARCH_FAIL: ggml_opt_result = -128; +pub const ggml_opt_result_GGML_LINESEARCH_MINIMUM_STEP: ggml_opt_result = -127; +pub const ggml_opt_result_GGML_LINESEARCH_MAXIMUM_STEP: ggml_opt_result = -126; +pub const ggml_opt_result_GGML_LINESEARCH_MAXIMUM_ITERATIONS: ggml_opt_result = -125; +pub const ggml_opt_result_GGML_LINESEARCH_INVALID_PARAMETERS: ggml_opt_result = -124; +pub type ggml_opt_result = ::std::os::raw::c_int; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct ggml_opt_params { + pub type_: ggml_opt_type, + pub n_threads: ::std::os::raw::c_int, + pub past: ::std::os::raw::c_int, + pub delta: f32, + pub max_no_improvement: ::std::os::raw::c_int, + pub print_forward_graph: bool, + pub print_backward_graph: bool, + pub adam: ggml_opt_params__bindgen_ty_1, + pub lbfgs: ggml_opt_params__bindgen_ty_2, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct ggml_opt_params__bindgen_ty_1 { + pub n_iter: ::std::os::raw::c_int, + pub alpha: f32, + pub beta1: f32, + pub beta2: f32, + pub eps: f32, + pub eps_f: f32, + pub eps_g: f32, +} +#[test] +fn bindgen_test_layout_ggml_opt_params__bindgen_ty_1() { + const UNINIT: ::std::mem::MaybeUninit = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 28usize, + concat!("Size of: ", stringify!(ggml_opt_params__bindgen_ty_1)) + ); + assert_eq!( + ::std::mem::align_of::(), + 4usize, + concat!("Alignment of ", stringify!(ggml_opt_params__bindgen_ty_1)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).n_iter) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params__bindgen_ty_1), + "::", + stringify!(n_iter) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).alpha) as usize - ptr as usize }, + 4usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params__bindgen_ty_1), + "::", + stringify!(alpha) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).beta1) as usize - ptr as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params__bindgen_ty_1), + "::", + stringify!(beta1) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).beta2) as usize - ptr as usize }, + 12usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params__bindgen_ty_1), + "::", + stringify!(beta2) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).eps) as usize - ptr as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params__bindgen_ty_1), + "::", + stringify!(eps) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).eps_f) as usize - ptr as usize }, + 20usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params__bindgen_ty_1), + "::", + stringify!(eps_f) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).eps_g) as usize - ptr as usize }, + 24usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params__bindgen_ty_1), + "::", + stringify!(eps_g) + ) + ); +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct ggml_opt_params__bindgen_ty_2 { + pub m: ::std::os::raw::c_int, + pub n_iter: ::std::os::raw::c_int, + pub max_linesearch: ::std::os::raw::c_int, + pub eps: f32, + pub ftol: f32, + pub wolfe: f32, + pub min_step: f32, + pub max_step: f32, + pub linesearch: ggml_linesearch, +} +#[test] +fn bindgen_test_layout_ggml_opt_params__bindgen_ty_2() { + const UNINIT: ::std::mem::MaybeUninit = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 36usize, + concat!("Size of: ", stringify!(ggml_opt_params__bindgen_ty_2)) + ); + assert_eq!( + ::std::mem::align_of::(), + 4usize, + concat!("Alignment of ", stringify!(ggml_opt_params__bindgen_ty_2)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).m) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params__bindgen_ty_2), + "::", + stringify!(m) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).n_iter) as usize - ptr as usize }, + 4usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params__bindgen_ty_2), + "::", + stringify!(n_iter) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).max_linesearch) as usize - ptr as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params__bindgen_ty_2), + "::", + stringify!(max_linesearch) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).eps) as usize - ptr as usize }, + 12usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params__bindgen_ty_2), + "::", + stringify!(eps) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).ftol) as usize - ptr as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params__bindgen_ty_2), + "::", + stringify!(ftol) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).wolfe) as usize - ptr as usize }, + 20usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params__bindgen_ty_2), + "::", + stringify!(wolfe) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).min_step) as usize - ptr as usize }, + 24usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params__bindgen_ty_2), + "::", + stringify!(min_step) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).max_step) as usize - ptr as usize }, + 28usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params__bindgen_ty_2), + "::", + stringify!(max_step) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).linesearch) as usize - ptr as usize }, + 32usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params__bindgen_ty_2), + "::", + stringify!(linesearch) + ) + ); +} +#[test] +fn bindgen_test_layout_ggml_opt_params() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 88usize, + concat!("Size of: ", stringify!(ggml_opt_params)) + ); + assert_eq!( + ::std::mem::align_of::(), + 4usize, + concat!("Alignment of ", stringify!(ggml_opt_params)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).type_) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params), + "::", + stringify!(type_) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).n_threads) as usize - ptr as usize }, + 4usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params), + "::", + stringify!(n_threads) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).past) as usize - ptr as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params), + "::", + stringify!(past) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).delta) as usize - ptr as usize }, + 12usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params), + "::", + stringify!(delta) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).max_no_improvement) as usize - ptr as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params), + "::", + stringify!(max_no_improvement) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).print_forward_graph) as usize - ptr as usize }, + 20usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params), + "::", + stringify!(print_forward_graph) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).print_backward_graph) as usize - ptr as usize }, + 21usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params), + "::", + stringify!(print_backward_graph) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).adam) as usize - ptr as usize }, + 24usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params), + "::", + stringify!(adam) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).lbfgs) as usize - ptr as usize }, + 52usize, + concat!( + "Offset of field: ", + stringify!(ggml_opt_params), + "::", + stringify!(lbfgs) + ) + ); +} +extern "C" { + pub fn ggml_opt_default_params(type_: ggml_opt_type) -> ggml_opt_params; +} +extern "C" { + pub fn ggml_opt( + ctx: *mut ggml_context, + params: ggml_opt_params, + f: *mut ggml_tensor, + ) -> ggml_opt_result; +} +extern "C" { + pub fn ggml_quantize_q4_0( + src: *const f32, + dst: *mut ::std::os::raw::c_void, + n: ::std::os::raw::c_int, + k: ::std::os::raw::c_int, + hist: *mut i64, + ) -> usize; +} +extern "C" { + pub fn ggml_quantize_q4_1( + src: *const f32, + dst: *mut ::std::os::raw::c_void, + n: ::std::os::raw::c_int, + k: ::std::os::raw::c_int, + hist: *mut i64, + ) -> usize; +} +extern "C" { + pub fn ggml_quantize_q4_2( + src: *const f32, + dst: *mut ::std::os::raw::c_void, + n: ::std::os::raw::c_int, + k: ::std::os::raw::c_int, + hist: *mut i64, + ) -> usize; +} +extern "C" { + pub fn ggml_quantize_q5_0( + src: *const f32, + dst: *mut ::std::os::raw::c_void, + n: ::std::os::raw::c_int, + k: ::std::os::raw::c_int, + hist: *mut i64, + ) -> usize; +} +extern "C" { + pub fn ggml_quantize_q5_1( + src: *const f32, + dst: *mut ::std::os::raw::c_void, + n: ::std::os::raw::c_int, + k: ::std::os::raw::c_int, + hist: *mut i64, + ) -> usize; +} +extern "C" { + pub fn ggml_quantize_q8_0( + src: *const f32, + dst: *mut ::std::os::raw::c_void, + n: ::std::os::raw::c_int, + k: ::std::os::raw::c_int, + hist: *mut i64, + ) -> usize; +} +extern "C" { + pub fn ggml_quantize_chunk( + type_: ggml_type, + src: *const f32, + dst: *mut ::std::os::raw::c_void, + start: ::std::os::raw::c_int, + n: ::std::os::raw::c_int, + hist: *mut i64, + ) -> usize; +} +extern "C" { + pub fn ggml_cpu_has_avx() -> ::std::os::raw::c_int; +} +extern "C" { + pub fn ggml_cpu_has_avx2() -> ::std::os::raw::c_int; +} +extern "C" { + pub fn ggml_cpu_has_avx512() -> ::std::os::raw::c_int; +} +extern "C" { + pub fn ggml_cpu_has_avx512_vbmi() -> ::std::os::raw::c_int; +} +extern "C" { + pub fn ggml_cpu_has_avx512_vnni() -> ::std::os::raw::c_int; +} +extern "C" { + pub fn ggml_cpu_has_fma() -> ::std::os::raw::c_int; +} +extern "C" { + pub fn ggml_cpu_has_neon() -> ::std::os::raw::c_int; +} +extern "C" { + pub fn ggml_cpu_has_arm_fma() -> ::std::os::raw::c_int; +} +extern "C" { + pub fn ggml_cpu_has_f16c() -> ::std::os::raw::c_int; +} +extern "C" { + pub fn ggml_cpu_has_fp16_va() -> ::std::os::raw::c_int; +} +extern "C" { + pub fn ggml_cpu_has_wasm_simd() -> ::std::os::raw::c_int; +} +extern "C" { + pub fn ggml_cpu_has_blas() -> ::std::os::raw::c_int; +} +extern "C" { + pub fn ggml_cpu_has_cublas() -> ::std::os::raw::c_int; +} +extern "C" { + pub fn ggml_cpu_has_clblast() -> ::std::os::raw::c_int; +} +extern "C" { + pub fn ggml_cpu_has_gpublas() -> ::std::os::raw::c_int; +} +extern "C" { + pub fn ggml_cpu_has_sse3() -> ::std::os::raw::c_int; +} +extern "C" { + pub fn ggml_cpu_has_vsx() -> ::std::os::raw::c_int; +} +pub type dequantize_row_q_t = ::std::option::Option< + unsafe extern "C" fn(x: *const ::std::os::raw::c_void, y: *mut f32, k: ::std::os::raw::c_int), +>; +pub type quantize_row_q_t = ::std::option::Option< + unsafe extern "C" fn(x: *const f32, y: *mut ::std::os::raw::c_void, k: ::std::os::raw::c_int), +>; +pub type vec_dot_q_t = ::std::option::Option< + unsafe extern "C" fn( + n: ::std::os::raw::c_int, + s: *mut f32, + x: *const ::std::os::raw::c_void, + y: *const ::std::os::raw::c_void, + ), +>; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct quantize_fns_t { + pub dequantize_row_q: dequantize_row_q_t, + pub quantize_row_q: quantize_row_q_t, + pub quantize_row_q_reference: quantize_row_q_t, + pub quantize_row_q_dot: quantize_row_q_t, + pub vec_dot_q: vec_dot_q_t, + pub vec_dot_type: ggml_type, +} +#[test] +fn bindgen_test_layout_quantize_fns_t() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 48usize, + concat!("Size of: ", stringify!(quantize_fns_t)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(quantize_fns_t)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).dequantize_row_q) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(quantize_fns_t), + "::", + stringify!(dequantize_row_q) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).quantize_row_q) as usize - ptr as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(quantize_fns_t), + "::", + stringify!(quantize_row_q) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).quantize_row_q_reference) as usize - ptr as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(quantize_fns_t), + "::", + stringify!(quantize_row_q_reference) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).quantize_row_q_dot) as usize - ptr as usize }, + 24usize, + concat!( + "Offset of field: ", + stringify!(quantize_fns_t), + "::", + stringify!(quantize_row_q_dot) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).vec_dot_q) as usize - ptr as usize }, + 32usize, + concat!( + "Offset of field: ", + stringify!(quantize_fns_t), + "::", + stringify!(vec_dot_q) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).vec_dot_type) as usize - ptr as usize }, + 40usize, + concat!( + "Offset of field: ", + stringify!(quantize_fns_t), + "::", + stringify!(vec_dot_type) + ) + ); +} +extern "C" { + pub fn ggml_internal_get_quantize_fn(i: usize) -> quantize_fns_t; +} diff --git a/tools/generate-ggml-bindings/Cargo.toml b/tools/generate-ggml-bindings/Cargo.toml new file mode 100644 index 00000000..0efc3ef6 --- /dev/null +++ b/tools/generate-ggml-bindings/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "generate-ggml-bindings" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +bindgen = "0.65.1" diff --git a/tools/generate-ggml-bindings/src/main.rs b/tools/generate-ggml-bindings/src/main.rs new file mode 100644 index 00000000..aca90252 --- /dev/null +++ b/tools/generate-ggml-bindings/src/main.rs @@ -0,0 +1,28 @@ +//! Helper tool to generate the bindings for the ggml crate. +//! +//! Assumed to be run from the root of the workspace. + +use std::path::PathBuf; + +fn main() { + const HEADER_PATH: &str = "ggml/sys/ggml/include/ggml/ggml.h"; + + let bindings = bindgen::Builder::default() + .header(HEADER_PATH) + // Suppress some warnings + .raw_line("#![allow(non_upper_case_globals)]") + .raw_line("#![allow(non_camel_case_types)]") + .raw_line("#![allow(non_snake_case)]") + .raw_line("#![allow(unused)]") + // Do not generate code for ggml's includes (stdlib) + .allowlist_file(HEADER_PATH) + .generate() + .expect("Unable to generate bindings"); + + let out_path = PathBuf::from("ggml").join("sys").join("src").join("lib.rs"); + bindings + .write_to_file(out_path) + .expect("Couldn't write bindings"); + + println!("Successfully updated bindings"); +}