1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
use super::{LlamaAttentionType, LlamaContextParams, LlamaFlashAttnType};
use crate::sampling::LlamaSampler;
impl LlamaContextParams {
/// Set the flash-attention mode (`Auto`, `Enabled`, or `Disabled`).
///
/// Maps to `llama_context_params.flash_attn_type`. Use
/// [`LlamaFlashAttnType::Auto`] to match llama.cpp defaults.
///
/// # Examples
///
/// ```rust
/// use llama_cpp_4::context::params::{LlamaContextParams, LlamaFlashAttnType};
/// let params = LlamaContextParams::default()
/// .with_flash_attn_type(LlamaFlashAttnType::Auto);
/// assert_eq!(params.flash_attn_type(), LlamaFlashAttnType::Auto);
/// ```
#[must_use]
pub fn with_flash_attn_type(mut self, flash_attn_type: LlamaFlashAttnType) -> Self {
self.context_params.flash_attn_type = flash_attn_type.into();
self
}
/// Get the configured flash-attention mode.
#[must_use]
pub fn flash_attn_type(&self) -> LlamaFlashAttnType {
LlamaFlashAttnType::from(self.context_params.flash_attn_type)
}
/// Set the attention type used when extracting embeddings.
///
/// Maps to `llama_context_params.attention_type`. Embedding models often
/// need [`LlamaAttentionType::NonCausal`]; generative decoding uses
/// [`LlamaAttentionType::Causal`].
///
/// # Examples
///
/// ```rust
/// use llama_cpp_4::context::params::{LlamaAttentionType, LlamaContextParams};
/// let params = LlamaContextParams::default()
/// .with_attention_type(LlamaAttentionType::Causal);
/// assert_eq!(params.attention_type(), LlamaAttentionType::Causal);
/// ```
#[must_use]
pub fn with_attention_type(mut self, attention_type: LlamaAttentionType) -> Self {
self.context_params.attention_type = attention_type.into();
self
}
/// Get the attention type used when extracting embeddings.
#[must_use]
pub fn attention_type(&self) -> LlamaAttentionType {
LlamaAttentionType::from(self.context_params.attention_type)
}
/// Set the maximum number of outputs per micro-batch.
///
/// Maps to `llama_context_params.n_outputs_max`. When `0`, llama.cpp uses
/// `n_batch` as the cap.
///
/// # Examples
///
/// ```rust
/// use llama_cpp_4::context::params::LlamaContextParams;
/// let params = LlamaContextParams::default().with_n_outputs_max(256);
/// assert_eq!(params.n_outputs_max(), 256);
/// ```
#[must_use]
pub fn with_n_outputs_max(mut self, n_outputs_max: u32) -> Self {
self.context_params.n_outputs_max = n_outputs_max;
self
}
/// Get the maximum number of outputs per micro-batch.
#[must_use]
pub fn n_outputs_max(&self) -> u32 {
self.context_params.n_outputs_max
}
/// Use a unified KV buffer across input sequences.
///
/// Maps to `llama_context_params.kv_unified`. Disabling can improve
/// throughput for batched decoding when sequences do not share a long prefix.
///
/// # Examples
///
/// ```rust
/// use llama_cpp_4::context::params::LlamaContextParams;
/// let params = LlamaContextParams::default().with_kv_unified(false);
/// assert!(!params.kv_unified());
/// ```
#[must_use]
pub fn with_kv_unified(mut self, kv_unified: bool) -> Self {
self.context_params.kv_unified = kv_unified;
self
}
/// Returns `true` when a unified KV buffer is enabled.
#[must_use]
pub fn kv_unified(&self) -> bool {
self.context_params.kv_unified
}
/// Use a full-size sliding-window-attention (SWA) KV cache.
///
/// Maps to `llama_context_params.swa_full`. When `false` and `n_seq_max > 1`,
/// llama.cpp may use a smaller per-sequence SWA window for better performance.
///
/// # Examples
///
/// ```rust
/// use llama_cpp_4::context::params::LlamaContextParams;
/// let params = LlamaContextParams::default().with_swa_full(true);
/// assert!(params.swa_full());
/// ```
#[must_use]
pub fn with_swa_full(mut self, swa_full: bool) -> Self {
self.context_params.swa_full = swa_full;
self
}
/// Returns `true` when full SWA cache is enabled.
#[must_use]
pub fn swa_full(&self) -> bool {
self.context_params.swa_full
}
/// Offload eligible host tensor operations to the active device.
///
/// Maps to `llama_context_params.op_offload`.
///
/// # Examples
///
/// ```rust
/// use llama_cpp_4::context::params::LlamaContextParams;
/// let params = LlamaContextParams::default().with_op_offload(true);
/// assert!(params.op_offload());
/// ```
#[must_use]
pub fn with_op_offload(mut self, op_offload: bool) -> Self {
self.context_params.op_offload = op_offload;
self
}
/// Returns `true` when host tensor ops are offloaded to device.
#[must_use]
pub fn op_offload(&self) -> bool {
self.context_params.op_offload
}
/// Pair this context with another for shared memory or cross-context results.
///
/// Maps to `llama_context_params.ctx_other`. The paired context is returned
/// by [`crate::context::LlamaContext::ctx_other`] after creation.
///
/// `other` must remain alive until [`crate::model::LlamaModel::new_context`]
/// returns.
///
/// # Examples
///
/// ```ignore
/// let target = model.new_context(&backend, LlamaContextParams::default())?;
/// let draft = model.new_context(
/// &backend,
/// LlamaContextParams::default().with_ctx_other(&target),
/// )?;
/// ```
#[must_use]
pub fn with_ctx_other(mut self, other: &crate::context::LlamaContext<'_>) -> Self {
self.context_params.ctx_other = other.context.as_ptr();
self
}
/// Set `YaRN` extrapolation mix factor.
///
/// Maps to `llama_context_params.yarn_ext_factor`. Negative values use the
/// model default. Only meaningful when [`super::RopeScalingType::Yarn`] is active.
///
/// # Examples
///
/// ```rust
/// use llama_cpp_4::context::params::LlamaContextParams;
/// let params = LlamaContextParams::default().with_yarn_ext_factor(1.0);
/// assert_eq!(params.yarn_ext_factor(), 1.0);
/// ```
#[must_use]
pub fn with_yarn_ext_factor(mut self, yarn_ext_factor: f32) -> Self {
self.context_params.yarn_ext_factor = yarn_ext_factor;
self
}
/// Get `YaRN` extrapolation mix factor (`yarn_ext_factor`).
#[must_use]
pub fn yarn_ext_factor(&self) -> f32 {
self.context_params.yarn_ext_factor
}
/// Set `YaRN` magnitude scaling factor.
///
/// Maps to `llama_context_params.yarn_attn_factor`.
///
/// # Examples
///
/// ```rust
/// use llama_cpp_4::context::params::LlamaContextParams;
/// let params = LlamaContextParams::default().with_yarn_attn_factor(1.0);
/// assert_eq!(params.yarn_attn_factor(), 1.0);
/// ```
#[must_use]
pub fn with_yarn_attn_factor(mut self, yarn_attn_factor: f32) -> Self {
self.context_params.yarn_attn_factor = yarn_attn_factor;
self
}
/// Get `YaRN` magnitude scaling factor (`yarn_attn_factor`).
#[must_use]
pub fn yarn_attn_factor(&self) -> f32 {
self.context_params.yarn_attn_factor
}
/// Set `YaRN` low correction dimension (`yarn_beta_fast`).
///
/// Maps to `llama_context_params.yarn_beta_fast`.
#[must_use]
pub fn with_yarn_beta_fast(mut self, yarn_beta_fast: f32) -> Self {
self.context_params.yarn_beta_fast = yarn_beta_fast;
self
}
/// Get `YaRN` low correction dimension.
#[must_use]
pub fn yarn_beta_fast(&self) -> f32 {
self.context_params.yarn_beta_fast
}
/// Set `YaRN` high correction dimension (`yarn_beta_slow`).
///
/// Maps to `llama_context_params.yarn_beta_slow`.
#[must_use]
pub fn with_yarn_beta_slow(mut self, yarn_beta_slow: f32) -> Self {
self.context_params.yarn_beta_slow = yarn_beta_slow;
self
}
/// Get `YaRN` high correction dimension.
#[must_use]
pub fn yarn_beta_slow(&self) -> f32 {
self.context_params.yarn_beta_slow
}
/// Set `YaRN` original context size.
///
/// Maps to `llama_context_params.yarn_orig_ctx`. `0` uses the model default.
///
/// # Examples
///
/// ```rust
/// use llama_cpp_4::context::params::LlamaContextParams;
/// let params = LlamaContextParams::default().with_yarn_orig_ctx(8192);
/// assert_eq!(params.yarn_orig_ctx(), 8192);
/// ```
#[must_use]
pub fn with_yarn_orig_ctx(mut self, yarn_orig_ctx: u32) -> Self {
self.context_params.yarn_orig_ctx = yarn_orig_ctx;
self
}
/// Get `YaRN` original context size (`yarn_orig_ctx`).
#[must_use]
pub fn yarn_orig_ctx(&self) -> u32 {
self.context_params.yarn_orig_ctx
}
/// Disable performance timing collection for this context.
///
/// Maps to `llama_context_params.no_perf`. When `true`, calls such as
/// [`crate::context::LlamaContext::timings`] return empty counters.
///
/// # Examples
///
/// ```rust
/// use llama_cpp_4::context::params::LlamaContextParams;
/// let params = LlamaContextParams::default().with_no_perf(true);
/// assert!(params.no_perf());
/// ```
#[must_use]
pub fn with_no_perf(mut self, no_perf: bool) -> Self {
self.context_params.no_perf = no_perf;
self
}
/// Returns `true` when perf timings are disabled for this context.
#[must_use]
pub fn no_perf(&self) -> bool {
self.context_params.no_perf
}
/// Register an abort callback checked during `decode()` on CPU backends.
///
/// Maps to `llama_context_params.abort_callback` / `abort_callback_data`.
/// The callback is invoked periodically during long decodes; return a
/// non-zero value to stop the current operation.
///
/// `user_data` is passed through unchanged and must remain valid for the
/// lifetime of any context created from these params.
#[must_use]
pub fn with_abort_callback(
mut self,
callback: llama_cpp_sys_4::ggml_abort_callback,
user_data: *mut std::ffi::c_void,
) -> Self {
self.context_params.abort_callback = callback;
self.context_params.abort_callback_data = user_data;
self
}
/// Assign per-sequence backend sampler chains.
///
/// Maps to `llama_context_params.samplers` / `n_samplers`. Each
/// [`LlamaSampler`] must be a sampler **chain** created with
/// `llama_sampler_chain_init`. The samplers are kept alive inside these
/// params until [`crate::model::LlamaModel::new_context`] returns.
///
/// Pair sequence ids with the chains that should run when decoding those
/// sequences on the backend.
///
/// # Examples
///
/// ```ignore
/// use llama_cpp_4::context::params::LlamaContextParams;
/// use llama_cpp_4::sampling::LlamaSampler;
///
/// let chain = LlamaSampler::chain_default(&model)?;
/// let params = LlamaContextParams::default()
/// .with_sampler_seq_configs([(0, chain)]);
/// assert_eq!(params.n_sampler_seq_configs(), 1);
/// ```
#[must_use]
pub fn with_sampler_seq_configs(
mut self,
configs: impl IntoIterator<Item = (i32, LlamaSampler)>,
) -> Self {
self.owned_samplers.clear();
self.sampler_configs.clear();
for (seq_id, sampler) in configs {
self.sampler_configs
.push(llama_cpp_sys_4::llama_sampler_seq_config {
seq_id,
sampler: sampler.sampler.as_ptr(),
});
self.owned_samplers.push(sampler);
}
if self.sampler_configs.is_empty() {
self.context_params.samplers = std::ptr::null_mut();
self.context_params.n_samplers = 0;
} else {
self.context_params.samplers = self.sampler_configs.as_mut_ptr();
self.context_params.n_samplers = self.sampler_configs.len();
}
self
}
/// Number of per-sequence sampler configs attached to these params.
///
/// Returns `0` when no chains were set or after [`Clone`] (sampler chains
/// are not duplicated).
#[must_use]
pub fn n_sampler_seq_configs(&self) -> usize {
self.sampler_configs.len()
}
}