Skip to main content

llama_cpp_sys_v3/
lib.rs

1use libloading::Library;
2use std::path::{Path, PathBuf};
3
4pub mod types;
5pub use types::*;
6
7#[derive(Debug, thiserror::Error)]
8pub enum LoadError {
9    #[error("DLL not found: {0}")]
10    NotFound(PathBuf),
11    #[error("Failed to load DLL: {0}")]
12    LoadFailed(#[from] libloading::Error),
13    #[error("Symbol not found: {0}")]
14    SymbolMissing(&'static str),
15}
16
17/// A loaded instance of the llama.cpp dynamic library.
18/// This struct holds the library handle and all resolved function pointers.
19pub struct LlamaLib {
20    // We must keep the libraries alive as long as the functions are used.
21    _libs: Vec<Library>,
22    pub symbols: LlamaSymbols,
23}
24
25macro_rules! resolve_symbols {
26    ($libs:expr, { $( $name:ident : $type:ty ),* $(,)? }) => {
27        LlamaSymbols {
28            $(
29                $name: {
30                    let mut found = None;
31                    for lib in $libs.iter() {
32                        if let Ok(sym) = unsafe { lib.get::<$type>(stringify!($name).as_bytes()) } {
33                            found = Some(*sym);
34                            break;
35                        }
36                    }
37                    found.ok_or(LoadError::SymbolMissing(stringify!($name)))?
38                },
39            )*
40        }
41    };
42}
43
44impl LlamaLib {
45    /// Attempt to load the llama.cpp library from the given path.
46    pub fn open(path: &Path) -> Result<Self, LoadError> {
47        if !path.exists() {
48            return Err(LoadError::NotFound(path.to_path_buf()));
49        }
50
51        let mut libs = Vec::new();
52
53        // Try to load ggml.dll first if it's in the same directory
54        if let Some(parent) = path.parent() {
55            let ggml_path = parent.join("ggml.dll");
56            if ggml_path.exists() {
57                #[cfg(target_os = "windows")]
58                let lib = unsafe {
59                    libloading::os::windows::Library::load_with_flags(
60                        &ggml_path,
61                        libloading::os::windows::LOAD_WITH_ALTERED_SEARCH_PATH,
62                    )?
63                };
64                #[cfg(not(target_os = "windows"))]
65                let lib = unsafe { libloading::Library::new(&ggml_path)? };
66
67                libs.push(libloading::Library::from(lib));
68            }
69        }
70
71        #[cfg(target_os = "windows")]
72        let main_lib = unsafe {
73            libloading::os::windows::Library::load_with_flags(
74                path,
75                libloading::os::windows::LOAD_WITH_ALTERED_SEARCH_PATH,
76            )?
77        };
78        #[cfg(not(target_os = "windows"))]
79        let main_lib = unsafe { Library::new(path)? };
80
81        libs.push(libloading::Library::from(main_lib));
82
83        // Resolve all required symbols here
84        let symbols = resolve_symbols!(libs, {
85            llama_backend_init: unsafe extern "C" fn(),
86            llama_backend_free: unsafe extern "C" fn(),
87            ggml_backend_load_all: unsafe extern "C" fn(),
88            ggml_backend_load_all_from_path: unsafe extern "C" fn(*const std::ffi::c_char),
89
90            llama_model_default_params: unsafe extern "C" fn() -> llama_model_params,
91            llama_model_load_from_file: unsafe extern "C" fn(*const std::ffi::c_char, llama_model_params) -> *mut llama_model,
92            llama_model_free: unsafe extern "C" fn(*mut llama_model),
93
94            llama_context_default_params: unsafe extern "C" fn() -> llama_context_params,
95            llama_init_from_model: unsafe extern "C" fn(*mut llama_model, llama_context_params) -> *mut llama_context,
96            llama_free: unsafe extern "C" fn(*mut llama_context),
97
98            llama_batch_get_one: unsafe extern "C" fn(*mut llama_token, i32) -> llama_batch,
99            llama_batch_init: unsafe extern "C" fn(i32, i32, i32) -> llama_batch,
100            llama_batch_free: unsafe extern "C" fn(llama_batch),
101
102            llama_decode: unsafe extern "C" fn(*mut llama_context, llama_batch) -> i32,
103            llama_kv_cache_clear: unsafe extern "C" fn(*mut llama_context),
104
105            llama_set_n_threads: unsafe extern "C" fn(*mut llama_context, u32, u32),
106            llama_model_get_vocab: unsafe extern "C" fn(*const llama_model) -> *const llama_vocab,
107            llama_vocab_n_tokens: unsafe extern "C" fn(*const llama_vocab) -> i32,
108            llama_n_vocab: unsafe extern "C" fn(*const llama_vocab) -> i32,
109            llama_n_ctx: unsafe extern "C" fn(*const llama_context) -> u32,
110
111            llama_get_logits: unsafe extern "C" fn(*mut llama_context) -> *mut f32,
112            llama_get_logits_ith: unsafe extern "C" fn(*mut llama_context, i32) -> *mut f32,
113
114            llama_token_get_text: unsafe extern "C" fn(*const llama_vocab, llama_token) -> *const std::ffi::c_char,
115            llama_tokenize: unsafe extern "C" fn(*const llama_vocab, *const std::ffi::c_char, i32, *mut llama_token, i32, bool, bool) -> i32,
116            llama_token_to_piece: unsafe extern "C" fn(*const llama_vocab, llama_token, *mut std::ffi::c_char, i32, i32, bool) -> i32,
117
118            llama_vocab_bos: unsafe extern "C" fn(*const llama_vocab) -> llama_token,
119            llama_vocab_eos: unsafe extern "C" fn(*const llama_vocab) -> llama_token,
120            llama_vocab_nl: unsafe extern "C" fn(*const llama_vocab) -> llama_token,
121            llama_vocab_is_eog: unsafe extern "C" fn(*const llama_vocab, llama_token) -> bool,
122
123            llama_print_system_info: unsafe extern "C" fn() -> *const std::ffi::c_char,
124
125            // Sampler API
126            llama_sampler_chain_init: unsafe extern "C" fn(llama_sampler_chain_params) -> *mut llama_sampler,
127            llama_sampler_init_greedy: unsafe extern "C" fn() -> *mut llama_sampler,
128            llama_sampler_free: unsafe extern "C" fn(*mut llama_sampler),
129            llama_sampler_init_temp: unsafe extern "C" fn(f32) -> *mut llama_sampler,
130            llama_sampler_init_top_k: unsafe extern "C" fn(i32) -> *mut llama_sampler,
131            llama_sampler_init_top_p: unsafe extern "C" fn(f32, usize) -> *mut llama_sampler,
132            llama_sampler_init_dist: unsafe extern "C" fn(u32) -> *mut llama_sampler,
133            llama_sampler_init_penalties: unsafe extern "C" fn(i32, f32, f32, f32) -> *mut llama_sampler,
134            llama_sampler_chain_add: unsafe extern "C" fn(*mut llama_sampler, *mut llama_sampler),
135            llama_sampler_sample: unsafe extern "C" fn(*mut llama_sampler, *mut llama_context, i32) -> llama_token,
136            llama_chat_apply_template: unsafe extern "C" fn(*const std::ffi::c_char, *const llama_chat_message, usize, bool, *mut std::ffi::c_char, i32) -> i32,
137            llama_model_chat_template: unsafe extern "C" fn(*const llama_model, *const std::ffi::c_char, *mut std::ffi::c_char, usize) -> i32,
138        });
139
140        Ok(Self {
141            _libs: libs,
142            symbols,
143        })
144    }
145}
146
147pub struct LlamaSymbols {
148    pub llama_backend_init: unsafe extern "C" fn(),
149    pub llama_backend_free: unsafe extern "C" fn(),
150    pub ggml_backend_load_all: unsafe extern "C" fn(),
151    pub ggml_backend_load_all_from_path: unsafe extern "C" fn(*const std::ffi::c_char),
152
153    pub llama_model_default_params: unsafe extern "C" fn() -> llama_model_params,
154    pub llama_model_load_from_file:
155        unsafe extern "C" fn(*const std::ffi::c_char, llama_model_params) -> *mut llama_model,
156    pub llama_model_free: unsafe extern "C" fn(*mut llama_model),
157
158    pub llama_context_default_params: unsafe extern "C" fn() -> llama_context_params,
159    pub llama_init_from_model:
160        unsafe extern "C" fn(*mut llama_model, llama_context_params) -> *mut llama_context,
161    pub llama_free: unsafe extern "C" fn(*mut llama_context),
162
163    pub llama_batch_get_one: unsafe extern "C" fn(*mut llama_token, i32) -> llama_batch,
164    pub llama_batch_init: unsafe extern "C" fn(i32, i32, i32) -> llama_batch,
165    pub llama_batch_free: unsafe extern "C" fn(llama_batch),
166
167    pub llama_decode: unsafe extern "C" fn(*mut llama_context, llama_batch) -> i32,
168    pub llama_kv_cache_clear: unsafe extern "C" fn(*mut llama_context),
169
170    pub llama_set_n_threads: unsafe extern "C" fn(*mut llama_context, u32, u32),
171    pub llama_model_get_vocab: unsafe extern "C" fn(*const llama_model) -> *const llama_vocab,
172    pub llama_vocab_n_tokens: unsafe extern "C" fn(*const llama_vocab) -> i32,
173    pub llama_n_vocab: unsafe extern "C" fn(*const llama_vocab) -> i32,
174    pub llama_n_ctx: unsafe extern "C" fn(*const llama_context) -> u32,
175
176    pub llama_get_logits: unsafe extern "C" fn(*mut llama_context) -> *mut f32,
177    pub llama_get_logits_ith: unsafe extern "C" fn(*mut llama_context, i32) -> *mut f32,
178
179    pub llama_token_get_text:
180        unsafe extern "C" fn(*const llama_vocab, llama_token) -> *const std::ffi::c_char,
181    pub llama_tokenize: unsafe extern "C" fn(
182        *const llama_vocab,
183        *const std::ffi::c_char,
184        i32,
185        *mut llama_token,
186        i32,
187        bool,
188        bool,
189    ) -> i32,
190    pub llama_token_to_piece: unsafe extern "C" fn(
191        *const llama_vocab,
192        llama_token,
193        *mut std::ffi::c_char,
194        i32,
195        i32,
196        bool,
197    ) -> i32,
198
199    pub llama_vocab_bos: unsafe extern "C" fn(*const llama_vocab) -> llama_token,
200    pub llama_vocab_eos: unsafe extern "C" fn(*const llama_vocab) -> llama_token,
201    pub llama_vocab_nl: unsafe extern "C" fn(*const llama_vocab) -> llama_token,
202    pub llama_vocab_is_eog: unsafe extern "C" fn(*const llama_vocab, llama_token) -> bool,
203
204    pub llama_print_system_info: unsafe extern "C" fn() -> *const std::ffi::c_char,
205
206    pub llama_sampler_chain_init:
207        unsafe extern "C" fn(llama_sampler_chain_params) -> *mut llama_sampler,
208    pub llama_sampler_init_greedy: unsafe extern "C" fn() -> *mut llama_sampler,
209    pub llama_sampler_free: unsafe extern "C" fn(*mut llama_sampler),
210    pub llama_sampler_init_temp: unsafe extern "C" fn(f32) -> *mut llama_sampler,
211    pub llama_sampler_init_top_k: unsafe extern "C" fn(i32) -> *mut llama_sampler,
212    pub llama_sampler_init_top_p: unsafe extern "C" fn(f32, usize) -> *mut llama_sampler,
213    pub llama_sampler_init_dist: unsafe extern "C" fn(u32) -> *mut llama_sampler,
214    pub llama_sampler_init_penalties:
215        unsafe extern "C" fn(i32, f32, f32, f32) -> *mut llama_sampler,
216    pub llama_sampler_chain_add: unsafe extern "C" fn(*mut llama_sampler, *mut llama_sampler),
217    pub llama_sampler_sample:
218        unsafe extern "C" fn(*mut llama_sampler, *mut llama_context, i32) -> llama_token,
219    pub llama_chat_apply_template: unsafe extern "C" fn(
220        *const std::ffi::c_char,
221        *const llama_chat_message,
222        usize,
223        bool,
224        *mut std::ffi::c_char,
225        i32,
226    ) -> i32,
227    pub llama_model_chat_template: unsafe extern "C" fn(
228        *const llama_model,
229        *const std::ffi::c_char,
230        *mut std::ffi::c_char,
231        usize,
232    ) -> i32,
233}