Skip to main content

llama_cpp_2/model/
params.rs

1//! A safe wrapper around `llama_model_params`.
2
3use crate::context::params::LlamaContextParams;
4use crate::model::params::kv_overrides::KvOverrides;
5use crate::LlamaCppError;
6use std::ffi::{c_char, c_void, CStr};
7use std::fmt::{Debug, Formatter};
8use std::pin::Pin;
9use std::ptr::null;
10
11pub mod kv_overrides;
12
13/// Result of [`LlamaModelParams::fit_params`], containing the fitted context size.
14#[cfg(feature = "common")]
15#[derive(Debug, Clone)]
16pub struct FitResult {
17    /// The context size after fitting (may have been reduced from the requested value).
18    pub n_ctx: u32,
19}
20
21/// Error returned by [`LlamaModelParams::fit_params`].
22#[cfg(feature = "common")]
23#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
24pub enum FitError {
25    /// Could not find allocations that are projected to fit available memory.
26    #[error("could not find allocations that fit available memory")]
27    Failure,
28    /// A hard error occurred during fitting (e.g. model not found at the specified path).
29    #[error("hard error during parameter fitting")]
30    Error,
31}
32
33#[allow(clippy::cast_possible_wrap)]
34#[allow(clippy::cast_possible_truncation)]
35const LLAMA_SPLIT_MODE_NONE: i8 = llama_cpp_sys_2::LLAMA_SPLIT_MODE_NONE as i8;
36#[allow(clippy::cast_possible_wrap)]
37#[allow(clippy::cast_possible_truncation)]
38const LLAMA_SPLIT_MODE_LAYER: i8 = llama_cpp_sys_2::LLAMA_SPLIT_MODE_LAYER as i8;
39#[allow(clippy::cast_possible_wrap)]
40#[allow(clippy::cast_possible_truncation)]
41const LLAMA_SPLIT_MODE_ROW: i8 = llama_cpp_sys_2::LLAMA_SPLIT_MODE_ROW as i8;
42#[allow(clippy::cast_possible_wrap)]
43#[allow(clippy::cast_possible_truncation)]
44const LLAMA_SPLIT_MODE_TENSOR: i8 = llama_cpp_sys_2::LLAMA_SPLIT_MODE_TENSOR as i8;
45
46/// A rusty wrapper around `llama_split_mode`.
47#[repr(i8)]
48#[derive(Copy, Clone, Debug, PartialEq, Eq)]
49pub enum LlamaSplitMode {
50    /// Single GPU
51    None = LLAMA_SPLIT_MODE_NONE,
52    /// Split layers and KV across GPUs
53    Layer = LLAMA_SPLIT_MODE_LAYER,
54    /// Split layers and KV across GPUs, use tensor parallelism if supported
55    Row = LLAMA_SPLIT_MODE_ROW,
56    /// Experimental tensor parallelism across GPUs
57    Tensor = LLAMA_SPLIT_MODE_TENSOR,
58}
59
60/// An error that occurs when unknown split mode is encountered.
61#[derive(Debug, Clone, Copy, PartialEq, Eq)]
62pub struct LlamaSplitModeParseError(pub i32);
63
64/// Create a `LlamaSplitMode` from a `i32`.
65///
66/// # Errors
67/// Returns `LlamaSplitModeParseError` if the value does not correspond to a valid `LlamaSplitMode`.
68impl TryFrom<i32> for LlamaSplitMode {
69    type Error = LlamaSplitModeParseError;
70
71    fn try_from(value: i32) -> Result<Self, Self::Error> {
72        let i8_value = value
73            .try_into()
74            .map_err(|_| LlamaSplitModeParseError(value))?;
75        match i8_value {
76            LLAMA_SPLIT_MODE_NONE => Ok(Self::None),
77            LLAMA_SPLIT_MODE_LAYER => Ok(Self::Layer),
78            LLAMA_SPLIT_MODE_ROW => Ok(Self::Row),
79            LLAMA_SPLIT_MODE_TENSOR => Ok(Self::Tensor),
80            _ => Err(LlamaSplitModeParseError(value)),
81        }
82    }
83}
84
85/// Create a `LlamaSplitMode` from a `u32`.
86///
87/// # Errors
88/// Returns `LlamaSplitModeParseError` if the value does not correspond to a valid `LlamaSplitMode`.
89impl TryFrom<u32> for LlamaSplitMode {
90    type Error = LlamaSplitModeParseError;
91
92    fn try_from(value: u32) -> Result<Self, Self::Error> {
93        let i8_value = value
94            .try_into()
95            .map_err(|_| LlamaSplitModeParseError(value.try_into().unwrap_or(i32::MAX)))?;
96        match i8_value {
97            LLAMA_SPLIT_MODE_NONE => Ok(Self::None),
98            LLAMA_SPLIT_MODE_LAYER => Ok(Self::Layer),
99            LLAMA_SPLIT_MODE_ROW => Ok(Self::Row),
100            LLAMA_SPLIT_MODE_TENSOR => Ok(Self::Tensor),
101            _ => Err(LlamaSplitModeParseError(
102                value.try_into().unwrap_or(i32::MAX),
103            )),
104        }
105    }
106}
107
108/// Create a `i32` from a `LlamaSplitMode`.
109impl From<LlamaSplitMode> for i32 {
110    fn from(value: LlamaSplitMode) -> Self {
111        match value {
112            LlamaSplitMode::None => LLAMA_SPLIT_MODE_NONE.into(),
113            LlamaSplitMode::Layer => LLAMA_SPLIT_MODE_LAYER.into(),
114            LlamaSplitMode::Row => LLAMA_SPLIT_MODE_ROW.into(),
115            LlamaSplitMode::Tensor => LLAMA_SPLIT_MODE_TENSOR.into(),
116        }
117    }
118}
119
120/// Create a `u32` from a `LlamaSplitMode`.
121impl From<LlamaSplitMode> for u32 {
122    fn from(value: LlamaSplitMode) -> Self {
123        match value {
124            LlamaSplitMode::None => LLAMA_SPLIT_MODE_NONE as u32,
125            LlamaSplitMode::Layer => LLAMA_SPLIT_MODE_LAYER as u32,
126            LlamaSplitMode::Row => LLAMA_SPLIT_MODE_ROW as u32,
127            LlamaSplitMode::Tensor => LLAMA_SPLIT_MODE_TENSOR as u32,
128        }
129    }
130}
131
132/// The default split mode is `Layer` in llama.cpp.
133impl Default for LlamaSplitMode {
134    fn default() -> Self {
135        LlamaSplitMode::Layer
136    }
137}
138
139/// The maximum number of devices supported.
140///
141/// The real maximum number of devices is the lesser one of this value and the value returned by
142/// `llama_cpp_2::max_devices()`.
143pub const LLAMA_CPP_MAX_DEVICES: usize = 16;
144
145/// A safe wrapper around `llama_model_params`.
146#[allow(clippy::module_name_repetitions)]
147pub struct LlamaModelParams {
148    pub(crate) params: llama_cpp_sys_2::llama_model_params,
149    kv_overrides: Vec<llama_cpp_sys_2::llama_model_kv_override>,
150    buft_overrides: Vec<llama_cpp_sys_2::llama_model_tensor_buft_override>,
151    devices: Pin<Box<[llama_cpp_sys_2::ggml_backend_dev_t; LLAMA_CPP_MAX_DEVICES]>>,
152    tensor_split: Vec<f32>,
153    progress_callback: Option<Box<dyn FnMut(f32) -> bool>>,
154}
155
156impl Debug for LlamaModelParams {
157    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
158        f.debug_struct("LlamaModelParams")
159            .field("n_gpu_layers", &self.params.n_gpu_layers)
160            .field("main_gpu", &self.params.main_gpu)
161            .field("vocab_only", &self.params.vocab_only)
162            .field("use_mmap", &self.params.use_mmap)
163            .field("use_mlock", &self.params.use_mlock)
164            .field("split_mode", &self.split_mode())
165            .field("devices", &self.devices)
166            .field("kv_overrides", &"vec of kv_overrides")
167            .finish()
168    }
169}
170
171impl LlamaModelParams {
172    /// See [`KvOverrides`]
173    ///
174    /// # Examples
175    ///
176    /// ```rust
177    /// # use llama_cpp_2::model::params::LlamaModelParams;
178    /// let params = Box::pin(LlamaModelParams::default());
179    /// let kv_overrides = params.kv_overrides();
180    /// let count = kv_overrides.into_iter().count();
181    /// assert_eq!(count, 0);
182    /// ```
183    #[must_use]
184    pub fn kv_overrides<'a>(&'a self) -> KvOverrides<'a> {
185        KvOverrides::new(self)
186    }
187
188    /// Appends a key-value override to the model parameters. It must be pinned as this creates a self-referential struct.
189    ///
190    /// # Examples
191    ///
192    /// ```rust
193    /// # use std::ffi::{CStr, CString};
194    /// use std::pin::pin;
195    /// # use llama_cpp_2::model::params::LlamaModelParams;
196    /// # use llama_cpp_2::model::params::kv_overrides::ParamOverrideValue;
197    /// let mut params = pin!(LlamaModelParams::default());
198    /// let key = CString::new("key").expect("CString::new failed");
199    /// params.as_mut().append_kv_override(&key, ParamOverrideValue::Int(50));
200    ///
201    /// let kv_overrides = params.kv_overrides().into_iter().collect::<Vec<_>>();
202    /// assert_eq!(kv_overrides.len(), 1);
203    ///
204    /// let (k, v) = &kv_overrides[0];
205    /// assert_eq!(v, &ParamOverrideValue::Int(50));
206    ///
207    /// assert_eq!(k.to_bytes(), b"key", "expected key to be 'key', was {:?}", k);
208    /// ```
209    #[allow(clippy::missing_panics_doc)] // panics are just to enforce internal invariants, not user errors
210    pub fn append_kv_override(
211        mut self: Pin<&mut Self>,
212        key: &CStr,
213        value: kv_overrides::ParamOverrideValue,
214    ) {
215        let kv_override = self
216            .kv_overrides
217            .get_mut(0)
218            .expect("kv_overrides did not have a next allocated");
219
220        assert_eq!(kv_override.key[0], 0, "last kv_override was not empty");
221
222        // There should be some way to do this without iterating over everything.
223        for (i, &c) in key.to_bytes_with_nul().iter().enumerate() {
224            kv_override.key[i] = c_char::try_from(c).expect("invalid character in key");
225        }
226
227        kv_override.tag = value.tag();
228        kv_override.__bindgen_anon_1 = value.value();
229
230        // set to null pointer for panic safety (as push may move the vector, invalidating the pointer)
231        self.params.kv_overrides = null();
232
233        // push the next one to ensure we maintain the iterator invariant of ending with a 0
234        self.kv_overrides
235            .push(llama_cpp_sys_2::llama_model_kv_override {
236                key: [0; 128],
237                tag: 0,
238                __bindgen_anon_1: llama_cpp_sys_2::llama_model_kv_override__bindgen_ty_1 {
239                    val_i64: 0,
240                },
241            });
242
243        // set the pointer to the (potentially) new vector
244        self.params.kv_overrides = self.kv_overrides.as_ptr();
245
246        eprintln!("saved ptr: {:?}", self.params.kv_overrides);
247    }
248}
249
250impl LlamaModelParams {
251    /// Adds buffer type overides to move all mixture-of-experts layers to CPU.
252    pub fn add_cpu_moe_override(self: Pin<&mut Self>) {
253        self.add_cpu_buft_override(c"\\.ffn_(up|down|gate)_(ch|)exps");
254    }
255
256    /// Appends a buffer type override to the model parameters, to move layers matching pattern to CPU.
257    /// It must be pinned as this creates a self-referential struct.
258    pub fn add_cpu_buft_override(mut self: Pin<&mut Self>, key: &CStr) {
259        let buft_override = self
260            .buft_overrides
261            .get_mut(0)
262            .expect("buft_overrides did not have a next allocated");
263
264        assert!(
265            buft_override.pattern.is_null(),
266            "last buft_override was not empty"
267        );
268
269        // There should be some way to do this without iterating over everything.
270        for &c in key.to_bytes_with_nul().iter() {
271            c_char::try_from(c).expect("invalid character in key");
272        }
273
274        buft_override.pattern = key.as_ptr();
275        buft_override.buft = unsafe { llama_cpp_sys_2::ggml_backend_cpu_buffer_type() };
276
277        // set to null pointer for panic safety (as push may move the vector, invalidating the pointer)
278        self.params.tensor_buft_overrides = null();
279
280        // push the next one to ensure we maintain the iterator invariant of ending with a 0
281        self.buft_overrides
282            .push(llama_cpp_sys_2::llama_model_tensor_buft_override {
283                pattern: std::ptr::null(),
284                buft: std::ptr::null_mut(),
285            });
286
287        // set the pointer to the (potentially) new vector
288        self.params.tensor_buft_overrides = self.buft_overrides.as_ptr();
289    }
290}
291
292#[cfg(feature = "common")]
293impl LlamaModelParams {
294    /// Automatically fit model parameters to available device memory.
295    ///
296    /// Wraps llama.cpp's `common_fit_params` (libcommon), which determines optimal `n_gpu_layers`,
297    /// `tensor_split`, and `tensor_buft_overrides` based on available VRAM. On success
298    /// the model and context params are updated in place.
299    ///
300    /// # Requirements
301    ///
302    /// Per the C API docstring, only parameters that still hold their default value
303    /// are modified. In practice this means:
304    /// - `n_gpu_layers` must be at its default (`-1`). Do not call
305    ///   [`with_n_gpu_layers`](Self::with_n_gpu_layers) before this.
306    /// - No `tensor_buft_overrides` may be set. Do not call
307    ///   [`add_cpu_buft_override`](Self::add_cpu_buft_override) or
308    ///   [`add_cpu_moe_override`](Self::add_cpu_moe_override) before this.
309    /// - `cparams.n_ctx` is only auto-selected if it is `0`; otherwise it is left alone.
310    ///
311    /// # Arguments
312    ///
313    /// - `model_path` — path to the GGUF model file.
314    /// - `cparams` — context parameters; `n_ctx` may be modified (see above).
315    /// - `margins` — memory margin per device in bytes. Must have at least
316    ///   `llama_max_devices()` elements.
317    /// - `n_ctx_min` — minimum context size to preserve when reducing memory usage.
318    /// - `log_level` — minimum log level for fitting output; lower levels are routed
319    ///   to the debug log.
320    ///
321    /// # Thread safety
322    ///
323    /// This function is **not** thread safe: the underlying C call mutates the global
324    /// llama logger state.
325    ///
326    /// # Errors
327    ///
328    /// Returns [`FitError::Failure`] if no fitting allocation could be found, or
329    /// [`FitError::Error`] on a hard error (e.g. the model file could not be read).
330    pub fn fit_params(
331        mut self: Pin<&mut Self>,
332        model_path: &CStr,
333        cparams: &mut LlamaContextParams,
334        margins: &mut [usize],
335        n_ctx_min: u32,
336        log_level: llama_cpp_sys_2::ggml_log_level,
337    ) -> Result<FitResult, FitError> {
338        let max_devices = unsafe { llama_cpp_sys_2::llama_max_devices() };
339        let max_buft = unsafe { llama_cpp_sys_2::llama_max_tensor_buft_overrides() };
340
341        // Allocate tensor_split output buffer.
342        self.tensor_split.clear();
343        self.tensor_split.resize(max_devices, 0.0);
344
345        // Reset and resize buft_overrides for fit output (null-terminated).
346        self.buft_overrides.clear();
347        self.buft_overrides.resize(
348            max_buft + 1,
349            llama_cpp_sys_2::llama_model_tensor_buft_override {
350                pattern: std::ptr::null(),
351                buft: std::ptr::null_mut(),
352            },
353        );
354
355        // Clear pointers before the call — fit writes directly into the buffers above.
356        self.params.tensor_split = null::<f32>();
357        self.params.tensor_buft_overrides = null();
358
359        let status = unsafe {
360            llama_cpp_sys_2::llama_rs_fit_params(
361                model_path.as_ptr(),
362                &raw mut self.params,
363                &raw mut cparams.context_params,
364                self.tensor_split.as_mut_ptr(),
365                self.buft_overrides.as_mut_ptr(),
366                margins.as_mut_ptr(),
367                n_ctx_min,
368                log_level,
369            )
370        };
371
372        // llama_rs_fit_params returns common_params_fit_status: 0 = success, 1 = failure, 2 = error.
373        match status {
374            0 => {}
375            1 => return Err(FitError::Failure),
376            _ => return Err(FitError::Error),
377        }
378
379        // Wire the owned buffers into the raw params.
380        self.params.tensor_split = self.tensor_split.as_ptr();
381        self.params.tensor_buft_overrides = self.buft_overrides.as_ptr();
382
383        Ok(FitResult {
384            n_ctx: cparams.context_params.n_ctx,
385        })
386    }
387}
388
389impl LlamaModelParams {
390    /// Get the number of layers to offload to the GPU.
391    #[must_use]
392    pub fn n_gpu_layers(&self) -> i32 {
393        self.params.n_gpu_layers
394    }
395
396    /// The GPU that is used for scratch and small tensors
397    #[must_use]
398    pub fn main_gpu(&self) -> i32 {
399        self.params.main_gpu
400    }
401
402    /// only load the vocabulary, no weights
403    #[must_use]
404    pub fn vocab_only(&self) -> bool {
405        self.params.vocab_only
406    }
407
408    /// use mmap if possible
409    #[must_use]
410    pub fn use_mmap(&self) -> bool {
411        self.params.use_mmap
412    }
413
414    /// force system to keep model in RAM
415    #[must_use]
416    pub fn use_mlock(&self) -> bool {
417        self.params.use_mlock
418    }
419
420    /// get the split mode
421    ///
422    /// # Errors
423    /// Returns `LlamaSplitModeParseError` if the unknown split mode is encountered.
424    pub fn split_mode(&self) -> Result<LlamaSplitMode, LlamaSplitModeParseError> {
425        LlamaSplitMode::try_from(self.params.split_mode)
426    }
427
428    /// get the devices
429    #[must_use]
430    pub fn devices(&self) -> Vec<usize> {
431        let mut backend_devices = Vec::new();
432        for i in 0..unsafe { llama_cpp_sys_2::ggml_backend_dev_count() } {
433            let dev = unsafe { llama_cpp_sys_2::ggml_backend_dev_get(i) };
434            backend_devices.push(dev);
435        }
436        let mut devices = Vec::new();
437        for &dev in self.devices.iter() {
438            if dev.is_null() {
439                break;
440            }
441            if let Some((index, _)) = backend_devices
442                .iter()
443                .enumerate()
444                .find(|&(_i, &d)| d == dev)
445            {
446                devices.push(index);
447            }
448        }
449        devices
450    }
451
452    /// sets the number of gpu layers to offload to the GPU.
453    /// ```
454    /// # use llama_cpp_2::model::params::LlamaModelParams;
455    /// let params = LlamaModelParams::default();
456    /// let params = params.with_n_gpu_layers(1);
457    /// assert_eq!(params.n_gpu_layers(), 1);
458    /// ```
459    #[must_use]
460    pub fn with_n_gpu_layers(mut self, n_gpu_layers: u32) -> Self {
461        // The only way this conversion can fail is if u32 overflows the i32 - in which case we set
462        // to MAX
463        let n_gpu_layers = i32::try_from(n_gpu_layers).unwrap_or(i32::MAX);
464        self.params.n_gpu_layers = n_gpu_layers;
465        self
466    }
467
468    /// sets the main GPU
469    ///
470    /// To enable this option, you must set `split_mode` to `LlamaSplitMode::None` to enable single GPU mode.
471    #[must_use]
472    pub fn with_main_gpu(mut self, main_gpu: i32) -> Self {
473        self.params.main_gpu = main_gpu;
474        self
475    }
476
477    /// sets `vocab_only`
478    #[must_use]
479    pub fn with_vocab_only(mut self, vocab_only: bool) -> Self {
480        self.params.vocab_only = vocab_only;
481        self
482    }
483
484    /// sets `use_mmap`
485    #[must_use]
486    pub fn with_use_mmap(mut self, use_mmap: bool) -> Self {
487        self.params.use_mmap = use_mmap;
488        self
489    }
490
491    /// sets `use_mlock`
492    #[must_use]
493    pub fn with_use_mlock(mut self, use_mlock: bool) -> Self {
494        self.params.use_mlock = use_mlock;
495        self
496    }
497
498    /// sets `split_mode`
499    #[must_use]
500    pub fn with_split_mode(mut self, split_mode: LlamaSplitMode) -> Self {
501        self.params.split_mode = split_mode.into();
502        self
503    }
504
505    /// sets `devices`
506    ///
507    /// The devices are specified as indices that correspond to the ggml backend device indices.
508    ///
509    /// The maximum number of devices is 16.
510    ///
511    /// You don't need to specify CPU or ACCEL devices.
512    ///
513    /// # Errors
514    /// Returns `LlamaCppError::BackendDeviceNotFound` if any device index is invalid.
515    pub fn with_devices(mut self, devices: &[usize]) -> Result<Self, LlamaCppError> {
516        for dev in self.devices.iter_mut() {
517            *dev = std::ptr::null_mut();
518        }
519        // Check device count
520        let max_devices = crate::max_devices().min(LLAMA_CPP_MAX_DEVICES);
521        if devices.len() > max_devices {
522            return Err(LlamaCppError::MaxDevicesExceeded(max_devices));
523        }
524        for (i, &dev) in devices.iter().enumerate() {
525            if dev >= unsafe { llama_cpp_sys_2::ggml_backend_dev_count() } {
526                return Err(LlamaCppError::BackendDeviceNotFound(dev));
527            }
528            let backend_dev = unsafe { llama_cpp_sys_2::ggml_backend_dev_get(dev) };
529            self.devices[i] = backend_dev;
530        }
531        if self.devices.is_empty() {
532            self.params.devices = std::ptr::null_mut();
533        } else {
534            self.params.devices = self.devices.as_mut_ptr();
535        }
536        Ok(self)
537    }
538
539    /// Set `no_alloc`
540    ///
541    /// If this parameter is true, don't allocate memory for the tensor data
542    ///
543    /// You can't use `no_alloc` with `use_mmap`, so this also sets `use_mmap` to false.
544    #[must_use]
545    pub fn with_no_alloc(mut self, no_alloc: bool) -> Self {
546        self.params.no_alloc = no_alloc;
547        if no_alloc {
548            self = self.with_use_mmap(false);
549        }
550        self
551    }
552
553    /// Get `no_alloc`
554    ///
555    /// If this parameter is true, don't allocate memory for the tensor data
556    #[must_use]
557    pub fn no_alloc(&self) -> bool {
558        self.params.no_alloc
559    }
560
561    /// Sets a callback invoked during loading with progress in `0.0..=1.0`.
562    /// Returning `false` aborts the load (it then fails with `NullResult`).
563    #[must_use]
564    pub fn with_progress_callback<F: FnMut(f32) -> bool + 'static>(mut self, callback: F) -> Self {
565        unsafe extern "C" fn trampoline<F: FnMut(f32) -> bool>(
566            progress: f32,
567            user_data: *mut c_void,
568        ) -> bool {
569            let callback = unsafe { &mut *user_data.cast::<F>() };
570            callback(progress)
571        }
572
573        let mut callback = Box::new(callback);
574        self.params.progress_callback_user_data = std::ptr::from_mut(&mut *callback).cast::<c_void>();
575        self.params.progress_callback = Some(trampoline::<F>);
576        self.progress_callback = Some(callback);
577        self
578    }
579}
580
581/// Default parameters for `LlamaModel`. (as defined in llama.cpp by `llama_model_default_params`)
582/// ```
583/// # use llama_cpp_2::model::params::LlamaModelParams;
584/// use llama_cpp_2::model::params::LlamaSplitMode;
585/// let params = LlamaModelParams::default();
586/// assert_eq!(params.n_gpu_layers(), -1, "n_gpu_layers should be -1");
587/// assert_eq!(params.main_gpu(), 0, "main_gpu should be 0");
588/// assert_eq!(params.vocab_only(), false, "vocab_only should be false");
589/// assert_eq!(params.use_mmap(), true, "use_mmap should be true");
590/// assert_eq!(params.use_mlock(), false, "use_mlock should be false");
591/// assert_eq!(params.split_mode(), Ok(LlamaSplitMode::Layer), "split_mode should be LAYER");
592/// assert_eq!(params.devices().len(), 0, "devices should be empty");
593/// assert_eq!(params.no_alloc(), false, "no_alloc should be false");
594/// ```
595impl Default for LlamaModelParams {
596    fn default() -> Self {
597        let default_params = unsafe { llama_cpp_sys_2::llama_model_default_params() };
598        LlamaModelParams {
599            params: default_params,
600            // push the next one to ensure we maintain the iterator invariant of ending with a 0
601            kv_overrides: vec![llama_cpp_sys_2::llama_model_kv_override {
602                key: [0; 128],
603                tag: 0,
604                __bindgen_anon_1: llama_cpp_sys_2::llama_model_kv_override__bindgen_ty_1 {
605                    val_i64: 0,
606                },
607            }],
608            buft_overrides: vec![llama_cpp_sys_2::llama_model_tensor_buft_override {
609                pattern: std::ptr::null(),
610                buft: std::ptr::null_mut(),
611            }],
612            devices: Box::pin([std::ptr::null_mut(); 16]),
613            tensor_split: Vec::new(),
614            progress_callback: None,
615        }
616    }
617}
618
619#[cfg(test)]
620mod tests {
621    use super::LlamaSplitMode;
622
623    #[test]
624    fn tensor_split_mode_round_trips() {
625        assert_eq!(
626            LlamaSplitMode::try_from(llama_cpp_sys_2::LLAMA_SPLIT_MODE_TENSOR),
627            Ok(LlamaSplitMode::Tensor)
628        );
629        assert_eq!(
630            u32::from(LlamaSplitMode::Tensor),
631            llama_cpp_sys_2::LLAMA_SPLIT_MODE_TENSOR as u32
632        );
633        assert_eq!(
634            i32::from(LlamaSplitMode::Tensor),
635            llama_cpp_sys_2::LLAMA_SPLIT_MODE_TENSOR as i32
636        );
637    }
638
639    #[test]
640    fn progress_callback_round_trips_and_can_abort() {
641        use super::LlamaModelParams;
642        use std::cell::Cell;
643        use std::rc::Rc;
644
645        let calls = Rc::new(Cell::new(0_u32));
646        let counter = Rc::clone(&calls);
647        let params = LlamaModelParams::default().with_progress_callback(move |_progress| {
648            counter.set(counter.get() + 1);
649            false
650        });
651
652        assert!(params.params.progress_callback.is_some());
653        assert!(!params.params.progress_callback_user_data.is_null());
654
655        let trampoline = params.params.progress_callback.unwrap();
656        let user_data = params.params.progress_callback_user_data;
657        let first = unsafe { trampoline(0.5, user_data) };
658        let second = unsafe { trampoline(1.0, user_data) };
659
660        assert!(!first && !second, "returning false signals an abort");
661        assert_eq!(calls.get(), 2);
662    }
663}