Skip to main content

ik_llama_cpp_2/model/
params.rs

1//! Model loading parameters ([`LlamaModelParams`]) over ik's `llama_model_params`.
2
3use ik_llama_cpp_sys as sys;
4
5/// Parameters controlling how a model is loaded.
6///
7/// Starts from `llama_model_default_params()`; builder methods override the
8/// fields v1 needs. ik's `llama_model_params` carries many fork-specific fields
9/// (mla, ncmoe, fit, repack_tensors, per-layer K/V types, …); those keep their
10/// defaults unless exposed here.
11#[derive(Debug, Clone)]
12pub struct LlamaModelParams {
13    pub(crate) params: sys::llama_model_params,
14}
15
16impl Default for LlamaModelParams {
17    fn default() -> Self {
18        // SAFETY: returns a fully-initialized POD struct by value.
19        Self {
20            params: unsafe { sys::llama_model_default_params() },
21        }
22    }
23}
24
25impl LlamaModelParams {
26    /// Number of layers to offload to the GPU (0 = CPU only).
27    ///
28    /// Takes `u32` to match `llama-cpp-2`; clamped into ik's `i32` field.
29    #[must_use]
30    pub fn with_n_gpu_layers(mut self, n: u32) -> Self {
31        self.params.n_gpu_layers = i32::try_from(n).unwrap_or(i32::MAX);
32        self
33    }
34
35    /// Whether to memory-map the model file (default true).
36    #[must_use]
37    pub fn with_use_mmap(mut self, use_mmap: bool) -> Self {
38        self.params.use_mmap = use_mmap;
39        self
40    }
41
42    /// Force the model into RAM (mlock).
43    #[must_use]
44    pub fn with_use_mlock(mut self, use_mlock: bool) -> Self {
45        self.params.use_mlock = use_mlock;
46        self
47    }
48
49    /// Load only the vocabulary (no weights).
50    #[must_use]
51    pub fn with_vocab_only(mut self, vocab_only: bool) -> Self {
52        self.params.vocab_only = vocab_only;
53        self
54    }
55
56    /// Load the MTP / NextN prediction layers if present in the model.
57    #[must_use]
58    pub fn with_mtp(mut self, mtp: bool) -> Self {
59        self.params.mtp = mtp;
60        self
61    }
62
63    /// Access the raw params (advanced/escape hatch).
64    #[must_use]
65    pub fn as_raw(&self) -> &sys::llama_model_params {
66        &self.params
67    }
68}