llama_cpp_sys_4/common.rs
1//! Manual wrapper for values in llama.cpp/common/common.h
2//!
3//! [`common_sampler_params`] mirrors upstream's `common_params_sampling` (kept in
4//! sync with llama.cpp `b10470`). It is a plain Rust convenience struct, not an
5//! FFI type: `grammar` and `logit_bias` are simplified to owned Rust values
6//! rather than upstream's `common_grammar` / `llama_logit_bias`, and the
7//! server/CLI-only fields (reasoning budget, grammar triggers) are omitted.
8
9use crate::LLAMA_DEFAULT_SEED;
10
11pub const COMMON_SAMPLER_TYPE_NONE: common_sampler_type = 0;
12pub const COMMON_SAMPLER_TYPE_DRY: common_sampler_type = 1;
13pub const COMMON_SAMPLER_TYPE_TOP_K: common_sampler_type = 2;
14pub const COMMON_SAMPLER_TYPE_TOP_P: common_sampler_type = 3;
15pub const COMMON_SAMPLER_TYPE_MIN_P: common_sampler_type = 4;
16// 5 was COMMON_SAMPLER_TYPE_TFS_Z — removed upstream, the slot is left unused.
17pub const COMMON_SAMPLER_TYPE_TYPICAL_P: common_sampler_type = 6;
18pub const COMMON_SAMPLER_TYPE_TEMPERATURE: common_sampler_type = 7;
19pub const COMMON_SAMPLER_TYPE_XTC: common_sampler_type = 8;
20pub const COMMON_SAMPLER_TYPE_INFILL: common_sampler_type = 9;
21pub const COMMON_SAMPLER_TYPE_PENALTIES: common_sampler_type = 10;
22pub const COMMON_SAMPLER_TYPE_TOP_N_SIGMA: common_sampler_type = 11;
23pub const COMMON_SAMPLER_TYPE_ADAPTIVE_P: common_sampler_type = 12;
24pub type common_sampler_type = ::core::ffi::c_uint;
25
26/// common sampler params
27#[repr(C)]
28#[derive(Debug, PartialEq)]
29pub struct common_sampler_params {
30 /// the seed used to initialize `llama_sampler`
31 pub seed: u32,
32 /// number of previous tokens to remember
33 pub n_prev: i32,
34 /// if greater than 0, output the probabilities of top `n_probs` tokens.
35 pub n_probs: i32,
36 /// 0 = disabled, otherwise samplers should return at least `min_keep` tokens
37 pub min_keep: i32,
38 /// <= 0 to use vocab size
39 pub top_k: i32,
40 /// 1.0 = disabled
41 pub top_p: f32,
42 /// 0.0 = disabled
43 pub min_p: f32,
44 /// 0.0 = disabled
45 pub xtc_probability: f32,
46 /// > 0.5 disables XTC
47 pub xtc_threshold: f32,
48 /// typical_p, 1.0 = disabled
49 pub typ_p: f32,
50 /// <= 0.0 to sample greedily, 0.0 to not output probabilities
51 pub temp: f32,
52 /// 0.0 = disabled
53 pub dynatemp_range: f32,
54 /// controls how entropy maps to temperature in dynamic temperature sampler
55 pub dynatemp_exponent: f32,
56 /// last n tokens to penalize (0 = disable penalty)
57 pub penalty_last_n: i32,
58 /// 1.0 = disabled
59 pub penalty_repeat: f32,
60 /// 0.0 = disabled
61 pub penalty_freq: f32,
62 /// 0.0 = disabled
63 pub penalty_present: f32,
64 /// 0.0 = disabled; DRY repetition penalty for tokens extending repetition:
65 pub dry_multiplier: f32,
66 /// 0.0 = disabled; multiplier * base ^ (length of sequence before token - allowed length)
67 pub dry_base: f32,
68 /// tokens extending repetitions beyond this receive penalty
69 pub dry_allowed_length: i32,
70 /// how many tokens to scan for repetitions (0 = disable penalty)
71 pub dry_penalty_last_n: i32,
72 /// select tokens near this probability (valid range 0.0 to 1.0; negative = disabled)
73 pub adaptive_target: f32,
74 /// EMA decay for adaptation; history ≈ 1/(1-decay) tokens (0.0 - 0.99)
75 pub adaptive_decay: f32,
76 /// 0 = disabled, 1 = mirostat, 2 = mirostat 2.0
77 pub mirostat: i32,
78 /// -1.0 = disabled
79 pub top_n_sigma: f32,
80 /// target entropy
81 pub mirostat_tau: f32,
82 /// learning rate
83 pub mirostat_eta: f32,
84 pub ignore_eos: bool,
85 /// disable performance metrics
86 pub no_perf: bool,
87 /// report timings per token
88 pub timing_per_token: bool,
89 pub dry_sequence_breakers: Vec<String>,
90 pub samplers: Vec<common_sampler_type>,
91 pub grammar: Vec<String>,
92 pub logit_bias: Vec<(i32, f64)>,
93}
94
95impl Default for common_sampler_params {
96 fn default() -> Self {
97 Self {
98 seed: LLAMA_DEFAULT_SEED, // the seed used to initialize llama_sampler
99 n_prev: 64, // number of previous tokens to remember
100 n_probs: 0, // if greater than 0, output the probabilities of top n_probs tokens.
101 min_keep: 0, // 0 = disabled, otherwise samplers should return at least min_keep tokens
102 top_k: 40, // <= 0 to use vocab size
103 top_p: 0.95, // 1.0 = disabled
104 min_p: 0.05, // 0.0 = disabled
105 xtc_probability: 0.00, // 0.0 = disabled
106 xtc_threshold: 0.10, // > 0.5 disables XTC
107 typ_p: 1.00, // typical_p, 1.0 = disabled
108 temp: 0.80, // <= 0.0 to sample greedily, 0.0 to not output probabilities
109 dynatemp_range: 0.00, // 0.0 = disabled
110 dynatemp_exponent: 1.00, // controls how entropy maps to temperature in dynamic temperature sampler
111 penalty_last_n: 64, // last n tokens to penalize (0 = disable penalty)
112 penalty_repeat: 1.00, // 1.0 = disabled
113 penalty_freq: 0.00, // 0.0 = disabled
114 penalty_present: 0.00, // 0.0 = disabled
115 dry_multiplier: 0.0, // 0.0 = disabled; DRY repetition penalty for tokens extending repetition:
116 dry_base: 1.75, // 0.0 = disabled; multiplier * base ^ (length of sequence before token - allowed length)
117 dry_allowed_length: 2, // tokens extending repetitions beyond this receive penalty
118 dry_penalty_last_n: 64, // how many tokens to scan for repetitions (0 = disable penalty)
119 adaptive_target: -1.0, // select tokens near this probability (negative = disabled)
120 adaptive_decay: 0.90, // EMA decay for adaptation; history ≈ 1/(1-decay) tokens
121 mirostat: 0, // 0 = disabled, 1 = mirostat, 2 = mirostat 2.0
122 top_n_sigma: -1.00, // -1.0 = disabled
123 mirostat_tau: 5.00, // target entropy
124 mirostat_eta: 0.10, // learning rate
125 ignore_eos: false,
126 no_perf: false, // disable performance metrics
127 timing_per_token: false, // report timings per token
128
129 dry_sequence_breakers: vec!["\n".into(), ":".into(), "\"".into(), "*".into()], // default sequence breakers for DRY
130
131 samplers: vec![
132 COMMON_SAMPLER_TYPE_PENALTIES,
133 COMMON_SAMPLER_TYPE_DRY,
134 COMMON_SAMPLER_TYPE_TOP_N_SIGMA,
135 COMMON_SAMPLER_TYPE_TOP_K,
136 COMMON_SAMPLER_TYPE_TYPICAL_P,
137 COMMON_SAMPLER_TYPE_TOP_P,
138 COMMON_SAMPLER_TYPE_MIN_P,
139 COMMON_SAMPLER_TYPE_XTC,
140 COMMON_SAMPLER_TYPE_TEMPERATURE,
141 ],
142
143 grammar: vec![], // optional BNF-like grammar to constrain sampling
144
145 logit_bias: vec![], // logit biases to apply
146 }
147 }
148}