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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
//! Safe wrapper around `llama_sampler`.
use std::borrow::Borrow;
use std::ffi::{CString, c_char};
use std::fmt::{Debug, Formatter};
use crate::context::LlamaContext;
use crate::ffi_error_reader::read_and_free_cpp_error;
use crate::model::LlamaModel;
use crate::token::LlamaToken;
use crate::token::data_array::LlamaTokenDataArray;
use crate::token::logit_bias::LlamaLogitBias;
use crate::{GrammarError, SampleError, SamplerAcceptError, SamplingError};
fn check_sampler_accept_status(
status: llama_cpp_bindings_sys::llama_rs_status,
error_ptr: *mut c_char,
) -> Result<(), SamplerAcceptError> {
match status {
llama_cpp_bindings_sys::LLAMA_RS_STATUS_OK => Ok(()),
llama_cpp_bindings_sys::LLAMA_RS_STATUS_INVALID_ARGUMENT => {
Err(SamplerAcceptError::InvalidArgument)
}
_ => Err(SamplerAcceptError::CppException(unsafe {
read_and_free_cpp_error(error_ptr)
})),
}
}
fn check_sampler_not_null(
sampler: *mut llama_cpp_bindings_sys::llama_sampler,
error_ptr: *mut c_char,
) -> Result<LlamaSampler, GrammarError> {
if sampler.is_null() {
Err(GrammarError::NullGrammar(unsafe {
read_and_free_cpp_error(error_ptr)
}))
} else {
Ok(LlamaSampler { sampler })
}
}
fn checked_u32_as_i32(value: u32) -> Result<i32, GrammarError> {
i32::try_from(value).map_err(|convert_error| {
GrammarError::IntegerOverflow(format!("value exceeds i32::MAX: {convert_error}"))
})
}
fn checked_usize_as_i32_sampling(value: usize) -> Result<i32, SamplingError> {
i32::try_from(value).map_err(|convert_error| {
SamplingError::IntegerOverflow(format!("value exceeds i32::MAX: {convert_error}"))
})
}
/// A safe wrapper around `llama_sampler`.
pub struct LlamaSampler {
/// Raw pointer to the underlying `llama_sampler`.
pub sampler: *mut llama_cpp_bindings_sys::llama_sampler,
}
impl Debug for LlamaSampler {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("LlamaSamplerChain").finish()
}
}
impl LlamaSampler {
/// Sample and accept a token from the idx-th output of the last evaluation.
///
/// # Errors
///
/// Returns [`SampleError`] if the C++ sampler throws an exception or if the index is invalid.
pub fn sample(&mut self, ctx: &LlamaContext, idx: i32) -> Result<LlamaToken, SampleError> {
let mut token: i32 = -1;
let mut error_ptr: *mut c_char = std::ptr::null_mut();
let status = unsafe {
llama_cpp_bindings_sys::llama_rs_sampler_sample(
self.sampler,
ctx.context.as_ptr(),
idx,
&raw mut token,
&raw mut error_ptr,
)
};
match status {
llama_cpp_bindings_sys::LLAMA_RS_STATUS_OK => Ok(LlamaToken(token)),
llama_cpp_bindings_sys::LLAMA_RS_STATUS_INVALID_ARGUMENT => {
Err(SampleError::InvalidArgument)
}
_ => Err(SampleError::CppException(unsafe {
read_and_free_cpp_error(error_ptr)
})),
}
}
/// Applies this sampler to a [`LlamaTokenDataArray`].
pub fn apply(&self, data_array: &mut LlamaTokenDataArray) {
data_array.apply_sampler(self);
}
/// Accepts a token from the sampler, possibly updating the internal state of certain samplers
/// (e.g. grammar, repetition, etc.)
///
/// # Errors
/// Returns [`SamplerAcceptError`] if the underlying sampler rejects the token.
pub fn accept(&mut self, token: LlamaToken) -> Result<(), SamplerAcceptError> {
self.try_accept(token)
}
/// Accepts several tokens from the sampler or context, possibly updating the internal state of
/// certain samplers (e.g. grammar, repetition, etc.)
///
/// # Errors
/// Returns [`SamplerAcceptError`] if the underlying sampler rejects any token.
pub fn accept_many(
&mut self,
tokens: impl IntoIterator<Item = impl Borrow<LlamaToken>>,
) -> Result<(), SamplerAcceptError> {
for token in tokens {
self.try_accept(*token.borrow())?;
}
Ok(())
}
/// Accepts several tokens from the sampler or context, possibly updating the internal state of
/// certain samplers (e.g. grammar, repetition, etc.)
///
/// # Errors
/// Returns [`SamplerAcceptError`] if the underlying sampler rejects any token.
pub fn with_tokens(
mut self,
tokens: impl IntoIterator<Item = impl Borrow<LlamaToken>>,
) -> Result<Self, SamplerAcceptError> {
self.accept_many(tokens)?;
Ok(self)
}
/// Try accepting a token from the sampler. Returns an error if the sampler throws.
///
/// # Errors
/// Returns an error if the underlying sampler rejects the token.
pub fn try_accept(&mut self, token: LlamaToken) -> Result<(), SamplerAcceptError> {
let mut error_ptr: *mut c_char = std::ptr::null_mut();
let status = unsafe {
llama_cpp_bindings_sys::llama_rs_sampler_accept(
self.sampler,
token.0,
&raw mut error_ptr,
)
};
check_sampler_accept_status(status, error_ptr)
}
/// Resets the internal state of the sampler.
///
/// This can be useful when you want to start fresh with a sampler without creating a new instance.
pub fn reset(&mut self) {
unsafe {
llama_cpp_bindings_sys::llama_sampler_reset(self.sampler);
}
}
/// Gets the random seed used by this sampler.
///
/// Returns:
/// - For random samplers (dist, mirostat, `mirostat_v2)`: returns their current seed
/// - For sampler chains: returns the first non-default seed found in reverse order
/// - For all other samplers: returns 0xFFFFFFFF
#[must_use]
pub fn get_seed(&self) -> u32 {
unsafe { llama_cpp_bindings_sys::llama_sampler_get_seed(self.sampler) }
}
/// Combines a list of samplers into a single sampler that applies each component sampler one
/// after another.
///
/// If you are using a chain to select a token, the chain should always end with one of
/// [`LlamaSampler::greedy`], [`LlamaSampler::dist`], [`LlamaSampler::mirostat`], and
/// [`LlamaSampler::mirostat_v2`].
#[must_use]
pub fn chain(samplers: impl IntoIterator<Item = Self>, no_perf: bool) -> Self {
unsafe {
let chain = llama_cpp_bindings_sys::llama_sampler_chain_init(
llama_cpp_bindings_sys::llama_sampler_chain_params { no_perf },
);
for sampler in samplers {
llama_cpp_bindings_sys::llama_sampler_chain_add(chain, sampler.sampler);
std::mem::forget(sampler);
}
Self { sampler: chain }
}
}
/// Same as [`Self::chain`] with `no_perf = false`.
///
/// # Example
/// ```rust
/// use llama_cpp_bindings::token::{
/// LlamaToken,
/// data::LlamaTokenData,
/// data_array::LlamaTokenDataArray
/// };
/// use llama_cpp_bindings::sampling::LlamaSampler;
/// use llama_cpp_bindings::llama_backend::LlamaBackend;
/// let backend = LlamaBackend::init().unwrap();
///
/// let mut data_array = LlamaTokenDataArray::new(vec![
/// LlamaTokenData::new(LlamaToken(0), 0., 0.),
/// LlamaTokenData::new(LlamaToken(1), 1., 0.),
/// LlamaTokenData::new(LlamaToken(2), 2., 0.),
/// ], false);
///
/// data_array.apply_sampler(&mut LlamaSampler::chain_simple([
/// LlamaSampler::temp(0.5),
/// LlamaSampler::greedy(),
/// ]));
///
/// assert_eq!(data_array.data[0].logit(), 0.);
/// assert_eq!(data_array.data[1].logit(), 2.);
/// assert_eq!(data_array.data[2].logit(), 4.);
///
/// assert_eq!(data_array.data.len(), 3);
/// assert_eq!(data_array.selected_token(), Some(LlamaToken(2)));
/// ```
#[must_use]
pub fn chain_simple(samplers: impl IntoIterator<Item = Self>) -> Self {
Self::chain(samplers, false)
}
/// Updates the logits `l_i' = l_i/t`. When `t <= 0.0`, the maximum logit is kept at its original
/// value, the rest are set to -inf
///
/// # Example:
/// ```rust
/// use llama_cpp_bindings::token::{
/// LlamaToken,
/// data::LlamaTokenData,
/// data_array::LlamaTokenDataArray
/// };
/// use llama_cpp_bindings::sampling::LlamaSampler;
///
/// let mut data_array = LlamaTokenDataArray::new(vec![
/// LlamaTokenData::new(LlamaToken(0), 0., 0.),
/// LlamaTokenData::new(LlamaToken(1), 1., 0.),
/// LlamaTokenData::new(LlamaToken(2), 2., 0.),
/// ], false);
///
/// data_array.apply_sampler(&mut LlamaSampler::temp(0.5));
///
/// assert_eq!(data_array.data[0].logit(), 0.);
/// assert_eq!(data_array.data[1].logit(), 2.);
/// assert_eq!(data_array.data[2].logit(), 4.);
/// ```
#[must_use]
pub fn temp(t: f32) -> Self {
let sampler = unsafe { llama_cpp_bindings_sys::llama_sampler_init_temp(t) };
Self { sampler }
}
/// Dynamic temperature implementation (a.k.a. entropy) described in the paper
/// <https://arxiv.org/abs/2309.02772>.
#[must_use]
pub fn temp_ext(t: f32, delta: f32, exponent: f32) -> Self {
let sampler =
unsafe { llama_cpp_bindings_sys::llama_sampler_init_temp_ext(t, delta, exponent) };
Self { sampler }
}
/// Top-K sampling described in academic paper "The Curious Case of Neural Text Degeneration"
/// <https://arxiv.org/abs/1904.09751>
///
/// # Example:
/// ```rust
/// use llama_cpp_bindings::token::{
/// LlamaToken,
/// data::LlamaTokenData,
/// data_array::LlamaTokenDataArray
/// };
/// use llama_cpp_bindings::sampling::LlamaSampler;
///
/// let mut data_array = LlamaTokenDataArray::new(vec![
/// LlamaTokenData::new(LlamaToken(0), 0., 0.),
/// LlamaTokenData::new(LlamaToken(1), 1., 0.),
/// LlamaTokenData::new(LlamaToken(2), 2., 0.),
/// LlamaTokenData::new(LlamaToken(3), 3., 0.),
/// ], false);
///
/// data_array.apply_sampler(&mut LlamaSampler::top_k(2));
///
/// assert_eq!(data_array.data.len(), 2);
/// assert_eq!(data_array.data[0].id(), LlamaToken(3));
/// assert_eq!(data_array.data[1].id(), LlamaToken(2));
/// ```
#[must_use]
pub fn top_k(k: i32) -> Self {
let sampler = unsafe { llama_cpp_bindings_sys::llama_sampler_init_top_k(k) };
Self { sampler }
}
/// Top-nσ sampling as described in academic paper "Top-nσ: Not All Logits Are You Need"
/// <https://arxiv.org/pdf/2411.07641>
///
/// This method filters logits by selecting only those within *n* standard deviations of the mean.
///
/// # Parameters
/// - `n`: Number of standard deviations from the mean to include in sampling
///
/// # Example
/// ```rust
/// use llama_cpp_bindings::sampling::LlamaSampler;
/// use llama_cpp_bindings::token::{
/// LlamaToken,
/// data::LlamaTokenData,
/// data_array::LlamaTokenDataArray
/// };
///
/// let mut data_array = LlamaTokenDataArray::new(vec![
/// LlamaTokenData::new(LlamaToken(0), 0.0, 0.0),
/// LlamaTokenData::new(LlamaToken(1), 1.0, 0.0),
/// LlamaTokenData::new(LlamaToken(2), 2.0, 0.0),
/// ], false);
///
/// data_array.apply_sampler(&mut LlamaSampler::top_n_sigma(2.0));
/// ```
#[must_use]
pub fn top_n_sigma(n: f32) -> Self {
let sampler = unsafe { llama_cpp_bindings_sys::llama_sampler_init_top_n_sigma(n) };
Self { sampler }
}
/// Locally Typical Sampling implementation described in the paper <https://arxiv.org/abs/2202.00666>.
#[must_use]
pub fn typical(p: f32, min_keep: usize) -> Self {
let sampler = unsafe { llama_cpp_bindings_sys::llama_sampler_init_typical(p, min_keep) };
Self { sampler }
}
/// Nucleus sampling described in academic paper "The Curious Case of Neural Text Degeneration"
/// <https://arxiv.org/abs/1904.09751>
#[must_use]
pub fn top_p(p: f32, min_keep: usize) -> Self {
let sampler = unsafe { llama_cpp_bindings_sys::llama_sampler_init_top_p(p, min_keep) };
Self { sampler }
}
/// Minimum P sampling as described in <https://github.com/ggerganov/llama.cpp/pull/3841>
#[must_use]
pub fn min_p(p: f32, min_keep: usize) -> Self {
let sampler = unsafe { llama_cpp_bindings_sys::llama_sampler_init_min_p(p, min_keep) };
Self { sampler }
}
/// XTC sampler as described in <https://github.com/oobabooga/text-generation-webui/pull/6335>
#[must_use]
pub fn xtc(p: f32, t: f32, min_keep: usize, seed: u32) -> Self {
let sampler =
unsafe { llama_cpp_bindings_sys::llama_sampler_init_xtc(p, t, min_keep, seed) };
Self { sampler }
}
/// Grammar sampler
///
/// # Errors
/// Returns an error if the grammar is invalid or the sampler cannot be initialized.
pub fn grammar(
model: &LlamaModel,
grammar_str: &str,
grammar_root: &str,
) -> Result<Self, GrammarError> {
let (grammar_str, grammar_root) =
Self::sanitize_grammar_strings(grammar_str, grammar_root)?;
let mut error_ptr: *mut c_char = std::ptr::null_mut();
let sampler = unsafe {
llama_cpp_bindings_sys::llama_rs_sampler_init_grammar(
model.vocab_ptr(),
grammar_str.as_ptr(),
grammar_root.as_ptr(),
&raw mut error_ptr,
)
};
check_sampler_not_null(sampler, error_ptr)
}
/// Lazy grammar sampler, introduced in <https://github.com/ggerganov/llama.cpp/pull/9639>
///
/// This sampler enforces grammar rules only when specific trigger words or tokens are encountered.
///
/// # Errors
/// Returns an error if the grammar or trigger words are invalid.
pub fn grammar_lazy(
model: &LlamaModel,
grammar_str: &str,
grammar_root: &str,
trigger_words: impl IntoIterator<Item = impl AsRef<[u8]>>,
trigger_tokens: &[LlamaToken],
) -> Result<Self, GrammarError> {
let (grammar_str, grammar_root) =
Self::sanitize_grammar_strings(grammar_str, grammar_root)?;
let trigger_words = Self::sanitize_trigger_words(trigger_words)?;
let mut error_ptr: *mut c_char = std::ptr::null_mut();
let mut trigger_word_ptrs: Vec<*const c_char> =
trigger_words.iter().map(|cs| cs.as_ptr()).collect();
let sampler = unsafe {
llama_cpp_bindings_sys::llama_rs_sampler_init_grammar_lazy(
model.vocab_ptr(),
grammar_str.as_ptr(),
grammar_root.as_ptr(),
trigger_word_ptrs.as_mut_ptr(),
trigger_word_ptrs.len(),
trigger_tokens.as_ptr().cast(),
trigger_tokens.len(),
&raw mut error_ptr,
)
};
check_sampler_not_null(sampler, error_ptr)
}
/// Lazy grammar sampler using regex trigger patterns.
///
/// Trigger patterns are regular expressions matched from the start of the
/// generation output. The grammar sampler will be fed content starting from
/// the first match group.
///
/// # Errors
/// Returns an error if the grammar or trigger patterns are invalid.
pub fn grammar_lazy_patterns(
model: &LlamaModel,
grammar_str: &str,
grammar_root: &str,
trigger_patterns: &[String],
trigger_tokens: &[LlamaToken],
) -> Result<Self, GrammarError> {
let (grammar_str, grammar_root) =
Self::sanitize_grammar_strings(grammar_str, grammar_root)?;
let trigger_patterns = Self::sanitize_trigger_patterns(trigger_patterns)?;
let mut error_ptr: *mut c_char = std::ptr::null_mut();
let mut trigger_pattern_ptrs: Vec<*const c_char> =
trigger_patterns.iter().map(|cs| cs.as_ptr()).collect();
let sampler = unsafe {
llama_cpp_bindings_sys::llama_rs_sampler_init_grammar_lazy_patterns(
model.vocab_ptr(),
grammar_str.as_ptr(),
grammar_root.as_ptr(),
trigger_pattern_ptrs.as_mut_ptr(),
trigger_pattern_ptrs.len(),
trigger_tokens.as_ptr().cast(),
trigger_tokens.len(),
&raw mut error_ptr,
)
};
check_sampler_not_null(sampler, error_ptr)
}
/// `LLGuidance` sampler for constrained decoding.
///
/// Uses the `llguidance` and `toktrie` Rust crates to enforce grammar constraints
/// during token sampling. Supports JSON schema, regex, Lark, and other grammar types.
///
/// # Errors
///
/// Returns [`GrammarError`] if the grammar is invalid or the sampler cannot be initialized.
pub fn llguidance(
model: &LlamaModel,
grammar_kind: &str,
grammar_data: &str,
) -> Result<Self, GrammarError> {
crate::llguidance_sampler::create_llg_sampler(model, grammar_kind, grammar_data)
}
fn sanitize_grammar_strings(
grammar_str: &str,
grammar_root: &str,
) -> Result<(CString, CString), GrammarError> {
if !grammar_str.contains(grammar_root) {
return Err(GrammarError::RootNotFound);
}
let grammar = CString::new(grammar_str).map_err(GrammarError::GrammarNullBytes)?;
let root = CString::new(grammar_root).map_err(GrammarError::GrammarNullBytes)?;
Ok((grammar, root))
}
fn sanitize_trigger_words(
trigger_words: impl IntoIterator<Item = impl AsRef<[u8]>>,
) -> Result<Vec<CString>, GrammarError> {
trigger_words
.into_iter()
.map(|word| CString::new(word.as_ref()).map_err(GrammarError::TriggerWordNullBytes))
.collect()
}
fn sanitize_trigger_patterns(
trigger_patterns: &[String],
) -> Result<Vec<CString>, GrammarError> {
trigger_patterns
.iter()
.map(|pattern| CString::new(pattern.as_str()).map_err(GrammarError::GrammarNullBytes))
.collect()
}
/// DRY sampler, designed by p-e-w, as described in:
/// <https://github.com/oobabooga/text-generation-webui/pull/5677>, porting Koboldcpp
/// implementation authored by pi6am: <https://github.com/LostRuins/koboldcpp/pull/982>
///
/// # Errors
/// Returns an error if any string in `seq_breakers` contains null bytes.
pub fn dry(
model: &LlamaModel,
multiplier: f32,
base: f32,
allowed_length: i32,
penalty_last_n: i32,
seq_breakers: impl IntoIterator<Item = impl AsRef<[u8]>>,
) -> Result<Self, GrammarError> {
let seq_breakers: Vec<CString> = seq_breakers
.into_iter()
.map(|seq_breaker| CString::new(seq_breaker.as_ref()))
.collect::<Result<Vec<_>, _>>()?;
let mut seq_breaker_pointers: Vec<*const c_char> = seq_breakers
.iter()
.map(|seq_breaker| seq_breaker.as_ptr())
.collect();
let n_ctx_train_value = model.n_ctx_train().map_err(|convert_error| {
GrammarError::IntegerOverflow(format!(
"n_ctx_train does not fit into u32: {convert_error}"
))
})?;
let n_ctx_train = checked_u32_as_i32(n_ctx_train_value)?;
let sampler = unsafe {
llama_cpp_bindings_sys::llama_sampler_init_dry(
model.vocab_ptr(),
n_ctx_train,
multiplier,
base,
allowed_length,
penalty_last_n,
seq_breaker_pointers.as_mut_ptr(),
seq_breaker_pointers.len(),
)
};
Ok(Self { sampler })
}
/// Penalizes tokens for being present in the context.
///
/// Parameters:
/// - ``penalty_last_n``: last n tokens to penalize (0 = disable penalty, -1 = context size)
/// - ``penalty_repeat``: 1.0 = disabled
/// - ``penalty_freq``: 0.0 = disabled
/// - ``penalty_present``: 0.0 = disabled
#[must_use]
pub fn penalties(
penalty_last_n: i32,
penalty_repeat: f32,
penalty_freq: f32,
penalty_present: f32,
) -> Self {
let sampler = unsafe {
llama_cpp_bindings_sys::llama_sampler_init_penalties(
penalty_last_n,
penalty_repeat,
penalty_freq,
penalty_present,
)
};
Self { sampler }
}
/// Mirostat 1.0 algorithm described in the paper <https://arxiv.org/abs/2007.14966>. Uses tokens instead of words.
///
/// # Parameters:
/// - ``n_vocab``: [`LlamaModel::n_vocab`]
/// - ``seed``: Seed to initialize random generation with.
/// - ``tau``: The target cross-entropy (or surprise) value you want to achieve for the
/// generated text. A higher value corresponds to more surprising or less predictable text,
/// while a lower value corresponds to less surprising or more predictable text.
/// - ``eta``: The learning rate used to update `mu` based on the error between the target and
/// observed surprisal of the sampled word. A larger learning rate will cause `mu` to be
/// updated more quickly, while a smaller learning rate will result in slower updates.
/// - ``m``: The number of tokens considered in the estimation of `s_hat`. This is an arbitrary
/// value that is used to calculate `s_hat`, which in turn helps to calculate the value of `k`.
/// In the paper, they use `m = 100`, but you can experiment with different values to see how
/// it affects the performance of the algorithm.
#[must_use]
pub fn mirostat(n_vocab: i32, seed: u32, tau: f32, eta: f32, m: i32) -> Self {
let sampler = unsafe {
llama_cpp_bindings_sys::llama_sampler_init_mirostat(n_vocab, seed, tau, eta, m)
};
Self { sampler }
}
/// Mirostat 2.0 algorithm described in the paper <https://arxiv.org/abs/2007.14966>. Uses tokens instead of words.
///
/// # Parameters:
/// - ``seed``: Seed to initialize random generation with.
/// - ``tau``: The target cross-entropy (or surprise) value you want to achieve for the
/// generated text. A higher value corresponds to more surprising or less predictable text,
/// while a lower value corresponds to less surprising or more predictable text.
/// - ``eta``: The learning rate used to update `mu` based on the error between the target and
/// observed surprisal of the sampled word. A larger learning rate will cause `mu` to be
/// updated more quickly, while a smaller learning rate will result in slower updates.
#[must_use]
pub fn mirostat_v2(seed: u32, tau: f32, eta: f32) -> Self {
let sampler =
unsafe { llama_cpp_bindings_sys::llama_sampler_init_mirostat_v2(seed, tau, eta) };
Self { sampler }
}
/// Selects a token at random based on each token's probabilities
#[must_use]
pub fn dist(seed: u32) -> Self {
let sampler = unsafe { llama_cpp_bindings_sys::llama_sampler_init_dist(seed) };
Self { sampler }
}
/// Selects the most likely token
///
/// # Example:
/// ```rust
/// use llama_cpp_bindings::token::{
/// LlamaToken,
/// data::LlamaTokenData,
/// data_array::LlamaTokenDataArray
/// };
/// use llama_cpp_bindings::sampling::LlamaSampler;
///
/// let mut data_array = LlamaTokenDataArray::new(vec![
/// LlamaTokenData::new(LlamaToken(0), 0., 0.),
/// LlamaTokenData::new(LlamaToken(1), 1., 0.),
/// ], false);
///
/// data_array.apply_sampler(&mut LlamaSampler::greedy());
///
/// assert_eq!(data_array.data.len(), 2);
/// assert_eq!(data_array.selected_token(), Some(LlamaToken(1)));
/// ```
#[must_use]
pub fn greedy() -> Self {
let sampler = unsafe { llama_cpp_bindings_sys::llama_sampler_init_greedy() };
Self { sampler }
}
/// Creates a sampler that applies bias values to specific tokens during sampling.
///
/// # Parameters
/// - ``n_vocab``: [`LlamaModel::n_vocab`]
/// - ``biases``: Slice of [`LlamaLogitBias`] values specifying token-bias pairs
///
/// # Errors
/// Returns [`SamplingError::IntegerOverflow`] if `biases.len()` exceeds `i32::MAX`.
///
/// # Example
/// ```rust
/// use llama_cpp_bindings::token::{LlamaToken, logit_bias::LlamaLogitBias};
/// use llama_cpp_bindings::sampling::LlamaSampler;
///
/// let biases = vec![
/// LlamaLogitBias::new(LlamaToken(1), 1.5), // Increase probability of token 1
/// LlamaLogitBias::new(LlamaToken(2), -1.0), // Decrease probability of token 2
/// ];
///
/// // Assuming vocab_size of 32000
/// let sampler = LlamaSampler::logit_bias(32000, &biases).unwrap();
/// ```
pub fn logit_bias(n_vocab: i32, biases: &[LlamaLogitBias]) -> Result<Self, SamplingError> {
let bias_count = checked_usize_as_i32_sampling(biases.len())?;
let data = biases
.as_ptr()
.cast::<llama_cpp_bindings_sys::llama_logit_bias>();
let sampler = unsafe {
llama_cpp_bindings_sys::llama_sampler_init_logit_bias(n_vocab, bias_count, data)
};
Ok(Self { sampler })
}
}
impl Drop for LlamaSampler {
fn drop(&mut self) {
unsafe {
llama_cpp_bindings_sys::llama_sampler_free(self.sampler);
}
}
}
#[cfg(test)]
mod tests {
use super::LlamaSampler;
use crate::GrammarError;
#[test]
fn sanitize_grammar_strings_valid() {
let result = LlamaSampler::sanitize_grammar_strings("root ::= \"hello\"", "root");
assert!(result.is_ok());
}
#[test]
fn sanitize_grammar_strings_root_not_found() {
let result = LlamaSampler::sanitize_grammar_strings("expr ::= \"hello\"", "root");
assert_eq!(result.err(), Some(GrammarError::RootNotFound));
}
#[test]
fn sanitize_grammar_strings_null_byte_in_grammar() {
let result = LlamaSampler::sanitize_grammar_strings("root ::= \"\0\"", "root");
assert!(matches!(
result.err(),
Some(GrammarError::GrammarNullBytes(_))
));
}
#[test]
fn sanitize_grammar_strings_null_byte_in_root() {
let result = LlamaSampler::sanitize_grammar_strings("ro\0ot ::= \"hello\"", "ro\0ot");
assert!(matches!(
result.err(),
Some(GrammarError::GrammarNullBytes(_))
));
}
#[test]
fn sanitize_trigger_words_valid() {
let words: Vec<&[u8]> = vec![b"hello", b"world"];
let result = LlamaSampler::sanitize_trigger_words(words);
assert!(result.is_ok());
assert_eq!(result.expect("valid trigger words").len(), 2);
}
#[test]
fn sanitize_trigger_words_empty_list() {
let words: Vec<&[u8]> = vec![];
let result = LlamaSampler::sanitize_trigger_words(words);
assert!(result.is_ok());
assert!(result.expect("valid trigger words").is_empty());
}
#[test]
fn sanitize_trigger_words_null_byte() {
let words: Vec<&[u8]> = vec![b"hel\0lo"];
let result = LlamaSampler::sanitize_trigger_words(words);
assert!(matches!(
result.err(),
Some(GrammarError::TriggerWordNullBytes(_))
));
}
#[test]
fn sanitize_trigger_patterns_valid() {
let patterns = vec!["^hello$".to_string(), "world.*".to_string()];
let result = LlamaSampler::sanitize_trigger_patterns(&patterns);
assert!(result.is_ok());
assert_eq!(result.expect("valid trigger patterns").len(), 2);
}
#[test]
fn sanitize_trigger_patterns_empty_list() {
let patterns: Vec<String> = vec![];
let result = LlamaSampler::sanitize_trigger_patterns(&patterns);
assert!(result.is_ok());
assert!(result.expect("valid trigger patterns").is_empty());
}
#[test]
fn sanitize_trigger_patterns_null_byte() {
let patterns = vec!["hel\0lo".to_string()];
let result = LlamaSampler::sanitize_trigger_patterns(&patterns);
assert!(matches!(
result.err(),
Some(GrammarError::GrammarNullBytes(_))
));
}
#[test]
fn apply_modifies_data_array() {
use crate::token::LlamaToken;
use crate::token::data::LlamaTokenData;
use crate::token::data_array::LlamaTokenDataArray;
let sampler = LlamaSampler::greedy();
let mut data_array = LlamaTokenDataArray::new(
vec![
LlamaTokenData::new(LlamaToken::new(0), 1.0, 0.0),
LlamaTokenData::new(LlamaToken::new(1), 5.0, 0.0),
],
false,
);
sampler.apply(&mut data_array);
assert_eq!(data_array.selected_token(), Some(LlamaToken::new(1)));
}
#[test]
fn accept_succeeds() {
let mut sampler = LlamaSampler::chain_simple([
LlamaSampler::penalties(64, 1.1, 0.0, 0.0),
LlamaSampler::greedy(),
]);
sampler
.accept(crate::token::LlamaToken::new(1))
.expect("test: accept should succeed");
}
#[test]
fn try_accept_succeeds_on_penalties_sampler() {
let mut sampler = LlamaSampler::chain_simple([
LlamaSampler::penalties(64, 1.1, 0.0, 0.0),
LlamaSampler::greedy(),
]);
let result = sampler.try_accept(crate::token::LlamaToken::new(42));
assert!(result.is_ok());
}
#[test]
fn accept_many_multiple_tokens() {
use crate::token::LlamaToken;
let mut sampler = LlamaSampler::chain_simple([
LlamaSampler::penalties(64, 1.1, 0.0, 0.0),
LlamaSampler::greedy(),
]);
sampler
.accept_many([LlamaToken::new(1), LlamaToken::new(2), LlamaToken::new(3)])
.expect("test: accept_many should succeed");
}
#[test]
fn with_tokens_builder_pattern() {
use crate::token::LlamaToken;
let _sampler = LlamaSampler::chain_simple([
LlamaSampler::penalties(64, 1.1, 0.0, 0.0),
LlamaSampler::greedy(),
])
.with_tokens([LlamaToken::new(10), LlamaToken::new(20)])
.expect("test: with_tokens should succeed");
}
#[test]
fn all_sampler_constructors() {
use crate::token::LlamaToken;
use crate::token::logit_bias::LlamaLogitBias;
let _temp = LlamaSampler::temp(0.8);
let _temp_ext = LlamaSampler::temp_ext(0.8, 0.1, 1.0);
let _top_k = LlamaSampler::top_k(40);
let _top_n_sigma = LlamaSampler::top_n_sigma(2.0);
let _top_p = LlamaSampler::top_p(0.9, 1);
let _min_p = LlamaSampler::min_p(0.05, 1);
let _typical = LlamaSampler::typical(0.9, 1);
let _xtc = LlamaSampler::xtc(0.1, 0.5, 1, 42);
let _dist = LlamaSampler::dist(42);
let _mirostat = LlamaSampler::mirostat(32000, 42, 5.0, 0.1, 100);
let _mirostat_v2 = LlamaSampler::mirostat_v2(42, 5.0, 0.1);
let biases = vec![LlamaLogitBias::new(LlamaToken::new(0), -100.0)];
let _logit_bias = LlamaSampler::logit_bias(32000, &biases);
let _chain = LlamaSampler::chain([LlamaSampler::greedy()], true);
}
#[test]
fn reset_and_get_seed() {
let mut sampler = LlamaSampler::dist(42);
sampler.reset();
let _seed = sampler.get_seed();
}
#[test]
fn debug_formatting() {
let sampler = LlamaSampler::greedy();
let debug_output = format!("{sampler:?}");
assert!(debug_output.contains("LlamaSampler"));
}
#[test]
fn checked_u32_as_i32_overflow() {
let result = super::checked_u32_as_i32(u32::MAX);
assert!(result.is_err());
}
#[test]
fn checked_usize_as_i32_sampling_overflow() {
let result = super::checked_usize_as_i32_sampling(usize::MAX);
assert!(result.is_err());
}
#[test]
fn check_sampler_accept_status_ok() {
let result = super::check_sampler_accept_status(
llama_cpp_bindings_sys::LLAMA_RS_STATUS_OK,
std::ptr::null_mut(),
);
assert!(result.is_ok());
}
#[test]
fn check_sampler_accept_status_invalid_argument() {
let result = super::check_sampler_accept_status(
llama_cpp_bindings_sys::LLAMA_RS_STATUS_INVALID_ARGUMENT,
std::ptr::null_mut(),
);
assert!(matches!(
result,
Err(crate::SamplerAcceptError::InvalidArgument)
));
}
#[test]
fn check_sampler_accept_status_exception() {
let result = super::check_sampler_accept_status(
llama_cpp_bindings_sys::LLAMA_RS_STATUS_EXCEPTION,
std::ptr::null_mut(),
);
assert!(matches!(
result,
Err(crate::SamplerAcceptError::CppException(_))
));
}
#[test]
fn check_sampler_not_null_returns_error() {
let result = super::check_sampler_not_null(std::ptr::null_mut(), std::ptr::null_mut());
assert!(result.is_err());
}
}