Skip to main content

llama_cpp_4/context/params/
advanced.rs

1use super::{LlamaAttentionType, LlamaContextParams, LlamaFlashAttnType};
2use crate::sampling::LlamaSampler;
3
4impl LlamaContextParams {
5    /// Set the flash-attention mode (`Auto`, `Enabled`, or `Disabled`).
6    ///
7    /// Maps to `llama_context_params.flash_attn_type`. Use
8    /// [`LlamaFlashAttnType::Auto`] to match llama.cpp defaults.
9    ///
10    /// # Examples
11    ///
12    /// ```rust
13    /// use llama_cpp_4::context::params::{LlamaContextParams, LlamaFlashAttnType};
14    /// let params = LlamaContextParams::default()
15    ///     .with_flash_attn_type(LlamaFlashAttnType::Auto);
16    /// assert_eq!(params.flash_attn_type(), LlamaFlashAttnType::Auto);
17    /// ```
18    #[must_use]
19    pub fn with_flash_attn_type(mut self, flash_attn_type: LlamaFlashAttnType) -> Self {
20        self.context_params.flash_attn_type = flash_attn_type.into();
21        self
22    }
23
24    /// Get the configured flash-attention mode.
25    #[must_use]
26    pub fn flash_attn_type(&self) -> LlamaFlashAttnType {
27        LlamaFlashAttnType::from(self.context_params.flash_attn_type)
28    }
29
30    /// Set the attention type used when extracting embeddings.
31    ///
32    /// Maps to `llama_context_params.attention_type`. Embedding models often
33    /// need [`LlamaAttentionType::NonCausal`]; generative decoding uses
34    /// [`LlamaAttentionType::Causal`].
35    ///
36    /// # Examples
37    ///
38    /// ```rust
39    /// use llama_cpp_4::context::params::{LlamaAttentionType, LlamaContextParams};
40    /// let params = LlamaContextParams::default()
41    ///     .with_attention_type(LlamaAttentionType::Causal);
42    /// assert_eq!(params.attention_type(), LlamaAttentionType::Causal);
43    /// ```
44    #[must_use]
45    pub fn with_attention_type(mut self, attention_type: LlamaAttentionType) -> Self {
46        self.context_params.attention_type = attention_type.into();
47        self
48    }
49
50    /// Get the attention type used when extracting embeddings.
51    #[must_use]
52    pub fn attention_type(&self) -> LlamaAttentionType {
53        LlamaAttentionType::from(self.context_params.attention_type)
54    }
55
56    /// Set the maximum number of outputs per micro-batch.
57    ///
58    /// Maps to `llama_context_params.n_outputs_max`. When `0`, llama.cpp uses
59    /// `n_batch` as the cap.
60    ///
61    /// # Examples
62    ///
63    /// ```rust
64    /// use llama_cpp_4::context::params::LlamaContextParams;
65    /// let params = LlamaContextParams::default().with_n_outputs_max(256);
66    /// assert_eq!(params.n_outputs_max(), 256);
67    /// ```
68    #[must_use]
69    pub fn with_n_outputs_max(mut self, n_outputs_max: u32) -> Self {
70        self.context_params.n_outputs_max = n_outputs_max;
71        self
72    }
73
74    /// Get the maximum number of outputs per micro-batch.
75    #[must_use]
76    pub fn n_outputs_max(&self) -> u32 {
77        self.context_params.n_outputs_max
78    }
79
80    /// Set the maximum number of outputs per sequence.
81    ///
82    /// Maps to `llama_context_params.n_outputs_max_per_seq`. **Defaults to `1`**
83    /// — a single output per sequence; pass `0` to fall back to
84    /// [`Self::n_outputs_max`] instead. Backend samplers are initialized for
85    /// this many outputs per sequence, so multi-output backend sampling (e.g.
86    /// speculative decoding) must raise it above `1`. llama.cpp clamps the
87    /// value to `n_outputs_max`.
88    ///
89    /// # Examples
90    ///
91    /// ```rust
92    /// use llama_cpp_4::context::params::LlamaContextParams;
93    /// let params = LlamaContextParams::default().with_n_outputs_max_per_seq(16);
94    /// assert_eq!(params.n_outputs_max_per_seq(), 16);
95    /// ```
96    #[must_use]
97    pub fn with_n_outputs_max_per_seq(mut self, n_outputs_max_per_seq: u32) -> Self {
98        self.context_params.n_outputs_max_per_seq = n_outputs_max_per_seq;
99        self
100    }
101
102    /// Get the maximum number of outputs per sequence.
103    #[must_use]
104    pub fn n_outputs_max_per_seq(&self) -> u32 {
105        self.context_params.n_outputs_max_per_seq
106    }
107
108    /// Set the device-side cache for `MoE` experts that live in host memory, in
109    /// bytes. `0` (the default) disables it.
110    ///
111    /// Maps to `llama_context_params.moe_cache_size`
112    /// ([llama.cpp #29887](https://github.com/ggml-org/llama.cpp/pull/29887),
113    /// spread across devices in
114    /// [#30112](https://github.com/ggml-org/llama.cpp/pull/30112)). This is for
115    /// the case where a mixture-of-experts model does not fit in VRAM and some
116    /// expert tensors stay in RAM: instead of streaming every expert across the
117    /// bus on every token, llama.cpp keeps the hottest ones in a GPU cache of
118    /// this size. The budget is split across devices the same way layers are,
119    /// so it is a total and not a per-device figure.
120    ///
121    /// # A non-zero budget is a requirement, not a hint
122    ///
123    /// llama.cpp refuses to build a context it cannot honour the budget on, so
124    /// [`new_context`](crate::model::LlamaModel::new_context) then fails with
125    /// [`LlamaContextLoadError::NullReturn`](crate::LlamaContextLoadError::NullReturn)
126    /// and the reason in the llama.cpp log. All of these must hold:
127    ///
128    /// - a GPU or integrated-GPU backend is present — a CPU-only build, or no
129    ///   offloaded layers, has nowhere to put the cache;
130    /// - the model is a mixture of experts;
131    /// - the split mode is not tensor/row split;
132    /// - the budget is big enough for one token's experts in at least one layer
133    ///   whose experts are all host-resident.
134    ///
135    /// So leave it at `0` unless you know the model and device layout call for
136    /// it; it is not safe to set speculatively.
137    ///
138    /// # Examples
139    ///
140    /// ```rust
141    /// use llama_cpp_4::context::params::LlamaContextParams;
142    /// let params = LlamaContextParams::default()
143    ///     .with_moe_cache_size(2 * 1024 * 1024 * 1024);
144    /// assert_eq!(params.moe_cache_size(), 2 * 1024 * 1024 * 1024);
145    /// ```
146    #[must_use]
147    pub fn with_moe_cache_size(mut self, bytes: usize) -> Self {
148        self.context_params.moe_cache_size = bytes;
149        self
150    }
151
152    /// Get the device-side `MoE` expert cache size in bytes. `0` means disabled.
153    #[must_use]
154    pub fn moe_cache_size(&self) -> usize {
155        self.context_params.moe_cache_size
156    }
157
158    /// Use a unified KV buffer across input sequences.
159    ///
160    /// Maps to `llama_context_params.kv_unified`. Disabling can improve
161    /// throughput for batched decoding when sequences do not share a long prefix.
162    ///
163    /// # Examples
164    ///
165    /// ```rust
166    /// use llama_cpp_4::context::params::LlamaContextParams;
167    /// let params = LlamaContextParams::default().with_kv_unified(false);
168    /// assert!(!params.kv_unified());
169    /// ```
170    #[must_use]
171    pub fn with_kv_unified(mut self, kv_unified: bool) -> Self {
172        self.context_params.kv_unified = kv_unified;
173        self
174    }
175
176    /// Returns `true` when a unified KV buffer is enabled.
177    #[must_use]
178    pub fn kv_unified(&self) -> bool {
179        self.context_params.kv_unified
180    }
181
182    /// Use a full-size sliding-window-attention (SWA) KV cache.
183    ///
184    /// Maps to `llama_context_params.swa_full`. When `false` and `n_seq_max > 1`,
185    /// llama.cpp may use a smaller per-sequence SWA window for better performance.
186    ///
187    /// # Examples
188    ///
189    /// ```rust
190    /// use llama_cpp_4::context::params::LlamaContextParams;
191    /// let params = LlamaContextParams::default().with_swa_full(true);
192    /// assert!(params.swa_full());
193    /// ```
194    #[must_use]
195    pub fn with_swa_full(mut self, swa_full: bool) -> Self {
196        self.context_params.swa_full = swa_full;
197        self
198    }
199
200    /// Returns `true` when full SWA cache is enabled.
201    #[must_use]
202    pub fn swa_full(&self) -> bool {
203        self.context_params.swa_full
204    }
205
206    /// Offload eligible host tensor operations to the active device.
207    ///
208    /// Maps to `llama_context_params.op_offload`.
209    ///
210    /// # Examples
211    ///
212    /// ```rust
213    /// use llama_cpp_4::context::params::LlamaContextParams;
214    /// let params = LlamaContextParams::default().with_op_offload(true);
215    /// assert!(params.op_offload());
216    /// ```
217    #[must_use]
218    pub fn with_op_offload(mut self, op_offload: bool) -> Self {
219        self.context_params.op_offload = op_offload;
220        self
221    }
222
223    /// Returns `true` when host tensor ops are offloaded to device.
224    #[must_use]
225    pub fn op_offload(&self) -> bool {
226        self.context_params.op_offload
227    }
228
229    /// Pair this context with another for shared memory or cross-context results.
230    ///
231    /// Maps to `llama_context_params.ctx_other`. The paired context is returned
232    /// by [`crate::context::LlamaContext::ctx_other`] after creation.
233    ///
234    /// # Safety
235    ///
236    /// llama.cpp stores the pointer to `other` in the new context's parameters
237    /// and reads through it for as long as that context lives — every decode
238    /// that consults the paired context's buffers, not just the construction
239    /// call. So `other` must outlive the context this builds. Dropping it
240    /// first and decoding again reads freed memory.
241    ///
242    /// This is the same contract as
243    /// [`LlamaContext::set_ctx_state_src`](crate::context::LlamaContext::set_ctx_state_src),
244    /// which pairs contexts the same way after construction.
245    ///
246    /// # Examples
247    ///
248    /// ```no_run
249    /// # use llama_cpp_4::context::params::LlamaContextParams;
250    /// # use llama_cpp_4::llama_backend::LlamaBackend;
251    /// # use llama_cpp_4::model::LlamaModel;
252    /// # fn f(backend: &LlamaBackend, model: &LlamaModel)
253    /// #     -> Result<(), Box<dyn std::error::Error>> {
254    /// let target = model.new_context(&backend, LlamaContextParams::default())?;
255    /// // SAFETY: `target` is declared before `draft`, so it is dropped after
256    /// // it and outlives every decode `draft` performs.
257    /// let draft = model.new_context(
258    ///     &backend,
259    ///     unsafe { LlamaContextParams::default().with_ctx_other(&target) },
260    /// )?;
261    /// # Ok(())
262    /// # }
263    /// ```
264    #[must_use]
265    pub unsafe fn with_ctx_other(mut self, other: &crate::context::LlamaContext<'_>) -> Self {
266        self.context_params.ctx_other = other.context.as_ptr();
267        self
268    }
269
270    /// Set `YaRN` extrapolation mix factor.
271    ///
272    /// Maps to `llama_context_params.yarn_ext_factor`. Negative values use the
273    /// model default. Only meaningful when [`super::RopeScalingType::Yarn`] is active.
274    ///
275    /// # Examples
276    ///
277    /// ```rust
278    /// use llama_cpp_4::context::params::LlamaContextParams;
279    /// let params = LlamaContextParams::default().with_yarn_ext_factor(1.0);
280    /// assert_eq!(params.yarn_ext_factor(), 1.0);
281    /// ```
282    #[must_use]
283    pub fn with_yarn_ext_factor(mut self, yarn_ext_factor: f32) -> Self {
284        self.context_params.yarn_ext_factor = yarn_ext_factor;
285        self
286    }
287
288    /// Get `YaRN` extrapolation mix factor (`yarn_ext_factor`).
289    #[must_use]
290    pub fn yarn_ext_factor(&self) -> f32 {
291        self.context_params.yarn_ext_factor
292    }
293
294    /// Set `YaRN` magnitude scaling factor.
295    ///
296    /// Maps to `llama_context_params.yarn_attn_factor`.
297    ///
298    /// # Examples
299    ///
300    /// ```rust
301    /// use llama_cpp_4::context::params::LlamaContextParams;
302    /// let params = LlamaContextParams::default().with_yarn_attn_factor(1.0);
303    /// assert_eq!(params.yarn_attn_factor(), 1.0);
304    /// ```
305    #[must_use]
306    pub fn with_yarn_attn_factor(mut self, yarn_attn_factor: f32) -> Self {
307        self.context_params.yarn_attn_factor = yarn_attn_factor;
308        self
309    }
310
311    /// Get `YaRN` magnitude scaling factor (`yarn_attn_factor`).
312    #[must_use]
313    pub fn yarn_attn_factor(&self) -> f32 {
314        self.context_params.yarn_attn_factor
315    }
316
317    /// Set `YaRN` low correction dimension (`yarn_beta_fast`).
318    ///
319    /// Maps to `llama_context_params.yarn_beta_fast`.
320    #[must_use]
321    pub fn with_yarn_beta_fast(mut self, yarn_beta_fast: f32) -> Self {
322        self.context_params.yarn_beta_fast = yarn_beta_fast;
323        self
324    }
325
326    /// Get `YaRN` low correction dimension.
327    #[must_use]
328    pub fn yarn_beta_fast(&self) -> f32 {
329        self.context_params.yarn_beta_fast
330    }
331
332    /// Set `YaRN` high correction dimension (`yarn_beta_slow`).
333    ///
334    /// Maps to `llama_context_params.yarn_beta_slow`.
335    #[must_use]
336    pub fn with_yarn_beta_slow(mut self, yarn_beta_slow: f32) -> Self {
337        self.context_params.yarn_beta_slow = yarn_beta_slow;
338        self
339    }
340
341    /// Get `YaRN` high correction dimension.
342    #[must_use]
343    pub fn yarn_beta_slow(&self) -> f32 {
344        self.context_params.yarn_beta_slow
345    }
346
347    /// Set `YaRN` original context size.
348    ///
349    /// Maps to `llama_context_params.yarn_orig_ctx`. `0` uses the model default.
350    ///
351    /// # Examples
352    ///
353    /// ```rust
354    /// use llama_cpp_4::context::params::LlamaContextParams;
355    /// let params = LlamaContextParams::default().with_yarn_orig_ctx(8192);
356    /// assert_eq!(params.yarn_orig_ctx(), 8192);
357    /// ```
358    #[must_use]
359    pub fn with_yarn_orig_ctx(mut self, yarn_orig_ctx: u32) -> Self {
360        self.context_params.yarn_orig_ctx = yarn_orig_ctx;
361        self
362    }
363
364    /// Get `YaRN` original context size (`yarn_orig_ctx`).
365    #[must_use]
366    pub fn yarn_orig_ctx(&self) -> u32 {
367        self.context_params.yarn_orig_ctx
368    }
369
370    /// Disable performance timing collection for this context.
371    ///
372    /// Maps to `llama_context_params.no_perf`. When `true`, calls such as
373    /// [`crate::context::LlamaContext::timings`] return empty counters.
374    ///
375    /// # Examples
376    ///
377    /// ```rust
378    /// use llama_cpp_4::context::params::LlamaContextParams;
379    /// let params = LlamaContextParams::default().with_no_perf(true);
380    /// assert!(params.no_perf());
381    /// ```
382    #[must_use]
383    pub fn with_no_perf(mut self, no_perf: bool) -> Self {
384        self.context_params.no_perf = no_perf;
385        self
386    }
387
388    /// Returns `true` when perf timings are disabled for this context.
389    #[must_use]
390    pub fn no_perf(&self) -> bool {
391        self.context_params.no_perf
392    }
393
394    /// Register an abort callback checked during `decode()` on CPU backends.
395    ///
396    /// Maps to `llama_context_params.abort_callback` / `abort_callback_data`.
397    /// The callback is invoked periodically during long decodes; return a
398    /// non-zero value to stop the current operation.
399    ///
400    /// `user_data` is passed through unchanged and must remain valid for the
401    /// lifetime of any context created from these params.
402    #[must_use]
403    pub fn with_abort_callback(
404        mut self,
405        callback: llama_cpp_sys_4::ggml_abort_callback,
406        user_data: *mut std::ffi::c_void,
407    ) -> Self {
408        self.context_params.abort_callback = callback;
409        self.context_params.abort_callback_data = user_data;
410        self
411    }
412
413    /// Assign per-sequence backend sampler chains.
414    ///
415    /// Maps to `llama_context_params.samplers` / `n_samplers`. Each
416    /// [`LlamaSampler`] must be a sampler **chain** created with
417    /// `llama_sampler_chain_init`. The samplers are kept alive inside these
418    /// params until [`crate::model::LlamaModel::new_context`] returns.
419    ///
420    /// Pair sequence ids with the chains that should run when decoding those
421    /// sequences on the backend.
422    ///
423    /// # Examples
424    ///
425    /// ```no_run
426    /// use llama_cpp_4::context::params::LlamaContextParams;
427    /// use llama_cpp_4::sampling::LlamaSampler;
428    /// # use llama_cpp_4::model::LlamaModel;
429    /// # fn f(model: &LlamaModel) -> Result<(), Box<dyn std::error::Error>> {
430    /// let chain = LlamaSampler::chain_simple([LlamaSampler::greedy()]);
431    /// let params = LlamaContextParams::default()
432    ///     .with_sampler_seq_configs([(0, chain)]);
433    /// assert_eq!(params.n_sampler_seq_configs(), 1);
434    /// # Ok(())
435    /// # }
436    /// ```
437    #[must_use]
438    pub fn with_sampler_seq_configs(
439        mut self,
440        configs: impl IntoIterator<Item = (i32, LlamaSampler)>,
441    ) -> Self {
442        self.owned_samplers.clear();
443        self.sampler_configs.clear();
444
445        for (seq_id, sampler) in configs {
446            self.sampler_configs
447                .push(llama_cpp_sys_4::llama_sampler_seq_config {
448                    seq_id,
449                    sampler: sampler.sampler.as_ptr(),
450                });
451            self.owned_samplers.push(sampler);
452        }
453
454        if self.sampler_configs.is_empty() {
455            self.context_params.samplers = std::ptr::null_mut();
456            self.context_params.n_samplers = 0;
457        } else {
458            self.context_params.samplers = self.sampler_configs.as_mut_ptr();
459            self.context_params.n_samplers = self.sampler_configs.len();
460        }
461
462        self
463    }
464
465    /// Number of per-sequence sampler configs attached to these params.
466    ///
467    /// Returns `0` when no chains were set or after [`Clone`] (sampler chains
468    /// are not duplicated).
469    #[must_use]
470    pub fn n_sampler_seq_configs(&self) -> usize {
471        self.sampler_configs.len()
472    }
473}